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