DOM2.pm

filter: Statementstmt 61.0% 91 / 149 Branchbran 33.3% 18 / 54 Conditioncond 46.4% 13 / 28 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 61.7% 21 / 34 Podpod 100% 11 / 11 totaltotal 53.6% 156 / 291 CC 41 SCAR 20.1
CC
41
Cov
53%
_adapt_hash
30.0
_serialise_doctype
14.8
_adapt_child
12.0
CRAP 7.5
« DOM2/Element/Comment.pm ↑ Summary DOM2/Attribute/Namespace.pm »
1 package XML::DOM2;
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 controlled, strict XML module for extentable xml objects.
9  
10 =head1 VERSION
11  
12 Version 0.06 - 2007-11-28
13  
14 =head1 SYNOPSIS
15  
16 my $xml = XML::DOM2->new( file => 'filename.xml' );
17 my $xml = XML::DOM2->new( data => '<xml>data</xml>' );
18 my $xml = XML::DOM2->new( fh => $file_handle );
19  
20 $xml->getChildren();
21  
22 =head1 DESCRIPTION
23  
24 XML::DOM2 is yet _another_ perl XML module.
25  
26 Features:
27  
28 * DOM Level2 Compilence in both document, elements and attributes
29 * NameSpace control for elements and attributes
30 * XPath (it's just one small method once you have a good DOM)
31 * Extendability:
32 * Document, Element or Attribute classes can be used as base class for other
33 kinds of document, element or attribute.
34 * Element and Attribute Handler allows element specific child elements and attribute objects.
35 * Element and Attribute serialisation overiding.
36 * Parsing with SAX (use XML::SAX::PurePerl for low dependancy installs)
37 * Internal serialisation
38  
39 =head1 METHODS
40  
41 =cut
42  
43 4 our $VERSION = '0.06';
44  
45 4 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 4 ▶use base "XML::DOM2::DOM::Document";
Subroutine BEGIN: called CC=1 SCAR=0.0
47 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 # All unspecified Elements
50 4 ▶use XML::DOM2::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 # Basic Element Types
52 4 ▶use XML::DOM2::Element::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 4 ▶use XML::DOM2::Element::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 4 ▶use XML::DOM2::Element::CDATA;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 # XML Parsing
57 4 ▶use XML::DOM2::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 4 ▶use XML::SAX::ParserFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 4 my %default_options = (
61 # processing options
62 printerror => 1, # print error messages to STDERR
63 raiseerror => 1, # die on errors (implies -printerror)
64 # rendering options
65 indent => "\t", # what to indent with
66 seperator => "\n", # element line (vertical) separator
67 nocredits => 0, # enable/disable credit note comment
68 );
69  
70 =head2 $class->new( file|data|fh )
71  
72 Create a new xml object, it will parse a file, data or a file handle if required or will await creation of nodes.
73  
74 =cut
75 sub new
76 4 ▶{
Subroutine new: called CC=5 SCAR=17.3
Pod documented
77 4 my ($proto, %o) = @_;
78 4 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
79  
80 4 ▶ if($o{'file'} or $o{'fh'} or $o{'data'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o{"file"} or $o{"fh"} or $o{"data"}) 2 2 4
Condition50%3/6
$o{"file"} or $o{"fh"} or $o{"data"}
l!l&&r!l&&!r
022
$o{"file"} or $o{"fh"}
l!l&&r!l&&!r
004
MC/DC33%1/3
$o{"file"} or $o{"fh"} or $o{"data"}
$o{"file"} $o{"fh"} $o{"data"}
Truth table50%2/4
A or B or C
A= $o{"file"}B= $o{"fh"}C= $o{"data"}
ABCresult
1XX1
01X1
0011
0000
81 2 return $class->parseDocument(%o);
82 }
83  
84 2 my $self = bless \%o, $class;
85 2 return $self;
86 }
87  
88 =head2 $object->parseDocument( %p )
89  
90 Parse existing xml data into a document, inputs taken from ->new;
91  
92 =cut
93 sub parseDocument
94 2 ▶{
Subroutine parseDocument: called CC=5 SCAR=17.1
Pod documented
95 2 my ($proto, %p) = @_;
96 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
97  
98 2 my $xml = $proto->createDocument( undef, 'newDocument' );
99 2 my $handler = XML::DOM2::Parser->new( document => $xml );
100 2 my $parser = XML::SAX::ParserFactory->parser( Handler => $handler );
101  
102 2 ▶ $parser->parse_uri($p{'file'}) if $p{'file'}; # URI
Branch: 1 branch
BranchTrueFalseTotal
if $p{"file"} 0 2 2
103 2 ▶ $parser->parse_string($p{'data'}) if $p{'data'}; # STRING DATA
Branch: 1 branch
BranchTrueFalseTotal
if $p{"data"} 2 0 2
104 2 ▶ $parser->parse_file($p{'fh'}) if $p{'fh'}; # FILE HANDLE
Branch: 1 branch
BranchTrueFalseTotal
if $p{"fh"} 0 2 2
105  
106 2 return $xml;
107 }
108  
109 =head2 $object->xmlify( %options )
110 =head2 $object->render( %options )
111 =head2 $object->to_xml( %options )
112 =head2 $object->serialise( %options )
113 =head2 $object->serialize( %options )
114  
115 Returns xml representation of xml document.
116  
117 Options:
118 seperator - default is carage return
119  
120 =cut
121 sub xmlify
122 1 ▶{
Subroutine xmlify: called CC=6 SCAR=19.2
Pod documented
123 1 my ($self,%attrs) = @_;
124 1 my ($decl,$ns);
125
126 1 ▶ my $sep = $attrs{'seperator'} || $self->{'seperator'} || "\n";
Condition40%2/5
$attrs{'seperator'} || $self->{'seperator'} || "\n"
l!l
01
$attrs{"seperator"} or $self->{"seperator"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$attrs{'seperator'} || $self->{'seperator'} || "\n"
$attrs{"seperator"} $self->{"seperator"}
Truth table33%1/3
A or B
A= $attrs{"seperator"}B= $self->{"seperator"}
ABresult
1X1
011
000
127 1 ▶ unless ($self->{'nocredits'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"nocredits"}) 1 0 1
128 #$self->documentElement->appendChild(
129 # $self->createComment( $self->_credit_comment ),
130 #);
131 }
132  
133 1 my $xml = '';
134 #write the xml header
135 1 $xml .= $self->_serialise_header(
136 seperator => $sep,
137 );
138  
139 #and write the dtd if this is inline
140 $xml .= $self->_serialise_doctype(
141 seperator => $sep,
142 1 ▶ ) unless $self->{'inline'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"inline"} 1 0 1
143  
144 1 ▶ $self->documentElement->setAttribute('xmlns', $self->namespace) if $self->namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->namespace 0 1 1
145 $xml .= $self->documentElement->xmlify(
146 namespace => $self->{'-namespace'},
147 seperator => $sep,
148 1 indent => $self->{'-indent'},
149 );
150 # Return xml string
151 1 return $xml;
152 }
153 4 *render=\&xmlify;
154 4 *to_xml=\&xmlify;
155 4 *serialise=\&xmlify;
156 4 *serialize=\&xmlify;
157  
158 =head2 I<$class>->adaptation( $name, $structure )
159  
160 Convert a perl structure and create a new xml document of it:
161  
162 $class->adaptation('xml', { foo => [ 'A', 'B', 'C' ], bar => 'D', kou => { 'A' => 1, 'B' => 2 } });
163  
164 Will convert to:
165  
166 "<xml><foo>A</foo><foo>B</foo><foo>C</foo><bar>D</bar><kou><A>1</A><B>2</B></xml>"
167  
168 $class->adaptation('xml', { 'foo' => [ { '+' => 'A', '_Letter' => '1' }, { '+' => 'B', '_Letter' => 2 } ] });
169  
170 Will convert to:
171  
172 "<xml><foo Letter="1">A</foo><foo Letter="2">B</foo></xml>"
173  
174 =cut
175 sub adaptation
176 0 ▶{
Subroutine adaptation: not called CC=1 SCAR=6.9
Pod documented
177 0 my ($class, $baseTag, $structure) = @_;
178 0 my $self = $class->new( baseTag => $baseTag );
179 0 my $root = $self->documentElement();
180 0 $class->_adapt_hash( $root, $structure );
181 0 return $self;
182 }
183  
184 # Adapt any kind of child object / data type
185 sub _adapt_child
186 0 ▶{
Subroutine _adapt_child: not called CC=3 SCAR=24.8
187 0 my ($class, $element, $data, $parent) = @_;
188 0 ▶ if(UNIVERSAL::isa($data, 'HASH')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($data, 'HASH')) { } 0 0 0
elsif (UNIVERSAL::isa($data, 'ARRAY')) { } 0 0 0
189 0 return $class->_adapt_hash( $element, $data, $parent );
190 } elsif(UNIVERSAL::isa($data, 'ARRAY')) {
191 0 return $class->_adapt_array( $element, $data, $parent );
192 } else {
193 0 return $class->_adapt_scalar( $element, scalar($data) );
194 }
195 }
196  
197 # Adapt a HASH ref into XML
198 sub _adapt_hash
199 0 ▶{
Subroutine _adapt_hash: not called CC=5 SCAR=34.0
200 0 my ($class, $element, $hash) = @_;
201  
202 0 foreach my $name (keys(%{$hash})) {
203 0 my $data = $hash->{$name};
204  
205 0 ▶ if($name eq '+') {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq '+') { } 0 0 0
elsif ($name =~ /^_(.+)$/) { } 0 0 0
206 0 $element->cdata($data)
207 } elsif($name =~ /^_(.+)$/) {
208 0 $element->setAttribute($1, $data);
209 } else {
210 0 my $isa = UNIVERSAL::isa($data, 'ARRAY');
211 0 ▶ my $child = $isa ? $name : $element->createElement( $name );
Branch: 1 branch
BranchTrueFalseTotal
$isa ? : 0 0 0
212 0 $class->_adapt_child( $child, $data, $element );
213 }
214 }
215 0 return $element;
216 }
217  
218 # Adapt an ARRAY ref into XML
219 sub _adapt_array
220 0 ▶{
Subroutine _adapt_array: not called CC=3 SCAR=24.8
221 0 my ($class, $name, $array, $parent) = @_;
222  
223 0 foreach my $data (@{$array}) {
224 0 my $isa = UNIVERSAL::isa($data, 'ARRAY');
225 0 ▶ my $child = $isa ? $name : $parent->createElement( $name );
Branch: 1 branch
BranchTrueFalseTotal
$isa ? : 0 0 0
226 0 $class->_adapt_child( $child, $data, $parent );
227 }
228 0 return $parent;
229 }
230  
231 # Adapt a SCALAR into XML
232 sub _adapt_scalar
233 0 ▶{
Subroutine _adapt_scalar: not called CC=2 SCAR=17.9
234 0 my ($self, $element, $scalar) = @_;
235 0 ▶ if(defined($scalar)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scalar) 0 0 0
236 0 my $result = $element->createElement( '#cdata-entity', text => scalar( $scalar ) );
237 }
238 0 return $element;
239 }
240  
241 =head2 $object->extension()
242
243 $extention = $xml->extention();
244  
245 Does not work, legacy option maybe enabled in later versions.
246  
247 =cut
248 sub extension
249 0 ▶{
Subroutine extension: not called CC=1 SCAR=6.9
Pod documented
250 0 my ($self) = @_;
251 0 return $self->{'-extension'};
252 }
253  
254  
255 =head1 OPTIONS
256  
257 =head2 $object->namespace( $set )
258  
259 Default document name space
260  
261 =cut
262 1 ▶sub namespace { shift->_option('namespace', @_); }
Subroutine namespace: called CC=1 SCAR=0.0
Pod documented
263  
264 =head2 $object->name( $set )
265  
266 Document localName
267  
268 =cut
269 0 ▶sub name { shift->_option('name', @_); }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
270  
271 =head2 $object->doctype()
272  
273 Document Type object
274  
275 =cut
276 22 ▶sub doctype { shift->_option('doctype', @_); }
Subroutine doctype: called CC=1 SCAR=0.0
Pod documented
277  
278 =head2 $object->version()
279  
280 XML Version
281  
282 =cut
283 0 ▶sub version { shift->_option('version', @_); }
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
284  
285 =head2 $object->encoding()
286  
287 XML Encoding
288  
289 =cut
290 0 ▶sub encoding { shift->_option('encoding', @_); }
Subroutine encoding: not called CC=1 SCAR=6.9
Pod documented
291  
292 =head2 $object->standalone()
293  
294 XML Standalone
295  
296 =cut
297 0 ▶sub standalone { shift->_option('standalone', @_); }
Subroutine standalone: not called CC=1 SCAR=6.9
Pod documented
298  
299 =head1 INTERNAL METHODS
300  
301 =head2 _serialise_doctype
302  
303 $xml->_serialise_doctype( seperator => "\n" );
304  
305 Returns the document type in an xml header form.
306  
307 =cut
308 sub _serialise_doctype
309 1 ▶{
Subroutine _serialise_doctype: called CC=6 SCAR=26.9
310 1 my ($self, %p) = @_;
311 1 my $sep = $p{'seperator'};
312 1 my $type = $self->documentType();
313 1 ▶ return '' if not $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 1 1
314  
315 1 my $id;
316 1 ▶ if ($type->publicId) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->publicId) { } 0 1 1
317 0 $id = 'PUBLIC "'.$type->publicId.'"';
318 0 ▶ $id .= ($type->systemId ? $sep.' "'.$type->systemId.'"' : '');
Branch: 1 branch
BranchTrueFalseTotal
$type->systemId ? : 0 0 0
319 } else {
320 #warn "I'm not returning a doctype because there is n public id: ".$type->publicId;
321 1 return '';
322 }
323  
324 0 my $extension = $self->_serialise_extension( seperator => $sep );
325 0 $type = $type->name;
326 0 ▶ warn "no TYPE defined!" if not defined($type);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
327 0 ▶ warn "no id!" if not defined($id);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
328 0 return $sep."<!DOCTYPE $type $id$extension>";
329 }
330  
331 =head2 _serialise_extention
332  
333 $xml->_serialise_extention( seperator => "\n" );
334  
335 Returns the document extentions.
336  
337 =cut
338 sub _serialise_extension
339 0 ▶{
Subroutine _serialise_extension: not called CC=2 SCAR=17.9
340 0 my ($self, %p) = @_;
341 0 my $sep = $p{'seperator'};
342 0 my $ex = '';
343 0 ▶ if ($self->extension) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->extension) 0 0 0
344 0 $ex .= $sep.$self->extension.$sep;
345 0 $ex = " [".$sep.$ex."]";
346 }
347 0 return $ex;
348 }
349  
350 =head2 _serialise_header
351  
352 $xml->_serialise_header( );
353  
354 The XML header, with version, encoding and standalone options.
355  
356 =cut
357 sub _serialise_header
358 1 ▶{
Subroutine _serialise_header: called CC=4 SCAR=14.6
359 1 my ($self, %p) = @_;
360  
361 1 ▶ my $version= $self->{'version'} || '1.0';
Condition50%1/2
$self->{'version'} || '1.0'
l!l
01
MC/DC0%0/1
$self->{'version'} || '1.0'
$self->{'version'}
Truth table50%1/2
A
A= $self->{'version'}
Aresult
11
00
362 1 ▶ my $encoding = $self->{'encoding'} || 'UTF-8';
Condition50%1/2
$self->{'encoding'} || 'UTF-8'
l!l
01
MC/DC0%0/1
$self->{'encoding'} || 'UTF-8'
$self->{'encoding'}
Truth table50%1/2
A
A= $self->{'encoding'}
Aresult
11
00
363 1 ▶ my $standalone = $self->{'stand_alone'} ||'yes';
Condition50%1/2
$self->{'stand_alone'} || 'yes'
l!l
01
MC/DC0%0/1
$self->{'stand_alone'} || 'yes'
$self->{'stand_alone'}
Truth table50%1/2
A
A= $self->{'stand_alone'}
Aresult
11
00
364  
365 1 return '<?xml version="'.$version.'" encoding="'.$encoding.'" standalone="'.$standalone.'"?>';
366 }
367  
368 =head2 _element_handle
369  
370 $xml->_element_handle( $type, %element-options );
371  
372 Returns an XML element based on $type, use to extentd element capabilties.
373  
374 =cut
375 sub _element_handle
376 20 ▶{
Subroutine _element_handle: called CC=7 SCAR=21.8
377 20 my ($self, $type, %opts) = @_;
378 20 ▶ confess "Element handler with no bleedin type!!" if not $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 20 20
379 20 ▶ if($type eq '#document' or $type eq $self->_document_name) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq '#document' or $type eq $self->_document_name) { } 2 18 20
elsif ($type eq '#comment') { } 0 18 18
elsif ($type eq '#cdata-entity') { } 0 18 18
Condition66%2/3
$type eq "#document" or $type eq $self->_document_name
l!l&&r!l&&!r
0218
MC/DC50%1/2
$type eq "#document" or $type eq $self->_document_name
$type eq "#document" $type eq $self->_document_name
Truth table66%2/3
A or B
A= $type eq "#document"B= $type eq $self->_document_name
ABresult
1X1
011
000
380 2 ▶ $opts{'documentTag'} = $type if $type ne '#document';
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "#document" 2 0 2
381 2 return XML::DOM2::Element::Document->new(%opts);
382 } elsif($type eq '#comment') {
383 0 return XML::DOM2::Element::Comment->new( delete($opts{'text'}), %opts);
384 } elsif($type eq '#cdata-entity') {
385 0 return XML::DOM2::Element::CDATA->new(delete($opts{'text'}), %opts);
386 }
387 18 return XML::DOM2::Element->new( $type, %opts );
388 }
389  
390 =head2 $object->_option( $name[, $data] )
391  
392 Set or get the required option.
393  
394 =cut
395 sub _option
396 23 ▶{
Subroutine _option: called CC=2 SCAR=7.6
397 23 my ($self, $option, $set) = @_;
398 23 ▶ if(defined($set)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $set) 0 23 23
399 0 $self->{$option} = $set;
400 }
401 23 return $self->{$option};
402 }
403  
404 =head2 $object->_can_contain_element()
405  
406 Does this node support element children.
407  
408 =cut
409 0 ▶sub _can_contain_element { 1 }
Subroutine _can_contain_element: not called CC=1 SCAR=6.9
410  
411  
412 =head2 $object->_document_name()
413  
414 Returns the doctype name or 'xml' as default, can be extended.
415  
416 =cut
417 20 ▶sub _document_name {
Subroutine _document_name: called CC=3 SCAR=12.5
418 20 my ($self) = @_;
419 20 ▶ if($self->{'baseTag'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"baseTag"}) 0 20 20
420 0 return $self->{'baseTag'};
421 }
422 20 ▶ return $self->doctype()->name() || 'xml';
Condition50%1/2
$self->doctype->name || 'xml'
l!l
200
MC/DC0%0/1
$self->doctype->name || 'xml'
$self->doctype->name
Truth table50%1/2
A
A= $self->doctype->name
Aresult
11
00
423 }
424  
425 =head2 $object->_credit_comment()
426  
427 Returns the comment credit used in the output
428  
429 =cut
430 0 ▶sub _credit_comment { "\nGenerated using the Perl XML::DOM2 Module V$VERSION\nWritten by Martin Owens\n" }
Subroutine _credit_comment: not called CC=1 SCAR=6.9
431  
432  
433 =head1 COPYRIGHT
434  
435 Martin Owens, doctormo@cpan.org
436  
437 =head1 CREDITS
438  
439 Based on SVG.pm by Ronan Oger, ronan@roasp.com
440  
441 =head1 SEE ALSO
442  
443 perl(1),L<XML::DOM2>,L<XML::DOM2::Parser>
444  
445 =cut
446 4 1;
« DOM2/Element/Comment.pm ↑ Summary DOM2/Attribute/Namespace.pm »