blib/lib/XML/Stream/Tree.pm

filter: Statementstmt 70.1% 188 / 268 Branchbran 64.6% 97 / 150 Conditioncond 45.2% 19 / 42 MC/DCmcdc 26.0% 6 / 23 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 4 totaltotal 64.2% 320 / 498 SCAR 61.9
CC
100
Cov
66%
GetXMLData
185.9
SetXMLData
182.0
_handle_close
15.0
CRAP 490.0
« blib/lib/XML/Stream/Node.pm ↑ Summary blib/lib/XML/Stream/Parser.pm »
1 ##############################################################################
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Library General Public
5 # License as published by the Free Software Foundation; either
6 # version 2 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Library General Public License for more details.
12 #
13 # You should have received a copy of the GNU Library General Public
14 # License along with this library; if not, write to the
15 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 # Boston, MA 02111-1307, USA.
17 #
18 # Jabber
19 # Copyright (C) 1998-2004 Jabber Software Foundation http://jabber.org/
20 #
21 ##############################################################################
22  
23 package XML::Stream::Tree;
24  
25 =head1 NAME
26  
27 XML::Stream::Tree - Functions to make building and parsing the tree easier
28 to work with.
29  
30 =head1 SYNOPSIS
31  
32 Just a collection of functions that do not need to be in memory if you
33 choose one of the other methods of data storage.
34  
35 =head1 FORMAT
36  
37 The result of parsing:
38  
39 <foo><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
40  
41 would be:
42 Tag Content
43 ==================================================================
44 [foo, [{},
45 head, [{id => "a"},
46 0, "Hello ",
47 em, [{},
48 0, "there"
49 ]
50 ],
51 bar, [{},
52 0, "Howdy",
53 ref, [{}]
54 ],
55 0, "do"
56 ]
57 ]
58  
59 The above was copied from the XML::Parser man page. Many thanks to
60 Larry and Clark.
61  
62 =head1 AUTHOR
63  
64 By Ryan Eatmon in March 2001 for http://jabber.org/
65  
66 Currently maintained by Darian Anthony Patrick.
67  
68 =head1 COPYRIGHT
69  
70 Copyright (C) 1998-2004 Jabber Software Foundation http://jabber.org/
71  
72 This module licensed under the LGPL, version 2.1.
73  
74 =cut
75  
76 12 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 12 ▶use vars qw( $VERSION $LOADED );
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 12 $VERSION = "1.23_07";
82 12 $LOADED = 1;
83  
84 ##############################################################################
85 #
86 # _handle_element - handles the main tag elements sent from the server.
87 # On an open tag it creates a new XML::Parser::Tree so
88 # that _handle_cdata and _handle_element can add data
89 # and tags to it later.
90 #
91 ##############################################################################
92 sub _handle_element
93 {
94 240 ▶ my $self;
Subroutine _handle_element: called CC=4 SCAR=13.9
95 240 ▶ $self = $_[0] if (ref($_[0]) eq "XML::Stream::Parser");
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "XML::Stream::Parser" 124 116 240
96 240 ▶ $self = shift unless (ref($_[0]) eq "XML::Stream::Parser");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "XML::Stream::Parser" 116 124 240
97 240 my ($sax, $tag, %att) = @_;
98 240 my $sid = $sax->getSID();
99  
100 240 $self->debug(2,"_handle_element: sid($sid) sax($sax) tag($tag) att(",%att,")");
101  
102 240 my @NEW;
103 240 ▶ if($#{$self->{SIDS}->{$sid}->{tree}} < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$self{'SIDS'}{$sid}{'tree'};} < 0) { } 38 202 240
104 {
105 38 push @{$self->{SIDS}->{$sid}->{tree}}, $tag;
106 }
107 else
108 {
109 202 push @{ $self->{SIDS}->{$sid}->{tree}[ $#{$self->{SIDS}->{$sid}->{tree}}]}, $tag;
110 }
111 240 push @NEW, \%att;
112 240 push @{$self->{SIDS}->{$sid}->{tree}}, \@NEW;
113 }
114  
115  
116 ##############################################################################
117 #
118 # _handle_cdata - handles the CDATA that is encountered. Also, in the
119 # spirit of XML::Parser::Tree it combines any sequential
120 # CDATA into one tag.
121 #
122 ##############################################################################
123 sub _handle_cdata
124 {
125 348 ▶ my $self;
Subroutine _handle_cdata: called CC=6 SCAR=18.1
126 348 ▶ $self = $_[0] if (ref($_[0]) eq "XML::Stream::Parser");
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "XML::Stream::Parser" 176 172 348
127 348 ▶ $self = shift unless (ref($_[0]) eq "XML::Stream::Parser");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "XML::Stream::Parser" 172 176 348
128 348 my ($sax, $cdata) = @_;
129 348 my $sid = $sax->getSID();
130  
131 348 $self->debug(2,"_handle_cdata: sid($sid) sax($sax) cdata($cdata)");
132  
133 348 ▶ return if ($#{$self->{SIDS}->{$sid}->{tree}} == -1);
Branch: 1 branch
BranchTrueFalseTotal
if $#{$$self{"SIDS"}{$sid}{"tree"};} == -1 36 312 348
134  
135 312 $self->debug(2,"_handle_cdata: sax($sax) cdata($cdata)");
136  
137 312 my $pos = $#{$self->{SIDS}->{$sid}->{tree}};
138 312 $self->debug(2,"_handle_cdata: pos($pos)");
139  
140 312 ▶ if ($pos > 0 && $self->{SIDS}->{$sid}->{tree}[$pos - 1] eq "0")
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > 0 and $self->{'SIDS'}{$sid}{'tree'}[$pos - 1] eq '0') { } 0 312 312
Condition33%1/3
$pos > 0 and $self->{"SIDS"}{$sid}{"tree"}[$pos - 1] eq 0
!ll&&!rl&&r
03120
MC/DC0%0/2
$pos > 0 and $self->{"SIDS"}{$sid}{"tree"}[$pos - 1] eq 0
$pos > 0 $self->{"SIDS"}{$sid}{"tree"}[$pos - 1] eq 0
Truth table33%1/3
A and B
A= $pos > 0B= $self->{"SIDS"}{$sid}{"tree"}[$pos - 1] eq 0
ABresult
0X0
100
111
141 {
142 0 $self->debug(2,"_handle_cdata: append cdata");
143 0 $self->{SIDS}->{$sid}->{tree}[$pos - 1] .= $cdata;
144 }
145 else
146 {
147 312 $self->debug(2,"_handle_cdata: new cdata");
148 312 push @{$self->{SIDS}->{$sid}->{tree}[$#{$self->{SIDS}->{$sid}->{tree}}]}, 0;
149 312 push @{$self->{SIDS}->{$sid}->{tree}[$#{$self->{SIDS}->{$sid}->{tree}}]}, $cdata;
150 }
151 }
152  
153  
154 ##############################################################################
155 #
156 # _handle_close - when we see a close tag we need to pop the last element
157 # from the list and push it onto the end of the previous
158 # element. This is how we build our hierarchy.
159 #
160 ##############################################################################
161 sub _handle_close
162 {
163 242 ▶ my $self;
Subroutine _handle_close: called CC=12 SCAR=27.1
164 242 ▶ $self = $_[0] if (ref($_[0]) eq "XML::Stream::Parser");
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "XML::Stream::Parser" 124 118 242
165 242 ▶ $self = shift unless (ref($_[0]) eq "XML::Stream::Parser");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "XML::Stream::Parser" 118 124 242
166 242 my ($sax, $tag) = @_;
167 242 my $sid = $sax->getSID();
168  
169 242 $self->debug(2,"_handle_close: sid($sid) sax($sax) tag($tag)");
170  
171 242 my $CLOSED = pop @{$self->{SIDS}->{$sid}->{tree}};
172  
173 242 $self->debug(2,"_handle_close: check(",$#{$self->{SIDS}->{$sid}->{tree}},")");
174  
175 242 ▶ if ($#{$self->{SIDS}->{$sid}->{tree}} == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$self{"SIDS"}{$sid}{"tree"};} == -1) 2 240 242
176 {
177 2 ▶ if ($self->{SIDS}->{$sid}->{rootTag} ne $tag)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"SIDS"}{$sid}{"rootTag"} ne $tag) 0 2 2
178 {
179 0 $self->{SIDS}->{$sid}->{streamerror} = "Root tag mis-match: <$self->{SIDS}->{$sid}->{rootTag}> ... </$tag>\n";
180 }
181 2 return;
182 }
183  
184 240 ▶ if($#{$self->{SIDS}->{$sid}->{tree}} < 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$self{'SIDS'}{$sid}{'tree'};} < 1) { } 38 202 240
185 {
186  
187 38 push @{$self->{SIDS}->{$sid}->{tree}}, $CLOSED;
188  
189 38 ▶ if (ref($self) ne "XML::Stream::Parser")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne "XML::Stream::Parser") 34 4 38
190 {
191 34 my $stream_prefix = $self->StreamPrefix($sid);
192  
193 34 ▶ if(defined($self->{SIDS}->{$sid}->{tree}->[0]) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'SIDS'}{$sid}{'tree'}[0] and $self->{'SIDS'}{$sid}{'tree'}[0] =~ /^$stream_prefix\:/) { } 0 34 34
Condition33%1/3
defined $self->{"SIDS"}{$sid}{"tree"}[0] and $self->{"SIDS"}{$sid}{"tree"}[0] =~ /^$stream_prefix\:/
!ll&&!rl&&r
0340
MC/DC0%0/2
defined $self->{"SIDS"}{$sid}{"tree"}[0] and $self->{"SIDS"}{$sid}{"tree"}[0] =~ /^$stream_prefix\:/
defined $self->{"SIDS"}{$sid}{"tree"}[0] $self->{"SIDS"}{$sid}{"tree"}[0] =~ /^$stream_prefix\:/
Truth table33%1/3
A and B
A= defined $self->{"SIDS"}{$sid}{"tree"}[0]B= $self->{"SIDS"}{$sid}{"tree"}[0] =~ /^$stream_prefix\:/
ABresult
0X0
100
111
194 ($self->{SIDS}->{$sid}->{tree}->[0] =~ /^${stream_prefix}\:/))
195 {
196 0 my @tree = @{$self->{SIDS}->{$sid}->{tree}};
197 0 $self->{SIDS}->{$sid}->{tree} = [];
198 0 $self->ProcessStreamPacket($sid,\@tree);
199 }
200 else
201 {
202 34 my @tree = @{$self->{SIDS}->{$sid}->{tree}};
203 34 $self->{SIDS}->{$sid}->{tree} = [];
204
205 34 my @special =
206 &XML::Stream::XPath(
207 \@tree,
208 '[@xmlns="'.&XML::Stream::ConstXMLNS("xmpp-sasl").'" or @xmlns="'.&XML::Stream::ConstXMLNS("xmpp-tls").'"]'
209 );
210 34 ▶ if ($#special > -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($#special > -1) { } 0 34 34
211 {
212 0 my $xmlns = &GetXMLData("value",\@tree,"","xmlns");
213  
214 0 ▶ $self->ProcessSASLPacket($sid,\@tree)
Branch: 1 branch
BranchTrueFalseTotal
if $xmlns eq &XML::Stream::ConstXMLNS("xmpp-sasl") 0 0 0
215 if ($xmlns eq &XML::Stream::ConstXMLNS("xmpp-sasl"));
216 0 ▶ $self->ProcessTLSPacket($sid,\@tree)
Branch: 1 branch
BranchTrueFalseTotal
if $xmlns eq &XML::Stream::ConstXMLNS("xmpp-tls") 0 0 0
217 if ($xmlns eq &XML::Stream::ConstXMLNS("xmpp-tls"));
218 }
219 else
220 {
221 34 &{$self->{CB}->{node}}($sid,\@tree);
222 }
223 }
224 }
225 }
226 else
227 {
228 202 push @{$self->{SIDS}->{$sid}->{tree}[$#{$self->{SIDS}->{$sid}->{tree}}]}, $CLOSED;
229 }
230 }
231  
232  
233 ##############################################################################
234 #
235 # SetXMLData - takes a host of arguments and sets a portion of the specified
236 # XML::Parser::Tree object with that data. The function works
237 # in two modes "single" or "multiple". "single" denotes that
238 # the function should locate the current tag that matches this
239 # data and overwrite it's contents with data passed in.
240 # "multiple" denotes that a new tag should be created even if
241 # others exist.
242 #
243 # type - single or multiple
244 # XMLTree - pointer to XML::Stream Tree object
245 # tag - name of tag to create/modify (if blank assumes
246 # working with top level tag)
247 # data - CDATA to set for tag
248 # attribs - attributes to ADD to tag
249 #
250 ##############################################################################
251 sub SetXMLData
252 {
253 0 ▶ my ($type,$XMLTree,$tag,$data,$attribs) = @_;
Subroutine SetXMLData: not called CC=13 SCAR=52.0
Pod undocumented
254 0 my ($key);
255  
256 0 ▶ if ($tag ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($tag ne '') { } 0 0 0
257 {
258 0 ▶ if ($type eq "single")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "single") 0 0 0
259 {
260 0 my ($child);
261 0 foreach $child (1..$#{$$XMLTree[1]})
262 {
263 0 ▶ if ($$XMLTree[1]->[$child] eq $tag)
Branch: 1 branch
BranchTrueFalseTotal
if ($XMLTree->[1][$child] eq $tag) 0 0 0
264 {
265 0 ▶ if ($data ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne "") 0 0 0
266 {
267 #todo: add code to handle writing the cdata again and appending it.
268 0 $$XMLTree[1]->[$child+1]->[1] = 0;
269 0 $$XMLTree[1]->[$child+1]->[2] = $data;
270 }
271 0 foreach $key (keys(%{$attribs}))
272 {
273 0 $$XMLTree[1]->[$child+1]->[0]->{$key} = $$attribs{$key};
274 }
275 0 return;
276 }
277 }
278 }
279 0 $$XMLTree[1]->[($#{$$XMLTree[1]}+1)] = $tag;
280 0 $$XMLTree[1]->[($#{$$XMLTree[1]}+1)]->[0] = {};
281 0 foreach $key (keys(%{$attribs}))
282 {
283 0 $$XMLTree[1]->[$#{$$XMLTree[1]}]->[0]->{$key} = $$attribs{$key};
284 }
285 0 ▶ if ($data ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne "") 0 0 0
286 {
287 0 $$XMLTree[1]->[$#{$$XMLTree[1]}]->[1] = 0;
288 0 $$XMLTree[1]->[$#{$$XMLTree[1]}]->[2] = $data;
289 }
290 }
291 else
292 {
293 0 foreach $key (keys(%{$attribs}))
294 {
295 0 $$XMLTree[1]->[0]->{$key} = $$attribs{$key};
296 }
297 0 ▶ if ($data ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne "") 0 0 0
298 {
299 0 ▶ if (($#{$$XMLTree[1]} > 0) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$XMLTree[1];} > 0 and $XMLTree->[1][$#{$$XMLTree[1];} - 1] eq '0') { } 0 0 0
Condition0%0/3
$#{$$XMLTree[1];} > 0 and $XMLTree->[1][$#{$$XMLTree[1];} - 1] eq 0
!ll&&!rl&&r
000
MC/DC0%0/2
$#{$$XMLTree[1];} > 0 and $XMLTree->[1][$#{$$XMLTree[1];} - 1] eq 0
$#{$$XMLTree[1];} > 0 $XMLTree->[1][$#{$$XMLTree[1];} - 1] eq 0
Truth table0%0/3
A and B
A= $#{$$XMLTree[1];} > 0B= $XMLTree->[1][$#{$$XMLTree[1];} - 1] eq 0
ABresult
0X0
100
111
300 0 ($$XMLTree[1]->[($#{$$XMLTree[1]}-1)] eq "0"))
301 {
302 0 $$XMLTree[1]->[$#{$$XMLTree[1]}] .= $data;
303 }
304 else
305 {
306 0 $$XMLTree[1]->[($#{$$XMLTree[1]}+1)] = 0;
307 0 $$XMLTree[1]->[($#{$$XMLTree[1]}+1)] = $data;
308 }
309 }
310 }
311 }
312  
313  
314 ##############################################################################
315 #
316 # GetXMLData - takes a host of arguments and returns various data structures
317 # that match them.
318 #
319 # type - "existence" - returns 1 or 0 if the tag exists in the
320 # top level.
321 # "value" - returns either the CDATA of the tag, or the
322 # value of the attribute depending on which is
323 # sought. This ignores any mark ups to the data
324 # and just returns the raw CDATA.
325 # "value array" - returns an array of strings representing
326 # all of the CDATA in the specified tag.
327 # This ignores any mark ups to the data
328 # and just returns the raw CDATA.
329 # "tree" - returns an XML::Parser::Tree object with the
330 # specified tag as the root tag.
331 # "tree array" - returns an array of XML::Parser::Tree
332 # objects each with the specified tag as
333 # the root tag.
334 # "child array" - returns a list of all children nodes
335 # not including CDATA nodes.
336 # "attribs" - returns a hash with the attributes, and
337 # their values, for the things that match
338 # the parameters
339 # "count" - returns the number of things that match
340 # the arguments
341 # "tag" - returns the root tag of this tree
342 # XMLTree - pointer to XML::Parser::Tree object
343 # tag - tag to pull data from. If blank then the top level
344 # tag is accessed.
345 # attrib - attribute value to retrieve. Ignored for types
346 # "value array", "tree", "tree array". If paired
347 # with value can be used to filter tags based on
348 # attributes and values.
349 # value - only valid if an attribute is supplied. Used to
350 # filter for tags that only contain this attribute.
351 # Useful to search through multiple tags that all
352 # reference different name spaces.
353 #
354 ##############################################################################
355 sub GetXMLData
356 {
357 873 ▶ my ($type,$XMLTree,$tag,$attrib,$value) = @_;
Subroutine GetXMLData: called CC=56 SCAR=52.3
Pod undocumented
358  
359 873 ▶ $tag = "" if !defined($tag);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 444 429 873
360 873 ▶ $attrib = "" if !defined($attrib);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrib 765 108 873
361 873 ▶ $value = "" if !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 873 0 873
362  
363 873 my $skipthis = 0;
364  
365 #---------------------------------------------------------------------------
366 # Check if a child tag in the root tag is being requested.
367 #---------------------------------------------------------------------------
368 873 ▶ if ($tag ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($tag ne '') { } 321 552 873
369 {
370 321 my $count = 0;
371 321 my @array;
372 321 foreach my $child (1..$#{$$XMLTree[1]})
373 {
374 3616 ▶ next if (($child/2) !~ /\./);
Branch: 1 branch
BranchTrueFalseTotal
unless ($child / 2) =~ /\./ 1806 1810 3616
375 1810 ▶ if (($$XMLTree[1]->[$child] eq $tag) || ($tag eq "*"))
Branch: 1 branch
BranchTrueFalseTotal
if ($XMLTree->[1][$child] eq $tag or $tag eq "*") 1175 635 1810
Condition100%3/3
$XMLTree->[1][$child] eq $tag or $tag eq "*"
l!l&&r!l&&!r
1121063635
MC/DC100%2/2
$XMLTree->[1][$child] eq $tag or $tag eq "*"
$XMLTree->[1][$child] eq $tag $tag eq "*"
Truth table100%3/3
A or B
A= $XMLTree->[1][$child] eq $tagB= $tag eq "*"
ABresult
1X1
011
000
376 {
377 1175 ▶ next if (ref($$XMLTree[1]->[$child]) eq "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if ref $XMLTree->[1][$child] eq "ARRAY" 0 1175 1175
378  
379 #---------------------------------------------------------------------
380 # Filter out tags that do not contain the attribute and value.
381 #---------------------------------------------------------------------
382 1175 ▶ next if (($value ne "") && ($attrib ne "") && exists($$XMLTree[1]->[$child+1]->[0]->{$attrib}) && ($$XMLTree[1]->[$child+1]->[0]->{$attrib} ne $value));
Branch: 1 branch
BranchTrueFalseTotal
if $value ne "" and $attrib ne "" and exists $XMLTree->[1][$child + 1][0]{$attrib} and $XMLTree->[1][$child + 1][0]{$attrib} ne $value 0 1175 1175
Condition33%3/9
$value ne "" and $attrib ne "" and exists $XMLTree->[1][$child + 1][0]{$attrib} and $XMLTree->[1][$child + 1][0]{$attrib} ne $value
!ll&&!rl&&r
117500
$value ne "" and $attrib ne "" and exists $XMLTree->[1][$child + 1][0]{$attrib}
!ll&&!rl&&r
117500
$value ne "" and $attrib ne ""
!ll&&!rl&&r
117500
MC/DC0%0/4
$value ne "" and $attrib ne "" and exists $XMLTree->[1][$child + 1][0]{$attrib} and $XMLTree->[1][$child + 1][0]{$attrib} ne $value
$value ne "" $attrib ne "" exists $XMLTree->[1][$child + 1][0]{$attrib} $XMLTree->[1][$child + 1][0]{$attrib} ne $value
Truth table20%1/5
A and B and C and D
A= $value ne ""B= $attrib ne ""C= exists $XMLTree->[1][$child + 1][0]{$attrib}D= $XMLTree->[1][$child + 1][0]{$attrib} ne $value
ABCDresult
0XXX0
10XX0
110X0
11100
11111
383 1175 ▶ next if (($attrib ne "") && ((ref($$XMLTree[1]->[$child+1]) ne "ARRAY") || !exists($$XMLTree[1]->[$child+1]->[0]->{$attrib})));
Branch: 1 branch
BranchTrueFalseTotal
if $attrib ne "" and ref $XMLTree->[1][$child + 1] ne "ARRAY" || !exists $XMLTree->[1][$child + 1][0]{$attrib} 0 1175 1175
Condition16%1/6
$attrib ne "" and ref $XMLTree->[1][$child + 1] ne "ARRAY" || !exists $XMLTree->[1][$child + 1][0]{$attrib}
!ll&&!rl&&r
117500
ref $XMLTree->[1][$child + 1] ne "ARRAY" or not exists $XMLTree->[1][$child + 1][0]{$attrib}
l!l&&r!l&&!r
000
MC/DC0%0/3
$attrib ne "" and ref $XMLTree->[1][$child + 1] ne "ARRAY" || !exists $XMLTree->[1][$child + 1][0]{$attrib}
$attrib ne "" ref $XMLTree->[1][$child + 1] ne "ARRAY" not exists $XMLTree->[1][$child + 1][0]{$attrib}
Truth table25%1/4
A and B or not(C)
A= $attrib ne ""B= ref $XMLTree->[1][$child + 1] ne "ARRAY"C= not exists $XMLTree->[1][$child + 1][0]{$attrib}
ABCresult
0XX0
1000
11X1
1011
384  
385 #---------------------------------------------------------------------
386 # Check for existence
387 #---------------------------------------------------------------------
388 1175 ▶ if ($type eq "existence")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "existence") 0 1175 1175
389 {
390 0 return 1;
391 }
392
393 #---------------------------------------------------------------------
394 # Return the raw CDATA value without mark ups, or the value of the
395 # requested attribute.
396 #---------------------------------------------------------------------
397 1175 ▶ if ($type eq "value")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "value") 4 1171 1175
398 {
399 4 ▶ if ($attrib eq "")
Branch: 1 branch
BranchTrueFalseTotal
if ($attrib eq "") 4 0 4
400 {
401 4 my $str = "";
402 4 my $next = 0;
403 4 my $index;
404 4 foreach $index (1..$#{$$XMLTree[1]->[$child+1]}) {
405 8 ▶ if ($next == 1) { $next = 0; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($next == 1) 4 4 8
406 4 ▶ if ($$XMLTree[1]->[$child+1]->[$index] eq "0") {
Branch: 1 branch
BranchTrueFalseTotal
if ($XMLTree->[1][$child + 1][$index] eq 0) 4 0 4
407 4 $str .= $$XMLTree[1]->[$child+1]->[$index+1];
408 4 $next = 1;
409 }
410 }
411 4 return $str;
412 }
413 return $$XMLTree[1]->[$child+1]->[0]->{$attrib}
414 0 ▶ if (exists $$XMLTree[1]->[$child+1]->[0]->{$attrib});
Branch: 1 branch
BranchTrueFalseTotal
if exists $XMLTree->[1][$child + 1][0]{$attrib} 0 0 0
415 }
416 #---------------------------------------------------------------------
417 # Return an array of values that represent the raw CDATA without
418 # mark up tags for the requested tags.
419 #---------------------------------------------------------------------
420 1171 ▶ if ($type eq "value array")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "value array") 0 1171 1171
421 {
422 0 ▶ if ($attrib eq "")
Branch: 1 branch
BranchTrueFalseTotal
if ($attrib eq '') { } 0 0 0
423 {
424 0 my $str = "";
425 0 my $next = 0;
426 0 my $index;
427 0 foreach $index (1..$#{$$XMLTree[1]->[$child+1]})
428 {
429 0 ▶ if ($next == 1) { $next = 0; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($next == 1) 0 0 0
430 0 ▶ if ($$XMLTree[1]->[$child+1]->[$index] eq "0")
Branch: 1 branch
BranchTrueFalseTotal
if ($XMLTree->[1][$child + 1][$index] eq 0) 0 0 0
431 {
432 0 $str .= $$XMLTree[1]->[$child+1]->[$index+1];
433 0 $next = 1;
434 }
435 }
436 0 push(@array,$str);
437 }
438 else
439 {
440 push(@array,$$XMLTree[1]->[$child+1]->[0]->{$attrib})
441 0 ▶ if (exists $$XMLTree[1]->[$child+1]->[0]->{$attrib});
Branch: 1 branch
BranchTrueFalseTotal
if exists $XMLTree->[1][$child + 1][0]{$attrib} 0 0 0
442 }
443 }
444 #---------------------------------------------------------------------
445 # Return a pointer to a new XML::Parser::Tree object that has the
446 # requested tag as the root tag.
447 #---------------------------------------------------------------------
448 1171 ▶ if ($type eq "tree")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tree") 0 1171 1171
449 {
450 0 my @tree = ( $$XMLTree[1]->[$child] , $$XMLTree[1]->[$child+1] );
451 0 return @tree;
452 }
453 #---------------------------------------------------------------------
454 # Return an array of pointers to XML::Parser::Tree objects that have
455 # the requested tag as the root tags.
456 #---------------------------------------------------------------------
457 1171 ▶ if ($type eq "tree array")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tree array") 144 1027 1171
458 {
459 144 my @tree = ( $$XMLTree[1]->[$child] , $$XMLTree[1]->[$child+1] );
460 144 push(@array,\@tree);
461 }
462 #---------------------------------------------------------------------
463 # Return a count of the number of tags that match
464 #---------------------------------------------------------------------
465 1171 ▶ if ($type eq "count")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "count") 108 1063 1171
466 {
467 108 ▶ if ($$XMLTree[1]->[$child] eq "0")
Branch: 1 branch
BranchTrueFalseTotal
if ($XMLTree->[1][$child] eq 0) 0 108 108
468 {
469 0 $skipthis = 1;
470 0 next;
471 }
472 108 ▶ if ($skipthis == 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($skipthis == 1) 0 108 108
473 {
474 0 $skipthis = 0;
475 0 next;
476 }
477 108 $count++;
478 }
479 #---------------------------------------------------------------------
480 # Return a count of the number of tags that match
481 #---------------------------------------------------------------------
482 1171 ▶ if ($type eq "child array")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "child array") 919 252 1171
483 {
484 919 my @tree = ( $$XMLTree[1]->[$child] , $$XMLTree[1]->[$child+1] );
485 919 ▶ push(@array,\@tree) if ($tree[0] ne "0");
Branch: 1 branch
BranchTrueFalseTotal
if $tree[0] ne 0 405 514 919
486 }
487 #---------------------------------------------------------------------
488 # Return the attribute hash that matches this tag
489 #---------------------------------------------------------------------
490 1171 ▶ if ($type eq "attribs")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "attribs") 0 1171 1171
491 {
492 0 return (%{$$XMLTree[1]->[$child+1]->[0]});
493 }
494 }
495 }
496 #-------------------------------------------------------------------------
497 # If we are returning arrays then return array.
498 #-------------------------------------------------------------------------
499 317 ▶ if (($type eq "tree array") || ($type eq "value array") ||
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tree array" or $type eq "value array" or $type eq "child array") 200 58 258
Condition83%5/6
$type eq "tree array" or $type eq "value array" or $type eq "child array"
l!l&&r!l&&!r
5920058
$type eq "tree array" or $type eq "value array"
l!l&&r!l&&!r
590258
MC/DC66%2/3
$type eq "tree array" or $type eq "value array" or $type eq "child array"
$type eq "tree array" $type eq "value array" $type eq "child array"
Truth table75%3/4
A or B or C
A= $type eq "tree array"B= $type eq "value array"C= $type eq "child array"
ABCresult
1XX1
01X1
0011
0000
500 ($type eq "child array"))
501 {
502 259 return @array;
503 }
504  
505 #-------------------------------------------------------------------------
506 # If we are returning then count, then do so
507 #-------------------------------------------------------------------------
508 58 ▶ if ($type eq "count")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "count") 58 0 58
509 {
510 58 return $count;
511 }
512 }
513 else
514 {
515 #-------------------------------------------------------------------------
516 # This is the root tag, so handle things a level up.
517 #-------------------------------------------------------------------------
518  
519 #-------------------------------------------------------------------------
520 # Return the raw CDATA value without mark ups, or the value of the
521 # requested attribute.
522 #-------------------------------------------------------------------------
523 552 ▶ if ($type eq "value")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "value") 125 427 552
524 {
525 125 ▶ if ($attrib eq "")
Branch: 1 branch
BranchTrueFalseTotal
if ($attrib eq "") 17 108 125
526 {
527 17 my $str = "";
528 17 my $next = 0;
529 17 my $index;
530 17 foreach $index (1..$#{$$XMLTree[1]})
531 {
532 38 ▶ if ($next == 1) { $next = 0; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($next == 1) 18 20 38
533 20 ▶ if ($$XMLTree[1]->[$index] eq "0")
Branch: 1 branch
BranchTrueFalseTotal
if ($XMLTree->[1][$index] eq 0) 18 2 20
534 {
535 18 $str .= $$XMLTree[1]->[$index+1];
536 18 $next = 1;
537 }
538 }
539 17 return $str;
540 }
541 return $$XMLTree[1]->[0]->{$attrib}
542 108 ▶ if (exists $$XMLTree[1]->[0]->{$attrib});
Branch: 1 branch
BranchTrueFalseTotal
if exists $XMLTree->[1][0]{$attrib} 30 78 108
543 }
544 #-------------------------------------------------------------------------
545 # Return a pointer to a new XML::Parser::Tree object that has the
546 # requested tag as the root tag.
547 #-------------------------------------------------------------------------
548 505 ▶ if ($type eq "tree")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tree") 0 505 505
549 {
550 0 my @tree = @{$$XMLTree};
551 0 return @tree;
552 }
553  
554 #-------------------------------------------------------------------------
555 # Return the 1 if the specified attribute exists in the root tag.
556 #-------------------------------------------------------------------------
557 505 ▶ if ($type eq "existence")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "existence") 0 505 505
558 {
559 0 ▶ return 1 if (($attrib ne "") && (exists($$XMLTree[1]->[0]->{$attrib})));
Branch: 1 branch
BranchTrueFalseTotal
if $attrib ne "" and exists $XMLTree->[1][0]{$attrib} 0 0 0
Condition0%0/3
$attrib ne "" and exists $XMLTree->[1][0]{$attrib}
!ll&&!rl&&r
000
MC/DC0%0/2
$attrib ne "" and exists $XMLTree->[1][0]{$attrib}
$attrib ne "" exists $XMLTree->[1][0]{$attrib}
Truth table0%0/3
A and B
A= $attrib ne ""B= exists $XMLTree->[1][0]{$attrib}
ABresult
0X0
100
111
560 }
561  
562 #-------------------------------------------------------------------------
563 # Return the attribute hash that matches this tag
564 #-------------------------------------------------------------------------
565 505 ▶ if ($type eq "attribs")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "attribs") 1 504 505
566 {
567 1 return %{$$XMLTree[1]->[0]};
568 }
569 #-------------------------------------------------------------------------
570 # Return the tag of this node
571 #-------------------------------------------------------------------------
572 504 ▶ if ($type eq "tag")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tag") 426 78 504
573 {
574 426 return $$XMLTree[0];
575 }
576 }
577 #---------------------------------------------------------------------------
578 # Return 0 if this was a request for existence, or "" if a request for
579 # a "value", or [] for "tree", "value array", and "tree array".
580 #---------------------------------------------------------------------------
581 78 ▶ return 0 if ($type eq "existence");
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "existence" 0 78 78
582 78 ▶ return "" if ($type eq "value");
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "value" 78 0 78
583 0 return [];
584 }
585  
586  
587 ##############################################################################
588 #
589 # BuildXML - takes an XML::Parser::Tree object and builds the XML string
590 # that it represents.
591 #
592 ##############################################################################
593 sub BuildXML
594 {
595 126 ▶ my ($parseTree,$rawXML) = @_;
Subroutine BuildXML: called CC=10 SCAR=23.1
Pod undocumented
596  
597 126 ▶ return "" if $#{$parseTree} == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $#{$parseTree;} == -1 0 126 126
598  
599 126 my $str = "";
600 126 ▶ if (ref($parseTree->[0]) eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parseTree->[0] eq "") 126 0 126
601 {
602 126 ▶ if ($parseTree->[0] eq "0")
Branch: 1 branch
BranchTrueFalseTotal
if ($parseTree->[0] eq 0) 68 58 126
603 {
604 68 return &XML::Stream::EscapeXML($parseTree->[1]);
605 }
606  
607 58 $str = "<".$parseTree->[0];
608 58 foreach my $att (sort {$a cmp $b} keys(%{$parseTree->[1]->[0]}))
609 {
610 18 $str .= " ".$att."='".&XML::Stream::EscapeXML($parseTree->[1]->[0]->{$att})."'";
611 }
612  
613 58 ▶ if (($#{$parseTree->[1]} > 0) || (defined($rawXML) && ($rawXML ne "")))
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$parseTree[1];} > 0 or defined $rawXML and $rawXML ne '') { } 54 4 58
Condition83%5/6
$#{$$parseTree[1];} > 0 or defined $rawXML and $rawXML ne ""
l!l&&r!l&&!r
5314
defined $rawXML and $rawXML ne ""
!ll&&!rl&&r
401
MC/DC66%2/3
$#{$$parseTree[1];} > 0 or defined $rawXML and $rawXML ne ""
$#{$$parseTree[1];} > 0 defined $rawXML $rawXML ne ""
Truth table75%3/4
A or B and C
A= $#{$$parseTree[1];} > 0B= defined $rawXMLC= $rawXML ne ""
ABCresult
1XX1
0111
00X0
0100
614 {
615 54 $str .= ">";
616
617 54 my $index = 1;
618 54 ▶ while($index <= $#{$parseTree->[1]})
Branch: 1 branch
BranchTrueFalseTotal
if ($index <= $#{$$parseTree[1];}) 109 54 163
619 {
620 109 my @newTree = ( $parseTree->[1]->[$index], $parseTree->[1]->[$index+1] );
621 109 $str .= &XML::Stream::Tree::BuildXML(\@newTree);
622 109 $index += 2;
623 }
624
625 54 ▶ $str .= $rawXML if defined($rawXML);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rawXML 17 37 54
626 54 $str .= "</".$parseTree->[0].">";
627 }
628 else
629 {
630 4 $str .= "/>";
631 }
632  
633 }
634  
635 58 return $str;
636 }
637  
638  
639 ##############################################################################
640 #
641 # XML2Config - takes an XML data tree and turns it into a hash of hashes.
642 # This only works for certain kinds of XML trees like this:
643 #
644 # <foo>
645 # <bar>1</bar>
646 # <x>
647 # <y>foo</y>
648 # </x>
649 # <z>5</z>
650 # </foo>
651 #
652 # The resulting hash would be:
653 #
654 # $hash{bar} = 1;
655 # $hash{x}->{y} = "foo";
656 # $hash{z} = 5;
657 #
658 # Good for config files.
659 #
660 ##############################################################################
661 sub XML2Config
662 {
663 59 ▶ my ($XMLTree) = @_;
Subroutine XML2Config: called CC=5 SCAR=16.1
Pod undocumented
664  
665 59 my %hash;
666 59 foreach my $tree (&XML::Stream::GetXMLData("tree array",$XMLTree,"*"))
667 {
668 144 ▶ if ($tree->[0] eq "0")
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->[0] eq '0') { } 86 58 144
669 {
670 86 ▶ return $tree->[1] unless ($tree->[1] =~ /^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $tree->[1] =~ /^\s*$/ 23 63 86
671 }
672 else
673 {
674 58 ▶ if (&XML::Stream::GetXMLData("count",$XMLTree,$tree->[0]) > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (&XML::Stream::GetXMLData('count', $XMLTree, $tree->[0]) > 1) { } 20 38 58
675 {
676 20 push(@{$hash{$tree->[0]}},&XML::Stream::XML2Config($tree));
677 }
678 else
679 {
680 38 $hash{$tree->[0]} = &XML::Stream::XML2Config($tree);
681 }
682 }
683 }
684 36 return \%hash;
685 }
686  
687  
688 12 1;
« blib/lib/XML/Stream/Node.pm ↑ Summary blib/lib/XML/Stream/Parser.pm »