blib/lib/XML/Stream/Node.pm

filter: Statementstmt 71.1% 217 / 305 Branchbran 63.1% 101 / 160 Conditioncond 64.4% 29 / 45 MC/DCmcdc 41.6% 10 / 24 Subroutinesub 77.4% 24 / 31 Podpod 0.0% 0 / 25 totaltotal 64.5% 381 / 590 CC 110 SCAR 37.2
CC
110
Cov
68%
GetXMLData
99.5
SetXMLData
72.0
debug
56.0
CRAP 41.2
« blib/lib/XML/Stream/Tree.pm ↑ Summary blib/lib/XML/Stream/XPath/Query.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::Node;
24  
25 =head1 NAME
26  
27 XML::Stream::Node - 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 This creates a hierarchy of Perl objects and provides various methods
36 to manipulate the structure of the tree. It is much like the C library
37 libxml.
38  
39 =head1 FORMAT
40  
41 The result of parsing:
42  
43 <foo><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo>
44  
45 would be:
46  
47 [ tag: foo
48 att: {}
49 children: [ tag: head
50 att: {id=>"a"}
51 children: [ tag: "__xmlstream__:node:cdata"
52 children: "Hello "
53 ]
54 [ tag: em
55 children: [ tag: "__xmlstream__:node:cdata"
56 children: "there"
57 ]
58 ]
59 ]
60 [ tag: bar
61 children: [ tag: "__xmlstream__:node:cdata"
62 children: "Howdy "
63 ]
64 [ tag: ref
65 ]
66 ]
67 [ tag: "__xmlstream__:node:cdata"
68 children: "do"
69 ]
70 ]
71  
72 =head1 METHODS
73  
74 new() - creates a new node. If you specify tag, then the root
75 new(tag) tag is set. If you specify data, then cdata is added
76 new(tag,cdata) to the node as well. Returns the created node.
77  
78 get_tag() - returns the root tag of the node.
79  
80 set_tag(tag) - set the root tag of the node to tag.
81  
82 add_child(node) - adds the specified node as a child to the current
83 add_child(tag) node, or creates a new node with the specified tag
84 add_child(tag,cdata) as the root node. Returns the node added.
85  
86 remove_child(node) - removes the child node from the current node.
87
88 remove_cdata() - removes all of the cdata children from the current node.
89  
90 add_cdata(string) - adds the string as cdata onto the current nodes
91 child list.
92  
93 get_cdata() - returns all of the cdata children concatenated together
94 into one string.
95  
96 get_attrib(attrib) - returns the value of the attrib if it is valid,
97 or returns undef is attrib is not a real
98 attribute.
99  
100 put_attrib(hash) - for each key/value pair specified, create an
101 attribute in the node.
102  
103 remove_attrib(attrib) - remove the specified attribute from the node.
104  
105 add_raw_xml(string,[string,...]) - directly add a string into the XML
106 packet as the last child, with no
107 translation.
108  
109 get_raw_xml() - return all of the XML in a single string, undef if there
110 is no raw XML to include.
111  
112 remove_raw_xml() - remove all raw XML strings.
113  
114 children() - return all of the children of the node in a list.
115  
116 attrib() - returns a hash containing all of the attributes on this
117 node.
118  
119 copy() - return a recursive copy of the node.
120  
121 XPath(path) - run XML::Stream::XPath on this node.
122
123 XPathCheck(path) - run XML::Stream::XPath on this node and return 1 or 0
124 to see if it matches or not.
125  
126 GetXML() - return the node in XML string form.
127  
128 =head1 AUTHOR
129  
130 By Ryan Eatmon in June 2002 for http://jabber.org/
131  
132 Currently maintained by Darian Anthony Patrick.
133  
134 =head1 COPYRIGHT
135  
136 Copyright (C) 1998-2004 Jabber Software Foundation http://jabber.org/
137  
138 This module licensed under the LGPL, version 2.1.
139  
140 =cut
141  
142 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
144 12 ▶use vars qw( $VERSION $LOADED );
Subroutine BEGIN: called CC=1 SCAR=0.0
145  
146 12 $VERSION = "1.23_06";
147 12 $LOADED = 1;
148  
149 sub new
150 {
151 768 ▶ my $proto = shift;
Subroutine new: called CC=5 SCAR=16.1
Pod undocumented
152 768 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
07680
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
153  
154 768 ▶ if (ref($_[0]) eq "XML::Stream::Node")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "XML::Stream::Node") 204 564 768
155 {
156 204 return $_[0];
157 }
158  
159 564 my $self = {};
160 564 bless($self, $proto);
161  
162 564 my ($tag,$data) = @_;
163  
164 564 ▶ $self->set_tag($tag) if defined($tag);
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag 560 4 564
165 564 ▶ $self->add_cdata($data) if defined($data);
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 2 562 564
166 564 $self->remove_raw_xml();
167  
168 564 return $self;
169 }
170  
171  
172 sub debug
173 {
174 0 ▶ my $self = shift;
Subroutine debug: not called CC=7 SCAR=40.3
Pod undocumented
175 0 my ($indent) = @_;
176  
177 0 ▶ $indent = "" unless defined($indent);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indent 0 0 0
178  
179 0 ▶ if ($self->{TAG} eq "__xmlstream__:node:cdata")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'TAG'} eq '__xmlstream__:node:cdata') { } 0 0 0
180 {
181 0 print $indent,"cdata(",join("",@{$self->{CHILDREN}}),")\n";
182 }
183 else
184 {
185 0 print $indent,"packet($self):\n";
186 0 print $indent,"tag: <$self->{TAG}\n";
187 0 ▶ if (scalar(keys(%{$self->{ATTRIBS}})) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$self{"ATTRIBS"};} > 0) 0 0 0
188 {
189 0 print $indent,"attribs:\n";
190 0 foreach my $key (sort {$a cmp $b} keys(%{$self->{ATTRIBS}}))
191 {
192 0 print $indent," $key = '$self->{ATTRIBS}->{$key}'\n";
193 }
194 }
195 0 ▶ if ($#{$self->{CHILDREN}} == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$self{'CHILDREN'};} == -1) { } 0 0 0
196 {
197 0 print $indent," />\n";
198 }
199 else
200 {
201 0 print $indent," >\n";
202 0 print $indent,"children:\n";
203 0 foreach my $child (@{$self->{CHILDREN}})
204 {
205 0 $child->debug($indent." ");
206 }
207 }
208 0 print $indent," </$self->{TAG}>\n";
209 }
210 }
211  
212  
213 sub children
214 {
215 715 ▶ my $self = shift;
Subroutine children: called CC=2 SCAR=6.9
Pod undocumented
216  
217 715 ▶ return () unless exists($self->{CHILDREN});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"CHILDREN"} 25 690 715
218 690 return @{$self->{CHILDREN}};
219 }
220  
221  
222 sub add_child
223 {
224 205 ▶ my $self = shift;
Subroutine add_child: called CC=1 SCAR=0.0
Pod undocumented
225  
226 205 my $child = XML::Stream::Node->new(@_);
227 205 push(@{$self->{CHILDREN}},$child);
228 205 return $child;
229 }
230  
231  
232 sub remove_child
233 {
234 0 ▶ my $self = shift;
Subroutine remove_child: not called CC=3 SCAR=24.8
Pod undocumented
235 0 my $child = shift;
236  
237 0 foreach my $index (0..$#{$self->{CHILDREN}})
238 {
239 0 ▶ if ($child == $self->{CHILDREN}->[$index])
Branch: 1 branch
BranchTrueFalseTotal
if ($child == $self->{"CHILDREN"}[$index]) 0 0 0
240 {
241 0 splice(@{$self->{CHILDREN}},$index,1);
242 0 last;
243 }
244 }
245 }
246  
247  
248 sub add_cdata
249 {
250 318 ▶ my $self = shift;
Subroutine add_cdata: called CC=2 SCAR=6.9
Pod undocumented
251 318 my $child = XML::Stream::Node->new("__xmlstream__:node:cdata");
252 318 foreach my $cdata (@_)
253 {
254 318 push(@{$child->{CHILDREN}},$cdata);
255 }
256 318 push(@{$self->{CHILDREN}},$child);
257 318 return $child;
258 }
259  
260  
261 sub get_cdata
262 {
263 23 ▶ my $self = shift;
Subroutine get_cdata: called CC=3 SCAR=11.0
Pod undocumented
264  
265 23 my $cdata = "";
266 23 foreach my $child (@{$self->{CHILDREN}})
267 {
268 25 ▶ $cdata .= join("",$child->children())
Branch: 1 branch
BranchTrueFalseTotal
if $child->get_tag eq "__xmlstream__:node:cdata" 24 1 25
269 if ($child->get_tag() eq "__xmlstream__:node:cdata");
270 }
271  
272 23 return $cdata;
273 }
274  
275  
276 sub remove_cdata
277 {
278 0 ▶ my $self = shift;
Subroutine remove_cdata: not called CC=4 SCAR=30.0
Pod undocumented
279  
280 0 my @remove = ();
281 0 foreach my $index (0..$#{$self->{CHILDREN}})
282 {
283 0 ▶ if ($self->{CHILDREN}->[$index]->get_tag() eq "__xmlstream__:node:cdata")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"CHILDREN"}[$index]->get_tag eq "__xmlstream__:node:cdata") 0 0 0
284 {
285  
286 0 unshift(@remove,$index);
287 }
288 }
289 0 foreach my $index (@remove)
290 {
291 0 splice(@{$self->{CHILDREN}},$index,1);
292 }
293 }
294  
295  
296 sub attrib
297 {
298 127 ▶ my $self = shift;
Subroutine attrib: called CC=2 SCAR=6.9
Pod undocumented
299 127 ▶ return () unless exists($self->{ATTRIBS});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"ATTRIBS"} 81 46 127
300 46 return %{$self->{ATTRIBS}};
301 }
302  
303  
304 sub get_attrib
305 {
306 223 ▶ my $self = shift;
Subroutine get_attrib: called CC=2 SCAR=6.9
Pod undocumented
307 223 my ($key) = @_;
308  
309 223 ▶ return unless exists($self->{ATTRIBS}->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"ATTRIBS"}{$key} 91 132 223
310 132 return $self->{ATTRIBS}->{$key};
311 }
312  
313  
314 sub put_attrib
315 {
316 244 ▶ my $self = shift;
Subroutine put_attrib: called CC=2 SCAR=6.9
Pod undocumented
317 244 my (%att) = @_;
318  
319 244 foreach my $key (keys(%att))
320 {
321 76 $self->{ATTRIBS}->{$key} = $att{$key};
322 }
323 }
324  
325  
326 sub remove_attrib
327 {
328 0 ▶ my $self = shift;
Subroutine remove_attrib: not called CC=2 SCAR=17.9
Pod undocumented
329 0 my ($key) = @_;
330  
331 0 ▶ return unless exists($self->{ATTRIBS}->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"ATTRIBS"}{$key} 0 0 0
332 0 delete($self->{ATTRIBS}->{$key});
333 }
334  
335  
336 sub add_raw_xml
337 {
338 17 ▶ my $self = shift;
Subroutine add_raw_xml: called CC=1 SCAR=0.0
Pod undocumented
339 17 my (@raw) = @_;
340  
341 17 push(@{$self->{RAWXML}},@raw);
342 }
343  
344 sub get_raw_xml
345 {
346 159 ▶ my $self = shift;
Subroutine get_raw_xml: called CC=2 SCAR=6.9
Pod undocumented
347  
348 159 ▶ return if ($#{$self->{RAWXML}} == -1);
Branch: 1 branch
BranchTrueFalseTotal
if $#{$$self{"RAWXML"};} == -1 106 53 159
349 53 return join("",@{$self->{RAWXML}});
350 }
351  
352  
353 sub remove_raw_xml
354 {
355 564 ▶ my $self = shift;
Subroutine remove_raw_xml: called CC=1 SCAR=0.0
Pod undocumented
356 564 $self->{RAWXML} = [];
357 }
358  
359  
360 sub get_tag
361 {
362 3972 ▶ my $self = shift;
Subroutine get_tag: called CC=1 SCAR=0.0
Pod undocumented
363  
364 3972 return $self->{TAG};
365 }
366  
367  
368 sub set_tag
369 {
370 564 ▶ my $self = shift;
Subroutine set_tag: called CC=1 SCAR=0.0
Pod undocumented
371 564 my ($tag) = @_;
372  
373 564 $self->{TAG} = $tag;
374 }
375  
376  
377 sub XPath
378 {
379 0 ▶ my $self = shift;
Subroutine XPath: not called CC=3 SCAR=24.8
Pod undocumented
380 0 my @results = &XML::Stream::XPath($self,@_);
381 0 ▶ return unless ($#results > -1);
Branch: 1 branch
BranchTrueFalseTotal
unless $#results > -1 0 0 0
382 0 ▶ return $results[0] unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
383 0 return @results;
384 }
385  
386  
387 sub XPathCheck
388 {
389 0 ▶ my $self = shift;
Subroutine XPathCheck: not called CC=1 SCAR=6.9
Pod undocumented
390 0 return &XML::Stream::XPathCheck($self,@_);
391 }
392  
393  
394 sub GetXML
395 {
396 5 ▶ my $self = shift;
Subroutine GetXML: called CC=1 SCAR=0.0
Pod undocumented
397  
398 5 return &BuildXML($self,@_);
399 }
400  
401  
402 sub copy
403 {
404 3 ▶ my $self = shift;
Subroutine copy: called CC=3 SCAR=11.0
Pod undocumented
405  
406 3 my $new_node = XML::Stream::Node->new();
407 3 $new_node->set_tag($self->get_tag());
408 3 $new_node->put_attrib($self->attrib());
409  
410 3 foreach my $child ($self->children())
411 {
412 5 ▶ if ($child->get_tag() eq "__xmlstream__:node:cdata")
Branch: 1 branch
BranchTrueFalseTotal
if ($child->get_tag eq '__xmlstream__:node:cdata') { } 4 1 5
413 {
414 4 $new_node->add_cdata($child->children());
415 }
416 else
417 {
418 1 $new_node->add_child($child->copy());
419 }
420 }
421  
422 3 return $new_node;
423 }
424  
425  
426  
427  
428  
429 ##############################################################################
430 #
431 # _handle_element - handles the main tag elements sent from the server.
432 # On an open tag it creates a new XML::Parser::Node so
433 # that _handle_cdata and _handle_element can add data
434 # and tags to it later.
435 #
436 ##############################################################################
437 sub _handle_element
438 {
439 240 ▶ my $self;
Subroutine _handle_element: called CC=4 SCAR=13.9
440 240 ▶ $self = $_[0] if (ref($_[0]) eq "XML::Stream::Parser");
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "XML::Stream::Parser" 118 122 240
441 240 ▶ $self = shift unless (ref($_[0]) eq "XML::Stream::Parser");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "XML::Stream::Parser" 122 118 240
442 240 my ($sax, $tag, %att) = @_;
443 240 my $sid = $sax->getSID();
444  
445 240 $self->debug(2,"Node: _handle_element: sid($sid) sax($sax) tag($tag) att(",%att,")");
446  
447 240 my $node = XML::Stream::Node->new($tag);
448 240 $node->put_attrib(%att);
449  
450 240 $self->debug(2,"Node: _handle_element: check(",$#{$self->{SIDS}->{$sid}->{node}},")");
451  
452 240 ▶ if ($#{$self->{SIDS}->{$sid}->{node}} >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$self{"SIDS"}{$sid}{"node"};} >= 0) 203 37 240
453 {
454 203 $self->{SIDS}->{$sid}->{node}->[$#{$self->{SIDS}->{$sid}->{node}}]->
455 add_child($node);
456 }
457  
458 240 push(@{$self->{SIDS}->{$sid}->{node}},$node);
459 }
460  
461  
462 ##############################################################################
463 #
464 # _handle_cdata - handles the CDATA that is encountered. Also, in the
465 # spirit of XML::Parser::Node it combines any sequential
466 # CDATA into one tag.
467 #
468 ##############################################################################
469 sub _handle_cdata
470 {
471 346 ▶ my $self;
Subroutine _handle_cdata: called CC=4 SCAR=13.9
472 346 ▶ $self = $_[0] if (ref($_[0]) eq "XML::Stream::Parser");
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "XML::Stream::Parser" 172 174 346
473 346 ▶ $self = shift unless (ref($_[0]) eq "XML::Stream::Parser");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "XML::Stream::Parser" 174 172 346
474 346 my ($sax, $cdata) = @_;
475 346 my $sid = $sax->getSID();
476  
477 346 $self->debug(2,"Node: _handle_cdata: sid($sid) sax($sax) cdata($cdata)");
478  
479 346 ▶ return if ($#{$self->{SIDS}->{$sid}->{node}} == -1);
Branch: 1 branch
BranchTrueFalseTotal
if $#{$$self{"SIDS"}{$sid}{"node"};} == -1 36 310 346
480  
481 310 $self->debug(2,"Node: _handle_cdata: sax($sax) cdata($cdata)");
482  
483 310 $self->{SIDS}->{$sid}->{node}->[$#{$self->{SIDS}->{$sid}->{node}}]->
484 add_cdata($cdata);
485 }
486  
487  
488 ##############################################################################
489 #
490 # _handle_close - when we see a close tag we need to pop the last element
491 # from the list and push it onto the end of the previous
492 # element. This is how we build our hierarchy.
493 #
494 ##############################################################################
495 sub _handle_close
496 {
497 242 ▶ my $self;
Subroutine _handle_close: called CC=12 SCAR=25.6
498 242 ▶ $self = $_[0] if (ref($_[0]) eq "XML::Stream::Parser");
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "XML::Stream::Parser" 118 124 242
499 242 ▶ $self = shift unless (ref($_[0]) eq "XML::Stream::Parser");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "XML::Stream::Parser" 124 118 242
500 242 my ($sax, $tag) = @_;
501 242 my $sid = $sax->getSID();
502  
503 242 $self->debug(2,"Node: _handle_close: sid($sid) sax($sax) tag($tag)");
504  
505 242 $self->debug(2,"Node: _handle_close: check(",$#{$self->{SIDS}->{$sid}->{node}},")");
506  
507 242 ▶ if ($#{$self->{SIDS}->{$sid}->{node}} == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$self{"SIDS"}{$sid}{"node"};} == -1) 2 240 242
508 {
509 2 $self->debug(2,"Node: _handle_close: rootTag($self->{SIDS}->{$sid}->{rootTag}) tag($tag)");
510 2 ▶ if ($self->{SIDS}->{$sid}->{rootTag} ne $tag)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"SIDS"}{$sid}{"rootTag"} ne $tag) 0 2 2
511 {
512 0 $self->{SIDS}->{$sid}->{streamerror} = "Root tag mis-match: <$self->{SIDS}->{$sid}->{rootTag}> ... </$tag>\n";
513 }
514 2 return;
515 }
516  
517 240 my $CLOSED = pop @{$self->{SIDS}->{$sid}->{node}};
518  
519 240 $self->debug(2,"Node: _handle_close: check2(",$#{$self->{SIDS}->{$sid}->{node}},")");
520
521 240 ▶ if($#{$self->{SIDS}->{$sid}->{node}} == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$self{"SIDS"}{$sid}{"node"};} == -1) 37 203 240
522 {
523 37 push @{$self->{SIDS}->{$sid}->{node}}, $CLOSED;
524  
525 37 ▶ if (ref($self) ne "XML::Stream::Parser")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne "XML::Stream::Parser") 35 2 37
526 {
527 35 my $stream_prefix = $self->StreamPrefix($sid);
528
529 35 ▶ if(defined($self->{SIDS}->{$sid}->{node}->[0]) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'SIDS'}{$sid}{'node'}[0] and $self->{'SIDS'}{$sid}{'node'}[0]->get_tag =~ /^$stream_prefix\:/) { } 1 34 35
Condition66%2/3
defined $self->{"SIDS"}{$sid}{"node"}[0] and $self->{"SIDS"}{$sid}{"node"}[0]->get_tag =~ /^$stream_prefix\:/
!ll&&!rl&&r
0341
MC/DC50%1/2
defined $self->{"SIDS"}{$sid}{"node"}[0] and $self->{"SIDS"}{$sid}{"node"}[0]->get_tag =~ /^$stream_prefix\:/
defined $self->{"SIDS"}{$sid}{"node"}[0] $self->{"SIDS"}{$sid}{"node"}[0]->get_tag =~ /^$stream_prefix\:/
Truth table66%2/3
A and B
A= defined $self->{"SIDS"}{$sid}{"node"}[0]B= $self->{"SIDS"}{$sid}{"node"}[0]->get_tag =~ /^$stream_prefix\:/
ABresult
0X0
100
111
530 ($self->{SIDS}->{$sid}->{node}->[0]->get_tag() =~ /^${stream_prefix}\:/))
531 {
532 1 my $node = $self->{SIDS}->{$sid}->{node}->[0];
533 1 $self->{SIDS}->{$sid}->{node} = [];
534 1 $self->ProcessStreamPacket($sid,$node);
535 }
536 else
537 {
538 34 my $node = $self->{SIDS}->{$sid}->{node}->[0];
539 34 $self->{SIDS}->{$sid}->{node} = [];
540  
541 34 my @special =
542 &XML::Stream::XPath(
543 $node,
544 '[@xmlns="'.&XML::Stream::ConstXMLNS("xmpp-sasl").'" or @xmlns="'.&XML::Stream::ConstXMLNS("xmpp-tls").'"]'
545 );
546 34 ▶ if ($#special > -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($#special > -1) { } 0 34 34
547 {
548 0 my $xmlns = $node->get_attrib("xmlns");
549
550 0 ▶ $self->ProcessSASLPacket($sid,$node)
Branch: 1 branch
BranchTrueFalseTotal
if $xmlns eq &XML::Stream::ConstXMLNS("xmpp-sasl") 0 0 0
551 if ($xmlns eq &XML::Stream::ConstXMLNS("xmpp-sasl"));
552 0 ▶ $self->ProcessTLSPacket($sid,$node)
Branch: 1 branch
BranchTrueFalseTotal
if $xmlns eq &XML::Stream::ConstXMLNS("xmpp-tls") 0 0 0
553 if ($xmlns eq &XML::Stream::ConstXMLNS("xmpp-tls"));
554 }
555 else
556 {
557 34 &{$self->{CB}->{node}}($sid,$node);
558 }
559 }
560 }
561 }
562 }
563  
564  
565 ##############################################################################
566 #
567 # SetXMLData - takes a host of arguments and sets a portion of the specified
568 # XML::Parser::Node object with that data. The function works
569 # in two modes "single" or "multiple". "single" denotes that
570 # the function should locate the current tag that matches this
571 # data and overwrite it's contents with data passed in.
572 # "multiple" denotes that a new tag should be created even if
573 # others exist.
574 #
575 # type - single or multiple
576 # XMLTree - pointer to XML::Stream Node object
577 # tag - name of tag to create/modify (if blank assumes
578 # working with top level tag)
579 # data - CDATA to set for tag
580 # attribs - attributes to ADD to tag
581 #
582 ##############################################################################
583 sub SetXMLData
584 {
585 0 ▶ my ($type,$XMLTree,$tag,$data,$attribs) = @_;
Subroutine SetXMLData: not called CC=8 SCAR=42.8
Pod undocumented
586  
587 0 ▶ if ($tag ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($tag ne '') { } 0 0 0
588 {
589 0 ▶ if ($type eq "single")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "single") 0 0 0
590 {
591 0 foreach my $child ($XMLTree->children())
592 {
593 0 ▶ if ($$XMLTree[1]->[$child] eq $tag)
Branch: 1 branch
BranchTrueFalseTotal
if ($XMLTree->[1][$child] eq $tag) 0 0 0
594 {
595 0 $XMLTree->remove_child($child);
596  
597 0 my $newChild = $XMLTree->add_child($tag);
598 0 $newChild->put_attrib(%{$attribs});
599 0 ▶ $newChild->add_cdata($data) if ($data ne "");
Branch: 1 branch
BranchTrueFalseTotal
if $data ne "" 0 0 0
600 0 return;
601 }
602 }
603 }
604 0 my $newChild = $XMLTree->add_child($tag);
605 0 $newChild->put_attrib(%{$attribs});
606 0 ▶ $newChild->add_cdata($data) if ($data ne "");
Branch: 1 branch
BranchTrueFalseTotal
if $data ne "" 0 0 0
607 }
608 else
609 {
610 0 $XMLTree->put_attrib(%{$attribs});
611 0 ▶ $XMLTree->add_cdata($data) if ($data ne "");
Branch: 1 branch
BranchTrueFalseTotal
if $data ne "" 0 0 0
612 }
613 }
614  
615  
616 ##############################################################################
617 #
618 # GetXMLData - takes a host of arguments and returns various data structures
619 # that match them.
620 #
621 # type - "existence" - returns 1 or 0 if the tag exists in the
622 # top level.
623 # "value" - returns either the CDATA of the tag, or the
624 # value of the attribute depending on which is
625 # sought. This ignores any mark ups to the data
626 # and just returns the raw CDATA.
627 # "value array" - returns an array of strings representing
628 # all of the CDATA in the specified tag.
629 # This ignores any mark ups to the data
630 # and just returns the raw CDATA.
631 # "tree" - returns an XML::Parser::Node object with the
632 # specified tag as the root tag.
633 # "tree array" - returns an array of XML::Parser::Node
634 # objects each with the specified tag as
635 # the root tag.
636 # "child array" - returns a list of all children nodes
637 # not including CDATA nodes.
638 # "attribs" - returns a hash with the attributes, and
639 # their values, for the things that match
640 # the parameters
641 # "count" - returns the number of things that match
642 # the arguments
643 # "tag" - returns the root tag of this tree
644 # XMLTree - pointer to XML::Parser::Node object
645 # tag - tag to pull data from. If blank then the top level
646 # tag is accessed.
647 # attrib - attribute value to retrieve. Ignored for types
648 # "value array", "tree", "tree array". If paired
649 # with value can be used to filter tags based on
650 # attributes and values.
651 # value - only valid if an attribute is supplied. Used to
652 # filter for tags that only contain this attribute.
653 # Useful to search through multiple tags that all
654 # reference different name spaces.
655 #
656 ##############################################################################
657 sub GetXMLData
658 {
659 905 ▶ my ($type,$XMLTree,$tag,$attrib,$value) = @_;
Subroutine GetXMLData: called CC=42 SCAR=46.0
Pod undocumented
660  
661 905 ▶ $tag = "" if !defined($tag);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 447 458 905
662 905 ▶ $attrib = "" if !defined($attrib);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrib 773 132 905
663 905 ▶ $value = "" if !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 905 0 905
664  
665 905 my $skipthis = 0;
666  
667 #-------------------------------------------------------------------------
668 # Check if a child tag in the root tag is being requested.
669 #-------------------------------------------------------------------------
670 905 ▶ if ($tag ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($tag ne '') { } 326 579 905
671 {
672 326 my $count = 0;
673 326 my @array;
674 326 foreach my $child ($XMLTree->children())
675 {
676 1819 ▶ if (($child->get_tag() eq $tag) || ($tag eq "*"))
Branch: 1 branch
BranchTrueFalseTotal
if ($child->get_tag eq $tag or $tag eq "*") 1184 635 1819
Condition100%3/3
$child->get_tag eq $tag or $tag eq "*"
l!l&&r!l&&!r
1121072635
MC/DC100%2/2
$child->get_tag eq $tag or $tag eq "*"
$child->get_tag eq $tag $tag eq "*"
Truth table100%3/3
A or B
A= $child->get_tag eq $tagB= $tag eq "*"
ABresult
1X1
011
000
677 {
678 #-------------------------------------------------------------
679 # Filter out tags that do not contain the attribute and value.
680 #-------------------------------------------------------------
681 1184 ▶ next if (($value ne "") && ($attrib ne "") && $child->get_attrib($attrib) && ($XMLTree->get_attrib($attrib) ne $value));
Branch: 1 branch
BranchTrueFalseTotal
if $value ne "" and $attrib ne "" and $child->get_attrib($attrib) and $XMLTree->get_attrib($attrib) ne $value 0 1184 1184
Condition33%3/9
$value ne "" and $attrib ne "" and $child->get_attrib($attrib) and $XMLTree->get_attrib($attrib) ne $value
!ll&&!rl&&r
118400
$value ne "" and $attrib ne "" and $child->get_attrib($attrib)
!ll&&!rl&&r
118400
$value ne "" and $attrib ne ""
!ll&&!rl&&r
118400
MC/DC0%0/4
$value ne "" and $attrib ne "" and $child->get_attrib($attrib) and $XMLTree->get_attrib($attrib) ne $value
$value ne "" $attrib ne "" $child->get_attrib($attrib) $XMLTree->get_attrib($attrib) ne $value
Truth table20%1/5
A and B and C and D
A= $value ne ""B= $attrib ne ""C= $child->get_attrib($attrib)D= $XMLTree->get_attrib($attrib) ne $value
ABCDresult
0XXX0
10XX0
110X0
11100
11111
682 1184 ▶ next if (($attrib ne "") && !$child->get_attrib($attrib));
Branch: 1 branch
BranchTrueFalseTotal
if $attrib ne "" and not $child->get_attrib($attrib) 0 1184 1184
Condition33%1/3
$attrib ne "" and not $child->get_attrib($attrib)
!ll&&!rl&&r
118400
MC/DC0%0/2
$attrib ne "" and not $child->get_attrib($attrib)
$attrib ne "" not $child->get_attrib($attrib)
Truth table33%1/3
A and not(B)
A= $attrib ne ""B= not $child->get_attrib($attrib)
ABresult
0X0
100
111
683  
684 #-------------------------------------------------------------
685 # Check for existence
686 #-------------------------------------------------------------
687 1184 ▶ if ($type eq "existence")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "existence") 0 1184 1184
688 {
689 0 return 1;
690 }
691 #-------------------------------------------------------------
692 # Return the raw CDATA value without mark ups, or the value of
693 # the requested attribute.
694 #-------------------------------------------------------------
695 1184 ▶ if ($type eq "value")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "value") 4 1180 1184
696 {
697 4 ▶ if ($attrib eq "")
Branch: 1 branch
BranchTrueFalseTotal
if ($attrib eq "") 4 0 4
698 {
699 4 my $str = $child->get_cdata();
700 4 return $str;
701 }
702 0 ▶ return $XMLTree->get_attrib($attrib)
Branch: 1 branch
BranchTrueFalseTotal
if defined $XMLTree->get_attrib($attrib) 0 0 0
703 if defined($XMLTree->get_attrib($attrib));
704 }
705 #-------------------------------------------------------------
706 # Return an array of values that represent the raw CDATA without
707 # mark up tags for the requested tags.
708 #-------------------------------------------------------------
709 1180 ▶ if ($type eq "value array")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "value array") 0 1180 1180
710 {
711 0 ▶ if ($attrib eq "")
Branch: 1 branch
BranchTrueFalseTotal
if ($attrib eq '') { } 0 0 0
712 {
713 0 my $str = $child->get_cdata();
714 0 push(@array,$str);
715 }
716 else
717 {
718 0 ▶ push(@array, $XMLTree->get_attrib($attrib))
Branch: 1 branch
BranchTrueFalseTotal
if defined $XMLTree->get_attrib($attrib) 0 0 0
719 if defined($XMLTree->get_attrib($attrib));
720 }
721 }
722 #-------------------------------------------------------------
723 # Return a pointer to a new XML::Parser::Tree object that has
724 # the requested tag as the root tag.
725 #-------------------------------------------------------------
726 1180 ▶ if ($type eq "tree")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tree") 0 1180 1180
727 {
728 0 return $child;
729 }
730 #-------------------------------------------------------------
731 # Return an array of pointers to XML::Parser::Tree objects
732 # that have the requested tag as the root tags.
733 #-------------------------------------------------------------
734 1180 ▶ if ($type eq "tree array")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tree array") 144 1036 1180
735 {
736 144 push(@array,$child);
737 }
738 #-------------------------------------------------------------
739 # Return an array of pointers to XML::Parser::Tree objects
740 # that have the requested tag as the root tags.
741 #-------------------------------------------------------------
742 1180 ▶ if ($type eq "child array")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "child array") 928 252 1180
743 {
744 928 ▶ push(@array,$child) if ($child->get_tag() ne "__xmlstream__:node:cdata");
Branch: 1 branch
BranchTrueFalseTotal
if $child->get_tag ne "__xmlstream__:node:cdata" 414 514 928
745 }
746 #-------------------------------------------------------------
747 # Return a count of the number of tags that match
748 #-------------------------------------------------------------
749 1180 ▶ if ($type eq "count")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "count") 108 1072 1180
750 {
751 108 $count++;
752 }
753 #-------------------------------------------------------------
754 # Return the attribute hash that matches this tag
755 #-------------------------------------------------------------
756 1180 ▶ if ($type eq "attribs")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "attribs") 0 1180 1180
757 {
758 0 return $XMLTree->attrib();
759 }
760 }
761 }
762 #---------------------------------------------------------------------
763 # If we are returning arrays then return array.
764 #---------------------------------------------------------------------
765 322 ▶ 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") 205 58 263
Condition83%5/6
$type eq "tree array" or $type eq "value array" or $type eq "child array"
l!l&&r!l&&!r
5920558
$type eq "tree array" or $type eq "value array"
l!l&&r!l&&!r
590263
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
766 ($type eq "child array"))
767 {
768 264 return @array;
769 }
770  
771 #---------------------------------------------------------------------
772 # If we are returning then count, then do so
773 #---------------------------------------------------------------------
774 58 ▶ if ($type eq "count")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "count") 58 0 58
775 {
776 58 return $count;
777 }
778 }
779 else
780 {
781 #---------------------------------------------------------------------
782 # This is the root tag, so handle things a level up.
783 #---------------------------------------------------------------------
784  
785 #---------------------------------------------------------------------
786 # Return the raw CDATA value without mark ups, or the value of the
787 # requested attribute.
788 #---------------------------------------------------------------------
789 579 ▶ if ($type eq "value")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "value") 149 430 579
790 {
791 149 ▶ if ($attrib eq "")
Branch: 1 branch
BranchTrueFalseTotal
if ($attrib eq "") 17 132 149
792 {
793 17 my $str = $XMLTree->get_cdata();
794 17 return $str;
795 }
796 132 ▶ return $XMLTree->get_attrib($attrib)
Branch: 1 branch
BranchTrueFalseTotal
if $XMLTree->get_attrib($attrib) 54 78 132
797 if $XMLTree->get_attrib($attrib);
798 }
799 #---------------------------------------------------------------------
800 # Return a pointer to a new XML::Parser::Tree object that has the
801 # requested tag as the root tag.
802 #---------------------------------------------------------------------
803 508 ▶ if ($type eq "tree")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tree") 0 508 508
804 {
805 0 return $XMLTree;
806 }
807  
808 #---------------------------------------------------------------------
809 # Return the 1 if the specified attribute exists in the root tag.
810 #---------------------------------------------------------------------
811 508 ▶ if ($type eq "existence")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "existence") 0 508 508
812 {
813 0 ▶ if ($attrib ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($attrib ne "") 0 0 0
814 {
815 0 ▶ return ($XMLTree->get_attrib($attrib) eq $value) if ($value ne "");
Branch: 1 branch
BranchTrueFalseTotal
if $value ne "" 0 0 0
816 0 return defined($XMLTree->get_attrib($attrib));
817 }
818 0 return 0;
819 }
820  
821 #---------------------------------------------------------------------
822 # Return the attribute hash that matches this tag
823 #---------------------------------------------------------------------
824 508 ▶ if ($type eq "attribs")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "attribs") 1 507 508
825 {
826 1 return $XMLTree->attrib();
827 }
828 #---------------------------------------------------------------------
829 # Return the tag of this node
830 #---------------------------------------------------------------------
831 507 ▶ if ($type eq "tag")
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tag") 429 78 507
832 {
833 429 return $XMLTree->get_tag();
834 }
835 }
836 #-------------------------------------------------------------------------
837 # Return 0 if this was a request for existence, or "" if a request for
838 # a "value", or [] for "tree", "value array", and "tree array".
839 #-------------------------------------------------------------------------
840 78 ▶ return 0 if ($type eq "existence");
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "existence" 0 78 78
841 78 ▶ return "" if ($type eq "value");
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "value" 78 0 78
842 0 return [];
843 }
844  
845  
846 ##############################################################################
847 #
848 # BuildXML - takes an XML::Parser::Tree object and builds the XML string
849 # that it represents.
850 #
851 ##############################################################################
852 sub BuildXML
853 {
854 123 ▶ my ($node,$rawXML) = @_;
Subroutine BuildXML: called CC=13 SCAR=25.7
Pod undocumented
855  
856 123 my $str = "<".$node->get_tag();
857  
858 123 my %attrib = $node->attrib();
859  
860 123 foreach my $att (sort {$a cmp $b} keys(%attrib))
861 {
862 38 $str .= " ".$att."='".&XML::Stream::EscapeXML($attrib{$att})."'";
863 }
864  
865 123 my @children = $node->children();
866 123 ▶ if (($#children > -1) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($#children > -1 or defined $rawXML and $rawXML ne '' or defined $node->get_raw_xml and $node->get_raw_xml ne '') { } 115 8 123
Condition83%10/12
$#children > -1 or defined $rawXML and $rawXML ne "" or defined $node->get_raw_xml and $node->get_raw_xml ne ""
l!l&&r!l&&!r
11418
$#children > -1 or defined $rawXML and $rawXML ne ""
l!l&&r!l&&!r
11319
defined $rawXML and $rawXML ne ""
!ll&&!rl&&r
901
defined $node->get_raw_xml and $node->get_raw_xml ne ""
!ll&&!rl&&r
801
MC/DC60%3/5
$#children > -1 or defined $rawXML and $rawXML ne "" or defined $node->get_raw_xml and $node->get_raw_xml ne ""
$#children > -1 defined $rawXML $rawXML ne "" defined $node->get_raw_xml $node->get_raw_xml ne ""
Truth table50%4/8
A or B and C or D and E
A= $#children > -1B= defined $rawXMLC= $rawXML ne ""D= defined $node->get_raw_xmlE= $node->get_raw_xml ne ""
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
867 (defined($rawXML) && ($rawXML ne "")) ||
868 (defined($node->get_raw_xml()) && ($node->get_raw_xml() ne ""))
869 )
870 {
871 115 $str .= ">";
872 115 foreach my $child (@children)
873 {
874 229 ▶ if ($child->get_tag() eq "__xmlstream__:node:cdata")
Branch: 1 branch
BranchTrueFalseTotal
if ($child->get_tag eq '__xmlstream__:node:cdata') { } 145 84 229
875 {
876 145 $str .= &XML::Stream::EscapeXML(join("",$child->children()));
877 }
878 else
879 {
880 84 $str .= &XML::Stream::Node::BuildXML($child);
881 }
882 }
883 115 ▶ $str .= $node->get_raw_xml()
Branch: 1 branch
BranchTrueFalseTotal
if defined $node->get_raw_xml and $node->get_raw_xml ne "" 17 98 115
Condition66%2/3
defined $node->get_raw_xml and $node->get_raw_xml ne ""
!ll&&!rl&&r
98017
MC/DC50%1/2
defined $node->get_raw_xml and $node->get_raw_xml ne ""
defined $node->get_raw_xml $node->get_raw_xml ne ""
Truth table66%2/3
A and B
A= defined $node->get_raw_xmlB= $node->get_raw_xml ne ""
ABresult
0X0
100
111
884 if (defined($node->get_raw_xml()) &&
885 ($node->get_raw_xml() ne "")
886 );
887 115 ▶ $str .= $rawXML if (defined($rawXML) && ($rawXML ne ""));
Branch: 1 branch
BranchTrueFalseTotal
if defined $rawXML and $rawXML ne "" 17 98 115
Condition66%2/3
defined $rawXML and $rawXML ne ""
!ll&&!rl&&r
98017
MC/DC50%1/2
defined $rawXML and $rawXML ne ""
defined $rawXML $rawXML ne ""
Truth table66%2/3
A and B
A= defined $rawXMLB= $rawXML ne ""
ABresult
0X0
100
111
888 115 $str .= "</".$node->get_tag().">";
889 }
890 else
891 {
892 8 $str .= "/>";
893 }
894  
895 123 return $str;
896 }
897  
898  
899 ##############################################################################
900 #
901 # XML2Config - takes an XML data tree and turns it into a hash of hashes.
902 # This only works for certain kinds of XML trees like this:
903 #
904 # <foo>
905 # <bar>1</bar>
906 # <x>
907 # <y>foo</y>
908 # </x>
909 # <z>5</z>
910 # </foo>
911 #
912 # The resulting hash would be:
913 #
914 # $hash{bar} = 1;
915 # $hash{x}->{y} = "foo";
916 # $hash{z} = 5;
917 #
918 # Good for config files.
919 #
920 ##############################################################################
921 sub XML2Config
922 {
923 59 ▶ my ($XMLTree) = @_;
Subroutine XML2Config: called CC=5 SCAR=16.1
Pod undocumented
924  
925 59 my %hash;
926 59 foreach my $tree (&XML::Stream::GetXMLData("tree array",$XMLTree,"*"))
927 {
928 144 ▶ if ($tree->get_tag() eq "__xmlstream__:node:cdata")
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->get_tag eq '__xmlstream__:node:cdata') { } 86 58 144
929 {
930 86 my $str = join("",$tree->children());
931 86 ▶ return $str unless ($str =~ /^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /^\s*$/ 23 63 86
932 }
933 else
934 {
935 58 ▶ if (&XML::Stream::GetXMLData("count",$XMLTree,$tree->get_tag()) > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (&XML::Stream::GetXMLData('count', $XMLTree, $tree->get_tag) > 1) { } 20 38 58
936 {
937 20 push(@{$hash{$tree->get_tag()}},&XML::Stream::XML2Config($tree));
938 }
939 else
940 {
941 38 $hash{$tree->get_tag()} = &XML::Stream::XML2Config($tree);
942 }
943 }
944 }
945 36 return \%hash;
946 }
947  
948  
949 12 1;
« blib/lib/XML/Stream/Tree.pm ↑ Summary blib/lib/XML/Stream/XPath/Query.pm »