XSH2/XPathToXML.pm

filter: Statementstmt 43.8% 93 / 212 Branchbran 30.0% 42 / 140 Conditioncond 20.0% 8 / 40 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 55.5% 10 / 18 Podpod 100% 10 / 10 totaltotal 36.6% 163 / 445 CC 87 SCAR 50.7
CC
87
Cov
36%
_createSteps
348.4
_lookup_namespace
99.2
parse
72.0
CRAP 159.5
« XSH2/Functions.pm ↑ Summary XSH2/LibXMLCompat.pm »
1 package XML::XSH2::XPathToXML;
2 ###
3 #
4 # Name: XML::XSH2::XPathToXML
5 # Version: 0.05
6 # Description: Parses a list of XPath/value pairs and returns an XML::LibXML note-tree.
7 # Original author: Kurt George Gjerde
8 # Extended by Petr Pajas
9 # Copyright: InterMedia, University of Bergen (2002)
10 # Licence: Same as Perl
11 #
12 ###
13  
14 ### POD at bottom.
15  
16  
17 ###
18  
19 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 $VERSION = '0.05';
25  
26 2 my $PACKAGE = __PACKAGE__;
27  
28 2 our ($QUOT,$NAMECHAR,$FIRSTNAMECHAR,$NAME,$STEP,$LAST_STEP,$FILTER,$PREDICATE,$AXIS);
29  
30 # regexps to parse XPath steps
31  
32 2 $NAMECHAR = '[-_.[:alnum:]]';
33 2 $FIRSTNAMECHAR = '[-_.[:alpha:]]';
34 2 $NAME = "(?:${FIRSTNAMECHAR}${NAMECHAR}*(?::${FIRSTNAMECHAR}${NAMECHAR}+)*)";
35  
36 2 $QUOT = q{(?:'[^']*'|"[^"]*")};
37 $PREDICATE = qr/
38 (?:
39 (?> [^][()"']* ) # non-parens without backtracking
40 |
41 '[^']*' | "[^"]*" # quotes
42 |
43 \[
44 (??{$PREDICATE}) # matching square brackets
45 \]
46 |
47 \(
48 (??{$PREDICATE}) # matching round brackets
49 \)
50 )*
51 2 /x;
52  
53 2 $FILTER = qr/(?:\[$PREDICATE\])/;
54  
55 2 $AXIS=qr{(?:(?:following-sibling|following|preceding|preceding-sibling|parent|ancestor|ancestor-or-self|descendant|self|descendant-or-self|child|namespace)::)?};
56 2 $STEP = qr{(?:(?:^|/)${AXIS}${NAME}${FILTER}*)};
57 2 $LAST_STEP = qr{(?:(?:^|/)(?:\@${NAME}|${AXIS}(?:${NAME}|comment[(][)]|text[(][)]|processing-instruction[(](?:\s*"${NAME}"\s*|\s*'${NAME}'\s*)[)]))${FILTER}*)};
58  
59 ### NEW
60 #
61 8 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod documented
62 8 my ($class, %args) = @_;
63 8 my $self = \%args;
64 8 bless $self, $class;
65  
66 8 ▶ $self->{version} ||= '1.0';
Condition50%1/2
$self->{"version"} ||= "1.0"
l!l
08
MC/DC0%0/1
$self->{"version"} ||= "1.0"
$self->{"version"}
Truth table50%1/2
A
A= $self->{"version"}
Aresult
11
00
67 8 ▶ $self->{debug} ||= 0;
Condition50%1/2
$self->{"debug"} ||= 0
l!l
08
MC/DC0%0/1
$self->{"debug"} ||= 0
$self->{"debug"}
Truth table50%1/2
A
A= $self->{"debug"}
Aresult
11
00
68 8 ▶ $self->{maxAutoSiblings} = 256 if not defined $self->{maxAutoSiblings};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"maxAutoSiblings"} 8 0 8
69  
70 8 $self->init();
71  
72 8 return $self;
73 }
74  
75  
76 ### CLOSE
77 #
78 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
79 0 my $self = shift;
80 0 undef $self->{tree};
81 0 undef $self->{node};
82 0 undef $self->{doc};
83 }
84  
85  
86 ### INIT
87 #
88 8 ▶sub init {
Subroutine init: called CC=4 SCAR=16.1
Pod documented
89 8 my $self = shift;
90 8 ▶ unless ($self->{doc}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"doc"}) 8 0 8
91 8 ▶ if (ref($self->{node})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'node'}) { } 8 0 8
92 8 $self->{doc} = $self->{node}->ownerDocument;
93 } else {
94 0 $self->{doc} = XML::LibXML::Document->createDocument( $self->{version}, $self->{encoding} );
95 0 $self->{doc}->setDocumentElement( $self->{doc}->createElement('root') );
96 }
97 }
98 8 ▶ $self->{node} ||= $self->{doc};
Condition33%1/3
$self->{"node"} ||= $self->{"doc"}
l!l&&r!l&&!r
800
MC/DC0%0/2
$self->{"node"} ||= $self->{"doc"}
$self->{"node"} $self->{"doc"}
Truth table33%1/3
A ||= B
A= $self->{"node"}B= $self->{"doc"}
ABresult
1X1
011
000
99 8 $self->{tree} = {};
100 }
101  
102  
103 ### RESET
104 #
105 0 ▶sub setContextNode {
Subroutine setContextNode: not called CC=4 SCAR=30.0
Pod documented
106 0 my ($self,$node) = @_;
107 0 undef $self->{tree};
108 0 ▶ unless (ref($node)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $node) 0 0 0
109 0 ▶ if ($self->{XPathContext}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'XPathContext'}) { } 0 0 0
110 0 $node=$self->{XPathContext}->find($node)->get_node(1);
111 } else {
112 0 $node=$self->{node}->find($node)->get_node(1);
113 }
114 }
115 0 ▶ if ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) { } 0 0 0
116 0 $self->{node} = $node;
117 0 $self->{doc} = $node->ownerDocument;
118 } else {
119 0 die "[$PACKAGE] Context node doesn't exist.\n";
120 }
121 }
122  
123  
124 ### RESET
125 #
126 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
127 0 my ($self,%args) = @_;
128 0 $self->close;
129 0 $self->init(%args);
130 }
131  
132  
133 ### GET IT
134 #
135 0 ▶sub contextNode { shift->{node} }
Subroutine contextNode: not called CC=1 SCAR=6.9
Pod documented
136  
137 0 ▶sub document { shift->{doc} }
Subroutine document: not called CC=1 SCAR=6.9
Pod documented
138  
139 0 ▶sub documentElement { shift->{doc}->documentElement }
Subroutine documentElement: not called CC=1 SCAR=6.9
Pod documented
140
141  
142 ### PARSE
143 #
144 0 ▶sub parse {
Subroutine parse: not called CC=8 SCAR=42.8
Pod documented
145 0 my ($self, $data, $value) = @_;
146
147 0 ▶ if (ref($data) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 0 0 0
elsif (ref $data eq 'ARRAY') { } 0 0 0
148 0 foreach my $xpath (sort keys %{$data}) {
149 0 ▶ print "$xpath\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
150 0 $self->createNode( $xpath, $data->{$xpath} );
151 }
152 } elsif (ref($data) eq 'ARRAY') {
153 # preserve order
154 0 ▶ die "[$PACKAGE] Array must have even number of elements.\n" if (@$data % 2);
Branch: 1 branch
BranchTrueFalseTotal
if @$data % 2 0 0 0
155 0 ▶ for (my $i=0; $i<@$data; $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$data) 0 0 0
156 0 ▶ print "$data->[$i]\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
157 0 $self->createNode( $data->[$i], $data->[$i+1] );
158 }
159 } else {
160 0 $self->createNode( $data, $value );
161 }
162
163 0 return $self->{doc}->documentElement();
164 }
165  
166  
167 ### _CREATE NODE
168 #
169 # Creates nodes, text nodes and attributes from an xpath and a value.
170 #
171 # Returns the node created
172 #
173  
174 8 ▶sub createNode {
Subroutine createNode: called CC=9 SCAR=29.5
Pod documented
175 8 my ($self, $xpath, $value, $context_node) = @_;
176  
177 # strip the uninteresting part
178 8 $xpath=~s{^\.\s+/}{};
179  
180 # roughly verify that we have an XPath in a supported form:
181 8 ▶ die "[$PACKAGE] Can't create nodes based on XPath $xpath\n" unless $xpath =~ m{^(?:\$${NAME}${FILTER}*)?$STEP*$LAST_STEP$};
Branch: 1 branch
BranchTrueFalseTotal
unless $xpath =~ /^(?:\$$XML::XSH2::XPathToXML::NAME$XML::XSH2::XPathToXML::FILTER*)?$XML::XSH2::XPathToXML::STEP*$XML::XSH2::XPathToXML::LAST_STEP$/ 0 8 8
182 # return;
183  
184 8 ▶ if ($xpath=~s{^/}{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xpath =~ s[^/][]) { } 3 5 8
185 # start from the document level
186 3 $self->_createSteps($self->{doc},$xpath,$value);
187 } else {
188 5 ▶ if ($xpath =~ s{^(\$${NAME}${FILTER}*)/}{./}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xpath =~ s[^(\$$XML::XSH2::XPathToXML::NAME$XML::XSH2::XPathToXML::FILTER*)/][./]) 0 5 5
189 0 $context_node = $1;
190 }
191 # start from the current node
192 5 ▶ if ($context_node ne "" and !ref($context_node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context_node ne '' and not ref $context_node) { } 0 5 5
Condition33%1/3
$context_node ne "" and not ref $context_node
!ll&&!rl&&r
500
MC/DC0%0/2
$context_node ne "" and not ref $context_node
$context_node ne "" not ref $context_node
Truth table33%1/3
A and not(B)
A= $context_node ne ""B= not ref $context_node
ABresult
0X0
100
111
193 0 ▶ if ($self->{XPathContext}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'XPathContext'}) { } 0 0 0
194 0 $context_node=$self->{XPathContext}->find($context_node)->get_node(1);
195 } else {
196 0 $context_node=$self->{node}->find($context_node)->get_node(1);
197 }
198 0 ▶ die "[$PACKAGE] Context node doesn't exist.\n" unless ($context_node);
Branch: 1 branch
BranchTrueFalseTotal
unless $context_node 0 0 0
199 } else {
200 5 ▶ $context_node ||= $self->{node};
Condition33%1/3
$context_node ||= $self->{"node"}
l!l&&r!l&&!r
050
MC/DC0%0/2
$context_node ||= $self->{"node"}
$context_node $self->{"node"}
Truth table33%1/3
A ||= B
A= $context_nodeB= $self->{"node"}
ABresult
1X1
011
000
201 }
202 5 $self->_createSteps($context_node,$xpath,$value);
203 }
204 }
205  
206 11 ▶sub _lookup_namespace {
Subroutine _lookup_namespace: called CC=11 SCAR=46.0
207 11 my ($self,$node,$name)=@_;
208 11 ▶ if ($name=~s/^(${NAMECHAR}+)://) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/^($XML::XSH2::XPathToXML::NAMECHAR+)://) { } 0 11 11
209 0 my $prefix = $1;
210 0 my $uri = $node->lookupNamespaceURI($prefix);
211 0 ▶ if (!defined($uri) and $self->{XPathContext} and
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $uri and $self->{"XPathContext"} and UNIVERSAL::can($self->{"XPathContext"}, "lookupNs")) 0 0 0
Condition0%0/6
not defined $uri and $self->{"XPathContext"} and UNIVERSAL::can($self->{"XPathContext"}, "lookupNs")
!ll&&!rl&&r
000
not defined $uri and $self->{"XPathContext"}
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $uri and $self->{"XPathContext"} and UNIVERSAL::can($self->{"XPathContext"}, "lookupNs")
not defined $uri $self->{"XPathContext"} UNIVERSAL::can($self->{"XPathContext"}, "lookupNs")
Truth table0%0/4
not(A) and B and C
A= not defined $uriB= $self->{"XPathContext"}C= UNIVERSAL::can($self->{"XPathContext"}, "lookupNs")
ABCresult
0XX0
10X0
1100
1111
212 UNIVERSAL::can($self->{XPathContext},'lookupNs')) {
213 0 $uri = $self->{XPathContext}->lookupNs($prefix)
214 }
215 0 ▶ if (!defined($uri) and ref($self->{namespaces})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $uri and ref $self->{"namespaces"}) 0 0 0
Condition0%0/3
not defined $uri and ref $self->{"namespaces"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $uri and ref $self->{"namespaces"}
not defined $uri ref $self->{"namespaces"}
Truth table0%0/3
not(A) and B
A= not defined $uriB= ref $self->{"namespaces"}
ABresult
0X0
100
111
216 0 $uri = $self->{namespaces}->{ $prefix };
217 }
218 0 ▶ if (!defined($uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $uri) { } 0 0 0
219 0 warn "Couldn't find namespace URI for the element ${prefix}:${node}!\n";
220 0 return ($prefix.':'.$name,undef);
221 } else {
222 # find the best suitable prefix
223 0 my $real_prefix = $node->lookupNamespacePrefix( $uri );
224 0 ▶ if ($real_prefix eq "" and !defined($node->lookupNamespaceURI( undef ))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($real_prefix eq "" and not defined $node->lookupNamespaceURI(undef)) 0 0 0
Condition0%0/3
$real_prefix eq "" and not defined $node->lookupNamespaceURI(undef)
!ll&&!rl&&r
000
MC/DC0%0/2
$real_prefix eq "" and not defined $node->lookupNamespaceURI(undef)
$real_prefix eq "" not defined $node->lookupNamespaceURI(undef)
Truth table0%0/3
A and not(B)
A= $real_prefix eq ""B= not defined $node->lookupNamespaceURI(undef)
ABresult
0X0
100
111
225 0 $real_prefix = $prefix;
226 }
227 0 ▶ return (($real_prefix ne "") ? $real_prefix.':'.$name : $name , $uri);
Branch: 1 branch
BranchTrueFalseTotal
$real_prefix ne '' ? : 0 0 0
228 }
229 } else {
230 11 return ($name,undef);
231 }
232 }
233  
234 11 ▶sub _insertNode {
Subroutine _insertNode: called CC=7 SCAR=27.0
235 11 my ( $self, $axis, $node, $next ) = @_;
236 11 ▶ if ($axis =~ /^$|^child::|^descendant(?:-or-self)?::/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($axis =~ /^$|^child::|^descendant(?:-or-self)?::/) { } 10 1 11
elsif ($axis =~ /^(following(?:-sibling)?)::/) { } 1 0 1
elsif ($axis =~ /^(preceding(?:-sibling)?)::/) { } 0 0 0
elsif ($axis =~ /^(parent|ancestor|ancestor-or-self|self|namespace)::/) { } 0 0 0
237 10 $node->appendChild( $next );
238 } elsif ($axis =~ /^(following(?:-sibling)?)::/) {
239 1 my $parent = $node->parentNode;
240 1 ▶ die "Can't create axis $1 on a document node" unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 1 1
241 1 $parent->insertAfter( $next, $node );
242 } elsif ($axis =~ /^(preceding(?:-sibling)?)::/) {
243 0 my $parent = $node->parentNode;
244 0 ▶ die "Can't create axis $1 on a document node" unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 0 0
245 0 $parent->insertBefore( $next, $node );
246 } elsif ($axis =~ /^(parent|ancestor|ancestor-or-self|self|namespace)::/) {
247 0 die "Can't create axis $1";
248 }
249 }
250  
251 8 ▶sub _createSteps {
Subroutine _createSteps: called CC=40 SCAR=58.5
252 8 my ($self,$node,$xpath,$value)=@_;
253  
254 8 ▶ while ($xpath ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($xpath ne "") 12 8 20
255 # get the first step
256 12 my $step = $xpath;
257 12 my $rest;
258 12 ▶ if ($step =~ s{^(.*?)\/(.*)$}{$1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step =~ s[^(.*?)\/(.*)$][$1]) 4 8 12
259 4 $rest = $2;
260 }
261  
262 12 ▶ print "$xpath : Processing step: $step (remains $rest)\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 12 12
263  
264 my $next = $self->{XPathContext} ?
265 12 ▶ $self->{XPathContext}->find($step,$node)->get_node(1) :
Branch: 1 branch
BranchTrueFalseTotal
$self->{'XPathContext'} ? : 12 0 12
266 $node->find($step)->get_node(1);
267  
268 12 ▶ unless ($next) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($next) 3 9 12
269 # auto-create the node(s) implied by the step
270  
271 3 ▶ if ($step =~ /^(?:@|attribute::)($NAME)/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($step =~ /^(?:@|attribute::)($XML::XSH2::XPathToXML::NAME)/) { } 0 3 3
elsif ($step =~ /^($XML::XSH2::XPathToXML::AXIS)text\(\)/) { } 0 3 3
elsif ($step =~ /^($XML::XSH2::XPathToXML::AXIS)comment\(\)/) { } 0 3 3
elsif ($step =~ /^($XML::XSH2::XPathToXML::AXIS)processing-instruction\(($XML::XSH2::XPathToXML::PREDICATE)\)/o) { } 0 3 3
272 0 my $name = $1;
273 0 ▶ if ($rest eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq '') { } 0 0 0
274 0 ▶ print "$xpath : auto-creating attribute $name for $step\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
275 0 my ($real_name,$uri) = $self->_lookup_namespace($node,$name);
276 0 ▶ if (defined($uri) and $uri ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri and $uri ne '') { } 0 0 0
Condition0%0/3
defined $uri and $uri ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $uri and $uri ne ""
defined $uri $uri ne ""
Truth table0%0/3
A and B
A= defined $uriB= $uri ne ""
ABresult
0X0
100
111
277 0 $node->setAttributeNS($uri,$name,$value);
278 0 return $node->getAttributeNodeNS($uri,$name);
279 } else {
280 0 $node->setAttribute($name,$value);
281 0 return $node->getAttributeNode($name);
282 }
283 } else {
284 0 die "[$PACKAGE] XPath steps follow after an attribute: $step/$rest\n";
285 }
286 } elsif ($step =~ /^($AXIS)text\(\)/) {
287 0 my $axis = $1;
288 0 ▶ if ($rest eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq '') { } 0 0 0
289 0 ▶ print "$xpath : auto-creating text() for $step\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
290 0 $next = $self->{doc}->createTextNode($value);
291 0 $self->_insertNode( $axis, $node, $next );
292 0 return $next;
293 } else {
294 0 die "[$PACKAGE] XPath steps follow after a text(): $step/$rest\n";
295 }
296 } elsif ($step =~ /^($AXIS)comment\(\)/) {
297 0 my $axis = $1;
298 0 ▶ if ($rest eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq '') { } 0 0 0
299 0 ▶ print "$xpath : auto-creating comment <!-- $value --> for $step\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
300 0 $next = $self->{doc}->createComment($value);
301 0 $self->_insertNode( $axis, $node, $next );
302 0 return $next;
303 } else {
304 0 die "[$PACKAGE] XPath steps follow after a text(): $step/$rest\n";
305 }
306 } elsif ($step =~ /^($AXIS)processing-instruction\((${PREDICATE})\)/o) {
307 0 my $axis = $1;
308 0 my $name = $2;
309 0 ▶ if ($name=~/^(?:\s*'([^']*)'|"([^"]*)"\s*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^(?:\s*'([^']*)'|"([^"]*)"\s*)$/) { } 0 0 0
310 0 $name=$1.$2;
311 0 ▶ if ($rest eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq '') { } 0 0 0
312 0 ▶ print "$xpath : auto-creating comment <!-- $value --> for $step\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
313 0 $next = $self->{doc}->createProcessingInstruction($name,$value);
314 0 $self->_insertNode( $axis, $node, $next );
315 0 return $next;
316 } else {
317 0 die "[$PACKAGE] XPath steps follow after a processing-instruction(): $step/$rest\n";
318 }
319 } else {
320 0 die "[$PACKAGE] Can't auto-create PI as specified ($name), use processing-instruction('name')\n";
321 }
322 } else {
323 3 my ($name,$axis);
324 3 ▶ if ($step =~ /^($AXIS)($NAME)(?!\()/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step =~ /^($XML::XSH2::XPathToXML::AXIS)($XML::XSH2::XPathToXML::NAME)(?!\()/) 3 0 3
325 3 $axis = $1;
326 3 $name = $2;
327 };
328 3 ▶ if ($name eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "") 0 3 3
329 0 die "[$PACKAGE] Can't determine element name from step $step\n";
330 }
331 3 my @auto;
332 3 ▶ do {{
Branch: 1 branch
BranchTrueFalseTotal
$next or do { do { { print "$xpath : auto-creating element $name for $step\n" if $self->{"debug"}; if ($self->{"maxAutoSiblings"} and @auto > $self->{"maxAutoSiblings"}) { $_->unlinkNode foreach (@auto); die qq[[$PACKAGE] Max automatic creation of siblings overflow ($self->{"maxAutoSiblings"}) for '${xpath}', step '${step}'!\n]; } ; () = $self->_lookup_namespace($node, $name); if (defined $uri and $uri ne "") { $next = $self->{"doc"}->createElementNS($uri, $real_name); } else { $next = $self->{"doc"}->createElement($real_name); } ; if ($node == $self->{"doc"}) { $node->setDocumentElement($next); last; } else { $self->_insertNode($axis, $node, $next); } ; push @auto, $next; $next = $self->{"XPathContext"} ? $self->{"XPathContext"}->find($step, $node)->get_node(1) : $node->find($step)->get_node(1); } } } 3 8 11
333 11 ▶ print "$xpath : auto-creating element $name for $step\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 11 11
334 11 ▶ if ($self->{maxAutoSiblings} && @auto>$self->{maxAutoSiblings}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"maxAutoSiblings"} and @auto > $self->{"maxAutoSiblings"}) 0 11 11
Condition33%1/3
$self->{"maxAutoSiblings"} and @auto > $self->{"maxAutoSiblings"}
!ll&&!rl&&r
0110
MC/DC0%0/2
$self->{"maxAutoSiblings"} and @auto > $self->{"maxAutoSiblings"}
$self->{"maxAutoSiblings"} @auto > $self->{"maxAutoSiblings"}
Truth table33%1/3
A and B
A= $self->{"maxAutoSiblings"}B= @auto > $self->{"maxAutoSiblings"}
ABresult
0X0
100
111
335 # unlink all added siblings
336 # print STDERR $self->{doc}->toString(1),"\n @auto\n";
337 0 $_->unlinkNode for @auto;
338 0 die "[$PACKAGE] Max automatic creation of siblings overflow ($self->{maxAutoSiblings}) for '$xpath', step '$step'!\n";
339 }
340 11 my ($real_name,$uri) = $self->_lookup_namespace($node,$name);
341 11 ▶ if (defined($uri) and $uri ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri and $uri ne '') { } 0 11 11
Condition33%1/3
defined $uri and $uri ne ""
!ll&&!rl&&r
1100
MC/DC0%0/2
defined $uri and $uri ne ""
defined $uri $uri ne ""
Truth table33%1/3
A and B
A= defined $uriB= $uri ne ""
ABresult
0X0
100
111
342 0 $next = $self->{doc}->createElementNS($uri,$real_name);
343 } else {
344 11 $next = $self->{doc}->createElement($real_name);
345 }
346 11 ▶ if ($node == $self->{doc}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node == $self->{'doc'}) { } 0 11 11
347 0 $node->setDocumentElement( $next );
348 # iterating won't help here
349 0 last;
350 } else {
351 11 $self->_insertNode( $axis, $node, $next );
352 }
353 11 push @auto,$next;
354  
355 $next = $self->{XPathContext} ?
356 11 ▶ $self->{XPathContext}->find($step,$node)->get_node(1) :
Branch: 1 branch
BranchTrueFalseTotal
$self->{'XPathContext'} ? : 11 0 11
357 $node->find($step)->get_node(1);
358 }} while (!$next);
359 }
360 }
361 12 $xpath = $rest;
362 12 $node = $next;
363 };
364  
365 8 ▶ if ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) { } 8 0 8
366 8 ▶ print "Setting value for $xpath\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 8 8
367 8 ▶ if ($node->nodeType == XML::LibXML::XML_ATTRIBUTE_NODE()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->nodeType == 2) { } 0 8 8
elsif ($node->nodeType != 1) { } 0 8 8
368 0 $node->setValue($value);
369 } elsif ($node->nodeType != XML::LibXML::XML_ELEMENT_NODE()) {
370 0 $node->setData($value);
371 } else {
372 8 ▶ $node->removeChildNodes() if $node->hasChildNodes;
Branch: 1 branch
BranchTrueFalseTotal
if $node->hasChildNodes 0 8 8
373 8 ▶ if (ref($value) and UNIVERSAL::isa($value,'XML::LibXML::Node')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and UNIVERSAL::isa($value, 'XML::LibXML::Node')) { } 0 8 8
Condition33%1/3
ref $value and UNIVERSAL::isa($value, "XML::LibXML::Node")
!ll&&!rl&&r
800
MC/DC0%0/2
ref $value and UNIVERSAL::isa($value, "XML::LibXML::Node")
ref $value UNIVERSAL::isa($value, "XML::LibXML::Node")
Truth table33%1/3
A and B
A= ref $valueB= UNIVERSAL::isa($value, "XML::LibXML::Node")
ABresult
0X0
100
111
374 0 $node->appendChild( $value );
375 } else {
376 8 ▶ $node->appendTextNode($value) if $value ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $value ne "" 6 2 8
377 }
378 }
379 } else {
380 0 warn "No node for XPath ($xpath)\n";
381 }
382 8 return $node;
383 }
384  
385 ###
386 #
387 # The following method use a hash to store all the xpaths and their nodes.
388 # Locating a node is purely based on looking up its xpath the way it's been
389 # written. Thus 'somenode[1]' and 'somenode[position()=1]' will be treated as
390 # two different nodes!
391 #
392 # Using proper xpath doc->find to retrieve nodes instead of the hash will
393 # eliminate this problem.
394 #
395 # Anyway:
396 # - if you have a 'node[1]' and a 'node[3]' a 2nd node will not be
397 # created inbetween.
398 # - if you have a 'node[position()=1]' and a 'node[2]' the nodes will
399 # be reversed in the doc (all xpaths are created in alphabetical order).
400 #
401 # ALL OF THESE PROBLEMS HAVE BEEN FIXED IN THE createNode METHOD ABOVE!
402 #
403 #
404 0 ▶sub _createNode_simple {
Subroutine _createNode_simple: not called CC=8 SCAR=42.8
405 0 my ($self, $xpath, $value) = @_;
406  
407 0 my $name = $xpath;
408 0 $name =~ s{^.*\/(.*)$}{$1};
409 0 $name =~ s{\[.*\]$}{};
410
411 0 my $parent = $xpath;
412 0 $parent =~ s{^(.*)\/.*$}{$1};
413  
414 0 ▶ if ($parent && !defined $self->{tree}->{$parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent and not defined $self->{"tree"}{$parent}) 0 0 0
Condition0%0/3
$parent and not defined $self->{"tree"}{$parent}
!ll&&!rl&&r
000
MC/DC0%0/2
$parent and not defined $self->{"tree"}{$parent}
$parent not defined $self->{"tree"}{$parent}
Truth table0%0/3
A and not(B)
A= $parentB= not defined $self->{"tree"}{$parent}
ABresult
0X0
100
111
415 0 $self->_createNode_simple($parent, undef);
416 }
417
418 # Attribute
419 0 ▶ if ($name =~ /^@/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^@/) { } 0 0 0
420 0 ▶ print " addAttribute: $xpath\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
421 0 $self->{tree}->{$parent}->setAttribute($name,$value);
422 }
423 # Element
424 else {
425 0 ▶ print " createElement: $xpath\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
426  
427 0 $self->{tree}->{$xpath} = $self->{doc}->createElement($name);
428
429 0 ▶ if ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) { } 0 0 0
430 0 $self->{tree}->{$parent}->appendChild( $self->{tree}->{$xpath} );
431 } else {
432 0 $self->{doc}->setDocumentElement( $self->{tree}->{$xpath} );
433 }
434
435 0 ▶ $self->{tree}->{$xpath}->appendTextNode($value) if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 0 0 0
436 }
437  
438 0 return undef;
439 }
440  
441  
442 ###########
443 2 1;
444  
445  
446 __END__
« XSH2/Functions.pm ↑ Summary XSH2/LibXMLCompat.pm »