blib/lib/XML/LibXML/Tools.pm

filter: Statementstmt 81.1% 297 / 366 Branchbran 58.4% 124 / 212 Conditioncond 46.0% 29 / 63 MC/DCmcdc 17.5% 7 / 40 Subroutinesub 88.2% 30 / 34 Podpod 52.6% 10 / 19 totaltotal 67.7% 497 / 734 CC 132 SCAR 31.9
CC
132
Cov
70%
domUpdate
53.3
domAdd
41.4
array2Dom
30.5
CRAP 24.3
↑ Summary
1 package XML::LibXML::Tools;
2  
3 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 @XML::LibXML::Tools::ISA = qw( Exporter );
5 5 @XML::LibXML::Tools::EXPORT = qw( BEFORE AFTER TO );
6 5 $XML::LibXML::Tools::VERSION = '1.00';
7 }
8  
9 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 our $croak = 1;
13  
14 5 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 ▶use constant BEFORE => "before";
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use constant AFTER => "after";
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use constant TO => "to";
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use constant DEEP => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 5 ▶use Class::AccessorMaker {
Subroutine BEGIN: called CC=1 SCAR=0.0
23 # object wide settings
24 5 defaultLocation => TO,
25  
26 # object holders
27 objectDom => "",
28 storeDom => 1,
29  
30 # error handling
31 error => undef,
32 errorMsg => undef,
33 croakOnError => 1,
34 showPath => 0,
35  
36 5 }, "new_init";
37  
38  
39 5 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
40 5 my ($self) = @_;
41  
42 5 $self->croakOnError($croak);
43 5 return $self;
44 }
45  
46 # prevent endless recursion.
47 5 my @already_seen = ();
48  
49 # ==========================================================================
50 # ERRORS AND SUCH
51 # ==========================================================================
52  
53 9 ▶sub addError{
Subroutine addError: called CC=4 SCAR=17.3
Pod documented
54 9 my $self = shift;
55 9 my $string = shift;
56  
57 9 ▶ $self->errorMsg( ( ($self->errorMsg()) ? ($self->errorMsg()."\n") : "") . $string );
Branch: 1 branch
BranchTrueFalseTotal
$self->errorMsg ? : 1 8 9
58 9 $self->error(1);
59  
60 9 ▶ if ( $self->croakOnError ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->croakOnError) 0 9 9
61 0 my @caller = caller(1);
62 0 my @calledby = caller(2);
63 0 ▶ my ($caller) = $calledby[3]||'';
Condition0%0/2
$calledby[3] || ''
l!l
00
MC/DC0%0/1
$calledby[3] || ''
$calledby[3]
Truth table0%0/2
A
A= $calledby[3]
Aresult
11
00
64 0 $caller =~ s/XML::LibXML::Tools:://;
65 0 croak("$string - $caller ($caller[1] line: $caller[2])");
66 }
67  
68 9 return($self);
69 }
70  
71 134 ▶sub resetError{
Subroutine resetError: called CC=1 SCAR=0.0
Pod documented
72 134 my $self = shift;
73 134 $self->error(0);
74 134 $self->errorMsg("");
75 134 return($self);
76 }
77  
78 221 ▶sub checkParams {
Subroutine checkParams: called CC=3 SCAR=11.3
Pod undocumented
79 221 my $self = shift;
80 221 my %param = @_;
81  
82 221 ▶ if ( my @missing = grep { !defined $param{params}->{$_} } @{$param{required}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missing) = grep({not defined $param{"params"}{$_};} @{$param{"required"};})) 0 221 221
83 0 $self->addError("Missing required parameter(s) ".join(", ", @missing));
84  
85 0 return undef;
86 }
87  
88 # shouldn't be here, but prevents warnings;
89 221 ▶ $param{params}->{depth} ||= 0;
Condition100%2/2
$param{"params"}{"depth"} ||= 0
l!l
20318
MC/DC100%1/1
$param{"params"}{"depth"} ||= 0
$param{"params"}{"depth"}
Truth table100%2/2
A
A= $param{"params"}{"depth"}
Aresult
11
00
90  
91 221 return 1;
92 }
93  
94 134 ▶sub resetHandlers {
Subroutine resetHandlers: called CC=2 SCAR=7.0
Pod undocumented
95 134 my $self = shift;
96 134 my $caller = [caller(1)]->[1];
97  
98 134 ▶ if ( $caller !~ /LibXML::Tools/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $caller =~ /LibXML::Tools/) 134 0 134
99 134 @already_seen = ();
100 134 $self->resetError;
101 }
102 }
103  
104 # ==========================================================================
105 # COMPLEX -> DOM
106 # ==========================================================================
107  
108 31 ▶sub complex2Dom {
Subroutine complex2Dom: called CC=12 SCAR=28.0
Pod documented
109 31 my $self = shift;
110 31 my %param = @_;
111 31 ▶ $param{depth} ||= 0;
Condition100%2/2
$param{"depth"} ||= 0
l!l
427
MC/DC100%1/1
$param{"depth"} ||= 0
$param{"depth"}
Truth table100%2/2
A
A= $param{"depth"}
Aresult
11
00
112  
113 31 ▶ warn "-" x $param{depth}, "complex2Dom\n" if $self->showPath;
Branch: 1 branch
BranchTrueFalseTotal
if $self->showPath 0 31 31
114  
115 31 my $prev = $self->showPath;
116 31 ▶ $self->showPath($param{showpath}) if exists $param{showpath};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param{"showpath"} 0 31 31
117  
118 # reset recursion safety
119 31 $self->resetHandlers();
120  
121 # create ourselves a Dom.
122 31 my $dom = XML::LibXML::Document->new();
123 31 $dom->setEncoding( "utf-8" );
124  
125 # find root element
126 31 my $ref = $param{data};
127 31 my $rootName = shift @$ref;
128 31 $ref = shift @$ref;
129  
130 31 ▶ if ( my $type = ref($rootName) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = ref $rootName) 0 31 31
131 0 ▶ if ( $type =~ /ARRAY/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /ARRAY/) { } 0 0 0
elsif ($type =~ /XML::LibXML/) { } 0 0 0
132 0 $rootName = shift @$rootName;
133 } elsif ( $type =~ /XML::LibXML/ ) {
134 # to complex.
135 0 $self->addError("complex2Dom: To complex! ($rootName)");
136 } else {
137 0 $self->addError("complex2Dom: No rootname! ($rootName)");
138 0 croak($self->errorMsg);
139 }
140 }
141  
142 # create and set it.
143 {
144 31 eval {
145 31 ▶ local $SIG{__DIE__} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
146 31 $dom->setDocumentElement($dom->createElement( $rootName ));
147 };
148 31 chomp $@;
149 31 ▶ $self->addError("complex2Dom: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 31 31
150 }
151 31 my $root = $dom->getDocumentElement;
152 my $res = $self->array2Dom( %param,
153 data => $ref,
154 31 depth => $param{depth}+1,
155 dom => $dom,
156 node => $root );
157  
158 # fake 1-node document support
159 31 ▶ if( ref($res) eq "XML::LibXML::Text" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq "XML::LibXML::Text") 0 31 31
160 0 $root->addChild($res);
161 }
162  
163 31 ▶ $self->objectDom($dom) if !$self->objectDom && $self->storeDom;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->objectDom and $self->storeDom 5 26 31
Condition66%2/3
not $self->objectDom and $self->storeDom
!ll&&!rl&&r
2605
MC/DC50%1/2
not $self->objectDom and $self->storeDom
not $self->objectDom $self->storeDom
Truth table66%2/3
not(A) and B
A= not $self->objectDomB= $self->storeDom
ABresult
0X0
100
111
164  
165 31 $self->showPath($prev);
166 31 ▶ return ($self->error) ? undef : $dom;
Branch: 1 branch
BranchTrueFalseTotal
$self->error ? : 0 31 31
167 }
168  
169 74 ▶sub array2Dom{
Subroutine array2Dom: called CC=20 SCAR=34.2
Pod documented
170 74 my $self = shift;
171 74 my %param = @_;
172  
173 74 ▶ $param{dom} ||= $self->objectDom;
Condition33%1/3
$param{"dom"} ||= $self->objectDom
l!l&&r!l&&!r
7400
MC/DC0%0/2
$param{"dom"} ||= $self->objectDom
$param{"dom"} $self->objectDom
Truth table33%1/3
A ||= B
A= $param{"dom"}B= $self->objectDom
ABresult
1X1
011
000
174 74 ▶ $self->checkParams( params => \%param,
Branch: 1 branch
BranchTrueFalseTotal
$self->checkParams("params", \%param, "required", ["data", "dom", "node"]) or return undef 74 0 74
175 required => [ qw(data dom node) ],
176 ) || return undef;
177  
178 74 $self->resetHandlers;
179  
180 74 my $type = ref($param{data});
181  
182 74 ▶ if ( $type and $self->alreadySeen($param{data}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type and $self->alreadySeen($param{"data"})) 0 74 74
Condition66%2/3
$type and $self->alreadySeen($param{"data"})
!ll&&!rl&&r
35390
MC/DC0%0/2
$type and $self->alreadySeen($param{"data"})
$type $self->alreadySeen($param{"data"})
Truth table66%2/3
A and B
A= $typeB= $self->alreadySeen($param{"data"})
ABresult
0X0
100
111
183 0 warn "Circular reference for $type at array2Dom\n";
184 0 return;
185 }
186  
187 74 my $prev = $self->showPath;
188 74 ▶ $self->showPath($param{showpath}) if exists $param{showpath};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param{"showpath"} 0 74 74
189  
190 74 ▶ warn "-" x $param{depth}, "array2Dom\n" if $self->showPath;
Branch: 1 branch
BranchTrueFalseTotal
if $self->showPath 0 74 74
191  
192 74 ▶ if( $type eq "ARRAY" ){
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 39 35 74
elsif ($type eq 'SCALAR') { } 0 35 35
elsif ($type =~ /XML::LibXML/) { } 0 35 35
193 39 my $first = 1;
194 39 my @tmp = @{$param{data}};
195  
196 39 ▶ while ( @tmp ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tmp) 57 39 96
197 57 my $key = shift @tmp;
198  
199 57 ▶ $key = '' if( !defined $key );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 57 57
200 57 my $val = shift @tmp;
201  
202 57 ▶ if( my $attr = isAttribute($key) ){
Branch: 3 branches
BranchTrueFalseTotal
if (my $attr = isAttribute($key)) { } 5 52 57
elsif (my $cmnt = isComment($key)) { } 2 50 52
elsif (my $type = ref $key) { } 3 47 50
203 5 my $newNode = $param{dom}->createAttribute($attr, $val);
204 $self->addNode( %param,
205 5 depth => $param{depth}+1,
206 newNode => $newNode );
207  
208 } elsif( my $cmnt = isComment($key) ) {
209 2 my $element = $param{dom}->createComment( $cmnt );
210 $self->addNode( %param,
211 2 depth => $param{depth}+1,
212 newNode => $element );
213  
214 } elsif ( my $type = ref($key) ) {
215 $self->domAdd(%param,
216 3 depth => $param{depth}+1,
217 data => [ $key ]);
218  
219 } else {
220 47 ▶ if( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 43 4 47
221 43 my $element = $param{dom}->createElement($key);
222 43 $param{node}->addChild($element);
223  
224 # also support XML-doms!
225 43 ▶ if( ref($val) =~ /XML::LibXML::Document|XML::LibXML::DocumentFragment/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($val) =~ /XML::LibXML::Document|XML::LibXML::DocumentFragment/) { } 0 43 43
226 0 my $content = "";
227 0 ▶ if( ref($val) =~ /XML::LibXML::Document$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($val) =~ /XML::LibXML::Document$/) { } 0 0 0
228 0 $content = $val->getDocumentElement->cloneNode( DEEP );
229 0 $content->setOwnerDocument($param{dom});
230 } else {
231 0 $content = $val;
232 }
233  
234 0 $element->addChild($content);
235  
236 } else {
237 my $res = $self->array2Dom( %param,
238 43 depth => $param{depth}+1,
239 data => $val,
240 node => $element);
241 }
242  
243 } else {
244 # key _is_ value!
245 $self->addNode(%param,
246 depth => $param{depth}+1,
247 newNode => $self->makeXMLFragment( %param,
248 4 ▶ depth => $param{depth}+1,
Condition50%1/2
$key || ''
l!l
40
MC/DC0%0/1
$key || ''
$key
Truth table50%1/2
A
A= $key
Aresult
11
00
249 data => $key || '' ) );
250 }
251 }
252 }
253  
254 39 return($param{node});
255  
256 } elsif ($type eq "SCALAR") {
257 $self->array2Dom( %param,
258 depth => $param{depth}+1,
259 0 data => scalar($param{data}) );
260  
261 } elsif ( $type =~ /XML::LibXML/ ) {
262 $self->addNode ( %param,
263 depth => $param{depth}+1,
264 newNode => $self->makeXMLFragment( %param,
265 0 depth => $param{depth}+1 ) );
266  
267 } else{
268 # it's a true scalar! -> return the value!
269 35 $self->ref2TextNode( %param, depth => $param{depth}+1 );
270 }
271  
272 35 $self->showPath($prev);
273 35 ▶ return ($self->error) ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->error ? : 0 35 35
274 }
275  
276 # ==========================================================================
277 # DOM MANIPULATION
278 # ==========================================================================
279  
280 38 ▶sub ref2TextNode {
Subroutine ref2TextNode: called CC=9 SCAR=27.2
Pod undocumented
281 38 my $self = shift;
282 38 my %param = @_;
283  
284 38 ▶ $param{dom} ||= $self->objectDom;
Condition33%1/3
$param{"dom"} ||= $self->objectDom
l!l&&r!l&&!r
3800
MC/DC0%0/2
$param{"dom"} ||= $self->objectDom
$param{"dom"} $self->objectDom
Truth table33%1/3
A ||= B
A= $param{"dom"}B= $self->objectDom
ABresult
1X1
011
000
285 38 ▶ $self->checkParams( params => \%param,
Branch: 1 branch
BranchTrueFalseTotal
$self->checkParams("params", \%param, "required", ["data", "dom", "node"]) or return undef 38 0 38
286 required => [ qw(data dom node) ] ) || return undef;
287  
288 38 my $prev = $self->showPath;
289 38 ▶ $self->showPath($param{showpath}) if exists $param{showpath};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param{"showpath"} 0 38 38
290 38 ▶ warn "-" x $param{depth}, "ref2TextNode\n" if $self->showPath;
Branch: 1 branch
BranchTrueFalseTotal
if $self->showPath 0 38 38
291  
292 # some times, some where an element is mistaken for a text - correct
293 # that mistake here.
294  
295 38 my $textNode;
296 38 ▶ if ( ref($param{data}) !~ /XML::LibXML/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref($param{'data'}) =~ /XML::LibXML/) { } 38 0 38
elsif (ref($param{'data'}) =~ /DocumentFragment|Element/) { } 0 0 0
elsif (ref($param{'data'}) =~ /NodeList/) { } 0 0 0
297 38 $textNode = $param{dom}->createTextNode($param{data});
298  
299 } elsif ( ref($param{data}) =~ /DocumentFragment|Element/ ) {
300 0 $textNode = $param{data};
301  
302 } elsif ( ref($param{data}) =~ /NodeList/ ) {
303 0 $textNode = $param{data}->shift;
304  
305 } else {
306 $self->addError("ref2TextNode: unknown reference type (".
307 0 ref($param{data}).") - can't make node");
308 }
309  
310 # reset location -> TO for text nodes
311 $self->addNode( %param,
312 location => TO,
313 38 depth => $param{depth}+1,
314 newNode => $textNode );
315  
316 38 $self->showPath($prev);
317  
318 38 ▶ return ($self->error) ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->error ? : 0 38 38
319 }
320  
321 14 ▶sub makeXMLFragment {
Subroutine makeXMLFragment: called CC=12 SCAR=27.1
Pod undocumented
322 14 my $self = shift;
323 14 my %param = @_;
324  
325 14 ▶ $param{dom} ||= $self->objectDom;
Condition33%1/3
$param{"dom"} ||= $self->objectDom
l!l&&r!l&&!r
1400
MC/DC0%0/2
$param{"dom"} ||= $self->objectDom
$param{"dom"} $self->objectDom
Truth table33%1/3
A ||= B
A= $param{"dom"}B= $self->objectDom
ABresult
1X1
011
000
326 14 ▶ $self->checkParams( params => \%param,
Branch: 1 branch
BranchTrueFalseTotal
$self->checkParams("params", \%param, "required", ["dom", "data"]) or return undef 14 0 14
327 required => [ qw(dom data) ]) || return undef;
328  
329 14 my $prev = $self->showPath;
330 14 ▶ $self->showPath($param{showpath}) if exists $param{showpath};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param{"showpath"} 0 14 14
331  
332 14 ▶ warn "-" x $param{depth}, "makeXMLFragment\n" if $self->showPath;
Branch: 1 branch
BranchTrueFalseTotal
if $self->showPath 0 14 14
333  
334 14 ▶ if ( ref($param{data}) =~ /DocumentFragment/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref($param{'data'}) =~ /DocumentFragment/) { } 0 14 14
elsif (ref($param{'data'}) =~ /NodeList/) { } 0 14 14
elsif (ref($param{'data'}) =~ /Element|Node/) { } 4 10 14
335 # for fragments, elements and nodes be content.
336 0 $self->showPath($prev);
337 return $param{data}
338  
339 0 } elsif ( ref($param{data}) =~ /NodeList/ ) {
340 0 $param{data} = $param{data}->shift->cloneNode( DEEP );
341  
342 } elsif ( ref($param{data}) =~ /Element|Node/ ) {
343 4 $self->showPath($prev);
344 4 return $param{data}->cloneNode( DEEP );
345  
346 }
347  
348 10 ▶ if ( ref($param{data}) =~ /ARRAY/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($param{"data"}) =~ /ARRAY/) 4 6 10
349 # make array-value a DOM
350 $param{data} = $self->complex2Dom( %param,
351 data => $param{data},
352 4 depth => $param{depth}+1 );
353 }
354  
355 10 my $type = ref($param{data});
356 10 my $content = "";
357  
358 10 ▶ if ( $type =~ /XML::LibXML::Document$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /XML::LibXML::Document$/) { } 5 5 10
elsif ($type =~ /DocumentFragment|Element/) { } 0 5 5
359 5 $content = $param{data}->getDocumentElement->cloneNode( DEEP );
360 $content->setOwnerDocument($param{dom})
361 5 } elsif ( $type =~ /DocumentFragment|Element/ ) {
362 0 $content = $param{data};
363  
364 } else {
365 5 $content = $param{dom}->createTextNode($param{data});
366 }
367  
368 10 $self->showPath($prev);
369  
370 10 ▶ return ($self->error) ? undef : $content;
Branch: 1 branch
BranchTrueFalseTotal
$self->error ? : 0 10 10
371 }
372  
373 11 ▶sub domUpdate {
Subroutine domUpdate: called CC=27 SCAR=39.8
Pod documented
374 11 my $self = shift;
375 11 my %param = @_;
376  
377 11 ▶ $param{dom} ||= $self->objectDom;
Condition33%1/3
$param{"dom"} ||= $self->objectDom
l!l&&r!l&&!r
1100
MC/DC0%0/2
$param{"dom"} ||= $self->objectDom
$param{"dom"} $self->objectDom
Truth table33%1/3
A ||= B
A= $param{"dom"}B= $self->objectDom
ABresult
1X1
011
000
378 11 ▶ $param{dom} || $self->addError("domUpdate : No DOM supplied!");
Branch: 1 branch
BranchTrueFalseTotal
$param{"dom"} or $self->addError("domUpdate : No DOM supplied!") 11 0 11
379  
380 11 ▶ if ( !$param{node} and $param{xpath} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $param{"node"} and $param{"xpath"}) 11 0 11
Condition33%1/3
not $param{"node"} and $param{"xpath"}
!ll&&!rl&&r
0011
MC/DC0%0/2
not $param{"node"} and $param{"xpath"}
not $param{"node"} $param{"xpath"}
Truth table33%1/3
not(A) and B
A= not $param{"node"}B= $param{"xpath"}
ABresult
0X0
100
111
381 11 $param{node} = $param{dom}->findnodes($param{xpath})->shift;
382 11 ▶ $param{node} || $self->addError("Couldn't execute XPATH on supplied DOM ($param{dom}) in domUpdate");
Branch: 1 branch
BranchTrueFalseTotal
$param{"node"} or $self->addError(qq[Couldn't execute XPATH on supplied DOM ($param{"dom"}) in domUpdate]) 11 0 11
383 }
384  
385 11 ▶ $self->checkParams( params => \%param,
Branch: 1 branch
BranchTrueFalseTotal
$self->checkParams("params", \%param, "required", ["dom", "node", "data"]) or return undef 11 0 11
386 required => [ qw(dom node data) ]) || return undef;
387  
388 11 my $prev = $self->showPath;
389 11 ▶ $self->showPath($param{showpath}) if exists $param{showpath};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param{"showpath"} 0 11 11
390  
391 11 ▶ warn "-" x $param{depth}, "domUpdate\n" if $self->showPath;
Branch: 1 branch
BranchTrueFalseTotal
if $self->showPath 0 11 11
392  
393 # reset recursion limiter.
394 11 $self->resetHandlers;
395  
396 11 my @tmp = @{$param{data}};
397 11 ▶ while (@tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tmp) 18 11 29
398 18 my $key = shift @tmp;
399  
400 18 my $value;
401 18 ▶ $value = shift @tmp if ( !ref($key) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $key 13 5 18
402  
403 18 ▶ if( my $attr = isAttribute($key) ){
Branch: 2 branches
BranchTrueFalseTotal
if (my $attr = isAttribute($key)) { } 4 14 18
elsif (isComment($key)) { } 3 11 14
404 # update attribute of tag
405 4 $param{node}->removeAttribute( $attr );
406  
407 4 my $newNode = $param{dom}->createAttribute($attr, $value);
408 $self->addNode( %param,
409 4 depth => $param{depth}+1,
410 newNode => $newNode );
411  
412 } elsif ( isComment($key) ) {
413 # make error - but don't croak
414 3 my $prev = $self->croakOnError; $self->croakOnError(0);
415 3 $self->addError("domUpdate doesn't support comments. Referting to domAdd");
416 3 $self->croakOnError($prev);
417  
418 $self->domAdd(%param,
419 3 depth => $param{depth}+1,
420 data => [ $key ]);
421  
422 } else {
423 # update value element of tag
424 11 my $type = ref($key);
425  
426 11 ▶ if ( !$type ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $type) { } 6 5 11
elsif ($type =~ /NodeList$/) { } 0 5 5
elsif ($type =~ /XML::LibXML/) { } 5 0 5
427 6 my $parent = $param{node}->findnodes("$key");
428 6 my $node = $parent->shift();
429  
430 6 ▶ if (!$node) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node) 5 1 6
431 # perhaps adding it is possible...
432 $self->domAdd( %param,
433 5 depth => $param{depth}+1,
434 data => [ $key => $value ] );
435 5 next;
436 }
437  
438 1 my ($elname) = $key =~ /([^\/]*)$/;
439 1 my $element = $param{dom}->createElement( $elname );
440 my $content = $self->makeXMLFragment(%param,
441 1 depth => $param{depth}+1,
442 data => $value);
443  
444 1 $element->addChild($content);
445 1 $node->replaceNode($element);
446  
447 } elsif ( $type =~ /NodeList$/ ) {
448  
449 0 foreach my $node ( $key->get_nodelist ) {
450 # add or replace?
451 0 my ($parentNode, $addOrReplace);
452 0 eval {
453 0 ▶ local $SIG{__DIE__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
454 0 ▶ $addOrReplace = ($parentNode = $param{node}->findnodes($node->nodeName)->shift)
Branch: 1 branch
BranchTrueFalseTotal
($parentNode = $param{'node'}->findnodes($node->nodeName)->shift) ? : 0 0 0
455 ? "replaceNode"
456 : "addChild";
457 };
458 0 ▶ chomp($@); $self->addError("domUpdate: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
459  
460 0 ▶ $addOrReplace ||= "addChild";
Condition0%0/2
$addOrReplace ||= "addChild"
l!l
00
MC/DC0%0/1
$addOrReplace ||= "addChild"
$addOrReplace
Truth table0%0/2
A
A= $addOrReplace
Aresult
11
00
461 0 ▶ $parentNode ||= $param{node};
Condition0%0/3
$parentNode ||= $param{"node"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$parentNode ||= $param{"node"}
$parentNode $param{"node"}
Truth table0%0/3
A ||= B
A= $parentNodeB= $param{"node"}
ABresult
1X1
011
000
462  
463 0 my $newNode = $node->cloneNode( DEEP );
464 0 $parentNode->$addOrReplace($newNode);
465 }
466  
467 } elsif ( $type =~ /XML::LibXML/ ) {
468 my $content = $self->makeXMLFragment(%param,
469 5 depth => $param{depth}+1,
470 data => $key);
471 5 my $node = $content->findnodes("/")->shift;
472  
473 # add or replace?
474 5 my ($parentNode, $addOrReplace);
475 5 eval {
476 5 ▶ local $SIG{__DIE__} = sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
477 5 ▶ $addOrReplace = ($parentNode = $param{node}->findnodes($node->nodeName)->shift)
Branch: 1 branch
BranchTrueFalseTotal
($parentNode = $param{'node'}->findnodes($node->nodeName)->shift) ? : 0 0 0
478 ? "replaceNode"
479 : "addChild";
480 };
481 5 ▶ chomp($@); $self->addError("domUpdate: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 5 0 5
482  
483 5 ▶ $addOrReplace ||= "addChild";
Condition50%1/2
$addOrReplace ||= "addChild"
l!l
05
MC/DC0%0/1
$addOrReplace ||= "addChild"
$addOrReplace
Truth table50%1/2
A
A= $addOrReplace
Aresult
11
00
484 5 ▶ $parentNode ||= $param{node};
Condition33%1/3
$parentNode ||= $param{"node"}
l!l&&r!l&&!r
050
MC/DC0%0/2
$parentNode ||= $param{"node"}
$parentNode $param{"node"}
Truth table33%1/3
A ||= B
A= $parentNodeB= $param{"node"}
ABresult
1X1
011
000
485  
486 5 $parentNode->$addOrReplace($content);
487  
488 } else {
489 0 $self->addError("Unknown type at domUpdate");
490 }
491 }
492 }
493  
494 11 $self->showPath($prev);
495  
496 11 ▶ return ($self->error) ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->error ? : 4 7 11
497 }
498  
499 13 ▶sub domAdd {
Subroutine domAdd: called CC=23 SCAR=37.2
Pod documented
500 13 my $self = shift;
501 13 my %param = @_;
502  
503 13 ▶ $param{dom} ||= $self->objectDom;
Condition33%1/3
$param{"dom"} ||= $self->objectDom
l!l&&r!l&&!r
1300
MC/DC0%0/2
$param{"dom"} ||= $self->objectDom
$param{"dom"} $self->objectDom
Truth table33%1/3
A ||= B
A= $param{"dom"}B= $self->objectDom
ABresult
1X1
011
000
504 13 ▶ if ( !$param{node} and $param{xpath} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $param{"node"} and $param{"xpath"}) 2 11 13
Condition66%2/3
not $param{"node"} and $param{"xpath"}
!ll&&!rl&&r
1102
MC/DC50%1/2
not $param{"node"} and $param{"xpath"}
not $param{"node"} $param{"xpath"}
Truth table66%2/3
not(A) and B
A= not $param{"node"}B= $param{"xpath"}
ABresult
0X0
100
111
505 2 $param{node} = $param{dom}->findnodes($param{xpath})->shift;
506 2 ▶ $param{node} || $self->addError("Couldn't execute XPATH on supplied DOM in domUpdate");
Branch: 1 branch
BranchTrueFalseTotal
$param{"node"} or $self->addError("Couldn't execute XPATH on supplied DOM in domUpdate") 2 0 2
507 }
508  
509 13 ▶ $self->checkParams( params => \%param,
Branch: 1 branch
BranchTrueFalseTotal
$self->checkParams("params", \%param, "required", ["dom", "node", "data"]) or return undef 13 0 13
510 required => [ qw(dom node data) ]) || return undef;
511  
512 13 my $prev = $self->showPath;
513 13 ▶ $self->showPath($param{showpath}) if exists $param{showpath};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param{"showpath"} 0 13 13
514  
515 13 ▶ warn "-" x $param{depth}, "domAdd\n" if $self->showPath;
Branch: 1 branch
BranchTrueFalseTotal
if $self->showPath 0 13 13
516  
517 # reset recursion limiter.
518 13 $self->resetHandlers;
519  
520 13 my @tmp = @{$param{data}};
521 13 ▶ while (@tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tmp) 13 12 25
522 13 my $key = shift @tmp;
523  
524 13 my $value;
525 13 ▶ $value = shift @tmp if ( !ref($key) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $key 10 3 13
526  
527 13 ▶ if ( my $attr = isAttribute($key) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $attr = isAttribute($key)) { } 0 13 13
elsif (my $comment = isComment($key)) { } 3 10 13
528 # update attribute of tag
529 0 $param{node}->removeAttribute( $attr );
530  
531 0 my $newNode = $param{dom}->createAttribute($attr, $value);
532 $self->addNode( %param,
533 0 depth => $param{depth}+1,
534 newNode => $newNode );
535  
536 } elsif ( my $comment = isComment($key) ) {
537 $self->addNode( %param,
538 depth => $param{depth}+1,
539 3 newNode => $param{dom}->createComment( $comment ) );
540  
541 } else {
542 # get the type.
543 10 my $type = ref ($key);
544  
545 # make a dom for arrays
546 10 ▶ if ( $type =~ /ARRAY/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /ARRAY/) 0 10 10
547 $key = $self->complex2Dom( %param,
548 data => $key,
549 0 depth => $param{depth}+1);
550 0 $type = ref ( $key );
551 }
552  
553 10 ▶ if ( $type =~ /NodeList/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type =~ /NodeList/) { } 0 10 10
elsif ($type =~ /XML::LibXML::Document$/) { } 1 9 10
elsif ($type =~ /XML::LibXML::/) { } 2 7 9
elsif (not $type) { } 7 0 7
554 ## adding OR moving a list of Nodes.
555 0 ▶ while ( my $node = $key->shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = $key->shift) 0 0 0
556 0 my $newNode = $node->cloneNode( DEEP );
557 $self->addNode( %param,
558 0 depth => $param{depth}+1,
559 newNode => $newNode );
560 }
561  
562 } elsif ( $type =~ /XML::LibXML::Document$/ ) {
563 ## adding a dom.
564 1 my $newNode = $key->getDocumentElement->cloneNode( DEEP );
565 1 $newNode->setOwnerDocument($param{dom});
566 $self->addNode( %param,
567 1 depth => $param{depth}+1,
568 newNode => $newNode );
569  
570 } elsif ( $type =~ /XML::LibXML::/ ) {
571 ## adding a fragment, element or node
572 2 my $newNode = $key->cloneNode( DEEP );
573 $self->addNode( %param,
574 2 depth => $param{depth}+1,
575 newNode => $newNode );
576  
577 } elsif ( !$type ) {
578 # perhaps it is a xml-scalar-ref
579 7 ▶ if ( $key =~ /SCALAR/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /SCALAR/) 0 7 7
580 0 my $pkey;
581 0 eval {
582 0 ▶ local $SIG{__DIE__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
583 0 $pkey = $key->nodeName;
584 };
585 0 ▶ $key = $pkey if !$@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
586 }
587  
588 7 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 4 3 7
589 4 my $array = $value;
590 4 ▶ if ( $key ) { $array = [$key => $value] }
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 4 0 4
591  
592 $self->addNode( %param,
593 depth => $param{depth}+1,
594 newNode =>
595 $self->makeXMLFragment( %param,
596 4 depth => $param{depth}+1,
597 data => $array )
598 );
599 } else {
600 3 my $newNode = $param{dom}->createElement( $key );
601 $self->ref2TextNode( %param,
602 node => $newNode,
603 3 depth => $param{depth}+1,
604 data => $value );
605 $self->addNode( %param,
606 3 depth => $param{depth}+1,
607 newNode => $newNode );
608 }
609 } else {
610 0 $self->addError("Couldn't determine what to do with $key");
611 }
612  
613 }
614 }
615  
616 12 $self->showPath($prev);
617 12 ▶ return ($self->error) ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->error ? : 0 12 12
618 }
619  
620 66 ▶sub addNode {
Subroutine addNode: called CC=12 SCAR=28.3
Pod undocumented
621 66 my $self = shift;
622 66 my %param = @_;
623  
624 66 ▶ $param{dom} ||= $self->objectDom;
Condition33%1/3
$param{"dom"} ||= $self->objectDom
l!l&&r!l&&!r
6600
MC/DC0%0/2
$param{"dom"} ||= $self->objectDom
$param{"dom"} $self->objectDom
Truth table33%1/3
A ||= B
A= $param{"dom"}B= $self->objectDom
ABresult
1X1
011
000
625 66 ▶ $self->checkParams( params => \%param,
Branch: 1 branch
BranchTrueFalseTotal
$self->checkParams("params", \%param, "required", ["dom", "node", "newNode"]) or return undef 66 0 66
626 required => [ qw(dom node newNode) ]) || return undef;
627  
628 66 ▶ $param{location} ||= $self->defaultLocation;
Condition66%2/3
$param{"location"} ||= $self->defaultLocation
l!l&&r!l&&!r
40260
MC/DC0%0/2
$param{"location"} ||= $self->defaultLocation
$param{"location"} $self->defaultLocation
Truth table66%2/3
A ||= B
A= $param{"location"}B= $self->defaultLocation
ABresult
1X1
011
000
629  
630 66 my $prev = $self->showPath;
631 66 ▶ $self->showPath($param{showpath}) if exists $param{showpath};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param{"showpath"} 0 66 66
632  
633 66 ▶ warn "-" x $param{depth}, "addNode\n" if $self->showPath;
Branch: 1 branch
BranchTrueFalseTotal
if $self->showPath 0 66 66
634  
635 66 ▶ if ( $param{node}->isSameNode ( $param{dom}->getDocumentElement )
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"node"}->isSameNode($param{"dom"}->getDocumentElement) and $param{"location"} ne "to") 1 65 66
Condition100%3/3
$param{"node"}->isSameNode($param{"dom"}->getDocumentElement) and $param{"location"} ne "to"
!ll&&!rl&&r
5781
MC/DC100%2/2
$param{"node"}->isSameNode($param{"dom"}->getDocumentElement) and $param{"location"} ne "to"
$param{"node"}->isSameNode($param{"dom"}->getDocumentElement) $param{"location"} ne "to"
Truth table100%3/3
A and B
A= $param{"node"}->isSameNode($param{"dom"}->getDocumentElement)B= $param{"location"} ne "to"
ABresult
0X0
100
111
636 and $param{location} ne TO ) {
637 1 $self->addError("addNode: can't add BEFORE or AFTER the root element");
638 }
639  
640 66 ▶ if ( $param{location} eq TO ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($param{'location'} eq 'to') { } 64 2 66
elsif ($param{'location'} eq 'after') { } 2 0 2
elsif ($param{'location'} eq 'before') { } 0 0 0
641 64 $param{node}->addChild($param{newNode});
642  
643 } elsif ( $param{location} eq AFTER ) {
644 2 my $parentNode = $param{node}->parentNode;
645 2 $parentNode->insertAfter( $param{newNode}, $param{node} );
646  
647 } elsif ( $param{location} eq BEFORE ) {
648 0 my $parentNode = $param{node}->parentNode;
649 0 $parentNode->insertBefore( $param{newNode}, $param{node} );
650  
651 } else {
652 0 $self->addError("Unknown adding location: $param{location} at addNode");
653 0 $self->showPath($prev);
654 0 return undef;
655 }
656  
657 65 $self->showPath($prev);
658 65 ▶ return ($self->error) ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->error ? : 0 65 65
659 }
660  
661 5 ▶sub domDelete {
Subroutine domDelete: called CC=13 SCAR=27.5
Pod documented
662 5 my $self = shift;
663 5 my %param = @_;
664  
665 # reset recursion (just for safety)
666 5 $self->resetHandlers;
667  
668 5 ▶ $param{dom} ||= $self->objectDom;
Condition33%1/3
$param{"dom"} ||= $self->objectDom
l!l&&r!l&&!r
500
MC/DC0%0/2
$param{"dom"} ||= $self->objectDom
$param{"dom"} $self->objectDom
Truth table33%1/3
A ||= B
A= $param{"dom"}B= $self->objectDom
ABresult
1X1
011
000
669 5 ▶ if ( !$param{node} and $param{xpath} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $param{"node"} and $param{"xpath"}) 5 0 5
Condition33%1/3
not $param{"node"} and $param{"xpath"}
!ll&&!rl&&r
005
MC/DC0%0/2
not $param{"node"} and $param{"xpath"}
not $param{"node"} $param{"xpath"}
Truth table33%1/3
not(A) and B
A= not $param{"node"}B= $param{"xpath"}
ABresult
0X0
100
111
670 5 $param{node} = $param{dom}->findnodes($param{xpath})->shift;
671 5 ▶ $param{node} || $self->addError("Couldn't execute XPATH on supplied DOM in domDelete");
Branch: 1 branch
BranchTrueFalseTotal
$param{"node"} or $self->addError("Couldn't execute XPATH on supplied DOM in domDelete") 5 0 5
672 }
673  
674 # allow shorthand
675 5 ▶ $param{deleteXPath} = $param{delete} if ( !$param{deleteXPath} and $param{delete} );
Branch: 1 branch
BranchTrueFalseTotal
if not $param{"deleteXPath"} and $param{"delete"} 2 3 5
Condition66%2/3
not $param{"deleteXPath"} and $param{"delete"}
!ll&&!rl&&r
302
MC/DC50%1/2
not $param{"deleteXPath"} and $param{"delete"}
not $param{"deleteXPath"} $param{"delete"}
Truth table66%2/3
not(A) and B
A= not $param{"deleteXPath"}B= $param{"delete"}
ABresult
0X0
100
111
676  
677 5 ▶ $self->checkParams( params => \%param,
Branch: 1 branch
BranchTrueFalseTotal
$self->checkParams("params", \%param, "required", ["dom", "node", "deleteXPath"]) or return undef 5 0 5
678 required => [ qw(dom node deleteXPath) ]) || return undef;
679  
680  
681 5 my $prev = $self->showPath;
682 5 ▶ $self->showPath($param{showpath}) if exists $param{showpath};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param{"showpath"} 0 5 5
683 5 ▶ warn "-" x $param{depth}, "domDelete\n" if $self->showPath;
Branch: 1 branch
BranchTrueFalseTotal
if $self->showPath 0 5 5
684  
685 # remove
686 5 my $set = $param{node}->findnodes($param{deleteXPath});
687 5 foreach my $deleteNode ( $set->get_nodelist ) {
688  
689 6 ▶ if ( $deleteNode->nodeType == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deleteNode->nodeType == 2) { } 1 5 6
690 # attributes need special treatment
691 1 $deleteNode->unbindNode;
692  
693 } else {
694 5 $param{node}->removeChild($deleteNode);
695 }
696 }
697  
698 5 $self->showPath($prev);
699 5 ▶ return ($self->error) ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->error ? : 0 5 5
700 }
701  
702 # ==========================================================================
703 # USEFULL TOOLS?
704 # ==========================================================================
705  
706 0 ▶sub analyseXpath {
Subroutine analyseXpath: not called CC=2 SCAR=17.9
Pod documented
707 0 my ( $self, $xpath ) = @_;
708  
709 # provides last part,number of xpath, parent
710 # eg /newlsetter[1]/section[2]/parot[2]
711 #
712 # returns ('parot', 2, '/newlsetter[1]/section[2]')
713 #
714  
715 0 my $index;
716 0 ▶ if ( $xpath =~ s/\[(\d+)\]$// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xpath =~ s/\[(\d+)\]$//) 0 0 0
717 0 $index = $1;
718 }
719  
720 0 my ($parent, $part) = $xpath =~ /^(.*)\/([^\/]+)$/;
721  
722 0 return($part, $index, $parent);
723 }
724  
725 9 ▶sub attribute{
Subroutine attribute: called CC=1 SCAR=0.0
Pod documented
726 9 my $self = shift;
727 9 my $name = shift;
728 9 my $val = shift;
729  
730 9 return( "$name=attr" => $val);
731 }
732  
733 88 ▶sub isAttribute {
Subroutine isAttribute: called CC=1 SCAR=0.0
Pod undocumented
734 88 my ($name) = shift =~ /^(.*)=attr$/;
735 88 return $name;
736 }
737  
738 5 ▶sub comment {
Subroutine comment: called CC=1 SCAR=0.0
Pod documented
739 5 my $self = shift;
740 5 my $comment = shift;
741  
742 5 return( "$comment=comment" => undef );
743 }
744  
745 79 ▶sub isComment {
Subroutine isComment: called CC=4 SCAR=14.1
Pod undocumented
746 79 my ($comment) = shift =~ /^(.*)=comment$/m;
747 79 ▶ $comment || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$comment or return undef 8 71 79
748 # comments look better with surrounding spaces
749 8 ▶ $comment = " " . $comment if $comment !~ /^\s/;
Branch: 1 branch
BranchTrueFalseTotal
unless $comment =~ /^\s/ 8 0 8
750 8 ▶ $comment .= " " if $comment !~ /\s$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $comment =~ /\s$/ 8 0 8
751  
752 8 return $comment;
753 }
754  
755 39 ▶sub alreadySeen {
Subroutine alreadySeen: called CC=2 SCAR=7.6
Pod undocumented
756 39 my $self = shift;
757 39 my $ref = shift;
758  
759 # returns 1 if already seen
760 # undef if not yet seen
761 #
762  
763 0 ▶ if ( grep { $ref == $_ } @already_seen ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$ref == $_;} @already_seen) { } 0 39 39
764 0 return 1;
765 } else {
766 39 push @already_seen , $ref;
767 }
768  
769 39 return undef;
770 }
771  
772 5 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
773 5 my $self = $_[0];
774  
775 5 $self = undef;
776 5 return undef;
777 }
778  
779 5 1;
780  
781 __END__
↑ Summary