lib/XML/LibXML/Simple.pm

filter: Statementstmt 93.3% 239 / 256 Branchbran 79.7% 161 / 202 Conditioncond 67.8% 78 / 115 MC/DCmcdc 43.2% 29 / 67 Subroutinesub 91.6% 22 / 24 Podpod 28.5% 2 / 7 totaltotal 79.1% 531 / 671 CC 152 SCAR 40.2
CC
152
Cov
83%
collapse
132.8
array_to_hash
34.5
_init
23.1
CRAP 55.5
↑ Summary
1 # Copyrights 2008-2020 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution XML-LibXML-Simple. Meta-POD processed
6 # with OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package XML::LibXML::Simple;
10 2 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 $VERSION = '1.01';
12  
13 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 our @EXPORT = qw(XMLin);
19 2 our @EXPORT_OK = qw(xml_in);
20  
21 2 ▶use XML::LibXML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use File::Basename qw/fileparse/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 ▶use Data::Dumper; #to be removed
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29  
30 2 my %known_opts = map +($_ => 1),
31 qw(keyattr keeproot forcecontent contentkey noattr searchpath
32 forcearray grouptags nsexpand normalisespace normalizespace
33 valueattr nsstrip parser parseropts hooknodes suppressempty);
34  
35 2 my @default_attributes = qw(name key id);
36 2 my $default_content_key = 'content';
37  
38 #-------------
39  
40 sub new(@)
41 84 ▶{ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
42 84 my $self = bless {}, $class;
43 84 my $opts = $self->{opts} = $self->_take_opts(@_);
44  
45 # parser object cannot be reused
46 !defined $opts->{parser}
47 84 ▶ or error __x"parser option for XMLin only";
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{"parser"} and "__x"->error("parser option for XMLin only") 0 84 84
48  
49 84 $self;
50 }
51  
52 #-------------
53  
54 sub XMLin
55 84 ▶{ my $self = @_ > 1 && blessed $_[0] && $_[0]->isa(__PACKAGE__) ? shift
Subroutine XMLin: called CC=9 SCAR=23.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) ? : 0 84 84
Condition83%5/6
@_ > 1 and builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
8310
@_ > 1 and builtin::blessed($_[0])
!ll&&!rl&&r
15681
MC/DC0%0/3
@_ > 1 and builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
@_ > 1 builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__)
Truth table75%3/4
A and B and C
A= @_ > 1B= builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)
ABCresult
0XX0
10X0
1100
1111
56 : __PACKAGE__->new;
57 84 my $target = shift;
58  
59 84 my $this = $self->_take_opts(@_);
60 82 my $opts = $self->_init($self->{opts}, $this);
61  
62 82 ▶ my $xml = $self->_get_xml($target, $opts)
Branch: 1 branch
BranchTrueFalseTotal
my $xml = $self->_get_xml($target, $opts) or return 79 0 79
63 or return;
64  
65 79 ▶ if(my $cb = $opts->{hooknodes})
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $opts->{"hooknodes"}) 0 79 79
66 0 { $self->{XCS_hooks} = $cb->($self, $xml);
67 }
68  
69 79 my $top = $self->collapse($xml, $opts);
70 79 ▶ if($opts->{keeproot})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"keeproot"}) 1 78 79
71 { my $subtop
72 1 ▶ = $opts->{forcearray_always} && ref $top ne 'ARRAY' ? [$top] : $top;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'forcearray_always'} && ref $top ne 'ARRAY' ? : 1 0 1
Condition33%1/3
$opts->{"forcearray_always"} and ref $top ne "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
$opts->{"forcearray_always"} and ref $top ne "ARRAY"
$opts->{"forcearray_always"} ref $top ne "ARRAY"
Truth table33%1/3
A and B
A= $opts->{"forcearray_always"}B= ref $top ne "ARRAY"
ABresult
0X0
100
111
73 1 $top = +{ $xml->localName => $subtop };
74 }
75  
76 79 $top;
77 }
78 2 *xml_in = \&XMLin;
79  
80 sub _get_xml($$)
81 82 ▶{ my ($self, $source, $opts) = @_;
Subroutine _get_xml: called CC=11 SCAR=25.7
82  
83 82 ▶ $source = $self->default_data_source($opts)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 1 81 82
84 unless defined $source;
85  
86 82 ▶ $source = \*STDIN
Branch: 1 branch
BranchTrueFalseTotal
if $source eq "-" 1 81 82
87 if $source eq '-';
88  
89 my $parser = $opts->{parser}
90 82 ▶ || $self->_create_parser($opts->{parseropts});
Condition33%1/3
$opts->{'parser'} || $self->_create_parser($opts->{'parseropts'})
l!l&&r!l&&!r
0820
MC/DC0%0/2
$opts->{'parser'} || $self->_create_parser($opts->{'parseropts'})
$opts->{'parser'} $self->_create_parser($opts->{'parseropts'})
Truth table33%1/3
A || B
A= $opts->{'parser'}B= $self->_create_parser($opts->{'parseropts'})
ABresult
1X1
011
000
91  
92 my $xml
93 = blessed $source &&
94 ( $source->isa('XML::LibXML::Document')
95 || $source->isa('XML::LibXML::Element' )) ? $source
96 : ref $source eq 'SCALAR' ? $parser->parse_string($$source)
97 : ref $source ? $parser->parse_fh($source)
98 : $source =~ m{^\s*\<.*?\>\s*$}s ? $parser->parse_string($source)
99 : $parser->parse_file
100 82 ▶ ($self->find_xml_file($source, @{$opts->{searchpath}}));
Branch: 4 branches
BranchTrueFalseTotal
builtin::blessed($source) && ($source->isa('XML::LibXML::Document') || $source->isa('XML::LibXML::Element')) ? : 0 82 82
ref $source eq 'SCALAR' ? : 0 82 82
ref $source ? : 2 80 82
$source =~ /^\s*\<.*?\>\s*$/s ? : 73 7 80
Condition50%3/6
builtin::blessed($source) and $source->isa("XML::LibXML::Document") || $source->isa("XML::LibXML::Element")
!ll&&!rl&&r
8110
$source->isa("XML::LibXML::Document") or $source->isa("XML::LibXML::Element")
l!l&&r!l&&!r
001
MC/DC0%0/3
builtin::blessed($source) and $source->isa("XML::LibXML::Document") || $source->isa("XML::LibXML::Element")
builtin::blessed($source) $source->isa("XML::LibXML::Document") $source->isa("XML::LibXML::Element")
Truth table50%2/4
A and B or C
A= builtin::blessed($source)B= $source->isa("XML::LibXML::Document")C= $source->isa("XML::LibXML::Element")
ABCresult
0XX0
1000
11X1
1011
101  
102 79 ▶ $xml = $xml->documentElement
Branch: 1 branch
BranchTrueFalseTotal
if $xml->isa("XML::LibXML::Document") 79 0 79
103 if $xml->isa('XML::LibXML::Document');
104  
105 79 $xml;
106 }
107  
108 sub _create_parser(@)
109 82 ▶{ my $self = shift;
Subroutine _create_parser: called CC=3 SCAR=13.5
110 0 ▶ my @popt = @_ != 1 ? @_ : ref $_[0] eq 'HASH' ? %{$_[0]} : @{$_[0]};
Branch: 2 branches
BranchTrueFalseTotal
@_ != 1 ? : 0 82 82
ref $_[0] eq 'HASH' ? : 82 0 82
111  
112 XML::LibXML->new
113 ( line_numbers => 1
114 , no_network => 1
115 , expand_xinclude => 0
116 , expand_entities => 1
117 , load_ext_dtd => 0
118 , ext_ent_handler =>
119 0 ▶ sub { alert __x"parsing external entities disabled"; '' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 , @popt
121 82 );
122 }
123  
124 sub _take_opts(@)
125 168 ▶{ my $self = shift;
Subroutine _take_opts: called CC=4 SCAR=13.9
126
127 168 my %opts;
128 168 ▶ @_ % 2==0
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 or die "ERROR: odd number of options.\n" 167 1 168
129 or die "ERROR: odd number of options.\n";
130  
131 167 ▶ while(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 119 166 285
132 119 { my ($key, $val) = (shift, shift);
133 119 my $lkey = lc $key;
134 119 $lkey =~ s/_//g;
135 119 ▶ $known_opts{$lkey} or croak "Unrecognised option: $key";
Branch: 1 branch
BranchTrueFalseTotal
$known_opts{$lkey} or croak("Unrecognised option: $key") 118 1 119
136 118 $opts{$lkey} = $val;
137 }
138  
139 166 \%opts;
140 }
141  
142 # Returns the name of the XML file to parse if no filename or XML string
143 # was provided explictly.
144  
145 sub default_data_source($)
146 1 ▶{ my ($self, $opts) = @_;
Subroutine default_data_source: called CC=2 SCAR=7.0
Pod undocumented
147  
148 1 my ($basename, $script_dir, $ext) = fileparse $0, qr[\.[^\.]+];
149  
150 # Add script directory to searchpath
151 1 ▶ unshift @{$opts->{searchpath}}, $script_dir
Branch: 1 branch
BranchTrueFalseTotal
if $script_dir 1 0 1
152 if $script_dir;
153  
154 1 "$basename.xml";
155 }
156  
157 sub _init($$)
158 82 ▶{ my ($self, $global, $this) = @_;
Subroutine _init: called CC=23 SCAR=31.4
159 82 my %opt = (%$global, %$this);
160  
161 82 ▶ if(defined $opt{contentkey})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{'contentkey'}) { } 48 34 82
162 48 { $opt{collapseagain} = $opt{contentkey} =~ s/^\-// }
163 34 else { $opt{contentkey} = $default_content_key }
164  
165 82 ▶ $opt{normalisespace} ||= $opt{normalizespace} || 0;
Condition100%5/5
$opt{"normalisespace"} ||= $opt{"normalizespace"} || 0
l!l&&r!l&&!r
4177
$opt{'normalizespace'} || 0
l!l
177
MC/DC100%2/2
$opt{"normalisespace"} ||= $opt{"normalizespace"} || 0
$opt{"normalisespace"} $opt{'normalizespace'}
Truth table100%3/3
A ||= B
A= $opt{"normalisespace"}B= $opt{'normalizespace'}
ABresult
1X1
011
000
166  
167 82 ▶ $opt{searchpath} ||= [];
Condition100%2/2
$opt{"searchpath"} ||= []
l!l
379
MC/DC100%1/1
$opt{"searchpath"} ||= []
$opt{"searchpath"}
Truth table100%2/2
A
A= $opt{"searchpath"}
Aresult
11
00
168 ref $opt{searchpath} eq 'ARRAY'
169 82 ▶ or $opt{searchpath} = [ $opt{searchpath} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $opt{"searchpath"} eq "ARRAY" or $opt{"searchpath"} = [$opt{"searchpath"}] 80 2 82
170  
171 82 ▶ my $fa = delete $opt{forcearray} || 0;
Condition100%2/2
delete $opt{'forcearray'} || 0
l!l
1369
MC/DC100%1/1
delete $opt{'forcearray'} || 0
delete $opt{'forcearray'}
Truth table100%2/2
A
A= delete $opt{'forcearray'}
Aresult
11
00
172 82 my (@fa_regex, %fa_elem);
173 82 ▶ if(ref $fa)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fa) { } 6 76 82
174 6 ▶ { foreach (ref $fa eq 'ARRAY' ? @$fa : $fa)
Branch: 1 branch
BranchTrueFalseTotal
ref $fa eq 'ARRAY' ? : 5 1 6
175 8 ▶ { if(ref $_ eq 'Regexp') { push @fa_regex, $_ }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'Regexp') { } 3 5 8
176 5 else { $fa_elem{$_} = 1 }
177 }
178 }
179 76 else { $opt{forcearray_always} = $fa }
180 82 $opt{forcearray_regex} = \@fa_regex;
181 82 $opt{forcearray_elem} = \%fa_elem;
182  
183 # Special cleanup for {keyattr} which could be arrayref or hashref,
184 # which behave differently.
185  
186 82 ▶ my $ka = $opt{keyattr} || \@default_attributes;
Condition100%2/2
$opt{'keyattr'} || \@default_attributes
l!l
3151
MC/DC100%1/1
$opt{'keyattr'} || \@default_attributes
$opt{'keyattr'}
Truth table100%2/2
A
A= $opt{'keyattr'}
Aresult
11
00
187 82 ▶ $ka = [ $ka ] unless ref $ka;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ka 3 79 82
188
189 82 ▶ if(ref $ka eq 'ARRAY')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ka eq 'ARRAY') { } 62 20 82
elsif (ref $ka eq 'HASH') { } 20 0 20
190 62 ▶ { if(@$ka) { $opt{keyattr} = $ka }
Branch: 1 branch
BranchTrueFalseTotal
if (@$ka) { } 60 2 62
191 2 else { delete $opt{keyattr} }
192 }
193 elsif(ref $ka eq 'HASH')
194 { # Convert keyattr => { elem => '+attr' }
195 # to keyattr => { elem => [ 'attr', '+' ] }
196 20 my %at;
197 20 ▶ while(my($k,$v) = each %$ka)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$ka) 23 20 43
198 23 { $v =~ /^(\+|-)?(.*)$/;
199 23 ▶ $at{$k} = [ $2, $1 || '' ];
Condition100%2/2
$1 || ''
l!l
617
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
200 }
201 20 $opt{keyattr} = \%at;
202 }
203  
204 # Special cleanup for {valueattr} which could be arrayref or hashref
205  
206 82 ▶ my $va = delete $opt{valueattr} || {};
Condition100%2/2
delete $opt{'valueattr'} || {}
l!l
478
MC/DC100%1/1
delete $opt{'valueattr'} || {}
delete $opt{'valueattr'}
Truth table100%2/2
A
A= delete $opt{'valueattr'}
Aresult
11
00
207 82 ▶ $va = +{ map +($_ => 1), @$va } if ref $va eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $va eq "ARRAY" 2 80 82
208 82 $opt{valueattrlist} = $va;
209  
210 # make sure there's nothing weird in {grouptags}
211  
212 82 ▶ !$opt{grouptags} || ref $opt{grouptags} eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
not $opt{"grouptags"} or ref $opt{"grouptags"} eq "HASH" or croak("Illegal value for 'GroupTags' option -expected a hashref") 82 0 82
Condition66%2/3
not $opt{"grouptags"} or ref $opt{"grouptags"} eq "HASH"
l!l&&r!l&&!r
7570
MC/DC0%0/2
not $opt{"grouptags"} or ref $opt{"grouptags"} eq "HASH"
not $opt{"grouptags"} ref $opt{"grouptags"} eq "HASH"
Truth table66%2/3
not(A) or B
A= not $opt{"grouptags"}B= ref $opt{"grouptags"} eq "HASH"
ABresult
1X1
011
000
213 or croak "Illegal value for 'GroupTags' option -expected a hashref";
214  
215 82 ▶ $opt{parseropts} ||= {};
Condition50%1/2
$opt{"parseropts"} ||= {}
l!l
082
MC/DC0%0/1
$opt{"parseropts"} ||= {}
$opt{"parseropts"}
Truth table50%1/2
A
A= $opt{"parseropts"}
Aresult
11
00
216  
217 82 \%opt;
218 }
219  
220 sub find_xml_file($@)
221 7 ▶{ my ($self, $file) = (shift, shift);
Subroutine find_xml_file: called CC=6 SCAR=17.9
Pod undocumented
222 7 ▶ my @search_path = @_ ? @_ : '.';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 4 3 7
223  
224 7 my ($filename, $filedir) = fileparse $file;
225  
226 7 ▶ if($filename eq $file)
Branch: 2 branches
BranchTrueFalseTotal
if ($filename eq $file) { } 4 3 7
elsif (-e $file) { } 2 1 3
227 4 { foreach my $path (@search_path)
228 6 { my $fullpath = File::Spec->catfile($path, $file);
229 6 ▶ return $fullpath if -e $fullpath;
Branch: 1 branch
BranchTrueFalseTotal
if -e $fullpath 2 4 6
230 }
231 }
232 elsif(-e $file) # Ignore searchpath if dir component
233 2 { return $file;
234 }
235  
236 3 local $" = ':';
237 3 die "data source $file not found in @search_path\n";
238 }
239  
240 sub _add_kv($$$$)
241 540 ▶{ my ($d, $k, $v, $opts) = @_;
Subroutine _add_kv: called CC=9 SCAR=22.0
242  
243 540 ▶ if(defined $d->{$k})
Branch: 4 branches
BranchTrueFalseTotal
if (defined $d->{$k}) { } 97 443 540
elsif (ref $v eq 'ARRAY') { } 2 441 443
elsif (ref $v eq 'HASH' and $k ne $opts->{'contentkey'} and $opts->{'forcearray_always'}) { } 24 417 441
elsif ($opts->{'forcearray_elem'}{$k} or grep(($k =~ /$_/), @{$$opts{'forcearray_regex'};})) { } 12 405 417
244 { # Combine duplicate attributes into arrayref if required
245 97 ▶ if(ref $d->{$k} eq 'ARRAY') { push @{$d->{$k}}, $v }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $d->{$k} eq 'ARRAY') { } 46 51 97
246 51 else { $d->{$k} = [ $d->{$k}, $v ] } }
247 2 elsif(ref $v eq 'ARRAY') { push @{$d->{$k}}, $v }
248 ▶ elsif(ref $v eq 'HASH'
Condition83%5/6
ref $v eq "HASH" and $k ne $opts->{"contentkey"} and $opts->{"forcearray_always"}
!ll&&!rl&&r
3427524
ref $v eq "HASH" and $k ne $opts->{"contentkey"}
!ll&&!rl&&r
342099
MC/DC66%2/3
ref $v eq "HASH" and $k ne $opts->{"contentkey"} and $opts->{"forcearray_always"}
ref $v eq "HASH" $k ne $opts->{"contentkey"} $opts->{"forcearray_always"}
Truth table75%3/4
A and B and C
A= ref $v eq "HASH"B= $k ne $opts->{"contentkey"}C= $opts->{"forcearray_always"}
ABCresult
0XX0
10X0
1100
1111
249 && $k ne $opts->{contentkey}
250 24 && $opts->{forcearray_always}) { push @{$d->{$k}}, $v }
251 ▶ elsif($opts->{forcearray_elem}{$k}
Condition100%3/3
$opts->{"forcearray_elem"}{$k} or grep(($k =~ /$_/), @{$$opts{"forcearray_regex"};})
l!l&&r!l&&!r
57405
MC/DC100%2/2
$opts->{"forcearray_elem"}{$k} or grep(($k =~ /$_/), @{$$opts{"forcearray_regex"};})
$opts->{"forcearray_elem"}{$k} grep(($k =~ /$_/), @{$$opts{"forcearray_regex"};})
Truth table100%3/3
A or B
A= $opts->{"forcearray_elem"}{$k}B= grep(($k =~ /$_/), @{$$opts{"forcearray_regex"};})
ABresult
1X1
011
000
252 412 || grep $k =~ $_, @{$opts->{forcearray_regex}}
253 12 ) { push @{$d->{$k}}, $v }
254 405 else { $d->{$k} = $v }
255 540 $d->{$k};
256 }
257  
258 # Takes the parse tree that XML::LibXML::Parser produced from the supplied
259 # XML and recurse through it 'collapsing' unnecessary levels of indirection
260 # (nested arrays etc) to produce a data structure that is easier to work with.
261  
262 sub _expand_name($)
263 0 ▶{ my $node = shift;
Subroutine _expand_name: not called CC=3 SCAR=24.8
264 0 ▶ my $uri = $node->namespaceURI || '';
Condition0%0/2
$node->namespaceURI || ''
l!l
00
MC/DC0%0/1
$node->namespaceURI || ''
$node->namespaceURI
Truth table0%0/2
A
A= $node->namespaceURI
Aresult
11
00
265 0 ▶ (length $uri ? "{$uri}" : '') . $node->localName;
Branch: 1 branch
BranchTrueFalseTotal
length $uri ? : 0 0 0
266 }
267  
268 sub collapse($$)
269 383 ▶{ my ($self, $xml, $opts) = @_;
Subroutine collapse: called CC=57 SCAR=48.9
Pod undocumented
270 383 ▶ $xml->isa('XML::LibXML::Element') or return;
Branch: 1 branch
BranchTrueFalseTotal
$xml->isa("XML::LibXML::Element") or return 383 0 383
271  
272 383 my (%data, $text);
273 383 my $hooks = $self->{XCS_hooks};
274  
275 383 ▶ unless($opts->{noattr})
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"noattr"}) 366 17 383
276 {
277 ATTR:
278 366 foreach my $attr ($xml->attributes)
279 {
280 236 my $value;
281 236 ▶ if($hooks && (my $hook = $hooks->{$attr->unique_key}))
Branch: 1 branch
BranchTrueFalseTotal
if ($hooks and my $hook = $$hooks{$attr->unique_key}) { } 0 236 236
Condition33%1/3
$hooks and my $hook = $$hooks{$attr->unique_key}
!ll&&!rl&&r
23600
MC/DC0%0/2
$hooks and my $hook = $$hooks{$attr->unique_key}
$hooks my $hook = $$hooks{$attr->unique_key}
Truth table33%1/3
A and B
A= $hooksB= my $hook = $$hooks{$attr->unique_key}
ABresult
0X0
100
111
282 0 { $value = $hook->($attr);
283 0 ▶ defined $value or next ATTR;
Branch: 1 branch
BranchTrueFalseTotal
defined $value or next ATTR 0 0 0
284 }
285 else
286 236 { $value = $attr->value;
287 }
288  
289 $value = $self->normalise_space($value)
290 236 ▶ if !ref $value && $opts->{normalisespace}==2;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $value and $opts->{"normalisespace"} == 2 2 234 236
Condition66%2/3
not ref $value and $opts->{"normalisespace"} == 2
!ll&&!rl&&r
02342
MC/DC50%1/2
not ref $value and $opts->{"normalisespace"} == 2
not ref $value $opts->{"normalisespace"} == 2
Truth table66%2/3
not(A) and B
A= not ref $valueB= $opts->{"normalisespace"} == 2
ABresult
0X0
100
111
291  
292 my $name
293 = !$attr->isa('XML::LibXML::Attr') ? $attr->nodeName
294 : $opts->{nsexpand} ? _expand_name($attr)
295 236 ▶ : $opts->{nsstrip} ? $attr->localName
Branch: 3 branches
BranchTrueFalseTotal
!$attr->isa('XML::LibXML::Attr') ? : 0 236 236
$opts->{'nsexpand'} ? : 0 236 236
$opts->{'nsstrip'} ? : 0 236 236
296 : $attr->nodeName;
297  
298 236 _add_kv \%data, $name => $value, $opts;
299 }
300 }
301 383 my $nr_attrs = keys %data;
302 383 my $nr_elems = 0;
303  
304 CHILD:
305 383 foreach my $child ($xml->childNodes)
306 {
307 841 ▶ if($child->isa('XML::LibXML::Text'))
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa("XML::LibXML::Text")) 537 304 841
308 537 { $text .= $child->data;
309 537 next CHILD;
310 }
311  
312 304 ▶ $child->isa('XML::LibXML::Element')
Branch: 1 branch
BranchTrueFalseTotal
$child->isa("XML::LibXML::Element") or next CHILD 304 0 304
313 or next CHILD;
314  
315 304 $nr_elems++;
316  
317 304 my $v;
318 304 ▶ if($hooks && (my $hook = $hooks->{$child->unique_key}))
Branch: 1 branch
BranchTrueFalseTotal
if ($hooks and my $hook = $$hooks{$child->unique_key}) { } 0 304 304
Condition33%1/3
$hooks and my $hook = $$hooks{$child->unique_key}
!ll&&!rl&&r
30400
MC/DC0%0/2
$hooks and my $hook = $$hooks{$child->unique_key}
$hooks my $hook = $$hooks{$child->unique_key}
Truth table33%1/3
A and B
A= $hooksB= my $hook = $$hooks{$child->unique_key}
ABresult
0X0
100
111
319 0 { $v = $hook->($child) }
320 304 else { $v = $self->collapse($child, $opts) }
321  
322 next CHILD
323 304 ▶ if ! defined $v && $opts->{suppressempty};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $v and $opts->{"suppressempty"} 0 304 304
Condition33%1/3
not defined $v and $opts->{"suppressempty"}
!ll&&!rl&&r
30400
MC/DC0%0/2
not defined $v and $opts->{"suppressempty"}
not defined $v $opts->{"suppressempty"}
Truth table33%1/3
not(A) and B
A= not defined $vB= $opts->{"suppressempty"}
ABresult
0X0
100
111
324  
325 my $name
326 = $opts->{nsexpand} ? _expand_name($child)
327 304 ▶ : $opts->{nsstrip} ? $child->localName
Branch: 2 branches
BranchTrueFalseTotal
$opts->{'nsexpand'} ? : 0 304 304
$opts->{'nsstrip'} ? : 0 304 304
328 : $child->nodeName;
329  
330 304 _add_kv \%data, $name => $v, $opts;
331 }
332  
333 $text = $self->normalise_space($text)
334 383 ▶ if defined $text && $opts->{normalisespace}==2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $text and $opts->{"normalisespace"} == 2 14 369 383
Condition100%3/3
defined $text and $opts->{"normalisespace"} == 2
!ll&&!rl&&r
10426514
MC/DC100%2/2
defined $text and $opts->{"normalisespace"} == 2
defined $text $opts->{"normalisespace"} == 2
Truth table100%3/3
A and B
A= defined $textB= $opts->{"normalisespace"} == 2
ABresult
0X0
100
111
335  
336 383 ▶ return $opts->{forcecontent} ? { $opts->{contentkey} => $text } : $text
Branch: 2 branches
BranchTrueFalseTotal
if $nr_attrs + $nr_elems == 0 and defined $text 136 247 383
$opts->{'forcecontent'} ? : 2 134 136
Condition100%3/3
$nr_attrs + $nr_elems == 0 and defined $text
!ll&&!rl&&r
2452136
MC/DC100%2/2
$nr_attrs + $nr_elems == 0 and defined $text
$nr_attrs + $nr_elems == 0 defined $text
Truth table100%3/3
A and B
A= $nr_attrs + $nr_elems == 0B= defined $text
ABresult
0X0
100
111
337 if $nr_attrs+$nr_elems==0 && defined $text;
338  
339 247 ▶ $data{$opts->{contentkey}} = $text
Branch: 1 branch
BranchTrueFalseTotal
if defined $text and $nr_elems == 0 34 213 247
Condition100%3/3
defined $text and $nr_elems == 0
!ll&&!rl&&r
10410934
MC/DC100%2/2
defined $text and $nr_elems == 0
defined $text $nr_elems == 0
Truth table100%3/3
A and B
A= defined $textB= $nr_elems == 0
ABresult
0X0
100
111
340 if defined $text && $nr_elems==0;
341  
342 # Roll up 'value' attributes (but only if no nested elements)
343  
344 247 ▶ if(keys %data==1)
Branch: 1 branch
BranchTrueFalseTotal
if (keys %data == 1) 101 146 247
345 101 { my ($k) = keys %data;
346 101 ▶ return $data{$k} if $opts->{valueattrlist}{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"valueattrlist"}{$k} 7 94 101
347 }
348  
349 # Turn arrayrefs into hashrefs if key fields present
350  
351 240 ▶ if($opts->{keyattr})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"keyattr"}) 233 7 240
352 233 ▶ { while(my ($key, $val) = each %data)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %data) 459 233 692
353 459 ▶ { $data{$key} = $self->array_to_hash($key, $val, $opts)
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "ARRAY" 84 375 459
354 if ref $val eq 'ARRAY';
355 }
356 }
357  
358 # disintermediate grouped tags
359  
360 240 ▶ if(my $gr = $opts->{grouptags})
Branch: 1 branch
BranchTrueFalseTotal
if (my $gr = $opts->{"grouptags"}) 21 219 240
361 {
362 ELEMENT:
363 21 ▶ while(my ($key, $val) = each %data)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %data) 43 21 64
364 43 ▶ { my $sub = $gr->{$key} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $gr->{$key} or next 8 35 43
365 8 ▶ if(ref $val eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 8 8
366 { next ELEMENT
367 0 ▶ if grep { keys %$_!=1 || !exists $_->{$sub} } @$val;
Branch: 1 branch
BranchTrueFalseTotal
if grep {keys %$_ != 1 or not exists $_->{$sub};} @$val 0 0 0
Condition0%0/3
keys %$_ != 1 || !exists $_->{$sub}
l!l&&r!l&&!r
000
MC/DC0%0/2
keys %$_ != 1 || !exists $_->{$sub}
keys %$_ != 1 !exists $_->{$sub}
Truth table0%0/3
A || not(B)
A= keys %$_ != 1B= !exists $_->{$sub}
ABresult
1X1
011
000
368 0 $data{$key} = { map { %{$_->{$sub}} } @$val };
369 }
370 else
371 8 ▶ { ref $val eq 'HASH' && keys %$val==1 or next;
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq "HASH" and keys %$val == 1 or next 8 0 8
Condition33%1/3
ref $val eq "HASH" and keys %$val == 1
!ll&&!rl&&r
008
MC/DC0%0/2
ref $val eq "HASH" and keys %$val == 1
ref $val eq "HASH" keys %$val == 1
Truth table33%1/3
A and B
A= ref $val eq "HASH"B= keys %$val == 1
ABresult
0X0
100
111
372 8 my ($child_key, $child_val) = %$val;
373 $data{$key} = $child_val
374 8 ▶ if $gr->{$key} eq $child_key;
Branch: 1 branch
BranchTrueFalseTotal
if $gr->{$key} eq $child_key 7 1 8
375 }
376 }
377 }
378  
379 # Fold hashes containing a single anonymous array up into just the array
380 return $data{anon}
381 if keys %data == 1
382 && exists $data{anon}
383 240 ▶ && ref $data{anon} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if keys %data == 1 and exists $data{"anon"} and ref $data{"anon"} eq "ARRAY" 12 228 240
Condition83%5/6
keys %data == 1 and exists $data{"anon"} and ref $data{"anon"} eq "ARRAY"
!ll&&!rl&&r
228012
keys %data == 1 and exists $data{"anon"}
!ll&&!rl&&r
1468212
MC/DC66%2/3
keys %data == 1 and exists $data{"anon"} and ref $data{"anon"} eq "ARRAY"
keys %data == 1 exists $data{"anon"} ref $data{"anon"} eq "ARRAY"
Truth table75%3/4
A and B and C
A= keys %data == 1B= exists $data{"anon"}C= ref $data{"anon"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
384  
385 # Suppress empty elements?
386 228 ▶ if(! keys %data && exists $opts->{suppressempty}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not keys %data and exists $opts->{"suppressempty"}) 0 228 228
Condition66%2/3
not keys %data and exists $opts->{"suppressempty"}
!ll&&!rl&&r
22620
MC/DC0%0/2
not keys %data and exists $opts->{"suppressempty"}
not keys %data exists $opts->{"suppressempty"}
Truth table66%2/3
not(A) and B
A= not keys %dataB= exists $opts->{"suppressempty"}
ABresult
0X0
100
111
387 0 my $sup = $opts->{suppressempty};
388 0 ▶ return +(defined $sup && $sup eq '') ? '' : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $sup && $sup eq '' ? : 0 0 0
Condition0%0/3
defined $sup and $sup eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sup and $sup eq ""
defined $sup $sup eq ""
Truth table0%0/3
A and B
A= defined $supB= $sup eq ""
ABresult
0X0
100
111
389 }
390  
391 # Roll up named elements with named nested 'value' attributes
392 228 ▶ if(my $va = $opts->{valueattrlist})
Branch: 1 branch
BranchTrueFalseTotal
if (my $va = $opts->{"valueattrlist"}) 228 0 228
393 228 ▶ { while(my($key, $val) = each %data)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %data) 458 228 686
394 458 ▶ { $va->{$key} && ref $val eq 'HASH' && keys %$val==1 or next;
Branch: 1 branch
BranchTrueFalseTotal
$va->{$key} and ref $val eq "HASH" and keys %$val == 1 or next 4 454 458
Condition66%4/6
$va->{$key} and ref $val eq "HASH" and keys %$val == 1
!ll&&!rl&&r
45404
$va->{$key} and ref $val eq "HASH"
!ll&&!rl&&r
45404
MC/DC33%1/3
$va->{$key} and ref $val eq "HASH" and keys %$val == 1
$va->{$key} ref $val eq "HASH" keys %$val == 1
Truth table50%2/4
A and B and C
A= $va->{$key}B= ref $val eq "HASH"C= keys %$val == 1
ABCresult
0XX0
10X0
1100
1111
395 4 $data{$key} = $val->{$va->{$key}};
396 }
397 }
398  
399 $nr_elems+$nr_attrs ? \%data
400 : !defined $text ? {}
401 228 ▶ : $opts->{forcecontent} ? { $opts->{contentkey} => $text }
Branch: 3 branches
BranchTrueFalseTotal
$nr_elems + $nr_attrs ? : 226 2 228
!defined($text) ? : 2 0 2
$opts->{'forcecontent'} ? : 0 0 0
402 : $text;
403 }
404  
405 sub normalise_space($)
406 20 ▶{ my $self = shift;
Subroutine normalise_space: called CC=1 SCAR=0.0
Pod undocumented
407 20 local $_ = shift;
408 20 s/^\s+//s;
409 20 s/\s+$//s;
410 20 s/\s\s+/ /sg;
411 20 $_;
412 }
413  
414 # Attempts to 'fold' an array of hashes into an hash of hashes. Returns a
415 # reference to the hash on success or the original array if folding is
416 # not possible. Behaviour is controlled by 'keyattr' option.
417 #
418  
419 sub array_to_hash($$$$)
420 84 ▶{ my ($self, $name, $in, $opts) = @_;
Subroutine array_to_hash: called CC=34 SCAR=35.4
Pod undocumented
421 84 my %out;
422  
423 84 ▶ my $ka = $opts->{keyattr} or return $in;
Branch: 1 branch
BranchTrueFalseTotal
my $ka = $opts->{"keyattr"} or return $in 84 0 84
424  
425 84 ▶ if(ref $ka eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ka eq 'HASH') { } 28 56 84
426 28 ▶ { my $newkey = $ka->{$name} or return $in;
Branch: 1 branch
BranchTrueFalseTotal
my $newkey = $ka->{$name} or return $in 20 8 28
427 20 my ($key, $flag) = @$newkey;
428  
429 20 foreach my $h (@$in)
430 44 ▶ { unless(ref $h eq 'HASH' && defined $h->{$key})
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $h eq "HASH" and defined $h->{$key}) 2 42 44
Condition66%2/3
ref $h eq "HASH" and defined $h->{$key}
!ll&&!rl&&r
0242
MC/DC50%1/2
ref $h eq "HASH" and defined $h->{$key}
ref $h eq "HASH" defined $h->{$key}
Truth table66%2/3
A and B
A= ref $h eq "HASH"B= defined $h->{$key}
ABresult
0X0
100
111
431 2 ▶ { warn "<$name> element has no '$key' key attribute\n" if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 1 1 2
432 2 return $in;
433 }
434  
435 42 my $val = $h->{$key};
436 42 ▶ if(ref $val)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) 2 40 42
437 2 ▶ { warn "<$name> element has non-scalar '$key' key attribute\n" if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 1 1 2
438 2 return $in;
439 }
440  
441 $val = $self->normalise_space($val)
442 40 ▶ if $opts->{normalisespace}==1;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"normalisespace"} == 1 2 38 40
443  
444 warn "<$name> element has non-unique value in '$key' "
445 40 ▶ . "key attribute: $val\n" if $^W && defined $out{$val};
Branch: 1 branch
BranchTrueFalseTotal
if $^W and defined $out{$val} 1 39 40
Condition100%3/3
$^W and defined $out{$val}
!ll&&!rl&&r
3451
MC/DC100%2/2
$^W and defined $out{$val}
$^W defined $out{$val}
Truth table100%3/3
A and B
A= $^WB= defined $out{$val}
ABresult
0X0
100
111
446  
447 40 $out{$val} = { %$h };
448 40 ▶ $out{$val}{"-$key"} = $out{$val}{$key} if $flag eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $flag eq "-" 4 36 40
449 40 ▶ delete $out{$val}{$key} if $flag ne '+';
Branch: 1 branch
BranchTrueFalseTotal
if $flag ne "+" 38 2 40
450 }
451 }
452  
453 else # Arrayref
454 56 { my $default_keys = "@default_attributes" eq "@$ka";
455  
456 ELEMENT:
457 56 foreach my $h (@$in)
458 78 ▶ { ref $h eq 'HASH' or return $in;
Branch: 1 branch
BranchTrueFalseTotal
ref $h eq "HASH" or return $in 50 28 78
459  
460 50 foreach my $key (@$ka)
461 81 { my $val = $h->{$key};
462 81 ▶ defined $val or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $val or next 42 39 81
463  
464 42 ▶ if(ref $val)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) 2 40 42
465 2 ▶ { warn "<$name> element has non-scalar '$key' key attribute"
Branch: 1 branch
BranchTrueFalseTotal
if $^W and not $default_keys 1 1 2
Condition66%2/3
$^W and not $default_keys
!ll&&!rl&&r
011
MC/DC50%1/2
$^W and not $default_keys
$^W not $default_keys
Truth table66%2/3
A and not(B)
A= $^WB= not $default_keys
ABresult
0X0
100
111
466 if $^W && ! $default_keys;
467 2 return $in;
468 }
469  
470 $val = $self->normalise_space($val)
471 40 ▶ if $opts->{normalisespace} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"normalisespace"} == 1 2 38 40
472  
473 warn "<$name> element has non-unique value in '$key' "
474 40 ▶ . "key attribute: $val" if $^W && $out{$val};
Branch: 1 branch
BranchTrueFalseTotal
if $^W and $out{$val} 1 39 40
Condition100%3/3
$^W and $out{$val}
!ll&&!rl&&r
3631
MC/DC100%2/2
$^W and $out{$val}
$^W $out{$val}
Truth table100%3/3
A and B
A= $^WB= $out{$val}
ABresult
0X0
100
111
475  
476 40 $out{$val} = { %$h };
477 40 delete $out{$val}{$key};
478 40 next ELEMENT;
479 }
480 8 return $in; # No keyfield matched
481 }
482 }
483  
484 $opts->{collapseagain}
485 34 ▶ or return \%out;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{"collapseagain"} or return \%out 27 7 34
486  
487 # avoid over-complicated structures like
488 # dir => { libexecdir => { content => '$exec_prefix/libexec' },
489 # localstatedir => { content => '$prefix' },
490 # }
491 # into
492 # dir => { libexecdir => '$exec_prefix/libexec',
493 # localstatedir => '$prefix',
494 # }
495  
496 27 my $contentkey = $opts->{contentkey};
497  
498 # first go through the values, checking that they are fit to collapse
499 27 foreach my $v (values %out)
500 35 ▶ { next if !defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 35 35
501 35 ▶ next if ref $v eq 'HASH' && keys %$v == 1 && exists $v->{$contentkey};
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "HASH" and keys %$v == 1 and exists $v->{$contentkey} 14 21 35
Condition83%5/6
ref $v eq "HASH" and keys %$v == 1 and exists $v->{$contentkey}
!ll&&!rl&&r
16514
ref $v eq "HASH" and keys %$v == 1
!ll&&!rl&&r
01619
MC/DC66%2/3
ref $v eq "HASH" and keys %$v == 1 and exists $v->{$contentkey}
ref $v eq "HASH" keys %$v == 1 exists $v->{$contentkey}
Truth table75%3/4
A and B and C
A= ref $v eq "HASH"B= keys %$v == 1C= exists $v->{$contentkey}
ABCresult
0XX0
10X0
1100
1111
502 21 ▶ next if ref $v eq 'HASH' && !keys %$v;
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "HASH" and not keys %$v 0 21 21
Condition33%1/3
ref $v eq "HASH" and not keys %$v
!ll&&!rl&&r
0210
MC/DC0%0/2
ref $v eq "HASH" and not keys %$v
ref $v eq "HASH" not keys %$v
Truth table33%1/3
A and not(B)
A= ref $v eq "HASH"B= not keys %$v
ABresult
0X0
100
111
503 21 return \%out;
504 }
505  
506 6 $out{$_} = $out{$_}{$contentkey} for keys %out;
507 6 \%out;
508 }
509
510 2 1;
511  
512 __END__
↑ Summary