/usr/share/perl5/XML/Parser/Lite/Tree.pm is in libxml-parser-lite-tree-perl 0.14-1.
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 | package XML::Parser::Lite::Tree;
use 5.006;
use strict;
use warnings;
use XML::Parser::LiteCopy;
our $VERSION = '0.14';
use vars qw( $parser );
sub instance {
return $parser if $parser;
$parser = __PACKAGE__->new;
}
sub new {
my $class = shift;
my $self = bless {}, $class;
my %opts = (ref $_[0]) ? ((ref $_[0] eq 'HASH') ? %{$_[0]} : () ) : @_;
$self->{opts} = \%opts;
$self->{__parser} = new XML::Parser::LiteCopy
Handlers => {
Start => sub { $self->_start_tag(@_); },
Char => sub { $self->_do_char(@_); },
CData => sub { $self->_do_cdata(@_); },
End => sub { $self->_end_tag(@_); },
Comment => sub { $self->_do_comment(@_); },
PI => sub { $self->_do_pi(@_); },
Doctype => sub { $self->_do_doctype(@_); },
};
$self->{process_ns} = $self->{opts}->{process_ns} || 0;
$self->{skip_white} = $self->{opts}->{skip_white} || 0;
return $self;
}
sub parse {
my ($self, $content) = @_;
my $root = {
'type' => 'root',
'children' => [],
};
$self->{tag_stack} = [$root];
$self->{__parser}->parse($content);
$self->cleanup($root);
if ($self->{skip_white}){
$self->strip_white($root);
}
if ($self->{process_ns}){
$self->{ns_stack} = {};
$self->mark_namespaces($root);
}
return $root;
}
sub _start_tag {
my $self = shift;
shift;
my $new_tag = {
'type' => 'element',
'name' => shift,
'attributes' => {},
'children' => [],
};
while (my $a_name = shift @_){
my $a_value = shift @_;
$new_tag->{attributes}->{$a_name} = $a_value;
}
push @{$self->{tag_stack}->[-1]->{children}}, $new_tag;
push @{$self->{tag_stack}}, $new_tag;
1;
}
sub _do_char {
my $self = shift;
shift;
for my $content(@_){
my $new_tag = {
'type' => 'text',
'content' => $content,
};
push @{$self->{tag_stack}->[-1]->{children}}, $new_tag;
}
1;
}
sub _do_cdata {
my $self = shift;
shift;
for my $content(@_){
my $new_tag = {
'type' => 'cdata',
'content' => $content,
};
push @{$self->{tag_stack}->[-1]->{children}}, $new_tag;
}
1;
}
sub _end_tag {
my $self = shift;
pop @{$self->{tag_stack}};
1;
}
sub _do_comment {
my $self = shift;
shift;
for my $content(@_){
my $new_tag = {
'type' => 'comment',
'content' => $content,
};
push @{$self->{tag_stack}->[-1]->{children}}, $new_tag;
}
1;
}
sub _do_pi {
my $self = shift;
shift;
push @{$self->{tag_stack}->[-1]->{children}}, {
'type' => 'pi',
'content' => shift,
};
1;
}
sub _do_doctype {
my $self = shift;
shift;
push @{$self->{tag_stack}->[-1]->{children}}, {
'type' => 'dtd',
'content' => shift,
};
1;
}
sub mark_namespaces {
my ($self, $obj) = @_;
my @ns_keys;
#
# mark
#
if ($obj->{type} eq 'element'){
#
# first, add any new NS's to the stack
#
my @keys = keys %{$obj->{attributes}};
for my $k(@keys){
if ($k =~ /^xmlns:(.*)$/){
push @{$self->{ns_stack}->{$1}}, $obj->{attributes}->{$k};
push @ns_keys, $1;
delete $obj->{attributes}->{$k};
}
if ($k eq 'xmlns'){
push @{$self->{ns_stack}->{__default__}}, $obj->{attributes}->{$k};
push @ns_keys, '__default__';
delete $obj->{attributes}->{$k};
}
}
#
# now - does this tag have a NS?
#
if ($obj->{name} =~ /^(.*?):(.*)$/){
$obj->{local_name} = $2;
$obj->{ns_key} = $1;
$obj->{ns} = $self->{ns_stack}->{$1}->[-1];
}else{
$obj->{local_name} = $obj->{name};
$obj->{ns} = $self->{ns_stack}->{__default__}->[-1];
}
#
# finally, add xpath-style namespace nodes
#
$obj->{namespaces} = {};
for my $key (keys %{$self->{ns_stack}}){
if (scalar @{$self->{ns_stack}->{$key}}){
my $uri = $self->{ns_stack}->{$key}->[-1];
$obj->{namespaces}->{$key} = $uri;
}
}
}
#
# descend
#
if ($obj->{type} eq 'root' || $obj->{type} eq 'element'){
for my $child (@{$obj->{children}}){
$self->mark_namespaces($child);
}
}
#
# pop from stack
#
for my $k (@ns_keys){
pop @{$self->{ns_stack}->{$k}};
}
}
sub strip_white {
my ($self, $obj) = @_;
if ($obj->{type} eq 'root' || $obj->{type} eq 'element'){
my $new_kids = [];
for my $child (@{$obj->{children}}){
if ($child->{type} eq 'text'){
if ($child->{content} =~ m/\S/){
push @{$new_kids}, $child;
}
}elsif ($child->{type} eq 'element'){
$self->strip_white($child);
push @{$new_kids}, $child;
}else{
push @{$new_kids}, $child;
}
}
$obj->{children} = $new_kids;
}
}
sub cleanup {
my ($self, $obj) = @_;
#
# cleanup PIs
#
if ($obj->{type} eq 'pi'){
my ($x, $y) = split /\s+/, $obj->{content}, 2;
$obj->{target} = $x;
$obj->{content} = $y;
}
#
# cleanup DTDs
#
if ($obj->{type} eq 'dtd'){
my ($x, $y) = split /\s+/, $obj->{content}, 2;
$obj->{name} = $x;
$obj->{content} = $y;
}
#
# recurse
#
if ($obj->{type} eq 'root' || $obj->{type} eq 'element'){
for my $child (@{$obj->{children}}){
$self->cleanup($child);
}
}
}
1;
__END__
=head1 NAME
XML::Parser::Lite::Tree - Lightweight XML tree builder
=head1 SYNOPSIS
use XML::Parser::Lite::Tree;
my $tree_parser = XML::Parser::Lite::Tree::instance();
my $tree = $tree_parser->parse($xml_data);
OR
my $tree = XML::Parser::Lite::Tree::instance()->parse($xml_data);
=head1 DESCRIPTION
This is a singleton class for parsing XML into a tree structure. How does this
differ from other XML tree generators? By using XML::Parser::Lite, which is a
pure perl XML parser. Using this module you can tree-ify simple XML without
having to compile any C.
For example, the following XML:
<foo woo="yay"><bar a="b" c="d" />hoopla</foo>
Parses into the following tree:
'children' => [
{
'children' => [
{
'children' => [],
'attributes' => {
'a' => 'b',
'c' => 'd'
},
'type' => 'element',
'name' => 'bar'
},
{
'content' => 'hoopla',
'type' => 'text'
}
],
'attributes' => {
'woo' => 'yay'
},
'type' => 'element',
'name' => 'foo'
}
],
'type' => 'root'
};
Each node contains a C<type> key, one of C<root>, C<element> and C<text>. C<root> is the
document root, and only contains an array ref C<children>. C<element> represents a normal
tag, and contains an array ref C<children>, a hash ref C<attributes> and a string C<name>.
C<text> nodes contain only a C<content> string.
=head1 METHODS
=over 4
=item C<instance()>
Returns an instance of the tree parser.
=item C<new( options... )>
Creates a new parser. Valid options include C<process_ns> to process namespaces.
=item C<parse($xml)>
Parses the xml in C<$xml> and returns the tree as a hash ref.
=back
=head1 AUTHOR
Copyright (C) 2004-2008, Cal Henderson, E<lt>cal@iamcal.comE<gt>
=head1 SEE ALSO
L<XML::Parser::Lite>.
=cut
|