DOM2/DOM/Document.pm

filter: Statementstmt 55.3% 73 / 132 Branchbran 34.0% 17 / 50 Conditioncond 23.5% 4 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 53.5% 15 / 28 Podpod 100% 22 / 22 totaltotal 50.3% 131 / 260 CC 33 SCAR 26.0
CC
33
Cov
47%
getElements
72.0
createElementNS
12.0
documentElement
8.0
CRAP 13.4
↑ Summary DOM2/Parser.pm »
1 package XML::DOM2::DOM::Document;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 XML::DOM2::DOM::Document
9  
10 =head1 DESCRIPTION
11  
12 Base class for document objects, extends the DOM with document specific methods.
13  
14 =head1 METHODS
15  
16 =cut
17  
18 4 ▶use XML::DOM2::Element::DocumentType;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use XML::DOM2::Attribute::Namespace;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use XML::DOM2;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head2 $class->createDocumentType( $qualifiedName, $publicId, $systemId, $dtd )
24  
25 Create a new XML Document Type.
26  
27 =cut
28 sub createDocumentType
29 2 ▶{
Subroutine createDocumentType: called CC=1 SCAR=0.0
Pod documented
30 2 my ($self, $qualifiedName, $publicId, $systemId, $dtd) = @_;
31 2 my $doctype = XML::DOM2::Element::DocumentType->new(
32 name => $qualifiedName,
33 publicid => $publicId,
34 systemid => $systemId,
35 dtd => $dtd,
36 );
37 2 return $doctype;
38 }
39  
40 =head2 $class->createDocument( $namespaceURI, $qualifiedName, $doctype )
41  
42 Creates a new XML Document.
43  
44 =cut
45 sub createDocument
46 2 ▶{
Subroutine createDocument: called CC=3 SCAR=12.0
Pod documented
47 2 my ($proto, $namespaceURI, $qualifiedName, $doctype) = @_;
48 2 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
49 2 ▶ $doctype ||= XML::DOM2->createDocumentType;
Condition33%1/3
$doctype ||= "XML::DOM2"->createDocumentType
l!l&&r!l&&!r
020
MC/DC0%0/2
$doctype ||= "XML::DOM2"->createDocumentType
$doctype "XML::DOM2"->createDocumentType
Truth table33%1/3
A ||= B
A= $doctypeB= "XML::DOM2"->createDocumentType
ABresult
1X1
011
000
50 2 my $document = $class->new(
51 namespace => $namespaceURI,
52 name => $qualifiedName,
53 doctype => $doctype,
54 );
55 2 $doctype->ownerDocument($document);
56 2 return $document;
57 }
58  
59 =head2 $document->documentElement()
60  
61 Returns the main document as an element! it's no longer a document object.
62  
63 =cut
64 sub documentElement
65 3 ▶{
Subroutine documentElement: called CC=6 SCAR=20.8
Pod documented
66 3 my ($self, $setObj) = @_;
67 3 ▶ if(not $self->{'element'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"element"}) 2 1 3
68 2 ▶ if($setObj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setObj) { } 2 0 2
69 2 ▶ confess "New Document element has no tag name" if not $setObj->localName;
Branch: 1 branch
BranchTrueFalseTotal
unless $setObj->localName 0 2 2
70 2 $self->{'element'} = $setObj;
71 2 ▶ if(ref($setObj) eq 'XML::DOM2::Element::Document') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $setObj eq "XML::DOM2::Element::Document") 2 0 2
72 2 $self->{'fragment'} = 1;
73 }
74 } else {
75 0 $self->{'element'} = $self->createElement(
76 '#document',
77 document => $self,
78 documentTag => $self->_document_name
79 );
80 0 ▶ if($self->{'namespace'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"namespace"}) 0 0 0
81 0 $self->{'element'}->setAttribute( 'xmlns', $self->{'namespace'} );
82 }
83 }
84 }
85 3 return $self->{'element'};
86 }
87  
88 =head2 $document->documentType()
89  
90 Returns a document type object for this document.
91  
92 =cut
93 sub documentType
94 1 ▶{
Subroutine documentType: called CC=1 SCAR=0.0
Pod documented
95 1 my ($self) = @_;
96 # if(not $self->{'doctype'}) {
97 # $self->{'doctype'} = $self->createDocumentType();
98 # }
99 1 return $self->{'doctype'};
100 }
101  
102 =head2 $document->addId( $id, $element )
103  
104 Adds an id of an element, used internaly.
105  
106 =cut
107 sub addId
108 2 ▶{
Subroutine addId: called CC=3 SCAR=11.6
Pod documented
109 2 my ($self, $id, $tag) = @_;
110 2 ▶ if(not defined($self->{'idlist'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"idlist"}) 2 0 2
111 2 $self->{'idlist'} = {};
112 }
113 2 ▶ if(not defined($self->{'idlist'}->{$id})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"idlist"}{$id}) 2 0 2
114 2 $self->{'idlist'}->{$id} = $tag;
115 2 return 1;
116 }
117 0 return undef;
118 }
119  
120 =head2 $document->removeId( $id )
121  
122 Removes an id of an element, used internaly.
123  
124 =cut
125 sub removeId
126 0 ▶{
Subroutine removeId: not called CC=1 SCAR=6.9
Pod documented
127 0 my ($self, $id) = @_;
128 0 return delete($self->{'idlist'}->{$id});
129 }
130  
131 =head2 $document->getElementById( $id )
132  
133 Returns the element with that id in this document.
134  
135 =cut
136 sub getElementById
137 2 ▶{
Subroutine getElementById: called CC=3 SCAR=11.0
Pod documented
138 2 my ($self, $id)=@_;
139 2 ▶ return undef unless defined($id);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 2 2
140 2 my $idlist = $self->{'idlist'};
141 2 ▶ if (exists $idlist->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $idlist->{$id}) 1 1 2
142 1 return $idlist->{$id};
143 }
144 1 return undef;
145 }
146  
147 =head2 $document->addElement( $element )
148  
149 Adds an element to the elements list, used internaly.
150  
151 =cut
152 sub addElement
153 18 ▶{
Subroutine addElement: called CC=3 SCAR=11.0
Pod documented
154 18 my ($self, $tag) = @_;
155 18 my $name = $tag->localName;
156 18 ▶ if(not defined($self->{'elist'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"elist"}) 2 16 18
157 2 $self->{'elist'} = {};
158 }
159 18 ▶ if(not defined($self->{'elist'}->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"elist"}{$name}) 14 4 18
160 14 $self->{'elist'}->{$name} = [];
161 }
162 18 $tag->{'tagindex'} = @{$self->{'elist'}->{$name}};
163 18 push @{$self->{'elist'}->{$name}}, $tag;
164 18 return 1;
165 }
166  
167 =head2 $document->removeElement( $element )
168  
169 Remove the specified element from the elements list, used internaly.
170  
171 =cut
172 sub removeElement
173 0 ▶{
Subroutine removeElement: not called CC=2 SCAR=17.9
Pod documented
174 0 my ($self, $tag) = @_;
175 0 my $name = $tag->getElementName;
176 0 splice @{$self->{'elist'}->{$name}}, $tag->{'tagindex'}, 1;
177 # Remove the elist name if no nodes;
178 # this keeps getElementNames function consistant
179 0 ▶ delete($self->{'elist'}->{$name}) unless @{$self->{'elist'}->{$name}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"elist"}{$name};} 0 0 0
180 }
181  
182 =head2 $document->getElements( $type )
183  
184 =head2 $document->getElementsByType( $type )
185  
186 =head2 $document->getElementsByName( $type )
187  
188 Get all elements of the specified type/tagName; if none is specified, get all elements in document.
189  
190 =cut
191 sub getElements
192 0 ▶{
Subroutine getElements: not called CC=8 SCAR=42.8
Pod documented
193 0 my ($self, $element) = @_;
194 0 ▶ return undef unless exists $self->{'elist'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"elist"} 0 0 0
195  
196 0 my $elist = $self->{'elist'};
197 0 ▶ if (defined $element) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $element) { } 0 0 0
198 0 ▶ if (exists $elist->{$element}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $elist->{$element}) 0 0 0
199 0 return wantarray?@{$elist->{$element}}:
200 0 ▶ $elist->{$element};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
201 }
202 0 ▶ return wantarray?():undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
203 } else {
204 # Return all elements for all types
205 0 my @elements;
206 0 foreach my $element_type (keys %$elist) {
207 0 push @elements,@{$elist->{$element_type}};
208 }
209 0 ▶ return wantarray?@elements:\@elements;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
210 }
211 }
212 4 *getElementsByType=\&getElements;
213 4 *getElementsByName=\&getElements;
214  
215 =head2 $document->getElementNames()
216  
217 =head2 $document->getElementTypes()
218  
219 Get all the element types in use in the document.
220  
221 =cut
222 sub getElementNames
223 0 ▶{
Subroutine getElementNames: not called CC=2 SCAR=17.9
Pod documented
224 0 my $self = shift;
225 0 my @types = keys %{$self->{'elist'}};
226  
227 0 ▶ return wantarray ? @types : \@types;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
228 }
229 4 *getElementTypes=\&getElementNames;
230  
231 =head2 $document->addDefinition( $def )
232  
233 Add a definition to the document.
234  
235 =cut
236 sub addDefinition
237 0 ▶{
Subroutine addDefinition: not called CC=2 SCAR=17.9
Pod documented
238 0 my ($self, $object) = @_;
239 0 ▶ $self->{'defs'} = [] if(not $self->{'defs'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"defs"} 0 0 0
240 0 push @{$self->{'defs'}}, $object;
241 0 return $self;
242 }
243  
244 =head2 $document->definitions( )
245  
246 Return all definitions in document.
247  
248 =cut
249 sub definitions
250 0 ▶{
Subroutine definitions: not called CC=2 SCAR=17.9
Pod documented
251 0 my ($self) = @_;
252 0 ▶ return $self->{'defs'} || [];
Condition0%0/2
$self->{'defs'} || []
l!l
00
MC/DC0%0/1
$self->{'defs'} || []
$self->{'defs'}
Truth table0%0/2
A
A= $self->{'defs'}
Aresult
11
00
253 }
254  
255 =head2 $document->getNamespace( $uri )
256  
257 Return a namespace based on the uri or prefix.
258  
259 =cut
260 sub getNamespace
261 2 ▶{
Subroutine getNamespace: called CC=4 SCAR=15.2
Pod documented
262 2 my ($self, $uri) = @_;
263 2 ▶ $self->{'xmlns'} = {} if not $self->{'xmlns'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"xmlns"} 2 0 2
264 2 ▶ if($uri eq 'xmlns' and not $self->{'xmlns'}->{'xmlns'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri eq "xmlns" and not $self->{"xmlns"}{"xmlns"}) 2 0 2
Condition33%1/3
$uri eq "xmlns" and not $self->{"xmlns"}{"xmlns"}
!ll&&!rl&&r
002
MC/DC0%0/2
$uri eq "xmlns" and not $self->{"xmlns"}{"xmlns"}
$uri eq "xmlns" not $self->{"xmlns"}{"xmlns"}
Truth table33%1/3
A and not(B)
A= $uri eq "xmlns"B= not $self->{"xmlns"}{"xmlns"}
ABresult
0X0
100
111
265 2 $self->{'xmlns'}->{'xmlns'} = XML::DOM2::Attribute::Namespace->new(
266 owner => $self,
267 name => 'xmlns',
268 prefix => 'xmlns',
269 uri => 'XML Namespace URI'
270 );
271 }
272 2 return $self->{'xmlns'}->{$uri};
273 }
274  
275 =head2 $document->createNamespace( $prefix, $uri )
276  
277 Create a new namespace within this document.
278  
279 =cut
280 sub createNamespace
281 0 ▶{
Subroutine createNamespace: not called CC=2 SCAR=17.9
Pod documented
282 0 my ($self, $prefix, $uri) = @_;
283 0 my $xmlns = $self->getNamespace( 'xmlns' );
284 0 $self->documentElement->setAttributeNS( $xmlns, $prefix, $uri );
285 0 my $ns = $self->documentElement->getAttributeNS( $xmlns, $prefix );
286 0 ▶ if(not $ns) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ns) 0 0 0
287 0 carp "Unable to create namespace, no attribute defined";
288 }
289 0 return $ns;
290 }
291  
292 =head2 $document->addNamespace( $namespace )
293  
294 Add namespace to this document.
295  
296 =cut
297 sub addNamespace
298 0 ▶{
Subroutine addNamespace: not called CC=1 SCAR=6.9
Pod documented
299 0 my ($self, $namespace) = @_;
300 0 $self->{'xmlns'}->{$namespace->ns_prefix} = $namespace;
301 0 $self->{'xmlns'}->{$namespace->ns_uri} = $namespace;
302 }
303  
304 =head2 $document->removeNamespace( $namespace )
305  
306 Remove a namespace from this document.
307  
308 =cut
309 sub removeNamespace
310 0 ▶{
Subroutine removeNamespace: not called CC=1 SCAR=6.9
Pod documented
311 0 my ($self, $namespace) = @_;
312 0 delete($self->{'xmlns'}->{$namespace->ns_prefix});
313 0 delete($self->{'xmlns'}->{$namespace->ns_uri});
314 }
315  
316 =head2 $document->createElement( $name, %options )
317  
318 Creates a new element of type name.
319  
320 =cut
321 sub createElement
322 20 ▶{
Subroutine createElement: called CC=3 SCAR=12.0
Pod documented
323 20 my ($self, $name, %opts) = @_;
324 20 ▶ croak "Unable to create element without a name" if not defined($name) or $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or $name eq "" 0 20 20
Condition33%1/3
not defined $name or $name eq ""
l!l&&r!l&&!r
0020
MC/DC0%0/2
not defined $name or $name eq ""
not defined $name $name eq ""
Truth table33%1/3
not(A) or B
A= not defined $nameB= $name eq ""
ABresult
1X1
011
000
325 20 my $element = $self->_element_handle( $name, %opts );
326 20 return $element;
327 }
328  
329 =head2 $document->createElementNS( $namespace, $name, %options )
330  
331 Create an element in a namespace.
332  
333 =cut
334 sub createElementNS
335 0 ▶{
Subroutine createElementNS: not called CC=3 SCAR=24.8
Pod documented
336 0 my ($self, $ns, $name, %opts) = @_;
337 0 ▶ croak "Unable to create element without a name" if not defined($name) or $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or $name eq "" 0 0 0
Condition0%0/3
not defined $name or $name eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $name or $name eq ""
not defined $name $name eq ""
Truth table0%0/3
not(A) or B
A= not defined $nameB= $name eq ""
ABresult
1X1
011
000
338 0 my $element = $self->_element_handle(
339 $name,
340 namespace => $ns,
341 name => $name,
342 %opts,
343 );
344 0 return $element;
345 }
346  
347 =head2 $document->createTextNode( $data )
348  
349 Create a textnode element.
350  
351 =cut
352 sub createTextNode
353 0 ▶{
Subroutine createTextNode: not called CC=1 SCAR=6.9
Pod documented
354 0 my ($self, $data) = @_;
355 0 return $self->_element_handle( '#cdata-entity', notag => 1 );
356 }
357  
358 =head2 $document->createComment( $data )
359  
360 Create a comment element
361  
362 =cut
363 sub createComment
364 0 ▶{
Subroutine createComment: not called CC=1 SCAR=6.9
Pod documented
365 0 my ($self, $data) = @_;
366 0 return $self->_element_handle( '#comment', text => $data );
367 }
368  
369 =head2 $document->createCDATASection( $data )
370  
371 create a CDATA element.
372  
373 =cut
374 sub createCDATASection
375 0 ▶{
Subroutine createCDATASection: not called CC=1 SCAR=6.9
Pod documented
376 0 my ($self, $data) = @_;
377 0 return $self->_element_handle( '#cdata-entity', notag => 0 );
378 }
379  
380 =head1 COPYRIGHT
381  
382 Martin Owens, doctormo@cpan.org
383  
384 =head1 SEE ALSO
385  
386 L<XML::DOM2>
387  
388 =cut
389 4 1;
↑ Summary DOM2/Parser.pm »