| 1 | package Froody::Walker::XML; | ||||||||||||||||||||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use base 'Froody::Walker::Driver'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use Froody::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 11 | my $logger = get_logger('froody.walker.xml'); | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Froody::Walker::XML - read and write XML | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Turn xml into data in the form of Implementation class returns. | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 15 | ▶ | sub init_source { | ||||||||||||||||||||||||||
|
Subroutine
init_source: called CC=3 SCAR=13.3
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 15 | my ($self, $source) = @_; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 15 | ▶ | if ($source and !ref($source)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$source and not ref $source
MC/DC0%0/2
$source and not ref $source
$source
not ref $source
Truth table33%1/3
A and not(B)
A= $sourceB= not ref $source
| |||||||||||||||||||||||||||||
| 27 | 0 | $source = XML::LibXML->new->parse_string( $source )->documentElement; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 15 | return $source; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 24 | ▶ | sub init_target { | ||||||||||||||||||||||||||
|
Subroutine
init_target: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 24 | my ($self, $xpath, $parent) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 24 | ▶ | my $doc = $parent ? $parent->ownerDocument | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | : XML::LibXML->createDocument("1.0", "UTF8"); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 24 | ▶ | return $doc->createDocumentFragment unless $xpath; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 24 | my $name = (split '/', $xpath)[-1]; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 24 | return $doc->createElement($name); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 52 | ▶ | sub validate_source { | ||||||||||||||||||||||||||
|
Subroutine
validate_source: called CC=10 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 52 | my ($self, $source, $path) = @_; | |||||||||||||||||||||||||||
| 48 | 52 | my $spec = $self->spec_for_xpath($path); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 52 | my %check = map { $_ => 1 } @{ $spec->{attr} }; | |||||||||||||||||||||||||||
| 51 | 52 | my @my_attr = map { $_->name } $source->attributes(); | |||||||||||||||||||||||||||
| 52 | 52 | for (@my_attr) { | |||||||||||||||||||||||||||
| 53 | 44 | ▶ | $logger->warn("bad attr '$_' in node '$path' (not in spec)") unless $check{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 52 | ▶ | %check = map { ($path ? "$path/$_" : $path) => 1 } @{ $spec->{elts} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 52 | ▶ | my @my_elem = map { $_->nodeType == 1 ? $_->nodeName : () } $source->childNodes(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 52 | for (@my_elem) { | |||||||||||||||||||||||||||
| 59 | 37 | ▶ | $logger->warn("bad element '$_' in node '$path' (not in spec)") unless $check{($path ? "$path/$_" : $path)}; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 52 | ▶ | if (!$spec->{text} and my $text = $self->read_text($source, $path)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $spec->{"text"} and my $text = $self->read_text($source, $path)
MC/DC0%0/2
not $spec->{"text"} and my $text = $self->read_text($source, $path)
not $spec->{"text"}
my $text = $self->read_text($source, $path)
Truth table66%2/3
not(A) and B
A= not $spec->{"text"}B= my $text = $self->read_text($source, $path)
| |||||||||||||||||||||||||||||
| 63 | 0 | warn $source->toString(1); | |||||||||||||||||||||||||||
| 64 | 0 | $logger->warn("bad text content '$text' for node '$path' (not in spec)"); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 52 | return 1; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 45 | ▶ | sub read_attribute { | ||||||||||||||||||||||||||
|
Subroutine
read_attribute: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 45 | my ($self, $source, $path, $attr) = @_; | |||||||||||||||||||||||||||
| 72 | 45 | return $source->getAttribute($attr); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 52 | ▶ | sub read_text { | ||||||||||||||||||||||||||
|
Subroutine
read_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 52 | my ($self, $source, $path) = @_; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 52 | my $text = $source->findvalue('./text()'); | |||||||||||||||||||||||||||
| 79 | 52 | $text =~ s/^\s+//; | |||||||||||||||||||||||||||
| 80 | 52 | $text =~ s/\s+$//; | |||||||||||||||||||||||||||
| 81 | 52 | return $text; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 36 | ▶ | sub child_sources { | ||||||||||||||||||||||||||
|
Subroutine
child_sources: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 36 | my ($self, $source, $xpath, $element) = @_; | |||||||||||||||||||||||||||
| 86 | 36 | return $source->findnodes( "./$element" ) ; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 35 | ▶ | sub _encode { | ||||||||||||||||||||||||||
|
Subroutine
_encode: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | 35 | my ($target,$value) = @_; | |||||||||||||||||||||||||||
| 91 | 35 | return Encode::encode($target->ownerDocument->encoding, $value) | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 22 | ▶ | sub write_attribute { | ||||||||||||||||||||||||||
|
Subroutine
write_attribute: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 22 | my ($self, $target, $path, $attr, $value) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 22 | $target->setAttribute($attr, _encode($target, $value)); | |||||||||||||||||||||||||||
| 98 | 22 | return $target; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 13 | ▶ | sub write_text { | ||||||||||||||||||||||||||
|
Subroutine
write_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 13 | my ($self, $target, $xpath_key, $value) = @_; | |||||||||||||||||||||||||||
| 103 | 13 | $target->appendText(_encode($target, $value)); | |||||||||||||||||||||||||||
| 104 | 13 | return $target; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 16 | ▶ | sub add_child_to_target { | ||||||||||||||||||||||||||
|
Subroutine
add_child_to_target: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 16 | my ($self, $target, $xpath, $element, $child) = @_; | |||||||||||||||||||||||||||
| 110 | 16 | $target->addChild($child); | |||||||||||||||||||||||||||
| 111 | 16 | return $target; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 BUGS | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | None known. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Please report any bugs you find via the CPAN RT system. | ||||||||||||||||||||||||||||
| 120 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody> | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Copyright Fotango 2005. All rights reserved. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Please see the main L<Froody> documentation for details of who has worked | ||||||||||||||||||||||||||||
| 127 | on this project. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | This module is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 130 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | L<Froody>, L<Froody::Walker> | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 11 | 1; | |||||||||||||||||||||||||||