blib/lib/XML/TreePP.pm

filter: Statementstmt 74.8% 417 / 557 Branchbran 64.0% 264 / 412 Conditioncond 53.9% 68 / 126 MC/DCmcdc 33.7% 26 / 77 Subroutinesub 73.1% 30 / 41 Podpod 21.6% 8 / 37 totaltotal 65.0% 813 / 1250 CC 258 SCAR 40.6
CC
258
Cov
68%
parsehttp_lwp
272.0
find_http_module
210.0
parsehttp_lite
132.0
CRAP 58.1
↑ Summary
1 =head1 NAME
2  
3 XML::TreePP -- Pure Perl implementation for parsing/writing XML documents
4  
5 =head1 SYNOPSIS
6  
7 parse an XML document from file into hash tree:
8  
9 use XML::TreePP;
10 my $tpp = XML::TreePP->new();
11 my $tree = $tpp->parsefile( "index.rdf" );
12 print "Title: ", $tree->{"rdf:RDF"}->{item}->[0]->{title}, "\n";
13 print "URL: ", $tree->{"rdf:RDF"}->{item}->[0]->{link}, "\n";
14  
15 write an XML document as string from hash tree:
16  
17 use XML::TreePP;
18 my $tpp = XML::TreePP->new();
19 my $tree = { rss => { channel => { item => [ {
20 title => "The Perl Directory",
21 link => "http://www.perl.org/",
22 }, {
23 title => "The Comprehensive Perl Archive Network",
24 link => "http://cpan.perl.org/",
25 } ] } } };
26 my $xml = $tpp->write( $tree );
27 print $xml;
28  
29 get a remote XML document by HTTP-GET and parse it into hash tree:
30  
31 use XML::TreePP;
32 my $tpp = XML::TreePP->new();
33 my $tree = $tpp->parsehttp( GET => "http://use.perl.org/index.rss" );
34 print "Title: ", $tree->{"rdf:RDF"}->{channel}->{title}, "\n";
35 print "URL: ", $tree->{"rdf:RDF"}->{channel}->{link}, "\n";
36  
37 get a remote XML document by HTTP-POST and parse it into hash tree:
38  
39 use XML::TreePP;
40 my $tpp = XML::TreePP->new( force_array => [qw( item )] );
41 my $cgiurl = "http://search.hatena.ne.jp/keyword";
42 my $keyword = "ajax";
43 my $cgiquery = "mode=rss2&word=".$keyword;
44 my $tree = $tpp->parsehttp( POST => $cgiurl, $cgiquery );
45 print "Link: ", $tree->{rss}->{channel}->{item}->[0]->{link}, "\n";
46 print "Desc: ", $tree->{rss}->{channel}->{item}->[0]->{description}, "\n";
47  
48 =head1 DESCRIPTION
49  
50 XML::TreePP module parses an XML document and expands it for a hash tree.
51 This generates an XML document from a hash tree as the opposite way around.
52 This is a pure Perl implementation and requires no modules depended.
53 This can also fetch and parse an XML document from remote web server
54 like the XMLHttpRequest object does at JavaScript language.
55  
56 =head1 EXAMPLES
57  
58 =head2 Parse XML file
59  
60 Sample XML document:
61  
62 <?xml version="1.0" encoding="UTF-8"?>
63 <family name="Kawasaki">
64 <father>Yasuhisa</father>
65 <mother>Chizuko</mother>
66 <children>
67 <girl>Shiori</girl>
68 <boy>Yusuke</boy>
69 <boy>Kairi</boy>
70 </children>
71 </family>
72  
73 Sample program to read a xml file and dump it:
74  
75 use XML::TreePP;
76 use Data::Dumper;
77 my $tpp = XML::TreePP->new();
78 my $tree = $tpp->parsefile( "family.xml" );
79 my $text = Dumper( $tree );
80 print $text;
81  
82 Result dumped:
83  
84 $VAR1 = {
85 'family' => {
86 '-name' => 'Kawasaki',
87 'father' => 'Yasuhisa',
88 'mother' => 'Chizuko',
89 'children' => {
90 'girl' => 'Shiori'
91 'boy' => [
92 'Yusuke',
93 'Kairi'
94 ],
95 }
96 }
97 };
98  
99 Details:
100  
101 print $tree->{family}->{father}; # the father's given name.
102  
103 The prefix '-' is added on every attribute's name.
104  
105 print $tree->{family}->{"-name"}; # the family name of the family
106  
107 The array is used because the family has two boys.
108  
109 print $tree->{family}->{children}->{boy}->[1]; # The second boy's name
110 print $tree->{family}->{children}->{girl}; # The girl's name
111  
112 =head2 Text node and attributes:
113  
114 If a element has both of a text node and attributes
115 or both of a text node and other child nodes,
116 value of a text node is moved to C<#text> like child nodes.
117  
118 use XML::TreePP;
119 use Data::Dumper;
120 my $tpp = XML::TreePP->new();
121 my $source = '<span class="author">Kawasaki Yusuke</span>';
122 my $tree = $tpp->parse( $source );
123 my $text = Dumper( $tree );
124 print $text;
125  
126 The result dumped is following:
127  
128 $VAR1 = {
129 'span' => {
130 '-class' => 'author',
131 '#text' => 'Kawasaki Yusuke'
132 }
133 };
134  
135 The special node name of C<#text> is used because this elements
136 has attribute(s) in addition to the text node.
137 See also L</text_node_key> option.
138  
139 =head1 METHODS
140  
141 =head2 new
142  
143 This constructor method returns a new XML::TreePP object with C<%options>.
144  
145 $tpp = XML::TreePP->new( %options );
146  
147 =head2 set
148  
149 This method sets a option value for C<option_name>.
150 If C<$option_value> is not defined, its option is deleted.
151  
152 $tpp->set( option_name => $option_value );
153  
154 See OPTIONS section below for details.
155  
156 =head2 get
157  
158 This method returns a current option value for C<option_name>.
159  
160 $tpp->get( 'option_name' );
161  
162 =head2 parse
163  
164 This method reads an XML document by string and returns a hash tree converted.
165 The first argument is a scalar or a reference to a scalar.
166  
167 $tree = $tpp->parse( $source );
168  
169 =head2 parsefile
170  
171 This method reads an XML document by file and returns a hash tree converted.
172 The first argument is a filename.
173  
174 $tree = $tpp->parsefile( $file );
175  
176 =head2 parsehttp
177  
178 This method receives an XML document from a remote server via HTTP and
179 returns a hash tree converted.
180  
181 $tree = $tpp->parsehttp( $method, $url, $body, $head );
182  
183 C<$method> is a method of HTTP connection: GET/POST/PUT/DELETE
184 C<$url> is an URI of an XML file.
185 C<$body> is a request body when you use POST method.
186 C<$head> is a request headers as a hash ref.
187 L<LWP::UserAgent> module or L<HTTP::Lite> module is required to fetch a file.
188  
189 ( $tree, $xml, $code ) = $tpp->parsehttp( $method, $url, $body, $head );
190  
191 In array context, This method returns also raw XML document received
192 and HTTP response's status code.
193  
194 =head2 write
195  
196 This method parses a hash tree and returns an XML document as a string.
197  
198 $source = $tpp->write( $tree, $encode );
199  
200 C<$tree> is a reference to a hash tree.
201  
202 =head2 writefile
203  
204 This method parses a hash tree and writes an XML document into a file.
205  
206 $tpp->writefile( $file, $tree, $encode );
207  
208 C<$file> is a filename to create.
209 C<$tree> is a reference to a hash tree.
210  
211 =head1 OPTIONS FOR PARSING XML
212  
213 This module accepts option parameters following:
214  
215 =head2 force_array
216  
217 This option allows you to specify a list of element names which
218 should always be forced into an array representation.
219  
220 $tpp->set( force_array => [ 'rdf:li', 'item', '-xmlns' ] );
221  
222 The default value is null, it means that context of the elements
223 will determine to make array or to keep it scalar or hash.
224 Note that the special wildcard name C<'*'> means all elements.
225  
226 =head2 force_hash
227  
228 This option allows you to specify a list of element names which
229 should always be forced into an hash representation.
230  
231 $tpp->set( force_hash => [ 'item', 'image' ] );
232  
233 The default value is null, it means that context of the elements
234 will determine to make hash or to keep it scalar as a text node.
235 See also L</text_node_key> option below.
236 Note that the special wildcard name C<'*'> means all elements.
237  
238 =head2 cdata_scalar_ref
239  
240 This option allows you to convert a cdata section into a reference
241 for scalar on parsing an XML document.
242  
243 $tpp->set( cdata_scalar_ref => 1 );
244  
245 The default value is false, it means that each cdata section is converted into a scalar.
246  
247 =head2 user_agent
248  
249 This option allows you to specify a HTTP_USER_AGENT string which
250 is used by parsehttp() method.
251  
252 $tpp->set( user_agent => 'Mozilla/4.0 (compatible; ...)' );
253  
254 The default string is C<'XML-TreePP/#.##'>, where C<'#.##'> is
255 substituted with the version number of this library.
256  
257 =head2 http_lite
258  
259 This option forces pasrsehttp() method to use a L<HTTP::Lite> instance.
260  
261 my $http = HTTP::Lite->new();
262 $tpp->set( http_lite => $http );
263  
264 =head2 lwp_useragent
265  
266 This option forces parsehttp() method to use a L<LWP::UserAgent> instance.
267  
268 my $ua = LWP::UserAgent->new();
269 $ua->timeout( 60 );
270 $ua->env_proxy;
271 $tpp->set( lwp_useragent => $ua );
272  
273 You may use this with L<LWP::UserAgent::WithCache>.
274  
275 =head2 base_class
276  
277 This blesses class name for each element's hashref.
278 Each class is named straight as a child class of it parent class.
279  
280 $tpp->set( base_class => 'MyElement' );
281 my $xml = '<root><parent><child key="val">text</child></parent></root>';
282 my $tree = $tpp->parse( $xml );
283 print ref $tree->{root}->{parent}->{child}, "\n";
284  
285 A hash for <child> element above is blessed to C<MyElement::root::parent::child>
286 class. You may use this with L<Class::Accessor>.
287  
288 =head2 elem_class
289  
290 This blesses class name for each element's hashref.
291 Each class is named horizontally under the direct child of C<MyElement>.
292  
293 $tpp->set( base_class => 'MyElement' );
294 my $xml = '<root><parent><child key="val">text</child></parent></root>';
295 my $tree = $tpp->parse( $xml );
296 print ref $tree->{root}->{parent}->{child}, "\n";
297  
298 A hash for <child> element above is blessed to C<MyElement::child> class.
299  
300 =head2 xml_deref
301  
302 This option dereferences the numeric character references, like &#xEB;,
303 &#28450;, etc., in an XML document when this value is true.
304  
305 $tpp->set( xml_deref => 1 );
306  
307 Note that, for security reasons and your convenient,
308 this module dereferences the predefined character entity references,
309 &amp;, &lt;, &gt;, &apos; and &quot;, and the numeric character
310 references up to U+007F without xml_deref per default.
311  
312 =head2 require_xml_decl
313  
314 This option requires XML declaration at the top of XML document to parse.
315  
316 $tpp->set( require_xml_decl => 1 );
317  
318 This will die when <?xml .../?> declration not found.
319  
320 =head1 OPTIONS FOR WRITING XML
321  
322 =head2 first_out
323  
324 This option allows you to specify a list of element/attribute
325 names which should always appears at first on output XML document.
326  
327 $tpp->set( first_out => [ 'link', 'title', '-type' ] );
328  
329 The default value is null, it means alphabetical order is used.
330  
331 =head2 last_out
332  
333 This option allows you to specify a list of element/attribute
334 names which should always appears at last on output XML document.
335  
336 $tpp->set( last_out => [ 'items', 'item', 'entry' ] );
337  
338 =head2 indent
339  
340 This makes the output more human readable by indenting appropriately.
341  
342 $tpp->set( indent => 2 );
343  
344 This doesn't strictly follow the XML specification but does looks nice.
345  
346 =head2 xml_decl
347  
348 This module inserts an XML declaration on top of the XML document generated
349 per default. This option forces to change it to another or just remove it.
350  
351 $tpp->set( xml_decl => '' );
352  
353 =head2 output_encoding
354  
355 This option allows you to specify a encoding of the XML document generated
356 by write/writefile methods.
357  
358 $tpp->set( output_encoding => 'UTF-8' );
359  
360 On Perl 5.8.0 and later, you can select it from every
361 encodings supported by Encode.pm. On Perl 5.6.x and before with
362 Jcode.pm, you can use C<Shift_JIS>, C<EUC-JP>, C<ISO-2022-JP> and
363 C<UTF-8>. The default value is C<UTF-8> which is recommended encoding.
364  
365 =head2 empty_element_tag_end
366  
367 $tpp->set( empty_element_tag_end => '>' );
368  
369 Set characters which close empty tag. The default value is ' />'.
370  
371 =head1 OPTIONS FOR BOTH
372  
373 =head2 utf8_flag
374  
375 This makes utf8 flag on for every element's value parsed
376 and makes it on for the XML document generated as well.
377  
378 $tpp->set( utf8_flag => 1 );
379  
380 Perl 5.8.1 or later is required to use this.
381  
382 =head2 attr_prefix
383  
384 This option allows you to specify a prefix character(s) which
385 is inserted before each attribute names.
386  
387 $tpp->set( attr_prefix => '@' );
388  
389 The default character is C<'-'>.
390 Or set C<'@'> to access attribute values like E4X, ECMAScript for XML.
391 Zero-length prefix C<''> is available as well, it means no prefix is added.
392  
393 =head2 text_node_key
394  
395 This option allows you to specify a hash key for text nodes.
396  
397 $tpp->set( text_node_key => '#text' );
398  
399 The default key is C<#text>.
400  
401 =head2 ignore_error
402  
403 This module calls Carp::croak function on an error per default.
404 This option makes all errors ignored and just returns.
405  
406 $tpp->set( ignore_error => 1 );
407  
408 =head2 use_ixhash
409  
410 This option keeps the order for each element appeared in XML.
411 L<Tie::IxHash> module is required.
412  
413 $tpp->set( use_ixhash => 1 );
414  
415 This makes parsing performance slow.
416 (about 100% slower than default)
417  
418 =head1 AUTHOR
419  
420 Yusuke Kawasaki, http://www.kawa.net/
421  
422 =head1 REPOSITORY
423  
424 https://github.com/kawanet/XML-TreePP
425  
426 =head1 COPYRIGHT
427  
428 The following copyright notice applies to all the files provided in
429 this distribution, including binary files, unless explicitly noted
430 otherwise.
431  
432 Copyright 2006-2010 Yusuke Kawasaki
433  
434 =head1 LICENSE
435  
436 This library is free software; you can redistribute it and/or modify
437 it under the same terms as Perl itself.
438  
439 =cut
440  
441 package XML::TreePP;
442 43 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
443 43 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
444 43 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
445  
446 43 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
447 43 $VERSION = '0.43';
448  
449 43 my $XML_ENCODING = 'UTF-8';
450 43 my $INTERNAL_ENCODING = 'UTF-8';
451 43 my $USER_AGENT = 'XML-TreePP/'.$VERSION.' ';
452 43 my $ATTR_PREFIX = '-';
453 43 my $TEXT_NODE_KEY = '#text';
454 43 my $USE_ENCODE_PM = ( $] >= 5.008 );
455 43 my $ALLOW_UTF8_FLAG = ( $] >= 5.008001 );
456  
457 43 my $EMPTY_ELEMENT_TAG_END = ' />';
458  
459 93 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
460 93 my $package = shift;
461 93 my $self = {@_};
462 93 bless $self, $package;
463 93 $self;
464 }
465  
466 17 ▶sub die {
Subroutine die: called CC=2 SCAR=6.9
Pod undocumented
467 17 my $self = shift;
468 17 my $mess = shift;
469 17 ▶ return if $self->{ignore_error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ignore_error"} 4 13 17
470 13 Carp::croak $mess;
471 }
472  
473 2 ▶sub warn {
Subroutine warn: called CC=2 SCAR=7.0
Pod undocumented
474 2 my $self = shift;
475 2 my $mess = shift;
476 2 ▶ return if $self->{ignore_error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ignore_error"} 0 2 2
477 2 Carp::carp $mess;
478 }
479  
480 34 ▶sub set {
Subroutine set: called CC=2 SCAR=6.9
Pod documented
481 34 my $self = shift;
482 34 my $key = shift;
483 34 my $val = shift;
484 34 ▶ if ( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 31 3 34
485 31 $self->{$key} = $val;
486 }
487 else {
488 3 delete $self->{$key};
489 }
490 }
491  
492 0 ▶sub get {
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
493 0 my $self = shift;
494 0 my $key = shift;
495 0 ▶ $self->{$key} if exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$key} 0 0 0
496 }
497  
498 3 ▶sub writefile {
Subroutine writefile: called CC=5 SCAR=16.3
Pod documented
499 3 my $self = shift;
500 3 my $file = shift;
501 3 ▶ my $tree = shift or return $self->die( 'Invalid tree' );
Branch: 1 branch
BranchTrueFalseTotal
my $tree = shift() or return $self->die("Invalid tree") 3 0 3
502 3 my $encode = shift;
503 3 ▶ return $self->die( 'Invalid filename' ) unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 3 3
504 3 my $text = $self->write( $tree, $encode );
505 3 ▶ if ( $ALLOW_UTF8_FLAG && utf8::is_utf8( $text ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ALLOW_UTF8_FLAG and utf8::is_utf8($text)) 1 2 3
Condition66%2/3
$ALLOW_UTF8_FLAG and utf8::is_utf8($text)
!ll&&!rl&&r
021
MC/DC50%1/2
$ALLOW_UTF8_FLAG and utf8::is_utf8($text)
$ALLOW_UTF8_FLAG utf8::is_utf8($text)
Truth table66%2/3
A and B
A= $ALLOW_UTF8_FLAGB= utf8::is_utf8($text)
ABresult
0X0
100
111
506 1 utf8::encode( $text );
507 }
508 3 $self->write_raw_xml( $file, $text );
509 }
510  
511 168 ▶sub write {
Subroutine write: called CC=21 SCAR=30.7
Pod documented
512 168 my $self = shift;
513 168 ▶ my $tree = shift or return $self->die( 'Invalid tree' );
Branch: 1 branch
BranchTrueFalseTotal
my $tree = shift() or return $self->die("Invalid tree") 165 3 168
514 165 ▶ my $from = $self->{internal_encoding} || $INTERNAL_ENCODING;
Condition33%1/3
$self->{'internal_encoding'} || $INTERNAL_ENCODING
l!l&&r!l&&!r
01650
MC/DC0%0/2
$self->{'internal_encoding'} || $INTERNAL_ENCODING
$self->{'internal_encoding'} $INTERNAL_ENCODING
Truth table33%1/3
A || B
A= $self->{'internal_encoding'}B= $INTERNAL_ENCODING
ABresult
1X1
011
000
515 165 ▶ my $to = shift || $self->{output_encoding} || $XML_ENCODING;
Condition83%5/6
shift() || $self->{'output_encoding'} || $XML_ENCODING
l!l&&r!l&&!r
161490
shift() or $self->{"output_encoding"}
l!l&&r!l&&!r
88149
MC/DC0%0/3
shift() || $self->{'output_encoding'} || $XML_ENCODING
shift() $self->{"output_encoding"} $XML_ENCODING
Truth table75%3/4
A or B || C
A= shift()B= $self->{"output_encoding"}C= $XML_ENCODING
ABCresult
1XX1
01X1
0011
0000
516 165 my $decl = $self->{xml_decl};
517 165 ▶ $decl = '<?xml version="1.0" encoding="' . $to . '" ?>' unless defined $decl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decl 163 2 165
518  
519 165 local $self->{__first_out};
520 165 ▶ if ( exists $self->{first_out} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"first_out"}) 6 159 165
521 6 my $keys = $self->{first_out};
522 6 ▶ $keys = [$keys] unless ref $keys;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $keys 0 6 6
523 6 $self->{__first_out} = { map { $keys->[$_] => $_ } 0 .. $#$keys };
524 }
525  
526 165 local $self->{__last_out};
527 165 ▶ if ( exists $self->{last_out} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"last_out"}) 2 163 165
528 2 my $keys = $self->{last_out};
529 2 ▶ $keys = [$keys] unless ref $keys;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $keys 0 2 2
530 2 $self->{__last_out} = { map { $keys->[$_] => $_ } 0 .. $#$keys };
531 }
532  
533 165 ▶ my $tnk = $self->{text_node_key} if exists $self->{text_node_key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"text_node_key"} 3 162 165
534 165 ▶ $tnk = $TEXT_NODE_KEY unless defined $tnk;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tnk 29 136 165
535 165 local $self->{text_node_key} = $tnk;
536  
537 165 ▶ my $apre = $self->{attr_prefix} if exists $self->{attr_prefix};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"attr_prefix"} 8 157 165
538 165 ▶ $apre = $ATTR_PREFIX unless defined $apre;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $apre 28 137 165
539 165 local $self->{__attr_prefix_len} = length($apre);
540 165 local $self->{__attr_prefix_rex} = $apre;
541  
542 165 local $self->{__indent};
543 165 ▶ if ( exists $self->{indent} && $self->{indent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"indent"} and $self->{"indent"}) 2 163 165
Condition100%3/3
exists $self->{"indent"} and $self->{"indent"}
!ll&&!rl&&r
16212
MC/DC100%2/2
exists $self->{"indent"} and $self->{"indent"}
exists $self->{"indent"} $self->{"indent"}
Truth table100%3/3
A and B
A= exists $self->{"indent"}B= $self->{"indent"}
ABresult
0X0
100
111
544 2 $self->{__indent} = ' ' x $self->{indent};
545 }
546  
547 165 ▶ if ( ! UNIVERSAL::isa( $tree, 'HASH' )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($tree, "HASH")) 3 162 165
548 3 return $self->die( 'Invalid tree' );
549 }
550  
551 162 my $text = $self->hash_to_xml( undef, $tree );
552 162 ▶ if ( $from && $to ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from and $to) 162 0 162
Condition33%1/3
$from and $to
!ll&&!rl&&r
00162
MC/DC0%0/2
$from and $to
$from $to
Truth table33%1/3
A and B
A= $fromB= $to
ABresult
0X0
100
111
553 162 my $stat = $self->encode_from_to( \$text, $from, $to );
554 160 ▶ return $self->die( "Unsupported encoding: $to" ) unless $stat;
Branch: 1 branch
BranchTrueFalseTotal
unless $stat 0 160 160
555 }
556  
557 160 ▶ return $text if ( $decl eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $decl eq "" 1 159 160
558 159 join( "\n", $decl, $text );
559 }
560  
561 0 ▶sub parsehttp {
Subroutine parsehttp: not called CC=7 SCAR=40.3
Pod documented
562 0 my $self = shift;
563  
564 0 local $self->{__user_agent};
565 0 ▶ if ( exists $self->{user_agent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'user_agent'}) { } 0 0 0
566 0 my $agent = $self->{user_agent};
567 0 ▶ $agent .= $USER_AGENT if ( $agent =~ /\s$/s );
Branch: 1 branch
BranchTrueFalseTotal
if $agent =~ /\s$/s 0 0 0
568 0 ▶ $self->{__user_agent} = $agent if ( $agent ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $agent ne "" 0 0 0
569 } else {
570 0 $self->{__user_agent} = $USER_AGENT;
571 }
572  
573 0 my $http = $self->{__http_module};
574 0 ▶ unless ( $http ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($http) 0 0 0
575 0 $http = $self->find_http_module(@_);
576 0 $self->{__http_module} = $http;
577 }
578 0 ▶ if ( $http eq 'LWP::UserAgent' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($http eq 'LWP::UserAgent') { } 0 0 0
elsif ($http eq 'HTTP::Lite') { } 0 0 0
579 0 return $self->parsehttp_lwp(@_);
580 }
581 elsif ( $http eq 'HTTP::Lite' ) {
582 0 return $self->parsehttp_lite(@_);
583 }
584 else {
585 0 return $self->die( "LWP::UserAgent or HTTP::Lite is required: $_[1]" );
586 }
587 }
588  
589 0 ▶sub find_http_module {
Subroutine find_http_module: not called CC=14 SCAR=53.5
Pod undocumented
590 0 ▶ my $self = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
591  
592 0 ▶ if ( exists $self->{lwp_useragent} && ref $self->{lwp_useragent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"lwp_useragent"} and ref $self->{"lwp_useragent"}) 0 0 0
Condition0%0/3
exists $self->{"lwp_useragent"} and ref $self->{"lwp_useragent"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"lwp_useragent"} and ref $self->{"lwp_useragent"}
exists $self->{"lwp_useragent"} ref $self->{"lwp_useragent"}
Truth table0%0/3
A and B
A= exists $self->{"lwp_useragent"}B= ref $self->{"lwp_useragent"}
ABresult
0X0
100
111
593 0 ▶ return 'LWP::UserAgent' if defined $LWP::UserAgent::VERSION;
Branch: 1 branch
BranchTrueFalseTotal
if defined $LWP::UserAgent::VERSION 0 0 0
594 0 ▶ return 'LWP::UserAgent' if &load_lwp_useragent();
Branch: 1 branch
BranchTrueFalseTotal
if &load_lwp_useragent() 0 0 0
595 0 return $self->die( "LWP::UserAgent is required: $_[1]" );
596 }
597  
598 0 ▶ if ( exists $self->{http_lite} && ref $self->{http_lite} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"http_lite"} and ref $self->{"http_lite"}) 0 0 0
Condition0%0/3
exists $self->{"http_lite"} and ref $self->{"http_lite"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"http_lite"} and ref $self->{"http_lite"}
exists $self->{"http_lite"} ref $self->{"http_lite"}
Truth table0%0/3
A and B
A= exists $self->{"http_lite"}B= ref $self->{"http_lite"}
ABresult
0X0
100
111
599 0 ▶ return 'HTTP::Lite' if defined $HTTP::Lite::VERSION;
Branch: 1 branch
BranchTrueFalseTotal
if defined $HTTP::Lite::VERSION 0 0 0
600 0 ▶ return 'HTTP::Lite' if &load_http_lite();
Branch: 1 branch
BranchTrueFalseTotal
if &load_http_lite() 0 0 0
601 0 return $self->die( "HTTP::Lite is required: $_[1]" );
602 }
603  
604 0 ▶ return 'LWP::UserAgent' if defined $LWP::UserAgent::VERSION;
Branch: 1 branch
BranchTrueFalseTotal
if defined $LWP::UserAgent::VERSION 0 0 0
605 0 ▶ return 'HTTP::Lite' if defined $HTTP::Lite::VERSION;
Branch: 1 branch
BranchTrueFalseTotal
if defined $HTTP::Lite::VERSION 0 0 0
606 0 ▶ return 'LWP::UserAgent' if &load_lwp_useragent();
Branch: 1 branch
BranchTrueFalseTotal
if &load_lwp_useragent() 0 0 0
607 0 ▶ return 'HTTP::Lite' if &load_http_lite();
Branch: 1 branch
BranchTrueFalseTotal
if &load_http_lite() 0 0 0
608 0 return $self->die( "LWP::UserAgent or HTTP::Lite is required: $_[1]" );
609 }
610  
611 0 ▶sub load_lwp_useragent {
Subroutine load_lwp_useragent: not called CC=2 SCAR=17.9
Pod undocumented
612 0 ▶ return $LWP::UserAgent::VERSION if defined $LWP::UserAgent::VERSION;
Branch: 1 branch
BranchTrueFalseTotal
if defined $LWP::UserAgent::VERSION 0 0 0
613 0 local $@;
614 0 eval { require LWP::UserAgent; };
615 0 $LWP::UserAgent::VERSION;
616 }
617  
618 0 ▶sub load_http_lite {
Subroutine load_http_lite: not called CC=2 SCAR=17.9
Pod undocumented
619 0 ▶ return $HTTP::Lite::VERSION if defined $HTTP::Lite::VERSION;
Branch: 1 branch
BranchTrueFalseTotal
if defined $HTTP::Lite::VERSION 0 0 0
620 0 local $@;
621 0 eval { require HTTP::Lite; };
622 0 $HTTP::Lite::VERSION;
623 }
624  
625 0 ▶sub load_tie_ixhash {
Subroutine load_tie_ixhash: not called CC=2 SCAR=17.9
Pod undocumented
626 0 ▶ return $Tie::IxHash::VERSION if defined $Tie::IxHash::VERSION;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Tie::IxHash::VERSION 0 0 0
627 0 local $@;
628 0 eval { require Tie::IxHash; };
629 0 $Tie::IxHash::VERSION;
630 }
631  
632 0 ▶sub parsehttp_lwp {
Subroutine parsehttp_lwp: not called CC=16 SCAR=56.1
Pod undocumented
633 0 my $self = shift;
634 0 ▶ my $method = shift or return $self->die( 'Invalid HTTP method' );
Branch: 1 branch
BranchTrueFalseTotal
my $method = shift() or return $self->die("Invalid HTTP method") 0 0 0
635 0 ▶ my $url = shift or return $self->die( 'Invalid URL' );
Branch: 1 branch
BranchTrueFalseTotal
my $url = shift() or return $self->die("Invalid URL") 0 0 0
636 0 my $body = shift;
637 0 my $header = shift;
638  
639 0 ▶ my $ua = $self->{lwp_useragent} if exists $self->{lwp_useragent};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"lwp_useragent"} 0 0 0
640 0 ▶ if ( ! ref $ua ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $ua) { } 0 0 0
641 0 $ua = LWP::UserAgent->new();
642 0 $ua->env_proxy();
643 0 ▶ $ua->agent( $self->{__user_agent} ) if defined $self->{__user_agent};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"__user_agent"} 0 0 0
644 } else {
645 0 ▶ $ua->agent( $self->{__user_agent} ) if exists $self->{user_agent};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"user_agent"} 0 0 0
646 }
647  
648 0 my $req = HTTP::Request->new( $method, $url );
649 0 my $ct = 0;
650 0 ▶ if ( ref $header ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $header) 0 0 0
651 0 foreach my $field ( sort keys %$header ) {
652 0 my $value = $header->{$field};
653 0 $req->header( $field => $value );
654 0 ▶ $ct ++ if ( $field =~ /^Content-Type$/i );
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /^Content-Type$/i 0 0 0
655 }
656 }
657 0 ▶ if ( defined $body && ! $ct ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body and not $ct) 0 0 0
Condition0%0/3
defined $body and not $ct
!ll&&!rl&&r
000
MC/DC0%0/2
defined $body and not $ct
defined $body not $ct
Truth table0%0/3
A and not(B)
A= defined $bodyB= not $ct
ABresult
0X0
100
111
658 0 $req->header( 'Content-Type' => 'application/x-www-form-urlencoded' );
659 }
660 0 ▶ $req->add_content_utf8($body) if defined $body;
Branch: 1 branch
BranchTrueFalseTotal
if defined $body 0 0 0
661 0 my $res = $ua->request($req);
662 0 my $code = $res->code();
663 0 my $text;
664 0 ▶ if ( $res->can( 'decoded_content' )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->can('decoded_content')) { } 0 0 0
665 0 $text = $res->decoded_content( charset => 'none' );
666 } else {
667 0 $text = $res->content(); # less than LWP 5.802
668 }
669 0 ▶ my $tree = $self->parse( \$text ) if $res->is_success();
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 0 0 0
670 0 ▶ wantarray ? ( $tree, $text, $code ) : $tree;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
671 }
672  
673 0 ▶sub parsehttp_lite {
Subroutine parsehttp_lite: not called CC=11 SCAR=48.8
Pod undocumented
674 0 my $self = shift;
675 0 ▶ my $method = shift or return $self->die( 'Invalid HTTP method' );
Branch: 1 branch
BranchTrueFalseTotal
my $method = shift() or return $self->die("Invalid HTTP method") 0 0 0
676 0 ▶ my $url = shift or return $self->die( 'Invalid URL' );
Branch: 1 branch
BranchTrueFalseTotal
my $url = shift() or return $self->die("Invalid URL") 0 0 0
677 0 my $body = shift;
678 0 my $header = shift;
679  
680 0 my $http = HTTP::Lite->new();
681 0 $http->method($method);
682 0 my $ua = 0;
683 0 ▶ if ( ref $header ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $header) 0 0 0
684 0 foreach my $field ( sort keys %$header ) {
685 0 my $value = $header->{$field};
686 0 $http->add_req_header( $field, $value );
687 0 ▶ $ua ++ if ( $field =~ /^User-Agent$/i );
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /^User-Agent$/i 0 0 0
688 }
689 }
690 0 ▶ if ( defined $self->{__user_agent} && ! $ua ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"__user_agent"} and not $ua) 0 0 0
Condition0%0/3
defined $self->{"__user_agent"} and not $ua
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"__user_agent"} and not $ua
defined $self->{"__user_agent"} not $ua
Truth table0%0/3
A and not(B)
A= defined $self->{"__user_agent"}B= not $ua
ABresult
0X0
100
111
691 0 $http->add_req_header( 'User-Agent', $self->{__user_agent} );
692 }
693 0 ▶ $http->{content} = $body if defined $body;
Branch: 1 branch
BranchTrueFalseTotal
if defined $body 0 0 0
694 0 ▶ my $code = $http->request($url) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $code = $http->request($url) or return 0 0 0
695 0 my $text = $http->body();
696 0 my $tree = $self->parse( \$text );
697 0 ▶ wantarray ? ( $tree, $text, $code ) : $tree;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
698 }
699  
700 33 ▶sub parsefile {
Subroutine parsefile: called CC=2 SCAR=7.0
Pod documented
701 33 my $self = shift;
702 33 my $file = shift;
703 33 ▶ return $self->die( 'Invalid filename' ) unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 33 33
704 33 my $text = $self->read_raw_xml($file);
705 32 $self->parse( \$text );
706 }
707  
708 274 ▶sub parse {
Subroutine parse: called CC=28 SCAR=34.5
Pod documented
709 274 my $self = shift;
710 274 ▶ my $text = ref $_[0] ? ${$_[0]} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 32 242 274
711 274 ▶ return $self->die( 'Null XML source' ) unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 273 274
712  
713 273 ▶ my $from = &xml_decl_encoding(\$text) || $XML_ENCODING;
Condition66%2/3
&xml_decl_encoding(\$text) || $XML_ENCODING
l!l&&r!l&&!r
1061670
MC/DC0%0/2
&xml_decl_encoding(\$text) || $XML_ENCODING
&xml_decl_encoding(\$text) $XML_ENCODING
Truth table66%2/3
A || B
A= &xml_decl_encoding(\$text)B= $XML_ENCODING
ABresult
1X1
011
000
714 273 ▶ my $to = $self->{internal_encoding} || $INTERNAL_ENCODING;
Condition33%1/3
$self->{'internal_encoding'} || $INTERNAL_ENCODING
l!l&&r!l&&!r
02730
MC/DC0%0/2
$self->{'internal_encoding'} || $INTERNAL_ENCODING
$self->{'internal_encoding'} $INTERNAL_ENCODING
Truth table33%1/3
A || B
A= $self->{'internal_encoding'}B= $INTERNAL_ENCODING
ABresult
1X1
011
000
715 273 ▶ if ( $from && $to ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from and $to) 273 0 273
Condition33%1/3
$from and $to
!ll&&!rl&&r
00273
MC/DC0%0/2
$from and $to
$from $to
Truth table33%1/3
A and B
A= $fromB= $to
ABresult
0X0
100
111
716 273 my $stat = $self->encode_from_to( \$text, $from, $to );
717 271 ▶ return $self->die( "Unsupported encoding: $from" ) unless $stat;
Branch: 1 branch
BranchTrueFalseTotal
unless $stat 0 271 271
718 }
719  
720 271 local $self->{__force_array};
721 271 local $self->{__force_array_all};
722 271 ▶ if ( exists $self->{force_array} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"force_array"}) 7 264 271
723 7 my $force = $self->{force_array};
724 7 ▶ $force = [$force] unless ref $force;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $force 1 6 7
725 7 $self->{__force_array} = { map { $_ => 1 } @$force };
726 7 $self->{__force_array_all} = $self->{__force_array}->{'*'};
727 }
728  
729 271 local $self->{__force_hash};
730 271 local $self->{__force_hash_all};
731 271 ▶ if ( exists $self->{force_hash} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"force_hash"}) 2 269 271
732 2 my $force = $self->{force_hash};
733 2 ▶ $force = [$force] unless ref $force;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $force 1 1 2
734 2 $self->{__force_hash} = { map { $_ => 1 } @$force };
735 2 $self->{__force_hash_all} = $self->{__force_hash}->{'*'};
736 }
737  
738 271 ▶ my $tnk = $self->{text_node_key} if exists $self->{text_node_key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"text_node_key"} 3 268 271
739 271 ▶ $tnk = $TEXT_NODE_KEY unless defined $tnk;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tnk 34 237 271
740 271 local $self->{text_node_key} = $tnk;
741  
742 271 ▶ my $apre = $self->{attr_prefix} if exists $self->{attr_prefix};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"attr_prefix"} 8 263 271
743 271 ▶ $apre = $ATTR_PREFIX unless defined $apre;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $apre 35 236 271
744 271 local $self->{attr_prefix} = $apre;
745  
746 271 ▶ if ( exists $self->{use_ixhash} && $self->{use_ixhash} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"use_ixhash"} and $self->{"use_ixhash"}) 0 271 271
Condition33%1/3
exists $self->{"use_ixhash"} and $self->{"use_ixhash"}
!ll&&!rl&&r
27100
MC/DC0%0/2
exists $self->{"use_ixhash"} and $self->{"use_ixhash"}
exists $self->{"use_ixhash"} $self->{"use_ixhash"}
Truth table33%1/3
A and B
A= exists $self->{"use_ixhash"}B= $self->{"use_ixhash"}
ABresult
0X0
100
111
747 0 ▶ return $self->die( "Tie::IxHash is required." ) unless &load_tie_ixhash();
Branch: 1 branch
BranchTrueFalseTotal
unless &load_tie_ixhash() 0 0 0
748 }
749  
750 # Avoid segfaults when receving random input (RT #42441)
751 271 ▶ if ( exists $self->{require_xml_decl} && $self->{require_xml_decl} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"require_xml_decl"} and $self->{"require_xml_decl"}) 3 268 271
Condition66%2/3
exists $self->{"require_xml_decl"} and $self->{"require_xml_decl"}
!ll&&!rl&&r
26803
MC/DC50%1/2
exists $self->{"require_xml_decl"} and $self->{"require_xml_decl"}
exists $self->{"require_xml_decl"} $self->{"require_xml_decl"}
Truth table66%2/3
A and B
A= exists $self->{"require_xml_decl"}B= $self->{"require_xml_decl"}
ABresult
0X0
100
111
752 3 ▶ return $self->die( "XML declaration not found" ) unless looks_like_xml(\$text);
Branch: 1 branch
BranchTrueFalseTotal
unless looks_like_xml(\$text) 2 1 3
753 }
754  
755 269 my $flat = $self->xml_to_flat(\$text);
756 269 ▶ my $class = $self->{base_class} if exists $self->{base_class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"base_class"} 1 268 269
757 269 my $tree = $self->flat_to_tree( $flat, '', $class );
758 268 ▶ if ( ref $tree ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tree) 268 0 268
759 268 ▶ if ( defined $class ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $class) { } 1 267 268
elsif (exists $self->{'elem_class'} and $self->{'elem_class'}) { } 1 532 533
760 1 bless( $tree, $class );
761 }
762 ▶ elsif ( exists $self->{elem_class} && $self->{elem_class} ) {
Condition66%2/3
exists $self->{"elem_class"} and $self->{"elem_class"}
!ll&&!rl&&r
26601
MC/DC50%1/2
exists $self->{"elem_class"} and $self->{"elem_class"}
exists $self->{"elem_class"} $self->{"elem_class"}
Truth table66%2/3
A and B
A= exists $self->{"elem_class"}B= $self->{"elem_class"}
ABresult
0X0
100
111
763 1 bless( $tree, $self->{elem_class} );
764 }
765 }
766 268 ▶ wantarray ? ( $tree, $text ) : $tree;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 268 268
767 }
768  
769 269 ▶sub xml_to_flat {
Subroutine xml_to_flat: called CC=28 SCAR=34.0
Pod undocumented
770 269 my $self = shift;
771 269 my $textref = shift; # reference
772 269 my $flat = [];
773 269 my $prefix = $self->{attr_prefix};
774 269 ▶ my $ixhash = ( exists $self->{use_ixhash} && $self->{use_ixhash} );
Condition33%1/3
exists $self->{'use_ixhash'} && $self->{'use_ixhash'}
!ll&&!rl&&r
26900
MC/DC0%0/2
exists $self->{'use_ixhash'} && $self->{'use_ixhash'}
exists $self->{'use_ixhash'} $self->{'use_ixhash'}
Truth table33%1/3
A && B
A= exists $self->{'use_ixhash'}B= $self->{'use_ixhash'}
ABresult
0X0
100
111
775  
776 269 my $deref = \&xml_unescape;
777 269 ▶ my $xml_deref = ( exists $self->{xml_deref} && $self->{xml_deref} );
Condition100%3/3
exists $self->{'xml_deref'} && $self->{'xml_deref'}
!ll&&!rl&&r
2431313
MC/DC100%2/2
exists $self->{'xml_deref'} && $self->{'xml_deref'}
exists $self->{'xml_deref'} $self->{'xml_deref'}
Truth table100%3/3
A && B
A= exists $self->{'xml_deref'}B= $self->{'xml_deref'}
ABresult
0X0
100
111
778 269 ▶ if ( $xml_deref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml_deref) 13 256 269
779 13 ▶ if (( exists $self->{utf8_flag} && $self->{utf8_flag} ) ||
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'utf8_flag'} and $self->{'utf8_flag'} or $ALLOW_UTF8_FLAG and utf8::is_utf8($$textref)) { } 7 6 13
Condition88%8/9
exists $self->{"utf8_flag"} and $self->{"utf8_flag"} or $ALLOW_UTF8_FLAG and utf8::is_utf8($$textref)
l!l&&r!l&&!r
526
exists $self->{"utf8_flag"} and $self->{"utf8_flag"}
!ll&&!rl&&r
355
$ALLOW_UTF8_FLAG and utf8::is_utf8($$textref)
!ll&&!rl&&r
062
MC/DC75%3/4
exists $self->{"utf8_flag"} and $self->{"utf8_flag"} or $ALLOW_UTF8_FLAG and utf8::is_utf8($$textref)
exists $self->{"utf8_flag"} $self->{"utf8_flag"} $ALLOW_UTF8_FLAG utf8::is_utf8($$textref)
Truth table57%4/7
A and B or C and D
A= exists $self->{"utf8_flag"}B= $self->{"utf8_flag"}C= $ALLOW_UTF8_FLAGD= utf8::is_utf8($$textref)
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
780 ( $ALLOW_UTF8_FLAG && utf8::is_utf8( $$textref ))) {
781 7 $deref = \&xml_deref_string;
782 } else {
783 6 $deref = \&xml_deref_octet;
784 }
785 }
786  
787 269 ▶ while ( $$textref =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($$textref =~ / ([^<]*) < (( \? ([^<>]*) \? )|( \!\[CDATA\[(.*?)\]\] )|( \!DOCTYPE\s+([^\[\]<>]*(?:\[.*?\]\s*)?) )|( \!--(.*?)-- )|( ([^\!\?\s<>](?:"[^"]*"|'[^']*'|[^"'<>])*) )) > ([^<]*) /gsx) 2358 269 2627
788 ([^<]*) <
789 ((
790 \? ([^<>]*) \?
791 )|(
792 \!\[CDATA\[(.*?)\]\]
793 )|(
794 \!DOCTYPE\s+([^\[\]<>]*(?:\[.*?\]\s*)?)
795 )|(
796 \!--(.*?)--
797 )|(
798 ([^\!\?\s<>](?:"[^"]*"|'[^']*'|[^"'<>])*)
799 ))
800 > ([^<]*)
801 }sxg ) {
802 my (
803 2358 $ahead, $match, $typePI, $contPI, $typeCDATA,
804 $contCDATA, $typeDocT, $contDocT, $typeCmnt, $contCmnt,
805 $typeElem, $contElem, $follow
806 )
807 = ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 );
808 2358 ▶ if ( defined $ahead && $ahead =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ahead and $ahead =~ /\S/) 0 2358 2358
Condition33%1/3
defined $ahead and $ahead =~ /\S/
!ll&&!rl&&r
023580
MC/DC0%0/2
defined $ahead and $ahead =~ /\S/
defined $ahead $ahead =~ /\S/
Truth table33%1/3
A and B
A= defined $aheadB= $ahead =~ /\S/
ABresult
0X0
100
111
809 0 $ahead =~ s/([^\040-\076])/sprintf("\\x%02X",ord($1))/eg;
810 0 $self->warn( "Invalid string: [$ahead] before <$match>" );
811 }
812  
813 2358 ▶ if ($typeElem) { # Element
Branch: 5 branches
BranchTrueFalseTotal
if ($typeElem) { } 2043 315 2358
elsif ($typeCDATA) { } 177 138 315
elsif ($typeCmnt) { } 28 110 138
elsif ($typeDocT) { } 0 110 110
elsif ($typePI) { } 110 0 110
814 2043 my $node = {};
815 2043 ▶ if ( $contElem =~ s#^/## ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($contElem =~ s[^/][]) { } 960 1083 2043
elsif ($contElem =~ s[/$][]) { } 123 960 1083
816 960 $node->{endTag}++;
817 }
818 elsif ( $contElem =~ s#/$## ) {
819 123 $node->{emptyTag}++;
820 }
821 else {
822 960 $node->{startTag}++;
823 }
824 2043 ▶ $node->{tagName} = $1 if ( $contElem =~ s#^(\S+)\s*## );
Branch: 1 branch
BranchTrueFalseTotal
if $contElem =~ s/^(\S+)\s*// 2043 0 2043
825 2043 ▶ unless ( $node->{endTag} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->{"endTag"}) 1083 960 2043
826 1083 my $attr;
827 1083 ▶ while ( $contElem =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($contElem =~ / ([^\s\=\"\']+)\s*=\s*(?:(")(.*?)"|'(.*?)') /gsx) 353 1083 1436
828 ([^\s\=\"\']+)\s*=\s*(?:(")(.*?)"|'(.*?)')
829 }sxg ) {
830 353 my $key = $1;
831 353 ▶ my $val = &$deref( $2 ? $3 : $4 );
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 329 24 353
832 353 ▶ if ( ! ref $attr ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $attr) 235 118 353
833 235 $attr = {};
834 235 ▶ tie( %$attr, 'Tie::IxHash' ) if $ixhash;
Branch: 1 branch
BranchTrueFalseTotal
if $ixhash 0 235 235
835 }
836 353 $attr->{$prefix.$key} = $val;
837 }
838 1083 ▶ $node->{attributes} = $attr if ref $attr;
Branch: 1 branch
BranchTrueFalseTotal
if ref $attr 235 848 1083
839 }
840 2043 push( @$flat, $node );
841 }
842 elsif ($typeCDATA) { ## CDATASection
843 177 ▶ if ( exists $self->{cdata_scalar_ref} && $self->{cdata_scalar_ref} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'cdata_scalar_ref'} and $self->{'cdata_scalar_ref'}) { } 114 63 177
Condition100%3/3
exists $self->{"cdata_scalar_ref"} and $self->{"cdata_scalar_ref"}
!ll&&!rl&&r
567114
MC/DC100%2/2
exists $self->{"cdata_scalar_ref"} and $self->{"cdata_scalar_ref"}
exists $self->{"cdata_scalar_ref"} $self->{"cdata_scalar_ref"}
Truth table100%3/3
A and B
A= exists $self->{"cdata_scalar_ref"}B= $self->{"cdata_scalar_ref"}
ABresult
0X0
100
111
844 114 push( @$flat, \$contCDATA ); # as reference for scalar
845 }
846 else {
847 63 push( @$flat, $contCDATA ); # as scalar like text node
848 }
849 }
850 elsif ($typeCmnt) { # Comment (ignore)
851 }
852 elsif ($typeDocT) { # DocumentType (ignore)
853 }
854 elsif ($typePI) { # ProcessingInstruction (ignore)
855 }
856 else {
857 0 $self->warn( "Invalid Tag: <$match>" );
858 }
859 2358 ▶ if ( $follow =~ /\S/ ) { # text node
Branch: 1 branch
BranchTrueFalseTotal
if ($follow =~ /\S/) 698 1660 2358
860 698 my $val = &$deref($follow);
861 698 push( @$flat, $val );
862 }
863 }
864 269 $flat;
865 }
866  
867 1229 ▶sub flat_to_tree {
Subroutine flat_to_tree: called CC=35 SCAR=35.6
Pod undocumented
868 1229 my $self = shift;
869 1229 my $source = shift;
870 1229 my $parent = shift;
871 1229 my $class = shift;
872 1229 my $tree = {};
873 1229 my $text = [];
874  
875 1229 ▶ if ( exists $self->{use_ixhash} && $self->{use_ixhash} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"use_ixhash"} and $self->{"use_ixhash"}) 0 1229 1229
Condition33%1/3
exists $self->{"use_ixhash"} and $self->{"use_ixhash"}
!ll&&!rl&&r
122900
MC/DC0%0/2
exists $self->{"use_ixhash"} and $self->{"use_ixhash"}
exists $self->{"use_ixhash"} $self->{"use_ixhash"}
Truth table33%1/3
A and B
A= exists $self->{"use_ixhash"}B= $self->{"use_ixhash"}
ABresult
0X0
100
111
876 0 tie( %$tree, 'Tie::IxHash' );
877 }
878  
879 1229 ▶ while ( scalar @$source ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$source) 2917 268 3185
880 2917 my $node = shift @$source;
881 2917 ▶ if ( !ref $node || UNIVERSAL::isa( $node, "SCALAR" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $node or UNIVERSAL::isa($node, "SCALAR")) 875 2042 2917
Condition100%3/3
not ref $node or UNIVERSAL::isa($node, "SCALAR")
l!l&&r!l&&!r
7611142042
MC/DC100%2/2
not ref $node or UNIVERSAL::isa($node, "SCALAR")
not ref $node UNIVERSAL::isa($node, "SCALAR")
Truth table100%3/3
not(A) or B
A= not ref $nodeB= UNIVERSAL::isa($node, "SCALAR")
ABresult
1X1
011
000
882 875 push( @$text, $node ); # cdata or text node
883 875 next;
884 }
885 2042 my $name = $node->{tagName};
886 2042 ▶ if ( $node->{endTag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"endTag"}) 959 1083 2042
887 959 ▶ last if ( $parent eq $name );
Branch: 1 branch
BranchTrueFalseTotal
if $parent eq $name 957 2 959
888 2 return $self->die( "Invalid tag sequence: <$parent></$name>" );
889 }
890 1083 my $elem = $node->{attributes};
891 1083 ▶ my $forcehash = $self->{__force_hash_all} || $self->{__force_hash}->{$name};
Condition100%3/3
$self->{'__force_hash_all'} || $self->{'__force_hash'}{$name}
l!l&&r!l&&!r
861069
MC/DC100%2/2
$self->{'__force_hash_all'} || $self->{'__force_hash'}{$name}
$self->{'__force_hash_all'} $self->{'__force_hash'}{$name}
Truth table100%3/3
A || B
A= $self->{'__force_hash_all'}B= $self->{'__force_hash'}{$name}
ABresult
1X1
011
000
892 1083 my $subclass;
893 1083 ▶ if ( defined $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class) 15 1068 1083
894 15 my $escname = $name;
895 15 $escname =~ s/\W/_/sg;
896 15 $subclass = $class.'::'.$escname;
897 }
898 1083 ▶ if ( $node->{startTag} ) { # recursive call
Branch: 2 branches
BranchTrueFalseTotal
if ($node->{'startTag'}) { } 960 123 1083
elsif ($forcehash and not ref $elem) { } 2 242 244
899 960 my $child = $self->flat_to_tree( $source, $name, $subclass );
900 958 ▶ next unless defined $child;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child 1 957 958
901 957 ▶ my $hasattr = scalar keys %$elem if ref $elem;
Branch: 1 branch
BranchTrueFalseTotal
if ref $elem 123 834 957
902 957 ▶ if ( UNIVERSAL::isa( $child, "HASH" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($child, 'HASH')) { } 189 768 957
903 189 ▶ if ( $hasattr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hasattr) { } 92 97 189
904 # some attributes and some child nodes
905 92 %$elem = ( %$elem, %$child );
906 }
907 else {
908 # some child nodes without attributes
909 97 $elem = $child;
910 }
911 }
912 else {
913 768 ▶ if ( $hasattr ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hasattr) { } 31 737 768
elsif ($forcehash) { } 7 730 737
914 # some attributes and text node
915 31 $elem->{$self->{text_node_key}} = $child;
916 }
917 elsif ( $forcehash ) {
918 # only text node without attributes
919 7 $elem = { $self->{text_node_key} => $child };
920 }
921 else {
922 # text node without attributes
923 730 $elem = $child;
924 }
925 }
926 }
927 ▶ elsif ( $forcehash && ! ref $elem ) {
Condition66%2/3
$forcehash and not ref $elem
!ll&&!rl&&r
12102
MC/DC50%1/2
$forcehash and not ref $elem
$forcehash not ref $elem
Truth table66%2/3
A and not(B)
A= $forcehashB= not ref $elem
ABresult
0X0
100
111
928 2 $elem = {};
929 }
930 # bless to a class by base_class or elem_class
931 1080 ▶ if ( ref $elem && UNIVERSAL::isa( $elem, "HASH" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $elem and UNIVERSAL::isa($elem, "HASH")) 341 739 1080
Condition100%3/3
ref $elem and UNIVERSAL::isa($elem, "HASH")
!ll&&!rl&&r
67366341
MC/DC100%2/2
ref $elem and UNIVERSAL::isa($elem, "HASH")
ref $elem UNIVERSAL::isa($elem, "HASH")
Truth table100%3/3
A and B
A= ref $elemB= UNIVERSAL::isa($elem, "HASH")
ABresult
0X0
100
111
932 341 ▶ if ( defined $subclass ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $subclass) { } 10 331 341
elsif (exists $self->{'elem_class'} and $self->{'elem_class'}) { } 10 642 652
933 10 bless( $elem, $subclass );
934 ▶ } elsif ( exists $self->{elem_class} && $self->{elem_class} ) {
Condition66%2/3
exists $self->{"elem_class"} and $self->{"elem_class"}
!ll&&!rl&&r
321010
MC/DC50%1/2
exists $self->{"elem_class"} and $self->{"elem_class"}
exists $self->{"elem_class"} $self->{"elem_class"}
Truth table66%2/3
A and B
A= exists $self->{"elem_class"}B= $self->{"elem_class"}
ABresult
0X0
100
111
935 10 my $escname = $name;
936 10 $escname =~ s/\W/_/sg;
937 10 my $elmclass = $self->{elem_class}.'::'.$escname;
938 10 bless( $elem, $elmclass );
939 }
940 }
941 # next unless defined $elem;
942 1080 ▶ $tree->{$name} ||= [];
Condition100%2/2
$tree->{$name} ||= []
l!l
148932
MC/DC100%1/1
$tree->{$name} ||= []
$tree->{$name}
Truth table100%2/2
A
A= $tree->{$name}
Aresult
11
00
943 1080 push( @{ $tree->{$name} }, $elem );
944 }
945 1225 ▶ if ( ! $self->{__force_array_all} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"__force_array_all"}) 1220 5 1225
946 1220 foreach my $key ( keys %$tree ) {
947 928 ▶ next if $self->{__force_array}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__force_array"}{$key} 8 920 928
948 920 ▶ next if ( 1 < scalar @{ $tree->{$key} } );
Branch: 1 branch
BranchTrueFalseTotal
if 1 < scalar @{$tree->{$key};} 21 899 920
949 899 $tree->{$key} = shift @{ $tree->{$key} };
950 }
951 }
952 1225 my $haschild = scalar keys %$tree;
953 1225 ▶ if ( scalar @$text ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @$text) { } 752 473 1225
elsif (not $haschild) { } 28 445 473
954 752 ▶ if ( scalar @$text == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @$text == 1) { } 677 75 752
elsif (not scalar grep({ref $_;} @$text)) { } 36 39 75
955 # one text node (normal)
956 677 $text = shift @$text;
957 }
958 198 elsif ( ! scalar grep {ref $_} @$text ) {
959 # some text node splitted
960 36 $text = join( '', @$text );
961 }
962 else {
963 # some cdata node
964 39 ▶ my $join = join( '', map {ref $_ ? $$_ : $_} @$text );
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 81 22 103
965 39 $text = \$join;
966 }
967 752 ▶ if ( $haschild ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($haschild) { } 12 740 752
968 # some child nodes and also text node
969 12 $tree->{$self->{text_node_key}} = $text;
970 }
971 else {
972 # only text node without child nodes
973 740 $tree = $text;
974 }
975 }
976 elsif ( ! $haschild ) {
977 # no child and no text
978 28 $tree = "";
979 }
980 1225 $tree;
981 }
982  
983 306 ▶sub hash_to_xml {
Subroutine hash_to_xml: called CC=29 SCAR=33.7
Pod undocumented
984 306 my $self = shift;
985 306 my $name = shift;
986 306 my $hash = shift;
987 306 my $out = [];
988 306 my $attr = [];
989 306 my $allkeys = [ keys %$hash ];
990 306 ▶ my $fo = $self->{__first_out} if ref $self->{__first_out};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"__first_out"} 29 277 306
991 306 ▶ my $lo = $self->{__last_out} if ref $self->{__last_out};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"__last_out"} 4 302 306
992 306 ▶ my $firstkeys = [ sort { $fo->{$a} <=> $fo->{$b} } grep { exists $fo->{$_} } @$allkeys ] if ref $fo;
Branch: 1 branch
BranchTrueFalseTotal
if ref $fo 29 277 306
993 306 ▶ my $lastkeys = [ sort { $lo->{$a} <=> $lo->{$b} } grep { exists $lo->{$_} } @$allkeys ] if ref $lo;
Branch: 1 branch
BranchTrueFalseTotal
if ref $lo 4 302 306
994 306 ▶ $allkeys = [ grep { ! exists $fo->{$_} } @$allkeys ] if ref $fo;
Branch: 1 branch
BranchTrueFalseTotal
if ref $fo 29 277 306
995 306 ▶ $allkeys = [ grep { ! exists $lo->{$_} } @$allkeys ] if ref $lo;
Branch: 1 branch
BranchTrueFalseTotal
if ref $lo 4 302 306
996 306 ▶ unless ( exists $self->{use_ixhash} && $self->{use_ixhash} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"use_ixhash"} and $self->{"use_ixhash"}) 306 0 306
Condition33%1/3
exists $self->{"use_ixhash"} and $self->{"use_ixhash"}
!ll&&!rl&&r
30600
MC/DC0%0/2
exists $self->{"use_ixhash"} and $self->{"use_ixhash"}
exists $self->{"use_ixhash"} $self->{"use_ixhash"}
Truth table33%1/3
A and B
A= exists $self->{"use_ixhash"}B= $self->{"use_ixhash"}
ABresult
0X0
100
111
997 306 $allkeys = [ sort @$allkeys ];
998 }
999 306 my $prelen = $self->{__attr_prefix_len};
1000 306 my $pregex = $self->{__attr_prefix_rex};
1001 306 my $textnk = $self->{text_node_key};
1002 306 ▶ my $tagend = $self->{empty_element_tag_end} || $EMPTY_ELEMENT_TAG_END;
Condition66%2/3
$self->{'empty_element_tag_end'} || $EMPTY_ELEMENT_TAG_END
l!l&&r!l&&!r
33030
MC/DC0%0/2
$self->{'empty_element_tag_end'} || $EMPTY_ELEMENT_TAG_END
$self->{'empty_element_tag_end'} $EMPTY_ELEMENT_TAG_END
Truth table66%2/3
A || B
A= $self->{'empty_element_tag_end'}B= $EMPTY_ELEMENT_TAG_END
ABresult
1X1
011
000
1003  
1004 306 foreach my $keys ( $firstkeys, $allkeys, $lastkeys ) {
1005 918 ▶ next unless ref $keys;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $keys 579 339 918
1006 339 ▶ my $elemkey = $prelen ? [ grep { substr($_,0,$prelen) ne $pregex } @$keys ] : $keys;
Branch: 1 branch
BranchTrueFalseTotal
$prelen ? : 333 6 339
1007 339 ▶ my $attrkey = $prelen ? [ grep { substr($_,0,$prelen) eq $pregex } @$keys ] : [];
Branch: 1 branch
BranchTrueFalseTotal
$prelen ? : 333 6 339
1008  
1009 339 foreach my $key ( @$elemkey ) {
1010 406 my $val = $hash->{$key};
1011 406 ▶ if ( !defined $val ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $val) { } 7 399 406
elsif (UNIVERSAL::isa($val, 'HASH')) { } 132 267 399
elsif (UNIVERSAL::isa($val, 'ARRAY')) { } 8 259 267
elsif (UNIVERSAL::isa($val, 'SCALAR')) { } 50 209 259
1012 7 ▶ next if ($key eq $textnk);
Branch: 1 branch
BranchTrueFalseTotal
if $key eq $textnk 1 6 7
1013 6 push( @$out, "<$key$tagend" );
1014 }
1015 elsif ( UNIVERSAL::isa( $val, 'HASH' ) ) {
1016 132 my $child = $self->hash_to_xml( $key, $val );
1017 132 push( @$out, $child );
1018 }
1019 elsif ( UNIVERSAL::isa( $val, 'ARRAY' ) ) {
1020 8 my $child = $self->array_to_xml( $key, $val );
1021 8 push( @$out, $child );
1022 }
1023 elsif ( UNIVERSAL::isa( $val, 'SCALAR' ) ) {
1024 50 my $child = $self->scalaref_to_cdata( $key, $val );
1025 50 push( @$out, $child );
1026 }
1027 else {
1028 209 my $ref = ref $val;
1029 209 ▶ $self->warn( "Unsupported reference type: $ref in $key" ) if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 2 207 209
1030 209 my $child = $self->scalar_to_xml( $key, $val );
1031 209 push( @$out, $child );
1032 }
1033 }
1034  
1035 339 foreach my $key ( @$attrkey ) {
1036 53 my $name = substr( $key, $prelen );
1037 53 my $val = &xml_escape( $hash->{$key} );
1038 53 push( @$attr, ' ' . $name . '="' . $val . '"' );
1039 }
1040 }
1041 306 my $jattr = join( '', @$attr );
1042  
1043 306 ▶ if ( defined $name && scalar @$out && ! grep { ! /^</s } @$out ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and scalar @$out and not grep({not /^</s;} @$out)) 103 203 306
Condition100%5/5
defined $name and scalar @$out and not grep({not /^</s;} @$out)
!ll&&!rl&&r
18023103
defined $name and scalar @$out
!ll
162144
MC/DC100%2/2
defined $name and scalar @$out and not grep({not /^</s;} @$out)
defined $name not grep({not /^</s;} @$out)
Truth table100%3/3
A and not(B)
A= defined $nameB= not grep({not /^</s;} @$out)
ABresult
0X0
100
111
1044 # Use human-friendly white spacing
1045 103 ▶ if ( defined $self->{__indent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"__indent"}) 6 97 103
1046 6 s/^(\s*<)/$self->{__indent}$1/mg foreach @$out;
1047 }
1048 103 unshift( @$out, "\n" );
1049 }
1050  
1051 306 my $text = join( '', @$out );
1052 306 ▶ if ( defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 144 162 306
1053 144 ▶ if ( scalar @$out ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$out) { } 126 18 144
1054 126 $text = "<$name$jattr>$text</$name>\n";
1055 }
1056 else {
1057 18 $text = "<$name$jattr$tagend\n";
1058 }
1059 }
1060 306 $text;
1061 }
1062  
1063 8 ▶sub array_to_xml {
Subroutine array_to_xml: called CC=8 SCAR=22.3
Pod undocumented
1064 8 my $self = shift;
1065 8 my $name = shift;
1066 8 my $array = shift;
1067 8 my $out = [];
1068 8 ▶ my $tagend = $self->{empty_element_tag_end} || $EMPTY_ELEMENT_TAG_END;
Condition33%1/3
$self->{'empty_element_tag_end'} || $EMPTY_ELEMENT_TAG_END
l!l&&r!l&&!r
080
MC/DC0%0/2
$self->{'empty_element_tag_end'} || $EMPTY_ELEMENT_TAG_END
$self->{'empty_element_tag_end'} $EMPTY_ELEMENT_TAG_END
Truth table33%1/3
A || B
A= $self->{'empty_element_tag_end'}B= $EMPTY_ELEMENT_TAG_END
ABresult
1X1
011
000
1069  
1070 8 foreach my $val (@$array) {
1071 16 ▶ if ( !defined $val ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $val) { } 1 15 16
elsif (UNIVERSAL::isa($val, 'HASH')) { } 12 3 15
elsif (UNIVERSAL::isa($val, 'ARRAY')) { } 0 3 3
elsif (UNIVERSAL::isa($val, 'SCALAR')) { } 0 3 3
1072 1 push( @$out, "<$name$tagend\n" );
1073 }
1074 elsif ( UNIVERSAL::isa( $val, 'HASH' ) ) {
1075 12 my $child = $self->hash_to_xml( $name, $val );
1076 12 push( @$out, $child );
1077 }
1078 elsif ( UNIVERSAL::isa( $val, 'ARRAY' ) ) {
1079 0 my $child = $self->array_to_xml( $name, $val );
1080 0 push( @$out, $child );
1081 }
1082 elsif ( UNIVERSAL::isa( $val, 'SCALAR' ) ) {
1083 0 my $child = $self->scalaref_to_cdata( $name, $val );
1084 0 push( @$out, $child );
1085 }
1086 else {
1087 3 my $ref = ref $val;
1088 3 ▶ $self->warn( "Unsupported reference type: $ref in $name" ) if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 0 3 3
1089 3 my $child = $self->scalar_to_xml( $name, $val );
1090 3 push( @$out, $child );
1091 }
1092 }
1093  
1094 8 my $text = join( '', @$out );
1095 8 $text;
1096 }
1097  
1098 50 ▶sub scalaref_to_cdata {
Subroutine scalaref_to_cdata: called CC=3 SCAR=11.0
Pod undocumented
1099 50 my $self = shift;
1100 50 my $name = shift;
1101 50 my $ref = shift;
1102 50 ▶ my $data = defined $$ref ? $$ref : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $$ref ? : 49 1 50
1103 50 $data =~ s#(]])(>)#$1]]><![CDATA[$2#g;
1104 50 my $text = '<![CDATA[' . $data . ']]>';
1105 50 ▶ $text = "<$name>$text</$name>\n" if ( $name ne $self->{text_node_key} );
Branch: 1 branch
BranchTrueFalseTotal
if $name ne $self->{"text_node_key"} 44 6 50
1106 50 $text;
1107 }
1108  
1109 212 ▶sub scalar_to_xml {
Subroutine scalar_to_xml: called CC=2 SCAR=6.9
Pod undocumented
1110 212 my $self = shift;
1111 212 my $name = shift;
1112 212 my $scalar = shift;
1113 212 my $copy = $scalar;
1114 212 my $text = &xml_escape($copy);
1115 212 ▶ $text = "<$name>$text</$name>\n" if ( $name ne $self->{text_node_key} );
Branch: 1 branch
BranchTrueFalseTotal
if $name ne $self->{"text_node_key"} 189 23 212
1116 212 $text;
1117 }
1118  
1119 3 ▶sub write_raw_xml {
Subroutine write_raw_xml: called CC=2 SCAR=7.0
Pod undocumented
1120 3 my $self = shift;
1121 3 my $file = shift;
1122 3 my $fh = Symbol::gensym();
1123 3 ▶ open( $fh, ">$file" ) or return $self->die( "$! - $file" );
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">$file" or return $self->die("$! - $file") 3 0 3
1124 3 print $fh @_;
1125 3 close($fh);
1126 }
1127  
1128 33 ▶sub read_raw_xml {
Subroutine read_raw_xml: called CC=2 SCAR=6.9
Pod undocumented
1129 33 my $self = shift;
1130 33 my $file = shift;
1131 33 my $fh = Symbol::gensym();
1132 33 ▶ open( $fh, $file ) or return $self->die( "$! - $file" );
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $file or return $self->die("$! - $file") 31 2 33
1133 31 local $/ = undef;
1134 31 my $text = <$fh>;
1135 31 close($fh);
1136 31 $text;
1137 }
1138  
1139 276 ▶sub looks_like_xml {
Subroutine looks_like_xml: called CC=2 SCAR=6.9
Pod undocumented
1140 276 my $textref = shift;
1141 276 my $args = ( $$textref =~ /^(?:\s*\xEF\xBB\xBF)?\s*<\?xml(\s+\S.*)\?>/s )[0];
1142 276 ▶ if ( ! $args ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args) 163 113 276
1143 163 return;
1144 }
1145 113 return $args;
1146 }
1147  
1148 273 ▶sub xml_decl_encoding {
Subroutine xml_decl_encoding: called CC=4 SCAR=13.9
Pod undocumented
1149 273 my $textref = shift;
1150 273 ▶ return unless defined $$textref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$textref 0 273 273
1151 273 ▶ my $args = looks_like_xml($textref) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $args = looks_like_xml($textref) or return 112 161 273
1152 112 ▶ my $getcode = ( $args =~ /\s+encoding=(".*?"|'.*?')/ )[0] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $getcode = ($args =~ /\s+encoding=(".*?"|'.*?')/)[0] or return 106 6 112
1153 106 $getcode =~ s/^['"]//;
1154 106 $getcode =~ s/['"]$//;
1155 106 $getcode;
1156 }
1157  
1158 435 ▶sub encode_from_to {
Subroutine encode_from_to: called CC=30 SCAR=47.5
Pod undocumented
1159 435 my $self = shift;
1160 435 ▶ my $txtref = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $txtref = shift() or return 435 0 435
1161 435 ▶ my $from = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $from = shift() or return 435 0 435
1162 435 ▶ my $to = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $to = shift() or return 435 0 435
1163  
1164 435 ▶ unless ( defined $Encode::EUCJPMS::VERSION ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Encode::EUCJPMS::VERSION) 435 0 435
1165 435 ▶ $from = 'EUC-JP' if ( $from =~ /\beuc-?jp-?(win|ms)$/i );
Branch: 1 branch
BranchTrueFalseTotal
if $from =~ /\beuc-?jp-?(win|ms)$/i 0 435 435
1166 435 ▶ $to = 'EUC-JP' if ( $to =~ /\beuc-?jp-?(win|ms)$/i );
Branch: 1 branch
BranchTrueFalseTotal
if $to =~ /\beuc-?jp-?(win|ms)$/i 0 435 435
1167 }
1168  
1169 435 my $RE_IS_UTF8 = qr/^utf-?8$/i;
1170 435 ▶ if ( $from =~ $RE_IS_UTF8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from =~ /$RE_IS_UTF8/i) 425 10 435
1171 425 $$txtref =~ s/^\xEF\xBB\xBF//s; # UTF-8 BOM (Byte Order Mark)
1172 }
1173  
1174 435 ▶ my $setflag = $self->{utf8_flag} if exists $self->{utf8_flag};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"utf8_flag"} 38 397 435
1175 435 ▶ if ( ! $ALLOW_UTF8_FLAG && $setflag ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ALLOW_UTF8_FLAG and $setflag) 0 435 435
Condition33%1/3
not $ALLOW_UTF8_FLAG and $setflag
!ll&&!rl&&r
43500
MC/DC0%0/2
not $ALLOW_UTF8_FLAG and $setflag
not $ALLOW_UTF8_FLAG $setflag
Truth table33%1/3
not(A) and B
A= not $ALLOW_UTF8_FLAGB= $setflag
ABresult
0X0
100
111
1176 0 return $self->die( "Perl 5.8.1 is required for utf8_flag: $]" );
1177 }
1178  
1179 435 ▶ if ( $USE_ENCODE_PM ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($USE_ENCODE_PM) { } 435 0 435
elsif (uc $from eq 'ISO-8859-1' || uc $from eq 'US-ASCII' || uc $from eq 'LATIN-1' and uc $to eq 'UTF-8') { } 0 0 0
1180 435 &load_encode();
1181 435 my $encver = ( $Encode::VERSION =~ /^([\d\.]+)/ )[0];
1182 435 ▶ my $check = ( $encver < 2.13 ) ? 0x400 : Encode::FB_XMLCREF();
Branch: 1 branch
BranchTrueFalseTotal
$encver < 2.13 ? : 0 435 435
1183  
1184 435 ▶ my $encfrom = Encode::find_encoding($from) if $from;
Branch: 1 branch
BranchTrueFalseTotal
if $from 435 0 435
1185 435 ▶ return $self->die( "Unknown encoding: $from" ) unless ref $encfrom;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $encfrom 2 433 435
1186 433 ▶ my $encto = Encode::find_encoding($to) if $to;
Branch: 1 branch
BranchTrueFalseTotal
if $to 433 0 433
1187 433 ▶ return $self->die( "Unknown encoding: $to" ) unless ref $encto;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $encto 2 431 433
1188  
1189 431 ▶ if ( $ALLOW_UTF8_FLAG && utf8::is_utf8( $$txtref ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ALLOW_UTF8_FLAG and utf8::is_utf8($$txtref)) { } 13 418 431
Condition66%2/3
$ALLOW_UTF8_FLAG and utf8::is_utf8($$txtref)
!ll&&!rl&&r
041813
MC/DC50%1/2
$ALLOW_UTF8_FLAG and utf8::is_utf8($$txtref)
$ALLOW_UTF8_FLAG utf8::is_utf8($$txtref)
Truth table66%2/3
A and B
A= $ALLOW_UTF8_FLAGB= utf8::is_utf8($$txtref)
ABresult
0X0
100
111
1190 13 ▶ if ( $to =~ $RE_IS_UTF8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to =~ /$RE_IS_UTF8/) { } 13 0 13
1191 # skip
1192 } else {
1193 0 $$txtref = $encto->encode( $$txtref, $check );
1194 }
1195 } else {
1196 418 $$txtref = $encfrom->decode( $$txtref );
1197 418 ▶ if ( $to =~ $RE_IS_UTF8 && $setflag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to =~ /$RE_IS_UTF8/i and $setflag) { } 15 403 418
Condition100%3/3
$to =~ /$RE_IS_UTF8/i and $setflag
!ll&&!rl&&r
1239115
MC/DC100%2/2
$to =~ /$RE_IS_UTF8/i and $setflag
$to =~ /$RE_IS_UTF8/i $setflag
Truth table100%3/3
A and B
A= $to =~ /$RE_IS_UTF8/iB= $setflag
ABresult
0X0
100
111
1198 # skip
1199 } else {
1200 403 $$txtref = $encto->encode( $$txtref, $check );
1201 }
1202 }
1203 }
1204 ▶ elsif ( ( uc($from) eq 'ISO-8859-1'
Condition0%0/9
uc $from eq "ISO-8859-1" || uc $from eq "US-ASCII" || uc $from eq "LATIN-1" and uc $to eq "UTF-8"
!ll&&!rl&&r
000
uc $from eq "ISO-8859-1" or uc $from eq "US-ASCII" or uc $from eq "LATIN-1"
l!l&&r!l&&!r
000
uc $from eq "ISO-8859-1" or uc $from eq "US-ASCII"
l!l&&r!l&&!r
000
MC/DC0%0/4
uc $from eq "ISO-8859-1" || uc $from eq "US-ASCII" || uc $from eq "LATIN-1" and uc $to eq "UTF-8"
uc $from eq "ISO-8859-1" uc $from eq "US-ASCII" uc $from eq "LATIN-1" uc $to eq "UTF-8"
Truth table0%0/7
A or B or C and D
A= uc $from eq "ISO-8859-1"B= uc $from eq "US-ASCII"C= uc $from eq "LATIN-1"D= uc $to eq "UTF-8"
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
1205 || uc($from) eq 'US-ASCII'
1206 || uc($from) eq 'LATIN-1' ) && uc($to) eq 'UTF-8' ) {
1207 0 &latin1_to_utf8($txtref);
1208 }
1209 else {
1210 0 my $jfrom = &get_jcode_name($from);
1211 0 my $jto = &get_jcode_name($to);
1212 0 ▶ return $to if ( uc($jfrom) eq uc($jto) );
Branch: 1 branch
BranchTrueFalseTotal
if uc $jfrom eq uc $jto 0 0 0
1213 0 ▶ if ( $jfrom && $jto ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jfrom and $jto) { } 0 0 0
Condition0%0/3
$jfrom and $jto
!ll&&!rl&&r
000
MC/DC0%0/2
$jfrom and $jto
$jfrom $jto
Truth table0%0/3
A and B
A= $jfromB= $jto
ABresult
0X0
100
111
1214 0 &load_jcode();
1215 0 ▶ if ( defined $Jcode::VERSION ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Jcode::VERSION) { } 0 0 0
1216 0 Jcode::convert( $txtref, $jto, $jfrom );
1217 }
1218 else {
1219 0 return $self->die( "Jcode.pm is required: $from to $to" );
1220 }
1221 }
1222 else {
1223 0 return $self->die( "Encode.pm is required: $from to $to" );
1224 }
1225 }
1226 431 $to;
1227 }
1228  
1229 0 ▶sub load_jcode {
Subroutine load_jcode: not called CC=2 SCAR=17.9
Pod undocumented
1230 0 ▶ return if defined $Jcode::VERSION;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Jcode::VERSION 0 0 0
1231 0 local $@;
1232 0 eval { require Jcode; };
1233 }
1234  
1235 435 ▶sub load_encode {
Subroutine load_encode: called CC=2 SCAR=11.6
Pod undocumented
1236 435 ▶ return if defined $Encode::VERSION;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Encode::VERSION 435 0 435
1237 0 local $@;
1238 0 eval { require Encode; };
1239 }
1240  
1241 0 ▶sub latin1_to_utf8 {
Subroutine latin1_to_utf8: not called CC=1 SCAR=6.9
Pod undocumented
1242 0 my $strref = shift;
1243 0 $$strref =~ s{
1244 ([\x80-\xFF])
1245 }{
1246 0 pack( 'C2' => 0xC0|(ord($1)>>6),0x80|(ord($1)&0x3F) )
1247 }exg;
1248 }
1249  
1250 0 ▶sub get_jcode_name {
Subroutine get_jcode_name: not called CC=5 SCAR=34.0
Pod undocumented
1251 0 my $src = shift;
1252 0 my $dst;
1253 0 ▶ if ( $src =~ /^utf-?8$/i ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($src =~ /^utf-?8$/i) { } 0 0 0
elsif ($src =~ /^euc.*jp(-?(win|ms))?$/i) { } 0 0 0
elsif ($src =~ /^(shift.*jis|cp932|windows-31j)$/i) { } 0 0 0
elsif ($src =~ /^iso-2022-jp/) { } 0 0 0
1254 0 $dst = 'utf8';
1255 }
1256 elsif ( $src =~ /^euc.*jp(-?(win|ms))?$/i ) {
1257 0 $dst = 'euc';
1258 }
1259 elsif ( $src =~ /^(shift.*jis|cp932|windows-31j)$/i ) {
1260 0 $dst = 'sjis';
1261 }
1262 elsif ( $src =~ /^iso-2022-jp/ ) {
1263 0 $dst = 'jis';
1264 }
1265 0 $dst;
1266 }
1267  
1268 265 ▶sub xml_escape {
Subroutine xml_escape: called CC=2 SCAR=6.9
Pod undocumented
1269 265 my $str = shift;
1270 265 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 1 264 265
1271 # except for TAB(\x09),CR(\x0D),LF(\x0A)
1272 264 $str =~ s{
1273 ([\x00-\x08\x0B\x0C\x0E-\x1F\x7F])
1274 }{
1275 8 sprintf( '&#%d;', ord($1) );
1276 }gex;
1277 264 $str =~ s/&(?!#(\d+;|x[\dA-Fa-f]+;))/&amp;/g;
1278 264 $str =~ s/</&lt;/g;
1279 264 $str =~ s/>/&gt;/g;
1280 264 $str =~ s/'/&apos;/g;
1281 264 $str =~ s/"/&quot;/g;
1282 264 $str;
1283 }
1284  
1285 973 ▶sub xml_unescape {
Subroutine xml_unescape: called CC=3 SCAR=11.0
Pod undocumented
1286 973 my $str = shift;
1287 973 my $map = {qw( quot " lt < gt > apos ' amp & )};
1288 973 $str =~ s{
1289 (&(?:\#(\d{1,3})|\#x([0-9a-fA-F]{1,2})|(quot|lt|gt|apos|amp));)
1290 }{
1291 6325 ▶ $4 ? $map->{$4} : &code_to_ascii( $3 ? hex($3) : $2, $1 );
Branch: 2 branches
BranchTrueFalseTotal
$4 ? : 6140 185 6325
$3 ? : 140 45 185
1292 }gex;
1293 973 $str;
1294 }
1295  
1296 36 ▶sub xml_deref_octet {
Subroutine xml_deref_octet: called CC=3 SCAR=11.0
Pod undocumented
1297 36 my $str = shift;
1298 36 my $map = {qw( quot " lt < gt > apos ' amp & )};
1299 36 $str =~ s{
1300 (&(?:\#(\d{1,7})|\#x([0-9a-fA-F]{1,6})|(quot|lt|gt|apos|amp));)
1301 }{
1302 36 ▶ $4 ? $map->{$4} : &code_to_utf8( $3 ? hex($3) : $2, $1 );
Branch: 2 branches
BranchTrueFalseTotal
$4 ? : 0 36 36
$3 ? : 12 24 36
1303 }gex;
1304 36 $str;
1305 }
1306  
1307 42 ▶sub xml_deref_string {
Subroutine xml_deref_string: called CC=3 SCAR=11.0
Pod undocumented
1308 42 my $str = shift;
1309 42 my $map = {qw( quot " lt < gt > apos ' amp & )};
1310 42 $str =~ s{
1311 (&(?:\#(\d{1,7})|\#x([0-9a-fA-F]{1,6})|(quot|lt|gt|apos|amp));)
1312 }{
1313 30 ▶ $4 ? $map->{$4} : pack( U => $3 ? hex($3) : $2 );
Branch: 2 branches
BranchTrueFalseTotal
$4 ? : 0 30 30
$3 ? : 6 24 30
1314 }gex;
1315 42 $str;
1316 }
1317  
1318 185 ▶sub code_to_ascii {
Subroutine code_to_ascii: called CC=3 SCAR=11.3
Pod undocumented
1319 185 my $code = shift;
1320 185 ▶ if ( $code <= 0x007F ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code <= 127) 158 27 185
1321 158 return pack( C => $code );
1322 }
1323 27 ▶ return shift if scalar @_; # default value
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 27 0 27
1324 0 sprintf( '&#%d;', $code );
1325 }
1326  
1327 36 ▶sub code_to_utf8 {
Subroutine code_to_utf8: called CC=6 SCAR=22.1
Pod undocumented
1328 36 my $code = shift;
1329 36 ▶ if ( $code <= 0x007F ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($code <= 127) { } 12 24 36
elsif ($code <= 2047) { } 12 12 24
elsif ($code <= 65535) { } 12 0 12
elsif ($code <= 1114111) { } 0 0 0
1330 12 return pack( C => $code );
1331 }
1332 elsif ( $code <= 0x07FF ) {
1333 12 return pack( C2 => 0xC0|($code>>6), 0x80|($code&0x3F));
1334 }
1335 elsif ( $code <= 0xFFFF ) {
1336 12 return pack( C3 => 0xE0|($code>>12), 0x80|(($code>>6)&0x3F), 0x80|($code&0x3F));
1337 }
1338 elsif ( $code <= 0x10FFFF ) {
1339 0 return pack( C4 => 0xF0|($code>>18), 0x80|(($code>>12)&0x3F), 0x80|(($code>>6)&0x3F), 0x80|($code&0x3F));
1340 }
1341 0 ▶ return shift if scalar @_; # default value
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 0 0 0
1342 0 sprintf( '&#x%04X;', $code );
1343 }
1344  
1345 43 1;
↑ Summary