blib/lib/Data/asXML.pm

filter: Statementstmt 99.0% 303 / 306 Branchbran 93.4% 86 / 92 Conditioncond 74.0% 20 / 27 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 100% 22 / 22 Podpod 100% 3 / 3 totaltotal 94.8% 441 / 465 CC 62 SCAR 27.6
CC
62
Cov
96%
decode
25.2
encode
24.0
_make_relative_xpath
7.0
CRAP 15.7
↑ Summary
1 package Data::asXML;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Data::asXML - convert data structures to/from XML
8  
9 =head1 SYNOPSIS
10  
11 use Data::asXML;
12 my $dxml = Data::asXML->new();
13 my $dom = $dxml->encode({
14 'some' => 'value',
15 'in' => [ qw(a data structure) ],
16 });
17  
18 my $data = $dxml->decode(q{
19 <HASH>
20 <KEY name="some"><VALUE>value</VALUE></KEY>
21 <KEY name="in">
22 <ARRAY>
23 <VALUE>a</VALUE>
24 <VALUE>data</VALUE>
25 <VALUE>structure</VALUE>
26 </ARRAY>
27 </KEY>
28 </HASH>
29 });
30  
31 my (%hash1, %hash2);
32 $hash1{other}=\%hash2;
33 $hash2{other}=\%hash1;
34 print Data::asXML->new->encode([1, \%hash1, \%hash2])->toString;
35
36 <ARRAY>
37 <VALUE>1</VALUE>
38 <HASH>
39 <KEY name="other">
40 <HASH>
41 <KEY name="other">
42 <HASH href="../../../../*[2]"/>
43 </KEY>
44 </HASH>
45 </KEY>
46 </HASH>
47 <HASH href="*[2]/*[1]/*[1]"/>
48 </ARRAY>
49  
50 For more examples see F<t/01_Data-asXML.t>.
51  
52 =head1 WARNING
53  
54 experimental, use at your own risk :-)
55  
56 =head1 DESCRIPTION
57  
58 There are couple of modules mapping XML to data structures. (L<XML::Compile>,
59 L<XML::TreePP>, L<XML::Simple>, ...) but they aim at making data structures
60 adapt to XML structure. This one defines couple of simple XML tags to represent
61 data structures. It makes the serialization to and from XML possible.
62  
63 For the moment it is an experiment. I plan to use it for passing data
64 structures as DOM to XSLT for transformations, so that I can match them
65 with XPATH similar way how I access them in Perl.
66  
67 /HASH/KEY[@name="key"]/VALUE
68 /HASH/KEY[@name="key2"]/ARRAY/*[3]/VALUE
69 /ARRAY/*[1]/VALUE
70 /ARRAY/*[2]/HASH/KEY[@name="key3"]/VALUE
71  
72 If you are looking for a module to serialize your data, without requirement
73 to do so in XML, you should probably better have a look at L<JSON::XS>
74 or L<Storable>.
75  
76 =cut
77  
78 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
86 2 ▶use XML::LibXML 'XML_ELEMENT_NODE';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶use URI::Escape qw(uri_escape uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
89 2 ▶use Test::Deep::NoTest 'eq_deeply';
Subroutine BEGIN: called CC=1 SCAR=0.0
90 2 ▶use XML::Char;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 2 ▶use MIME::Base64 'decode_base64';
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 2 our $VERSION = '0.07';
94  
95 2 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 =head1 PROPERTIES
98  
99 =over 4
100  
101 =item pretty
102  
103 (default 1 - true) will insert text nodes to the XML to make the output indented.
104  
105 =item safe_mode
106  
107 (default undef - false)
108  
109 in case of C<encode()> perform the xml string decoding back and will compare
110 the two data structures to be sure the data can be reconstructed back without
111 errors.
112  
113 in case of a C<decode()> it will decode to data then encode to xml string and from
114 xml string decode back to data. this two data values are then compared.
115  
116 Both compares is done using L<Test::Deep::NoTest>::eq_deeply.
117  
118 =item namespace
119  
120 (default undef - false)
121  
122 adds xml:ns attribute to the root element. if C<namespace> is set to 1
123 the xml:ns will be L<http://search.cpan.org/perldoc?Data::asXML> otherwise
124 it will be the value of C<namespace>.
125  
126 =back
127  
128 =cut
129  
130 2 __PACKAGE__->mk_accessors(qw{
131 pretty
132 safe_mode
133 namespace
134 namespace_prefix
135 });
136  
137 =head1 METHODS
138  
139 =head2 new()
140  
141 Object constructor.
142  
143 =cut
144  
145 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
146 7 my $class = shift;
147 7 my $self = $class->SUPER::new({
148 'pretty' => 1,
149 @_
150 });
151
152 7 return $self;
153 }
154  
155 308 ▶sub _xml {
Subroutine _xml: called CC=2 SCAR=6.9
156 308 my($self) = @_;
157 308 ▶ if(not exists $self->{'_xml'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_xml"}) 4 304 308
158 4 my $xml = XML::LibXML::Document->new("1.0", "UTF-8");
159 4 $self->{'_xml'} = $xml;
160 }
161 308 return $self->{'_xml'};
162 }
163  
164  
165 154 ▶sub _indent {
Subroutine _indent: called CC=2 SCAR=6.9
166 154 my $self = shift;
167 154 my $where = shift;
168 154 my $indent = shift;
169
170 154 ▶ $where->addChild( $self->_xml->createTextNode( "\n".("\t" x $indent) ) )
Branch: 1 branch
BranchTrueFalseTotal
if $self->pretty 146 8 154
171 if $self->pretty;
172 }
173  
174 121 ▶sub _createElement {
Subroutine _createElement: called CC=3 SCAR=11.0
175 121 my $self = shift;
176 121 my $name = shift;
177 121 my $namespace = $self->namespace;
178 121 my $namespace_prefix = $self->namespace_prefix;
179  
180 121 ▶ $name = join(':',$namespace_prefix,$name)
Branch: 1 branch
BranchTrueFalseTotal
if $namespace_prefix 3 118 121
181 if $namespace_prefix;
182  
183 121 ▶ if ($namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace) { } 6 115 121
184 6 return $self->_xml->createElementNS( $namespace, $name );
185 }
186 else {
187 115 return $self->_xml->createElement($name);
188 }
189 }
190  
191 =head2 encode($what)
192  
193 From structure C<$what> generates L<XML::LibXML::Document> DOM. Call
194 C<< ->toString >> to get XML string. For more actions see L<XML::LibXML>.
195  
196 =cut
197  
198 101 ▶sub encode {
Subroutine encode: called CC=24 SCAR=31.8
Pod documented
199 101 my $self = shift;
200 101 my $what = shift;
201 101 ▶ my $pos = shift || 1;
Condition100%2/2
shift() || 1
l!l
3764
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
202 101 my $where;
203
204 101 my $safe_mode = $self->safe_mode;
205 101 $self->safe_mode(0);
206 101 ▶ my $add_namespace = $self->namespace || 0;
Condition100%2/2
$self->namespace || 0
l!l
497
MC/DC100%1/1
$self->namespace || 0
$self->namespace
Truth table100%2/2
A
A= $self->namespace
Aresult
11
00
207 101 ▶ $add_namespace = "http://search.cpan.org/perldoc?Data::asXML"
Branch: 1 branch
BranchTrueFalseTotal
if $add_namespace eq 1 1 100 101
208 if $add_namespace eq '1';
209 101 $self->namespace(0);
210 101 ▶ $self->namespace($add_namespace)
Branch: 1 branch
BranchTrueFalseTotal
if $add_namespace 4 97 101
211 if $add_namespace;
212
213 101 state $indent = 0;
214  
215 101 ▶ if (not $self->{'_cur_xpath_steps'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_cur_xpath_steps"}) 4 97 101
216 4 $self->{'_href_mapping'} = {};
217 4 $self->{'_cur_xpath_steps'} = [];
218 }
219
220 # create DOM for hash element
221 101 ▶ if (ref($what) eq 'HASH') {
Branch: 5 branches
BranchTrueFalseTotal
if (ref $what eq 'HASH') { } 27 74 101
elsif (ref $what eq 'ARRAY') { } 16 58 74
elsif (ref $what eq 'REF') { } 5 53 58
elsif (ref $what eq 'SCALAR') { } 10 43 53
elsif (ref $what eq '') { } 43 0 43
222 27 $where = $self->_createElement('HASH');
223 27 $indent++;
224 27 push @{$self->{'_cur_xpath_steps'}}, $pos;
225 # already encoded reference
226 27 ▶ if (exists $self->{'_href_mapping'}->{$what}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_href_mapping"}{$what}) 10 17 27
227 $where->setAttribute(
228 'href' =>
229 $self->_make_relative_xpath(
230 [ split(',', $self->{'_href_mapping'}->{$what}) ],
231 10 $self->{'_cur_xpath_steps'}
232 )
233 );
234 10 $indent--;
235 10 pop @{$self->{'_cur_xpath_steps'}};
236 10 return $where;
237 }
238 17 $self->{'_href_mapping'}->{$what} = $self->_xpath_steps_string();
239
240 17 my $key_pos = 0;
241 17 foreach my $key (sort keys %{$what}) {
242 26 my $value = $what->{$key};
243 26 $key_pos++;
244 26 $self->_indent($where, $indent);
245 26 $indent++;
246  
247 26 my $el = $self->_createElement('KEY');
248 26 push @{$self->{'_cur_xpath_steps'}}, $key_pos;
249 26 $self->_indent($el, $indent);
250 26 $el->setAttribute('name', $key);
251 26 $el->addChild($self->encode($value));
252  
253 26 $indent--;
254 26 $self->_indent($el, $indent);
255 26 pop @{$self->{'_cur_xpath_steps'}};
256  
257 26 $where->addChild($el);
258 }
259
260 17 $indent--;
261 17 $self->_indent($where, $indent);
262 17 pop @{$self->{'_cur_xpath_steps'}};
263 }
264 # create DOM for array element
265 elsif (ref($what) eq 'ARRAY') {
266 16 $where = $self->_createElement('ARRAY');
267 16 $indent++;
268 16 push @{$self->{'_cur_xpath_steps'}}, $pos;
269 # already encoded reference
270 16 ▶ if (exists $self->{'_href_mapping'}->{$what}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_href_mapping"}{$what}) 2 14 16
271 $where->setAttribute(
272 'href' =>
273 $self->_make_relative_xpath(
274 [ split(',', $self->{'_href_mapping'}->{$what}) ],
275 2 $self->{'_cur_xpath_steps'}
276 )
277 );
278 2 $indent--;
279 2 pop @{$self->{'_cur_xpath_steps'}};
280 2 return $where;
281 }
282 14 $self->{'_href_mapping'}->{$what.''} = $self->_xpath_steps_string();
283
284 14 my $array_pos = 0;
285 14 foreach my $value (@{$what}) {
286 37 $array_pos++;
287 37 $self->_indent($where, $indent);
288 37 $where->addChild($self->encode($value, $array_pos));
289 }
290
291 14 $indent--;
292 14 $self->_indent($where, $indent);
293 14 pop @{$self->{'_cur_xpath_steps'}};
294 }
295 # create element for pure reference
296 elsif (ref($what) eq 'REF') {
297 5 $where = $self->_createElement('REF');
298 5 $indent++;
299 5 push @{$self->{'_cur_xpath_steps'}}, $pos;
300 # already encoded reference
301 5 ▶ if (exists $self->{'_href_mapping'}->{$what}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_href_mapping"}{$what}) 1 4 5
302 $where->setAttribute(
303 'href' =>
304 $self->_make_relative_xpath(
305 [ split(',', $self->{'_href_mapping'}->{$what}) ],
306 1 $self->{'_cur_xpath_steps'}
307 )
308 );
309 1 $indent--;
310 1 pop @{$self->{'_cur_xpath_steps'}};
311 1 return $where;
312 }
313 4 $self->{'_href_mapping'}->{$what.''} = $self->_xpath_steps_string();
314
315 4 $self->_indent($where, $indent);
316 4 $where->addChild($self->encode($$what));
317
318 4 $indent--;
319 4 $self->_indent($where, $indent);
320 4 pop @{$self->{'_cur_xpath_steps'}};
321 }
322 # scalar reference
323 elsif (ref($what) eq 'SCALAR') {
324 10 push @{$self->{'_cur_xpath_steps'}}, $pos;
325 # already encoded reference
326 10 ▶ if (exists $self->{'_href_mapping'}->{$what}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_href_mapping"}{$what}) 4 6 10
327 4 $where = $self->_createElement('VALUE');
328 $where->setAttribute(
329 'href' =>
330 $self->_make_relative_xpath(
331 [ split(',', $self->{'_href_mapping'}->{$what}) ],
332 4 $self->{'_cur_xpath_steps'}
333 )
334 );
335 4 pop @{$self->{'_cur_xpath_steps'}};
336 4 return $where;
337 }
338 6 $self->{'_href_mapping'}->{$what.''} = $self->_xpath_steps_string();
339  
340 6 $where = $self->encode($$what);
341 6 $where->setAttribute('subtype' => 'ref');
342  
343 6 pop @{$self->{'_cur_xpath_steps'}};
344 }
345 # create text node
346 elsif (ref($what) eq '') {
347 43 $where = $self->_createElement('VALUE');
348 43 ▶ if (defined $what) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $what) { } 41 2 43
349 # uri escape if it contains invalid XML characters
350 41 ▶ if (not XML::Char->valid($what)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("XML::Char"->valid($what)) 2 39 41
351 $what = join q(), map {
352 2 ▶ (/[[:^print:]]/ or q(%) eq $_) ? uri_escape $_ : $_
Branch: 1 branch
BranchTrueFalseTotal
/[[:^print:]]/ || '%' eq $_ ? : 10 0 10
Condition33%1/3
/[[:^print:]]/ or "%" eq $_
l!l&&r!l&&!r
1000
MC/DC0%0/2
/[[:^print:]]/ or "%" eq $_
/[[:^print:]]/ "%" eq $_
Truth table33%1/3
A or B
A= /[[:^print:]]/B= "%" eq $_
ABresult
1X1
011
000
353 } split //, $what;
354 2 $where->setAttribute('type' => 'uriEscape');
355 }
356 41 $where->addChild( $self->_xml->createTextNode( $what ) );
357 }
358 else {
359 # no better way to distinguish between empty string and undef - see http://rt.cpan.org/Public/Bug/Display.html?id=51442
360 2 $where->setAttribute('type' => 'undef');
361 }
362
363 }
364 #
365 else {
366 0 die 'unknown reference - '.$what;
367 }
368  
369 # cleanup at the end
370 84 ▶ if ($indent == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent == 0) 29 55 84
371 29 $self->{'_href_mapping'} = {};
372 29 $self->{'_cur_xpath_steps'} = [];
373 }
374  
375 # in safe_mode decode back the xml string and compare the data structures
376 84 ▶ if ($safe_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($safe_mode) 1 83 84
377 1 my $xml_string = $where->toString;
378 1 my $what_decoded = eval { $self->decode($xml_string) };
379
380 1 ▶ die 'encoding failed '.$@.' of '.eval('use Data::Dumper; Dumper([$what, $xml_string, $what_decoded])').' failed'
Branch: 1 branch
BranchTrueFalseTotal
unless eq_deeply($what, $what_decoded) 0 1 1
381 if not eq_deeply($what, $what_decoded);
382
383 # set back the safe mode after all was encoded
384 1 $self->safe_mode($safe_mode);
385 }
386  
387 84 return $where;
388 }
389  
390 137 ▶sub _xpath_steps_string {
Subroutine _xpath_steps_string: called CC=2 SCAR=7.0
391 137 my $self = shift;
392 137 ▶ my $path_array = shift || $self->{'_cur_xpath_steps'};
Condition66%2/3
shift() || $self->{'_cur_xpath_steps'}
l!l&&r!l&&!r
201170
MC/DC0%0/2
shift() || $self->{'_cur_xpath_steps'}
shift() $self->{'_cur_xpath_steps'}
Truth table66%2/3
A || B
A= shift()B= $self->{'_cur_xpath_steps'}
ABresult
1X1
011
000
393 137 return join(',',@{$path_array});
394 }
395  
396 23 ▶sub _make_relative_xpath {
Subroutine _make_relative_xpath: called CC=7 SCAR=19.5
397 23 my $self = shift;
398 23 my $orig_path = shift;
399 23 my $cur_path = shift;
400
401 # find how many elements (from beginning) the paths are sharing
402 23 my $common_root_index = 0;
403 23 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($common_root_index < @$orig_path and $orig_path->[$common_root_index] == $cur_path->[$common_root_index]) 34 23 57
Condition100%3/3
$common_root_index < @$orig_path and $orig_path->[$common_root_index] == $cur_path->[$common_root_index]
!ll&&!rl&&r
61734
MC/DC100%2/2
$common_root_index < @$orig_path and $orig_path->[$common_root_index] == $cur_path->[$common_root_index]
$common_root_index < @$orig_path $orig_path->[$common_root_index] == $cur_path->[$common_root_index]
Truth table100%3/3
A and B
A= $common_root_index < @$orig_pathB= $orig_path->[$common_root_index] == $cur_path->[$common_root_index]
ABresult
0X0
100
111
404 ($common_root_index < @$orig_path)
405 and ($orig_path->[$common_root_index] == $cur_path->[$common_root_index])
406 ) {
407 34 $common_root_index++;
408 }
409
410 # add '..' to move up the element hierarchy until the common element
411 23 my @rel_path = ();
412 23 my $i = $common_root_index+1;
413 23 ▶ while ($i < scalar @$cur_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$cur_path) 31 23 54
414 31 push @rel_path, '..';
415 31 $i++;
416 }
417
418 # add the original element path steps
419 23 $i = $common_root_index;
420 23 ▶ while ($i < scalar @$orig_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$orig_path) 37 23 60
421 37 push @rel_path, $orig_path->[$i];
422 37 $i++;
423 }
424
425 # in case of self referencing the element index is needed
426 23 ▶ if ($i == $common_root_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $common_root_index) 6 17 23
427 6 push @rel_path, '..', $orig_path->[-1];
428 }
429
430 # return relative xpath
431 23 ▶ return join('/', map { $_ eq '..' ? $_ : '*['.$_.']' } @rel_path);
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '..' ? : 37 43 80
432 }
433  
434 =head2 decode($xmlstring)
435  
436 Takes C<$xmlstring> and converts to data structure.
437  
438 =cut
439  
440 121 ▶sub decode {
Subroutine decode: called CC=25 SCAR=32.3
Pod documented
441 121 my $self = shift;
442 121 my $xml = shift;
443 121 ▶ my $pos = shift || 1;
Condition100%2/2
shift() || 1
l!l
3982
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
444  
445 # in safe_mode "encode+decode" the decoded data for comparing
446 121 ▶ if ($self->safe_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->safe_mode) 1 120 121
447 1 $self->safe_mode(0);
448 1 my $data = $self->decode($xml, $pos);
449 1 my $data_redecoded = eval { $self->decode(
450 $self->encode($data)->toString,
451 $pos,
452 )};
453 1 ▶ die 'redecoding failed "'.$@.'" of '.eval('use Data::Dumper; Dumper([$xml, $data, $data_redecoded])').' failed'
Branch: 1 branch
BranchTrueFalseTotal
unless eq_deeply($data, $data_redecoded) 0 1 1
454 if not eq_deeply($data, $data_redecoded);
455 1 $self->safe_mode(1);
456 1 return $data;
457 }
458  
459 120 ▶ if (not $self->{'_cur_xpath_steps'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_cur_xpath_steps"}) 2 118 120
460 2 local $self->{'_href_mapping'} = {};
461 2 local $self->{'_cur_xpath_steps'} = [];
462 }
463  
464 120 my $value;
465
466 120 ▶ if (not blessed $xml) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($xml)) 27 93 120
467 27 my $parser = XML::LibXML->new();
468 27 my $doc = $parser->parse_string($xml);
469 27 my $root_element = $doc->documentElement();
470
471 27 return $self->decode($root_element);
472 }
473
474 93 ▶ if ($xml->nodeName eq 'HASH') {
Branch: 4 branches
BranchTrueFalseTotal
if ($xml->nodeName eq 'HASH') { } 26 67 93
elsif ($xml->nodeName eq 'ARRAY') { } 16 51 67
elsif ($xml->nodeName eq 'REF') { } 5 46 51
elsif ($xml->nodeName eq 'VALUE') { } 46 0 46
475 26 ▶ if (my $xpath_path = $xml->getAttribute('href')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $xpath_path = $xml->getAttribute("href")) 10 16 26
476 10 my $href_key = $self->_href_key($xpath_path);
477 10 ▶ return $self->{'_href_mapping'}->{$href_key} || die 'invalid reference - '.$href_key.' ('.$xml->toString.')';
Condition50%1/2
$self->{'_href_mapping'}{$href_key} || die('invalid reference - ' . $href_key . ' (' . $xml->toString . ')')
l!l
100
MC/DC0%0/1
$self->{'_href_mapping'}{$href_key} || die('invalid reference - ' . $href_key . ' (' . $xml->toString . ')')
$self->{'_href_mapping'}{$href_key}
Truth table50%1/2
A
A= $self->{'_href_mapping'}{$href_key}
Aresult
11
00
478 }
479
480 16 push @{$self->{'_cur_xpath_steps'}}, $pos;
481
482 16 my %data;
483 16 $self->{'_href_mapping'}->{$self->_xpath_steps_string()} = \%data;
484 my @keys =
485 25 grep { $_->nodeName eq 'KEY' }
486 16 grep { $_->nodeType eq XML_ELEMENT_NODE }
487 $xml->childNodes()
488 ;
489 16 my $key_pos = 1;
490 16 foreach my $key (@keys) {
491 25 push @{$self->{'_cur_xpath_steps'}}, $key_pos;
492 25 my $key_name = $key->getAttribute('name');
493 25 my $key_value = $self->decode(grep { $_->nodeType eq XML_ELEMENT_NODE } $key->childNodes()); # is always only one
494 25 $data{$key_name} = $key_value;
495 25 pop @{$self->{'_cur_xpath_steps'}};
496 25 $key_pos++;
497 }
498 16 pop @{$self->{'_cur_xpath_steps'}};
499 16 return \%data;
500 }
501 elsif ($xml->nodeName eq 'ARRAY') {
502 16 ▶ if (my $xpath_path = $xml->getAttribute('href')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $xpath_path = $xml->getAttribute("href")) 2 14 16
503 2 my $href_key = $self->_href_key($xpath_path);
504
505 2 ▶ return $self->{'_href_mapping'}->{$href_key} || die 'invalid reference - '.$href_key.' ('.$xml->toString.')';
Condition50%1/2
$self->{'_href_mapping'}{$href_key} || die('invalid reference - ' . $href_key . ' (' . $xml->toString . ')')
l!l
20
MC/DC0%0/1
$self->{'_href_mapping'}{$href_key} || die('invalid reference - ' . $href_key . ' (' . $xml->toString . ')')
$self->{'_href_mapping'}{$href_key}
Truth table50%1/2
A
A= $self->{'_href_mapping'}{$href_key}
Aresult
11
00
506 }
507  
508 14 push @{$self->{'_cur_xpath_steps'}}, $pos;
509  
510 14 my @data;
511 14 $self->{'_href_mapping'}->{$self->_xpath_steps_string()} = \@data;
512
513 14 my $array_element_pos = 1;
514 14 @data = map { $self->decode($_, $array_element_pos++) } grep { $_->nodeType eq XML_ELEMENT_NODE } $xml->childNodes();
515 14 pop @{$self->{'_cur_xpath_steps'}};
516 14 return \@data;
517 }
518 elsif ($xml->nodeName eq 'REF') {
519 5 ▶ if (my $xpath_path = $xml->getAttribute('href')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $xpath_path = $xml->getAttribute("href")) 1 4 5
520 1 my $href_key = $self->_href_key($xpath_path);
521 1 ▶ return $self->{'_href_mapping'}->{$href_key} || die 'invalid reference - '.$href_key.' ('.$xml->toString.')';
Condition50%1/2
$self->{'_href_mapping'}{$href_key} || die('invalid reference - ' . $href_key . ' (' . $xml->toString . ')')
l!l
10
MC/DC0%0/1
$self->{'_href_mapping'}{$href_key} || die('invalid reference - ' . $href_key . ' (' . $xml->toString . ')')
$self->{'_href_mapping'}{$href_key}
Truth table50%1/2
A
A= $self->{'_href_mapping'}{$href_key}
Aresult
11
00
522 }
523  
524 4 push @{$self->{'_cur_xpath_steps'}}, $pos;
525  
526 4 my $data;
527 4 $self->{'_href_mapping'}->{$self->_xpath_steps_string()} = \$data;
528
529 4 ($data) = map { $self->decode($_) } grep { $_->nodeType eq XML_ELEMENT_NODE } $xml->childNodes();
530  
531 4 pop @{$self->{'_cur_xpath_steps'}};
532 4 return \$data;
533 }
534 elsif ($xml->nodeName eq 'VALUE') {
535 46 ▶ if (my $xpath_path = $xml->getAttribute('href')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $xpath_path = $xml->getAttribute("href")) 4 42 46
536 4 my $href_key = $self->_href_key($xpath_path);
537 4 ▶ return $self->{'_href_mapping'}->{$href_key} || die 'invalid reference - '.$href_key.' ('.$xml->toString.')';
Condition50%1/2
$self->{'_href_mapping'}{$href_key} || die('invalid reference - ' . $href_key . ' (' . $xml->toString . ')')
l!l
40
MC/DC0%0/1
$self->{'_href_mapping'}{$href_key} || die('invalid reference - ' . $href_key . ' (' . $xml->toString . ')')
$self->{'_href_mapping'}{$href_key}
Truth table50%1/2
A
A= $self->{'_href_mapping'}{$href_key}
Aresult
11
00
538 }
539  
540 42 push @{$self->{'_cur_xpath_steps'}}, $pos;
541 42 my $value;
542 42 $self->{'_href_mapping'}->{$self->_xpath_steps_string()} = \$value;
543 42 pop @{$self->{'_cur_xpath_steps'}};
544
545 42 ▶ my $type = $xml->getAttribute('type') // '';
Condition100%2/2
$xml->getAttribute("type") // ""
l!l
438
MC/DC100%1/1
$xml->getAttribute("type") // ""
$xml->getAttribute("type")
Truth table100%2/2
A
A= $xml->getAttribute("type")
Aresult
11
00
546 42 ▶ my $subtype = $xml->getAttribute('subtype') // '';
Condition100%2/2
$xml->getAttribute("subtype") // ""
l!l
636
MC/DC100%1/1
$xml->getAttribute("subtype") // ""
$xml->getAttribute("subtype")
Truth table100%2/2
A
A= $xml->getAttribute("subtype")
Aresult
11
00
547 42 ▶ if ($type eq 'undef')
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'undef') { } 2 40 42
elsif ($type eq 'base64') { } 0 40 40
elsif ($type eq 'uriEscape') { } 2 38 40
548 2 { $value = undef; }
549 elsif ($type eq 'base64')
550 0 { $value = decode_base64($xml->textContent) } # left for backwards compatibility, will be removed one day...
551 elsif ($type eq 'uriEscape')
552 2 { $value = uri_unescape $xml->textContent; }
553 else
554 38 { $value = $xml->textContent }
555 42 ▶ return \$value
Branch: 1 branch
BranchTrueFalseTotal
if $subtype eq "ref" 6 36 42
556 if ($subtype eq 'ref');
557 36 return $value;
558 }
559 else {
560 0 die 'invalid (unknown) element "'.$xml->toString.'"'
561 }
562
563 }
564  
565 20 ▶sub _href_key {
Subroutine _href_key: called CC=4 SCAR=13.9
566 20 my $self = shift;
567 20 my $xpath_steps_string = shift;
568
569 20 my @path = @{$self->{'_cur_xpath_steps'}};
570 my @xpath_steps =
571 20 ▶ map { $_ =~ m/^\*\[(\d+)\]$/xms ? $1 : $_ }
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^\*\[(\d+)\]$/msx ? : 35 32 67
572 split('/', $xpath_steps_string)
573 ;
574
575 20 my $i = 0;
576 20 ▶ while ($i < @xpath_steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @xpath_steps) 67 20 87
577 67 ▶ if ($xpath_steps[$i] eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($xpath_steps[$i] eq '..') { } 32 35 67
578 32 pop(@path);
579 }
580 else {
581 35 push(@path, $xpath_steps[$i]);
582 }
583 67 $i++;
584 }
585 20 return $self->_xpath_steps_string(\@path)
586 }
587  
588 2 1;
589  
590  
591 __END__
↑ Summary