DOM2/DOM/Element.pm

filter: Statementstmt 49.8% 169 / 339 Branchbran 29.4% 50 / 170 Conditioncond 42.3% 11 / 26 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 48.0% 25 / 52 Podpod 100% 48 / 48 totaltotal 47.0% 306 / 651 CC 105 SCAR 22.8
CC
105
Cov
43%
cloneNode
42.0
getAttributeNS
20.0
getAttributes
18.1
CRAP 9.8
« DOM2/Element/Document.pm ↑ Summary DOM2/Element.pm »
1 package XML::DOM2::DOM::Element;
2  
3 =head1 NAME
4  
5 XML::DOM2::DOM::Element - A library of DOM (Document Object Model) methods for XML Elements.
6  
7 =head1 DESCRIPTION
8  
9 Provides all the DOM method for XML Elements
10  
11 =head1 METHODS
12  
13 =cut
14  
15 4 ▶use base "XML::DOM2::DOM::NameSpace";
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head2 $element->getFirstChild()
21  
22 =head2 $element->firstChild()
23  
24 Returns the elements first child in it's children list
25  
26 =cut
27 1 ▶sub getFirstChild ($) {
Subroutine getFirstChild: called CC=2 SCAR=7.6
Pod documented
28 1 my $self=shift;
29  
30 1 ▶ if (my @children=$self->getChildren) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@children) = $self->getChildren) 1 0 1
31 1 return $children[0];
32 }
33 0 return undef;
34 }
35 4 *firstChild=\&getFirstChild;
36  
37 =head2 $element->getLastChild()
38  
39 =head2 $element->lastChild()
40  
41 Returns the elements last child in it's children list
42  
43 =cut
44 0 ▶sub getLastChild ($) {
Subroutine getLastChild: not called CC=2 SCAR=17.9
Pod documented
45 0 my $self=shift;
46  
47 0 ▶ if (my @children=$self->getChildren) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@children) = $self->getChildren) 0 0 0
48 0 return $children[-1];
49 }
50  
51 0 return undef;
52 }
53 4 *lastChild=\&getLastChild;
54  
55 =head2 $element->getChildIndex( @children )
56  
57 Return the array index of this element in the parent or the passed list (if there is one).
58  
59 =cut
60 1 ▶sub getChildIndex ($;@) {
Subroutine getChildIndex: called CC=5 SCAR=22.7
Pod documented
61 1 my ($self,@children)=@_;
62  
63 1 ▶ unless (@children) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@children) 0 1 1
64 0 my $parent=$self->getParent();
65 0 @children=$parent->getChildren();
66 0 ▶ return undef unless @children;
Branch: 1 branch
BranchTrueFalseTotal
unless @children 0 0 0
67 }
68  
69 1 for my $index (0..$#children) {
70 1 ▶ return $index if $children[$index] == $self;
Branch: 1 branch
BranchTrueFalseTotal
if $children[$index] == $self 1 0 1
71 }
72  
73 0 return undef;
74 }
75  
76 =head2 $element->getChildAtIndex( $index )
77  
78 Return the element at the specified index (the index can be negative).
79  
80 =cut
81 0 ▶sub getChildAtIndex ($$;@) {
Subroutine getChildAtIndex: not called CC=3 SCAR=24.8
Pod documented
82 0 my ($self,$index,@children)=@_;
83  
84 0 ▶ unless (@children) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@children) 0 0 0
85 0 my $parent=$self->getParent();
86 0 @children=$parent->getChildren();
87 0 ▶ return undef unless @children;
Branch: 1 branch
BranchTrueFalseTotal
unless @children 0 0 0
88 }
89  
90 0 return $children[$index];
91 }
92  
93 =head2 $element->getNextSibling()
94  
95 =head2 $element->nextSibling()
96  
97 Return the next element to this element in the parents child list.
98  
99 =cut
100 1 ▶sub getNextSibling ($) {
Subroutine getNextSibling: called CC=4 SCAR=15.5
Pod documented
101 1 my $self=shift;
102  
103 1 ▶ if (my $parent=$self->getParent) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->getParent) 1 0 1
104 1 my @children=$parent->getChildren();
105 1 my $index=$self->getChildIndex(@children);
106 1 ▶ if (defined $index and scalar(@children)>$index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index and scalar @children > $index) 1 0 1
Condition33%1/3
defined $index and scalar @children > $index
!ll&&!rl&&r
001
MC/DC0%0/2
defined $index and scalar @children > $index
defined $index scalar @children > $index
Truth table33%1/3
A and B
A= defined $indexB= scalar @children > $index
ABresult
0X0
100
111
107 1 return $children[$index+1];
108 }
109 }
110 0 return undef;
111 }
112 4 *nextSibling=\&getNextSibling;
113  
114 =head2 $element->getPreviousSibling()
115  
116 =head2 $element->previousSibling()
117  
118 Return the previous element to this element in the parents child list.
119  
120 =cut
121 0 ▶sub getPreviousSibling ($) {
Subroutine getPreviousSibling: not called CC=3 SCAR=24.8
Pod documented
122 0 my $self=shift;
123  
124 0 ▶ if (my $parent=$self->getParent) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->getParent) 0 0 0
125 0 my @children=$parent->getChildren();
126 0 my $index=$self->getChildIndex(@children);
127 0 ▶ if ($index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index) 0 0 0
128 0 return $children[$index-1];
129 }
130 }
131  
132 0 return undef;
133 }
134 4 *previousSibling=\&getPreviousSibling;
135  
136 =head2 $element->getChildren()
137  
138 =head2 $element->getChildElements()
139  
140 =head2 $element->getChildNodes()
141  
142 Returns all the elements children.
143  
144 =cut
145 24 ▶sub getChildren ($) {
Subroutine getChildren: called CC=2 SCAR=6.9
Pod documented
146 24 my $self=shift;
147 24 ▶ if ($self->{'children'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"children"}) 16 8 24
148 16 return @{$self->{'children'}};
149 }
150 8 return ();
151 }
152 4 *getChildElements=\&getChildren;
153 4 *getChildNodes=\&getChildren;
154  
155 =head2 $element->getChildrenByName( $name )
156  
157 Returns all the elements children with that tag name (including namespace prefix).
158  
159 =cut
160 sub getChildrenByName
161 0 ▶{
Subroutine getChildrenByName: not called CC=3 SCAR=24.8
Pod documented
162 0 my ($self, $name) = @_;
163 0 ▶ if(defined($self->{'child'}->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"child"}{$name}) 0 0 0
164 0 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
165 0 return @{$self->{'child'}->{$name}};
166 } else {
167 0 return $self->{'child'}->{$name}->[0];
168 }
169 }
170 }
171  
172 =head2 $element->hasChildren()
173  
174 =head2 $element->hasChildElements()
175  
176 =head2 $element->hasChildNodes()
177  
178 Returns 1 if this element has children.
179  
180 =cut
181 39 ▶sub hasChildren ($) {
Subroutine hasChildren: called CC=3 SCAR=11.0
Pod documented
182 39 my $self=shift;
183  
184 39 ▶ if (exists $self->{'children'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"children"}) 8 31 39
185 8 ▶ if (scalar @{$self->{'children'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"children"};}) 8 0 8
186 8 return 1;
187 }
188 }
189  
190 31 return 0;
191 }
192 4 *hasChildElements=\&hasChildren;
193 4 *hasChildNodes=\&hasChildren;
194  
195 =head2 $element->getParent()
196  
197 =head2 $element->getParentElement()
198  
199 =head2 $element->getParentNode()
200  
201 Returns the object of the parent element.
202  
203 =cut
204 85 ▶sub getParent ($) {
Subroutine getParent: called CC=2 SCAR=6.9
Pod documented
205 85 my $self=shift;
206  
207 85 ▶ if ($self->{'parent'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parent"}) 49 36 85
208 49 return $self->{'parent'};
209 }
210  
211 36 return undef;
212 }
213 4 *getParentElement=\&getParent;
214 4 *getParentNode=\&getParent;
215  
216 =head2 $element->setParent( $element )
217  
218 =head2 $element->setParentElement( $element )
219  
220 $element->setParent($parent);
221  
222 Sets the parent node, used internaly.
223  
224 =cut
225 18 ▶sub setParent ($$) {
Subroutine setParent: called CC=3 SCAR=12.7
Pod documented
226 18 my ($self,$parent) = @_;
227  
228 18 ▶ if(ref($parent) or not defined($parent)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parent or not defined $parent) 18 0 18
Condition33%1/3
ref $parent or not defined $parent
l!l&&r!l&&!r
1800
MC/DC0%0/2
ref $parent or not defined $parent
ref $parent not defined $parent
Truth table33%1/3
A or not(B)
A= ref $parentB= not defined $parent
ABresult
1X1
011
000
229 18 $self->{'parent'} = $parent;
230 18 return 1;
231 }
232  
233 0 return undef;
234 }
235 4 *setParentElement=\&setParent;
236  
237 =head2 $element->getParents()
238  
239 =head2 $element->getParentElements()
240  
241 =head2 $element->getParentNodes()
242  
243 =head2 $element->getAncestors()
244  
245 Return a list of the parents of the current element, starting from the immediate parent. The
246 last member of the list should be the document element.
247  
248 =cut
249 0 ▶sub getParents {
Subroutine getParents: not called CC=3 SCAR=24.8
Pod documented
250 0 my $self=shift;
251  
252 0 my $parent = $self->getParent;
253 0 ▶ return undef unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 0 0
254  
255 0 my @parents;
256 0 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 0 0 0
257 0 push @parents,$parent;
258 0 $parent=$parent->getParent;
259 }
260  
261 0 return @parents;
262 }
263 4 *getParentElements=\&getParents;
264 4 *getParentNodes=\&getParents;
265 4 *getAncestors=\&getParents;
266  
267 =head2 $element->isAncestor( $node )
268  
269 Returns true if the current element is an ancestor of the descendant element.
270  
271 =cut
272 0 ▶sub isAncestor ($$) {
Subroutine isAncestor: not called CC=3 SCAR=24.8
Pod documented
273 0 my ($self,$descendant)=@_;
274  
275 0 my @parents=$descendant->getParents();
276 0 foreach my $parent (@parents) {
277 0 ▶ return 1 if $parent==$self;
Branch: 1 branch
BranchTrueFalseTotal
if $parent == $self 0 0 0
278 }
279  
280 0 return 0;
281 }
282  
283 =head2 $element->isDescendant( $node )
284  
285 Return true if the crrent element is the descendant of the ancestor element.
286  
287 =cut
288 0 ▶sub isDescendant ($$) {
Subroutine isDescendant: not called CC=3 SCAR=24.8
Pod documented
289 0 my ($self,$ancestor)=@_;
290  
291 0 my @parents=$self->getParents();
292 0 foreach my $parent (@parents) {
293 0 ▶ return 1 if $parent==$ancestor;
Branch: 1 branch
BranchTrueFalseTotal
if $parent == $ancestor 0 0 0
294 }
295  
296 0 return 0;
297 }
298  
299 =head2 $element->getSiblings()
300  
301 Returns a list of sibling elements.
302  
303 =cut
304 0 ▶sub getSiblings ($) {
Subroutine getSiblings: not called CC=3 SCAR=24.8
Pod documented
305 0 my $self=shift;
306  
307 0 ▶ if (my $parent=$self->getParent) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->getParent) 0 0 0
308 0 return $parent->getChildren();
309 }
310  
311 0 ▶ return wantarray?():undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
312 }
313  
314 =head2 $element->hasSiblings()
315  
316 Returns true if the elements has sibling elements.
317  
318 =cut
319 0 ▶sub hasSiblings ($) {
Subroutine hasSiblings: not called CC=3 SCAR=24.8
Pod documented
320 0 my $self=shift;
321  
322 0 ▶ if (my $parent=$self->getParent) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->getParent) 0 0 0
323 0 my $siblings=scalar($parent->getChildren);
324 0 ▶ return 1 if $siblings>=2;
Branch: 1 branch
BranchTrueFalseTotal
if $siblings >= 2 0 0 0
325 }
326  
327 0 return undef;
328 }
329  
330 =head2 $element->getElementName()
331  
332 =head2 $element->getElementType()
333  
334 =head2 $element->getType()
335  
336 =head2 $element->getTagName()
337  
338 =head2 $element->getTagType()
339  
340 =head2 $element->getNodeName()
341  
342 =head2 $element->getNodeType()
343  
344 Return a string containing the name (i.e. the type, not the Id) of an element.
345  
346 =cut
347 0 ▶sub getElementName ($) {
Subroutine getElementName: not called CC=1 SCAR=6.9
Pod documented
348 0 my $self=shift;
349  
350 0 return $self->name;
351 }
352 4 *getType=\&getElementName;
353 4 *getElementType=\&getElementName;
354 4 *getTagName=\&getElementName;
355 4 *getTagType=\&getElementName;
356 4 *getNodeName=\&getElementName;
357 4 *getNodeType=\&getElementName;
358  
359 =head2 $element->getElementId()
360  
361 Return a string containing the elements Id (unique identifier string).
362  
363 =cut
364 18 ▶sub getElementId ($) {
Subroutine getElementId: called CC=2 SCAR=7.6
Pod documented
365 18 my $self=shift;
366  
367 18 ▶ if (exists $self->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"id"}) 0 18 18
368 0 return $self->{id};
369 }
370  
371 18 return undef;
372 }
373  
374 =head2 $element->getAttribute( $attributeName )
375  
376 Returns the specified attribute in the element, will return a
377 serialised string instead of posible attribute object if serialise set.
378  
379 =cut
380 sub getAttribute
381 10 ▶{
Subroutine getAttribute: called CC=1 SCAR=0.0
Pod documented
382 10 my ($self, $name) = @_;
383 10 my $attribute = $self->{'attributes'}->{''}->{$name};
384 10 return $attribute;
385 }
386  
387 =head2 $element->getAttributes( $serialise, $ns )
388  
389 Returns a list of attributes in various forms.
390  
391 =cut
392 sub getAttributes
393 3 ▶{
Subroutine getAttributes: called CC=10 SCAR=29.0
Pod documented
394 3 my ($self, $serialise, $ns) = @_;
395 3 my @names = $self->getAttributeNamesNS($ns);
396 3 my %attributes;
397 my @attributes;
398 3 foreach my $nsr (@names) {
399 3 my ($sns, $name) = @{$nsr};
400 3 my $attribute;
401 3 ▶ if($sns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sns) { } 0 3 3
402 0 $attribute = $self->getAttributeNS($sns, $name, $serialise);
403 } else {
404 3 $attribute = $self->getAttribute($name, $serialise);
405 }
406 3 ▶ if(not defined($attribute)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attribute) 0 3 3
407 0 die "Something is very wrong with the attributes";
408 }
409 3 ▶ if(not ref($attribute)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $attribute) 0 3 3
410 0 die "An attribute should always be an object: ($name:$attribute) ".$self->name."\n";
411 }
412 3 ▶ if($serialise <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($serialise <= 1) { } 0 3 3
413 0 $attributes{$attribute->name} = $attribute;
414 } else {
415 3 push @attributes, $attribute->serialise_full;
416 }
417 }
418 3 ▶ if($serialise <= 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($serialise <= 1) { } 0 3 3
elsif ($serialise == 2) { } 0 3 3
419 0 ▶ return wantarray ? %attributes : \%attributes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
420 } elsif($serialise == 2) {
421 0 ▶ return wantarray ? @attributes : \@attributes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
422 } else {
423 3 return join(' ', @attributes);
424 }
425 }
426  
427 =head2 $element->getAttributeNames()
428  
429 Returns a list of attribute names, used internaly.
430  
431 =cut
432 sub getAttributeNames
433 3 ▶{
Subroutine getAttributeNames: called CC=5 SCAR=16.6
Pod documented
434 3 my ($self, $ns) = @_;
435 3 ▶ my $prefix = $ns ? $ns->ns_prefix : '';
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 0 3 3
436 3 ▶ warn "The prefix is undefined!" if not defined($prefix);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 0 3 3
437 3 my @names;
438 3 foreach my $name (keys(%{$self->{'attributes'}->{$prefix}})) {
439 3 push @names, $name;
440 }
441 3 ▶ return wantarray ? @names : \@names;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
442 }
443  
444 =head2 $element->getAttributeNamesNS( $namespace )
445  
446 Returns a list of attribute names, used internaly.
447  
448 =cut
449 sub getAttributeNamesNS
450 3 ▶{
Subroutine getAttributeNamesNS: called CC=5 SCAR=16.7
Pod documented
451 3 my ($self, $ns) = @_;
452 # Default Namespace
453 3 my @names;
454  
455 # Get all other name spaces
456 3 ▶ my @ns = $ns ? ($ns) : $self->getAttributeNamespaces;
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 0 3 3
457  
458 3 foreach my $sns (@ns) {
459 3 ▶ if(defined($sns)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sns) { } 3 0 3
460 3 foreach my $name ($self->getAttributeNames($sns)) {
461 3 push @names, [ $sns, $name ];
462 }
463 } else {
464 0 warn "One of the name spaces is not defined\n";
465 }
466 }
467 3 return @names;
468 }
469  
470 =head2 $element->getAttributeNamespaces()
471  
472 Returns a list of attribute names, used internaly.
473  
474 =cut
475 sub getAttributeNamespaces
476 3 ▶{
Subroutine getAttributeNamespaces: called CC=2 SCAR=7.0
Pod documented
477 3 my ($self) = @_;
478 3 ▶ return map { $_ ne '' ? $self->document->getNamespace($_) : '' } keys(%{$self->{'attributes'}});
Branch: 1 branch
BranchTrueFalseTotal
$_ ne '' ? : 0 3 3
479 }
480  
481 =head2 $element->hasAttribute( $attributeName )
482  
483 Returns true if this element as this attribute.
484  
485 =cut
486 sub hasAttribute
487 0 ▶{
Subroutine hasAttribute: not called CC=2 SCAR=17.9
Pod documented
488 0 my ($self, $name) = @_;
489 0 ▶ return 1 if exists( $self->{'attributes'}->{''}->{$name} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"attributes"}{""}{$name} 0 0 0
490 }
491  
492 =head2 $element->hasAttributeNS( $namespace, $attributeName )
493  
494 Returns true if this attribute in this namespace is in this element.
495  
496 =cut
497 sub hasAttributeNS
498 0 ▶{
Subroutine hasAttributeNS: not called CC=2 SCAR=17.9
Pod documented
499 0 my ($self, $ns, $name) = @_;
500 0 my $prefix = $ns->ns_prefix;
501 0 ▶ return 1 if exists( $self->{'attributes'}->{$prefix}->{$name} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"attributes"}{$prefix}{$name} 0 0 0
502 }
503  
504 =head2 $element->hasAttributes()
505  
506 Return true is element has any attributes
507  
508 =cut
509  
510 sub hasAttributes
511 10 ▶{
Subroutine hasAttributes: called CC=3 SCAR=11.0
Pod documented
512 10 my ($self) = @_;
513 10 ▶ return 1 if $self->{'attributes'} and keys(%{ $self->{'attributes'} })
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"} and keys %{$$self{"attributes"};} 3 7 10
Condition66%2/3
$self->{"attributes"} and keys %{$$self{"attributes"};}
!ll&&!rl&&r
703
MC/DC50%1/2
$self->{"attributes"} and keys %{$$self{"attributes"};}
$self->{"attributes"} keys %{$$self{"attributes"};}
Truth table66%2/3
A and B
A= $self->{"attributes"}B= keys %{$$self{"attributes"};}
ABresult
0X0
100
111
514 }
515  
516 =head2 $element->setAttribute( $attribute, $value )
517  
518 Set an attribute on this element, it will accept serialised strings or objects.
519  
520 =cut
521 sub setAttribute
522 6 ▶{
Subroutine setAttribute: called CC=7 SCAR=22.0
Pod documented
523 6 my ($self, $name, $value) = @_;
524 6 ▶ confess "Name is not defined" if not $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 6 6
525 6 my $existing = $self->getAttribute($name);
526 # This ensures that ids are updated in a sane way.
527 6 ▶ if ($name eq "id" and $self->document and defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "id" and $self->document and defined $value) 2 4 6
Condition66%4/6
$name eq "id" and $self->document and defined $value
!ll&&!rl&&r
402
$name eq "id" and $self->document
!ll&&!rl&&r
402
MC/DC33%1/3
$name eq "id" and $self->document and defined $value
$name eq "id" $self->document defined $value
Truth table50%2/4
A and B and C
A= $name eq "id"B= $self->documentC= defined $value
ABCresult
0XX0
10X0
1100
1111
528 # Set the new id
529 2 ▶ if($self->document->addId($value, $self)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->document->addId($value, $self)) { } 2 0 2
530 2 ▶ if($existing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing) 0 2 2
531 # Remove the old id
532 0 my $oldvalue = $existing->serialise;
533 0 $self->document->removeId($oldvalue);
534 }
535 } else {
536 0 $self->error('setAttribute', "Id '$value' already exists in document, unable to modify attribute");
537 0 return undef;
538 }
539 }
540  
541 # Some elements can't contain attributes
542 6 $self->{'attributes'}->{''}->{$name} = $self->_get_attribute_object( $name, $value, undef, $existing );
543 6 return 1;
544 }
545  
546 sub _get_attribute_object
547 6 ▶{
Subroutine _get_attribute_object: called CC=6 SCAR=19.5
548 6 my ($self, $name, $value, $ns, $existing) = @_;
549 6 ▶ if(not $self->_can_contain_attributes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_can_contain_attributes) 0 6 6
550 0 $self->error('setAttribute', "This Element can not contain attributes. (".$self->getElementName.")");
551 0 return undef;
552 }
553 # undef means delete attribute
554 6 ▶ return $self->removeAttribute($name) if not defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 6 6
555 6 my $result;
556 # This is to handle attributes handled by objects
557 6 ▶ if($self->_has_attribute($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_attribute($name)) 6 0 6
558 6 $result = $existing;
559 6 ▶ if(not $result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 6 0 6
560 # Create a new attribute
561 6 $result = $self->_attribute_handle( $name, name => $name, namespace => $ns, owner => $self );
562
563 }
564 6 ▶ croak "Unable to setAttribute, _attribute_handle does not exist (".ref($self).":$name)" if not ref($result);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result 0 6 6
565 6 $result->deserialise($value);
566 }
567 6 return $result;
568 }
569  
570 =head2 $element->removeAttribute( $name )
571  
572 Remove a single attribute from this element.
573  
574 =cut
575 sub removeAttribute
576 0 ▶{
Subroutine removeAttribute: not called CC=2 SCAR=17.9
Pod documented
577 0 my ($self, $name) = @_;
578 0 ▶ if($self->hasAttribute($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hasAttribute($name)) 0 0 0
579 0 my $attribute = delete($self->{'attributes'}->{''}->{$name});
580 0 $attribute->delete;
581 }
582 }
583  
584 =head2 $element->removeAttributeNS( $namespace, $name )
585  
586 Remove a single attribute from this element.
587  
588 =cut
589 sub removeAttributeNS
590 0 ▶{
Subroutine removeAttributeNS: not called CC=2 SCAR=17.9
Pod documented
591 0 my ($self, $ns, $name) = @_;
592 0 ▶ if($self->hasAttributeNS($ns, $name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hasAttributeNS($ns, $name)) 0 0 0
593 0 my $attribute = delete($self->{'attributes'}->{$ns->ns_prefix}->{$name});
594 0 $attribute->delete;
595 }
596 }
597  
598 =head2 $element->getAttributeNS( $namespace, $name )
599  
600 Returns an attributes namespace in this element.
601  
602 =cut
603 sub getAttributeNS
604 0 ▶{
Subroutine getAttributeNS: not called CC=4 SCAR=30.0
Pod documented
605 0 my ($self, $ns, $name) = @_;
606 0 ▶ if(not ref($ns)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ns) 0 0 0
607 0 confess "You must give ns methods the name space object, not just the URI or Prefix (skipped)";
608 }
609 0 my $prefix = $ns->ns_prefix;
610 0 ▶ $prefix = '' if not $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix 0 0 0
611 0 ▶ if($self->{'attributes'}->{$prefix}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"attributes"}{$prefix}{$name}) 0 0 0
612 0 return $self->{'attributes'}->{$prefix}->{$name};
613 }
614 }
615  
616 =head2 $element->setAttributeNS( $namespace, $name, $value )
617  
618 Sets an attributes namespace in this element.
619  
620 =cut
621 sub setAttributeNS
622 0 ▶{
Subroutine setAttributeNS: not called CC=3 SCAR=24.8
Pod documented
623 0 my ($self, $ns, $name, $value) = @_;
624 0 ▶ if(not ref($ns)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ns) 0 0 0
625 0 confess "You must give ns methods the name space object, not just the URI or Prefix (skipped)";
626 }
627 0 my $prefix = $ns->ns_prefix;
628 0 $self->{'attributes'}->{$prefix}->{$name} = $self->_get_attribute_object($name, $value, $ns);
629 0 ▶ if(not $self->{'attributes'}->{$prefix}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"attributes"}{$prefix}{$name}) 0 0 0
630 0 warn "setAttributeNS was unable to set the attribute ";
631 }
632 }
633  
634 =head2 $element->cdata( $text )
635  
636 Rerieve and set this elements cdata (non tag cdata form)
637  
638 =cut
639 sub cdata
640 19 ▶{
Subroutine cdata: called CC=4 SCAR=15.2
Pod documented
641 19 my ($self, $text) = @_;
642 19 ▶ if($self->hasChildren()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hasChildren) 0 19 19
643 0 $self->error(value => "Unable to get cdata for element with children, xml error!");
644 0 return;
645 }
646 19 ▶ if(defined($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 12 7 19
647 12 ▶ if(ref($text) =~ /CDATA/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($text) =~ /CDATA/) { } 0 12 12
648 0 $self->{'cdata'} = $text;
649 } else {
650 12 $self->{'cdata'} = XML::DOM2::Element::CDATA->new($text, notag => 1);
651 }
652 }
653 19 return $self->{'cdata'};
654 }
655  
656 =head2 $element->hasCDATA()
657  
658 Return true if this element has cdata.
659  
660 =cut
661 6 ▶sub hasCDATA ($) {
Subroutine hasCDATA: called CC=1 SCAR=0.0
Pod documented
662 6 my $self=shift;
663 6 return exists($self->{'cdata'});
664 }
665  
666 =head2 $element->document()
667  
668 Return this elements document, returns undef if no document available.
669  
670 =cut
671 sub document
672 94 ▶{
Subroutine document: called CC=3 SCAR=18.1
Pod documented
673 94 my ($self) = @_;
674 94 ▶ return $self->{'document'} if ref($self->{'document'});
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"document"} 94 0 94
675 0 ▶ if($self->getParent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getParent) { } 0 0 0
676 0 return $self->getParent->document;
677 } else {
678 0 confess "Where you expecting an orphaned element ".$self->localName."\n";
679 0 return undef;
680 }
681 }
682  
683 =head2 $element->insertBefore( $node, $childNode )
684  
685 =head2 $element->insertChildBefore( $node, $childNode )
686  
687 =head2 $element->insertNodeBefore( $node, $childNode )
688  
689 =head2 $element->insertElementBefore( $node, $childNode )
690  
691 Inserts a new element just before the referenced child.
692  
693 =cut
694 sub insertBefore
695 0 ▶{
Subroutine insertBefore: not called CC=3 SCAR=24.8
Pod documented
696 0 my ($self, $newChild, $refChild) = @_;
697 0 ▶ return $self->appendElement($newChild) if not $refChild;
Branch: 1 branch
BranchTrueFalseTotal
unless $refChild 0 0 0
698 0 my $index = $self->findChildIndex($refChild);
699 0 ▶ return 0 if $index < 0; # NO_FOUND_ERR
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 0 0
700 0 return $self->insertAtIndex($newChild, $index);
701 }
702 4 *insertChildBefore=\&insertBefore;
703 4 *insertNodeBefore=\&insertBefore;
704 4 *insertElementBefore=\&insertBefore;
705  
706 =head2 $element->insertAfter( $node, $childNode )
707  
708 =head2 $element->insertChildAfter( $node, $childNode )
709  
710 =head2 $element->insertElementAfter( $node, $childNode )
711  
712 =head2 $element->insertNodeAfter( $node, $childNode )
713  
714 Inserts a new child element just after the referenced child.
715  
716 =cut
717 sub insertAfter
718 0 ▶{
Subroutine insertAfter: not called CC=3 SCAR=24.8
Pod documented
719 0 my ($self, $newChild, $refChild) = @_;
720 0 ▶ return $self->appendElement($newChild) if not $refChild;
Branch: 1 branch
BranchTrueFalseTotal
unless $refChild 0 0 0
721 0 my $index = $self->findChildIndex($refChild);
722 0 ▶ return 0 if $index < 0; # NO_FOUND_ERR
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 0 0
723 0 return $self->insertAtIndex($newChild, $index+1);
724 }
725 4 *insertChildAfter=\&insertAfter;
726 4 *insertNodeAfter=\&insertAfter;
727 4 *insertElementAfter=\&insertAfter;
728  
729 =head2 $element->insertSiblingAfter( $node )
730  
731 Inserts the child just after the current element (effects parent).
732  
733 =cut
734 sub insertSiblingAfter
735 0 ▶{
Subroutine insertSiblingAfter: not called CC=2 SCAR=17.9
Pod documented
736 0 my ($self, $newChild) = @_;
737 0 ▶ return $self->getParent->insertAfter($newChild, $self) if $self->getParent;
Branch: 1 branch
BranchTrueFalseTotal
if $self->getParent 0 0 0
738 0 return 0;
739 }
740  
741 =head2 $element->insertSiblingBefore( $node )
742  
743 Inserts the child just before the current element (effects parent).
744  
745 =cut
746 sub insertSiblingBefore
747 0 ▶{
Subroutine insertSiblingBefore: not called CC=2 SCAR=17.9
Pod documented
748 0 my ($self, $newChild) = @_;
749 0 ▶ return $self->getParent->insertBefore($newChild, $self) if $self->getParent;
Branch: 1 branch
BranchTrueFalseTotal
if $self->getParent 0 0 0
750 0 return 0;
751 }
752  
753 =head2 $element->replaceChild( $newChild, $oldChild )
754  
755 Replace an old child with a new element, returns old element.
756  
757 =cut
758 sub replaceChild
759 0 ▶{
Subroutine replaceChild: not called CC=3 SCAR=24.8
Pod documented
760 0 my ($self, $newChild, $oldChild) = @_;
761 # Replace newChild if it is in this list of children already
762 0 ▶ $self->removeChild($newChild) if $newChild->getParent eq $self;
Branch: 1 branch
BranchTrueFalseTotal
if $newChild->getParent eq $self 0 0 0
763 # We need the index of the node to replace
764 0 my $index = $self->findChildIndex($oldChild);
765 0 ▶ return 0 if($index < 0); # NOT_FOUND_ERR
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 0 0
766 # Replace and bind new node with it's family
767 0 $self->removeChildAtIndex($index);
768 0 $self->insertChildAtIndex($index);
769 0 return $oldChild;
770 }
771  
772 =head2 $element->replaceElement( $newElement )
773  
774 =head2 $element->replaceNode( $newElement )
775  
776 Replace an old element with a new element in the parents context; element becomes orphaned.
777  
778 =cut
779 sub replaceElement
780 0 ▶{
Subroutine replaceElement: not called CC=1 SCAR=6.9
Pod documented
781 0 my ($self, $newElement) = @_;
782 0 return $self->getParent->replaceChild($newElement, $self);
783 }
784 4 *replaceNode=\&replaceElement;
785  
786 =head2 $element->removeChild( $child )
787  
788 Remove a child from this element, returns the orphaned element.
789  
790 =cut
791 sub removeChild
792 0 ▶{
Subroutine removeChild: not called CC=3 SCAR=24.8
Pod documented
793 0 my ($self, $oldChild) = @_;
794 0 my $index = $self->findChildIndex($oldChild);
795 0 ▶ return 0 if(not defined $index or $index < 0); # NOT_FOUND_ERR
Branch: 1 branch
BranchTrueFalseTotal
if not defined $index or $index < 0 0 0 0
Condition0%0/3
not defined $index or $index < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $index or $index < 0
not defined $index $index < 0
Truth table0%0/3
not(A) or B
A= not defined $indexB= $index < 0
ABresult
1X1
011
000
796 0 return $self->removeChildAtIndex($index);
797 }
798  
799 =head2 $element->removeElement()
800  
801 =head2 $element->removeNode()
802  
803 Removes this element from it's parent; element becomes orphaned.
804  
805 =cut
806 sub removeElement
807 0 ▶{
Subroutine removeElement: not called CC=1 SCAR=6.9
Pod documented
808 0 my ($self) = @_;
809 0 return $self->getParent->removeChild($self);
810 }
811 4 *removeNode=\&removeElement;
812  
813 =head2 $element->appendChild( $node )
814  
815 =head2 $element->appendElement( $node )
816  
817 =head2 $element->appendNode( $node )
818  
819 Adds the new child to the end of this elements children list.
820  
821 =cut
822 sub appendChild
823 18 ▶{
Subroutine appendChild: called CC=2 SCAR=6.9
Pod documented
824 18 my ($self, $element) = @_;
825 18 ▶ return $self->insertAtIndex( $element, scalar($self->getChildren) || 0 );
Condition100%2/2
scalar $self->getChildren || 0
l!l
108
MC/DC100%1/1
scalar $self->getChildren || 0
scalar $self->getChildren
Truth table100%2/2
A
A= scalar $self->getChildren
Aresult
11
00
826 }
827 4 *appendElement=\&appendChild;
828 4 *appendNode=\&appendChild;
829  
830 =head2 $element->cloneNode( $deep )
831  
832 =head2 $element->cloneElement( $deep )
833  
834 Clones the current element, deep allows all child elements to be cloned.
835 The new element is an orphan with all the same id's and atributes as this element.
836  
837 =cut
838 sub cloneNode
839 0 ▶{
Subroutine cloneNode: not called CC=6 SCAR=37.4
Pod documented
840 0 my ($self, $deep) = @_;
841 0 my $clone = XML::DOM2::Element->new($self->localName);
842 0 foreach my $key (keys(%{$self})) {
843 0 ▶ if($key ne 'children' and $key ne 'parent') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne "children" and $key ne "parent") 0 0 0
Condition0%0/3
$key ne "children" and $key ne "parent"
!ll&&!rl&&r
000
MC/DC0%0/2
$key ne "children" and $key ne "parent"
$key ne "children" $key ne "parent"
Truth table0%0/3
A and B
A= $key ne "children"B= $key ne "parent"
ABresult
0X0
100
111
844 0 $clone->{$key} = $self->{$key};
845 }
846 }
847 # We need to clone the children if deep is specified.
848 0 ▶ if($deep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep) 0 0 0
849 0 foreach my $child ($self->getChilden) {
850 0 my $childClone = $child->cloneNode($deep);
851 0 $clone->appendChild($childClone);
852 }
853 }
854 0 return $clone;
855 }
856 4 *cloneElement=\&cloneNode;
857  
858 =head2 $element->findChildIndex( $child )
859  
860 Scans through children trying to find this child in the list.
861  
862 =cut
863 sub findChildIndex
864 0 ▶{
Subroutine findChildIndex: not called CC=3 SCAR=24.8
Pod documented
865 0 my ($self, $refChild) = @_;
866 0 my $index;
867 0 foreach my $child ($self->getChildren) {
868 0 ▶ return $index if $child eq $refChild;
Branch: 1 branch
BranchTrueFalseTotal
if $child eq $refChild 0 0 0
869 0 $index++;
870 }
871 0 return -1;
872 }
873  
874 =head2 $element->insertAtIndex( $node, $index )
875  
876 Adds the new child at the specified index to this element.
877  
878 =cut
879 sub insertAtIndex
880 18 ▶{
Subroutine insertAtIndex: called CC=7 SCAR=21.4
Pod documented
881 18 my ($self, $newChild, $index) = @_;
882 18 ▶ confess "Unable to insertAtIndex no index defined" if not defined($index);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 18 18
883 18 my $id = $newChild->getElementId();
884 18 ▶ if($self->document) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->document) { } 18 0 18
885 18 ▶ if($id && not $self->document->addId($id, $newChild)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id and not $self->document->addId($id, $newChild)) 0 18 18
Condition33%1/3
$id and not $self->document->addId($id, $newChild)
!ll&&!rl&&r
1800
MC/DC0%0/2
$id and not $self->document->addId($id, $newChild)
$id not $self->document->addId($id, $newChild)
Truth table33%1/3
A and not(B)
A= $idB= not $self->document->addId($id, $newChild)
ABresult
0X0
100
111
886 0 $self->error($id => "Id already exists in document");
887 0 return undef;
888 }
889 18 $self->document->addElement($newChild);
890 } else {
891 0 warn("Unable to insert element ".$self->getElementName." not document defined");
892 0 return 0;
893 }
894 # Remove the child from other documents and nodes
895 18 ▶ $newChild->getParent->removeChild($newChild) if $newChild->getParent;
Branch: 1 branch
BranchTrueFalseTotal
if $newChild->getParent 0 18 18
896  
897 # This index supports the getChildrenByName function
898 18 ▶ if($self->{'child'}->{$newChild->name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'child'}{$newChild->name}) { } 4 14 18
899 4 push @{$self->{'child'}->{$newChild->name}}, $newChild;
900 } else {
901 14 $self->{'child'}->{$newChild->name} = [ $newChild ];
902 }
903  
904 # Set in new parent
905 18 splice @{$self->{'children'}}, $index, 0, $newChild;
906 18 $newChild->setParent($self);
907 18 return 1;
908 }
909  
910 =head2 $element->removeChildAtIndex( $index )
911  
912 Removed the child at index and returns the now orphaned element.
913  
914 =cut
915 sub removeChildAtIndex
916 0 ▶{
Subroutine removeChildAtIndex: not called CC=3 SCAR=24.8
Pod documented
917 0 my ($self, $index) = @_;
918 0 my $oldChild = splice @{$self->{'children'}}, $index, 1;
919 0 my $id = $oldChild->getElementId();
920 0 ▶ $self->document->removeId($id) if($id);
Branch: 1 branch
BranchTrueFalseTotal
if $id 0 0 0
921 0 $self->document->removeElement($oldChild);
922 0 $oldChild->setParent(undef);
923 0 ▶ if(not $self->hasChildren) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->hasChildren) 0 0 0
924 0 delete $self->{'childen'};
925 }
926 0 return $oldChild;
927 }
928  
929 =head2 $element->createChildElement( $name, %options )
930  
931 =head2 $element->createElement( $name, %options )
932  
933 Not DOM2, creates a child element, appending to current element.
934  
935 The advantage to using this method is the elements created
936 with $document->createElement create basic element objects or
937 base objects (those specified in the XML base class or it's kin)
938 Elements created with this could offer more complex objects back.
939  
940 Example: an SVG Gradiant will have stop elements under it, creating
941 stop elements with $document->createElement will return an XML::DOM2::Element
942 create a stop element with $element->createChildElement and it will
943 return an SVG2::Element::Gradiant::Stop object (although both would
944 output the same xml) and it would also prevent you from creating invalid
945 child elements such as a group within a text element.
946  
947 $element->createChildElement($name, %opts);
948  
949 =cut
950  
951 sub createChildElement
952 18 ▶{
Subroutine createChildElement: called CC=2 SCAR=7.2
Pod documented
953 18 my ($self, $name, %opts) = @_;
954 18 my $element = $self->_element_handle($name, %opts, document => $self->document() );
955 18 ▶ if(ref($element) =~ /CDATA/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($element) =~ /CDATA/) { } 0 18 18
956 0 $self->cdata( $element );
957 } else {
958 18 $self->appendChild($element);
959 }
960 18 return $element;
961 }
962 4 *createElement=\&createChildElement;
963  
964 =head1 AUTHOR
965  
966 Martin Owens, doctormo@postmaster.co.uk
967  
968 =head1 SEE ALSO
969  
970 perl(1), L<XML::DOM2>, L<XML::DOM2::Element>
971  
972 L<http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core.html> DOM at the W3C
973  
974 =cut
975  
976 4 return 1;
« DOM2/Element/Document.pm ↑ Summary DOM2/Element.pm »