XPath/Node/Element.pm

filter: Statementstmt 8.4% 19 / 225 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 14.2% 5 / 35 Podpod n/a totaltotal 6.4% 24 / 370 CC 56 SCAR 31.6
CC
56
Cov
6%
DESTROY
56.0
getNamespace
56.0
appendChild
42.0
CRAP 23.5
« XPath/Function.pm ↑ Summary XPath/Builder.pm »
1 package XML::XPath::Node::Element;
2  
3 1 $VERSION = '1.26';
4  
5 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw/@ISA/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 @ISA = ('XML::XPath::Node');
9  
10 package XML::XPath::Node::ElementImpl;
11  
12 1 ▶use vars qw/@ISA/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 @ISA = ('XML::XPath::NodeImpl', 'XML::XPath::Node::Element');
14 1 ▶use XML::XPath::Node ':node_keys';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub new {
17 0 ▶ my ($class, $tag, $prefix) = @_;
Subroutine new: not called CC=1 SCAR=6.9
18  
19 0 my $pos = XML::XPath::Node->nextPos;
20 0 my @vals;
21 0 @vals[node_global_pos, node_prefix, node_children, node_name, node_attribs] =
22 ($pos, $prefix, [], $tag, []);
23  
24 0 my $self = \@vals;
25 0 bless $self, $class;
26 }
27  
28 0 ▶sub getNodeType { ELEMENT_NODE }
Subroutine getNodeType: not called CC=1 SCAR=6.9
29  
30 0 ▶sub isElementNode { 1; }
Subroutine isElementNode: not called CC=1 SCAR=6.9
31  
32 sub appendChild {
33 0 ▶ my $self = shift;
Subroutine appendChild: not called CC=6 SCAR=37.4
34 0 my $newnode = shift;
35  
36 0 ▶ if (shift) { # called from internal to XML::XPath
Branch: 1 branch
BranchTrueFalseTotal
if (shift()) { } 0 0 0
37 # warn "AppendChild $newnode to $self\n";
38 0 push @{$self->[node_children]}, $newnode;
39 0 $newnode->setParentNode($self);
40 0 $newnode->set_pos($#{$self->[node_children]});
41 }
42 else {
43 0 ▶ if (@{$self->[node_children]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->[4];}) { } 0 0 0
44 0 $self->insertAfter($newnode, $self->[node_children][-1]);
45 }
46 else {
47 0 my $pos_number = $self->get_global_pos() + 1;
48  
49 0 ▶ if (my $brother = $self->getNextSibling()) { # optimisation
Branch: 1 branch
BranchTrueFalseTotal
if (my $brother = $self->getNextSibling) { } 0 0 0
50 0 ▶ if ($pos_number == $brother->get_global_pos()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos_number == $brother->get_global_pos) 0 0 0
51 0 $self->renumber('following::node()', +5);
52 }
53 }
54 else {
55 0 eval {
56 0 ▶ if ($pos_number == $self->findnodes('following::node()')->get_node(1)->get_global_pos()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos_number == $self->findnodes("following::node()")->get_node(1)->get_global_pos) 0 0 0
57 0 $self->renumber('following::node()', +5);
58 }
59 };
60 }
61  
62 0 push @{$self->[node_children]}, $newnode;
63 0 $newnode->setParentNode($self);
64 0 $newnode->set_pos($#{$self->[node_children]});
65 0 $newnode->set_global_pos($pos_number);
66 }
67 }
68 }
69  
70 sub removeChild {
71 0 ▶ my ($self, $delnode) = @_;
Subroutine removeChild: not called CC=2 SCAR=17.9
72  
73 0 my $pos = $delnode->get_pos;
74 # warn "removeChild: $pos\n";
75 # warn "children: ", scalar @{$self->[node_children]}, "\n";
76  
77 # my $node = $self->[node_children][$pos];
78 # warn "child at $pos is: $node\n";
79  
80 0 splice @{$self->[node_children]}, $pos, 1;
81  
82 # warn "children now: ", scalar @{$self->[node_children]}, "\n";
83  
84 0 ▶ for (my $i = $pos; $i < @{$self->[node_children]}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->[4];}) 0 0 0
85 # warn "Changing pos of child: $i\n";
86 0 $self->[node_children][$i]->set_pos($i);
87 }
88  
89 0 $delnode->del_parent_link;
90  
91 }
92  
93 sub appendIdElement {
94 0 ▶ my ($self, $val, $element) = @_;
Subroutine appendIdElement: not called CC=1 SCAR=6.9
95 # warn "Adding '$val' to ID hash\n";
96 0 $self->[node_ids]{$val} = $element;
97 }
98  
99 sub DESTROY {
100 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=7 SCAR=40.3
101 # warn "DESTROY ELEMENT: ", $self->[node_name], "\n";
102 # warn "DESTROY ROOT\n" unless $self->[node_name];
103  
104 0 foreach my $kid ($self->getChildNodes) {
105 0 ▶ $kid && $kid->del_parent_link;
Branch: 1 branch
BranchTrueFalseTotal
$kid and $kid->del_parent_link 0 0 0
106 }
107  
108 0 foreach my $attr ($self->getAttributeNodes) {
109 0 ▶ $attr && $attr->del_parent_link;
Branch: 1 branch
BranchTrueFalseTotal
$attr and $attr->del_parent_link 0 0 0
110 }
111  
112 0 foreach my $ns ($self->getNamespaceNodes) {
113 0 ▶ $ns && $ns->del_parent_link;
Branch: 1 branch
BranchTrueFalseTotal
$ns and $ns->del_parent_link 0 0 0
114 }
115  
116 # $self->[node_children] = undef;
117 # $self->[node_attribs] = undef;
118 # $self->[node_namespaces] = undef;
119 }
120  
121 sub getName {
122 0 ▶ my $self = shift;
Subroutine getName: not called CC=1 SCAR=6.9
123  
124 0 $self->[node_name];
125 }
126  
127 sub getTagName {
128 0 ▶ shift->getName(@_);
Subroutine getTagName: not called CC=1 SCAR=6.9
129 }
130  
131 sub getLocalName {
132 0 ▶ my $self = shift;
Subroutine getLocalName: not called CC=1 SCAR=6.9
133  
134 0 my $local = $self->[node_name];
135 0 $local =~ s/.*://;
136 0 return $local;
137 }
138  
139 sub getChildNodes {
140 0 ▶ my $self = shift;
Subroutine getChildNodes: not called CC=2 SCAR=17.9
141  
142 0 ▶ return wantarray ? @{$self->[node_children]} : $self->[node_children];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
143 }
144  
145 sub getChildNode {
146 0 ▶ my $self = shift;
Subroutine getChildNode: not called CC=3 SCAR=24.8
147  
148 0 my ($pos) = @_;
149 0 ▶ if ($pos < 1 || $pos > @{$self->[node_children]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 1 or $pos > @{$self->[4];}) 0 0 0
Condition0%0/3
$pos < 1 or $pos > @{$self->[4];}
l!l&&r!l&&!r
000
MC/DC0%0/2
$pos < 1 or $pos > @{$self->[4];}
$pos < 1 $pos > @{$self->[4];}
Truth table0%0/3
A or B
A= $pos < 1B= $pos > @{$self->[4];}
ABresult
1X1
011
000
150 0 return;
151 }
152 0 return $self->[node_children][$pos - 1];
153 }
154  
155 sub getFirstChild {
156 0 ▶ my $self = shift;
Subroutine getFirstChild: not called CC=2 SCAR=17.9
157  
158 0 ▶ return unless @{$self->[node_children]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->[4];} 0 0 0
159 0 return $self->[node_children][0];
160 }
161  
162 sub getLastChild {
163 0 ▶ my $self = shift;
Subroutine getLastChild: not called CC=2 SCAR=17.9
164  
165 0 ▶ return unless @{$self->[node_children]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->[4];} 0 0 0
166 0 return $self->[node_children][-1];
167 }
168  
169 sub getAttributeNode {
170 0 ▶ my ($self, $name) = @_;
Subroutine getAttributeNode: not called CC=3 SCAR=24.8
171  
172 0 my $attribs = $self->[node_attribs];
173 0 foreach my $attr (@$attribs) {
174 0 ▶ return $attr if $attr->getName eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $attr->getName eq $name 0 0 0
175 }
176 }
177  
178 sub getAttribute {
179 0 ▶ my $self = shift;
Subroutine getAttribute: not called CC=2 SCAR=17.9
180  
181 0 my $attr = $self->getAttributeNode(@_);
182 0 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 0 0 0
183 0 return $attr->getValue;
184 }
185 }
186  
187 sub getAttributes {
188 0 ▶ my $self = shift;
Subroutine getAttributes: not called CC=4 SCAR=30.0
189  
190 0 ▶ if ($self->[node_attribs]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[6]) 0 0 0
191 0 ▶ return wantarray ? @{$self->[node_attribs]} : $self->[node_attribs];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
192 }
193  
194 0 ▶ return wantarray ? () : [];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
195 }
196  
197 sub appendAttribute {
198 0 ▶ my $self = shift;
Subroutine appendAttribute: not called CC=6 SCAR=37.4
199 0 my $attribute = shift;
200  
201 0 ▶ if (shift) { # internal call
Branch: 1 branch
BranchTrueFalseTotal
if (shift()) { } 0 0 0
202 0 push @{$self->[node_attribs]}, $attribute;
203 0 $attribute->setParentNode($self);
204 0 $attribute->set_pos($#{$self->[node_attribs]});
205 }
206 else {
207 0 my $node_num;
208 0 ▶ if (@{$self->[node_attribs]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->[6];}) { } 0 0 0
209 0 $node_num = $self->[node_attribs][-1]->get_global_pos() + 1;
210 }
211 else {
212 0 $node_num = $self->get_global_pos() + 1;
213 }
214  
215 0 eval {
216 0 ▶ if (@{$self->[node_children]}) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->[4];}) { } 0 0 0
elsif ($node_num == $self->findnodes('following::node()')->get_node(1)->get_global_pos) { } 0 0 0
217 0 ▶ if ($node_num == $self->[node_children][-1]->get_global_pos()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node_num == $self->[4][-1]->get_global_pos) 0 0 0
218 0 $self->renumber('descendant::node() | following::node()', +5);
219 }
220 }
221 elsif ($node_num ==
222 $self->findnodes('following::node()')->get_node(1)->get_global_pos()) {
223 0 $self->renumber('following::node()', +5);
224 }
225 };
226  
227 0 push @{$self->[node_attribs]}, $attribute;
228 0 $attribute->setParentNode($self);
229 0 $attribute->set_pos($#{$self->[node_attribs]});
230 0 $attribute->set_global_pos($node_num);
231  
232 }
233 }
234  
235 sub removeAttribute {
236 0 ▶ my ($self, $attrib) = @_;
Subroutine removeAttribute: not called CC=3 SCAR=24.8
237  
238 0 ▶ if (!ref($attrib)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $attrib) 0 0 0
239 0 $attrib = $self->getAttributeNode($attrib);
240 }
241  
242 0 my $pos = $attrib->get_pos;
243  
244 0 splice @{$self->[node_attribs]}, $pos, 1;
245  
246 0 ▶ for (my $i = $pos; $i < @{$self->[node_attribs]}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->[6];}) 0 0 0
247 0 $self->[node_attribs][$i]->set_pos($i);
248 }
249  
250 0 $attrib->del_parent_link;
251 }
252  
253 sub setAttribute {
254 0 ▶ my ($self, $name, $value) = @_;
Subroutine setAttribute: not called CC=4 SCAR=30.0
255  
256 0 ▶ if (my $attrib = $self->getAttributeNode($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attrib = $self->getAttributeNode($name)) 0 0 0
257 0 $attrib->setNodeValue($value);
258 0 return $attrib;
259 }
260  
261 0 my ($nsprefix) = ($name =~ /^($XML::XPath::Parser::NCName):($XML::XPath::Parser::NCName)$/o);
262  
263 0 ▶ if ($nsprefix && !$self->getNamespace($nsprefix)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nsprefix and not $self->getNamespace($nsprefix)) 0 0 0
Condition0%0/3
$nsprefix and not $self->getNamespace($nsprefix)
!ll&&!rl&&r
000
MC/DC0%0/2
$nsprefix and not $self->getNamespace($nsprefix)
$nsprefix not $self->getNamespace($nsprefix)
Truth table0%0/3
A and not(B)
A= $nsprefixB= not $self->getNamespace($nsprefix)
ABresult
0X0
100
111
264 0 die "No namespace matches prefix: $nsprefix";
265 }
266  
267 0 my $newnode = XML::XPath::Node::Attribute->new($name, $value, $nsprefix);
268 0 $self->appendAttribute($newnode);
269 }
270  
271 sub setAttributeNode {
272 0 ▶ my ($self, $node) = @_;
Subroutine setAttributeNode: not called CC=4 SCAR=30.0
273  
274 0 ▶ if (my $attrib = $self->getAttributeNode($node->getName)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attrib = $self->getAttributeNode($node->getName)) 0 0 0
275 0 $attrib->setNodeValue($node->getValue);
276 0 return $attrib;
277 }
278  
279 0 my ($nsprefix) = ($node->getName() =~ /^($XML::XPath::Parser::NCName):($XML::XPath::Parser::NCName)$/o);
280  
281 0 ▶ if ($nsprefix && !$self->getNamespace($nsprefix)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nsprefix and not $self->getNamespace($nsprefix)) 0 0 0
Condition0%0/3
$nsprefix and not $self->getNamespace($nsprefix)
!ll&&!rl&&r
000
MC/DC0%0/2
$nsprefix and not $self->getNamespace($nsprefix)
$nsprefix not $self->getNamespace($nsprefix)
Truth table0%0/3
A and not(B)
A= $nsprefixB= not $self->getNamespace($nsprefix)
ABresult
0X0
100
111
282 0 die "No namespace matches prefix: $nsprefix";
283 }
284  
285 0 $self->appendAttribute($node);
286 }
287  
288 sub getNamespace {
289 0 ▶ my ($self, $prefix) = @_;
Subroutine getNamespace: not called CC=7 SCAR=40.3
290  
291 0 ▶ $prefix ||= $self->getPrefix || '#default';
Condition0%0/5
$prefix ||= $self->getPrefix || "#default"
l!l&&r!l&&!r
000
$self->getPrefix || '#default'
l!l
00
MC/DC0%0/2
$prefix ||= $self->getPrefix || "#default"
$prefix $self->getPrefix
Truth table0%0/3
A ||= B
A= $prefixB= $self->getPrefix
ABresult
1X1
011
000
292 0 ▶ my $namespaces = $self->[node_namespaces] || [];
Condition0%0/2
$self->[7] || []
l!l
00
MC/DC0%0/1
$self->[7] || []
$self->[7]
Truth table0%0/2
A
A= $self->[7]
Aresult
11
00
293 0 foreach my $ns (@$namespaces) {
294 0 ▶ return $ns if $ns->getPrefix eq $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $ns->getPrefix eq $prefix 0 0 0
295 }
296  
297 0 my $parent = $self->getParentNode;
298  
299 0 ▶ return $parent->getNamespace($prefix) if $parent;
Branch: 1 branch
BranchTrueFalseTotal
if $parent 0 0 0
300 }
301  
302 sub getNamespaces {
303 0 ▶ my $self = shift;
Subroutine getNamespaces: not called CC=4 SCAR=30.0
304  
305 0 ▶ if ($self->[node_namespaces]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[7]) 0 0 0
306 0 ▶ return wantarray ? @{$self->[node_namespaces]} : $self->[node_namespaces];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
307 }
308  
309 0 ▶ return wantarray ? () : [];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
310 }
311  
312 0 ▶sub getNamespaceNodes { goto &getNamespaces }
Subroutine getNamespaceNodes: not called CC=1 SCAR=6.9
313  
314 sub appendNamespace {
315 0 ▶ my ($self, $ns) = @_;
Subroutine appendNamespace: not called CC=1 SCAR=6.9
316  
317 0 push @{$self->[node_namespaces]}, $ns;
318 0 $ns->setParentNode($self);
319 0 $ns->set_pos($#{$self->[node_namespaces]});
320 }
321  
322 sub getPrefix {
323 0 ▶ my $self = shift;
Subroutine getPrefix: not called CC=1 SCAR=6.9
324 0 $self->[node_prefix];
325 }
326  
327 sub getExpandedName {
328 0 ▶ my $self = shift;
Subroutine getExpandedName: not called CC=1 SCAR=6.9
329 0 warn "Expanded name not implemented for ", ref($self), "\n";
330 0 return;
331 }
332  
333 sub _to_sax {
334 0 ▶ my ($self, $doch, $dtdh, $enth) = @_;
Subroutine _to_sax: not called CC=5 SCAR=34.0
335  
336 0 my $tag = $self->getName;
337 0 my @attr;
338  
339 0 for my $attr ($self->getAttributes) {
340 0 push @attr, $attr->getName, $attr->getValue;
341 }
342  
343 0 my $ns = $self->getNamespace($self->[node_prefix]);
344 0 ▶ if ($ns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns) { } 0 0 0
345 0 $doch->start_element(
346 {
347 Name => $tag,
348 Attributes => { @attr },
349 NamespaceURI => $ns->getExpanded,
350 Prefix => $ns->getPrefix,
351 LocalName => $self->getLocalName,
352 }
353 );
354 }
355 else {
356 0 $doch->start_element(
357 {
358 Name => $tag,
359 Attributes => { @attr },
360 }
361 );
362 }
363  
364 0 for my $kid ($self->getChildNodes) {
365 0 $kid->_to_sax($doch, $dtdh, $enth);
366 }
367  
368 0 ▶ if ($ns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns) { } 0 0 0
369 0 $doch->end_element(
370 {
371 Name => $tag,
372 NamespaceURI => $ns->getExpanded,
373 Prefix => $ns->getPrefix,
374 LocalName => $self->getLocalName
375 }
376 );
377 }
378 else {
379 0 $doch->end_element( { Name => $tag } );
380 }
381 }
382  
383 sub string_value {
384 0 ▶ my $self = shift;
Subroutine string_value: not called CC=4 SCAR=30.0
385  
386 0 my $string = '';
387 0 foreach my $kid (@{$self->[node_children]}) {
388 0 ▶ if ($kid->getNodeType == ELEMENT_NODE
Branch: 1 branch
BranchTrueFalseTotal
if ($kid->getNodeType == 1 or $kid->getNodeType == 3) 0 0 0
Condition0%0/3
$kid->getNodeType == 1 or $kid->getNodeType == 3
l!l&&r!l&&!r
000
MC/DC0%0/2
$kid->getNodeType == 1 or $kid->getNodeType == 3
$kid->getNodeType == 1 $kid->getNodeType == 3
Truth table0%0/3
A or B
A= $kid->getNodeType == 1B= $kid->getNodeType == 3
ABresult
1X1
011
000
389 || $kid->getNodeType == TEXT_NODE) {
390 0 $string .= $kid->string_value;
391 }
392 }
393  
394 0 return $string;
395 }
396  
397 sub toString {
398 0 ▶ my ($self, $norecurse) = @_;
Subroutine toString: not called CC=4 SCAR=30.0
399  
400 0 my $string = '';
401 0 ▶ if (! $self->[node_name] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[5]) 0 0 0
402 # root node
403 0 return join('', map { $_->toString($norecurse) } @{$self->[node_children]});
404 }
405  
406 0 $string .= "<" . $self->[node_name];
407 0 $string .= join('', map { $_->toString } @{$self->[node_namespaces]});
408 0 $string .= join('', map { $_->toString } @{$self->[node_attribs]});
409  
410 0 ▶ if (@{$self->[node_children]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->[4];}) { } 0 0 0
411 0 $string .= ">";
412  
413 0 ▶ if (!$norecurse) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($norecurse) 0 0 0
414 0 $string .= join('', map { $_->toString($norecurse) } @{$self->[node_children]});
415 }
416  
417 0 $string .= "</" . $self->[node_name] . ">";
418 }
419 else {
420 0 $string .= " />";
421 }
422  
423 0 return $string;
424 }
425  
426 1 1;
427 __END__
« XPath/Function.pm ↑ Summary XPath/Builder.pm »