/usr/share/perl5/XML/Node.pm is in libxml-node-perl 0.11-7.
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 | # Copyright (c) 1999 Chang Liu
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
package XML::Node;
#use strict;
#use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
=head1 NAME
XML::Node - Node-based XML parsing: an simplified interface to XML::Parser
=head1 SYNOPSIS
use XML::Node;
$xml_node = new XML::Node;
$xml_node->register( $nodetype, $callback_type => \&callback_function );
$xml_node->register( $nodetype, $callback_type => \$variable );
open(FOO, 'xmlgenerator |');
$p3->parse(*FOO);
close(FOO);
$xml_node->parsefile( $xml_filename );
=head1 DESCRIPTION
If you are only interested in processing certain nodes in an XML file, this
module can help you simplify your Perl scripts significantly.
The XML::Node module allows you to register callback functions or variables for
any XML node. If you register a call back function, it will be called when
the node of the type you specified are encountered. If you register a variable,
the content of a XML node will be appended to that variable automatically.
Subroutine register accepts both absolute and relative node registrations.
Here is an example of absolute path registration:
1. register(">TestCase>Name", "start" => \&handle_TestCase_Name_start);
Here are examples of single node name registration:
2. register( "Name", "start" => \&handle_Name_start);
3. register( "Name", "end" => \&handle_Name_end);
4. register( "Name", "char" => \&handle_Name_char);
Here is an example of attribute registration:
5. register(">TestCase:Author", "attr" => \$testcase_author);
Abosolute path trigger condition is recommended because a "Name" tage could appear in different
places and stands for differe names.
Example:
1 <Testcase>
2 <Name>Something</Name>
3 <Oracle>
4 <Name>Something</Name>
5 </Oracle>
6 </Testcase>
Statement 1 causes &handle_TestCase_Name_start to be called when parsing Line 2. Statements 2,3,4 cause the three handler subroutines to be called when parsing both Line 2 and Line 4.
This module uses XML::Parser.
=head1 EXAMPLE
Examples "test.pl" and "parse_orders.pl" come with this perl module.
=head1 SEE ALSO
XML::Parser
=head1 NOTE
When you register a variable, XML::Node appends strings found to that variable. So please be sure to clear that variable before it is used again.
=head1 AUTHORS
Chang Liu <liu@ics.uci.edu>
=head1 LAST MODIFIED
$Date: 2001/12/10 11:38:28 $
=cut
use Exporter;
$VERSION = "0.11";
@ISA = ('Exporter');
@EXPORT = qw (®ister &parse &parsefile);
use XML::Parser;
use Carp;
if ($ENV{DEBUG}) {
print "DEBUG:XML::Node.pm VERSION $VERSION\n";
}
my $instance = 0;
my @selves = ();
my $myinstance;
sub new{
my $class = shift;
my $self = {
INSTANCE => $instance,
START_HANDLERS => {},
END_HANDLERS => {},
CHAR_HANDLERS => {},
ATTR_HANDLERS => {},
CURRENT_TAG => "",
CURRENT_PATH => "",
};
bless $self, $class;
$selves[$instance++] = $self;
return $self;
}
sub register
{
$self = shift or croak "XML::Node --self is expected as THE first parameter \®ister.\n";
my $node = shift or croak "XML::Node --a node path is expected as arg1 in \®ister.\n";
my $type = shift or croak "XML::Node --node type is expected as arg2 in \®ister.\n";
my $handler = shift or croak "XML::Node --a handler is expected as arg3 in \®ister.\n";
if ($type eq "start") {
$self->{START_HANDLERS}->{$node} = $handler;
} elsif ($type eq "end") {
$self->{END_HANDLERS}->{$node} = $handler;
} elsif ($type eq "char") {
$self->{CHAR_HANDLERS}->{$node} = $handler;
} elsif ($type eq "attr") {
$self->{ATTR_HANDLERS}->{$node} = $handler;
} else {
croak "XML::Node --unknown handler type $type for node $node\n";
}
}
sub parsefile
{
$self = shift or croak "XML::Node --self is expected as THE first parameter \®ister.\n";
my $xml_file = shift or croak "XML::Node --an XML filename is expected in \&parse.\n";
$myinstance = $self->{INSTANCE};
carp "XML::Node - invoking parser [$myinstance]" if $ENV{DEBUG};
my $my_handlers = qq {
sub handle_start_$myinstance
{
&handle_start($myinstance, \@_);
}
sub handle_end_$myinstance
{
&handle_end($myinstance, \@_);
}
sub handle_char_$myinstance
{
&handle_char($myinstance, \@_);
}
\$XML::Node::parser = new XML::Parser(Handlers => { Start => \\& handle_start_$myinstance,
End => \\& handle_end_$myinstance,
Char => \\& handle_char_$myinstance } );
};
#carp "[[[[[[[[[[[[[[[[$my_handlers]]]]]]]]]]]]]]";
eval ($my_handlers);
$parser->parsefile("$xml_file");
}
sub parse
{
$self = shift or croak "XML::Node --self is expected as THE first parameter \®ister.\n";
$myinstance = $self->{INSTANCE};
carp "XML::Node - invoking parser [$myinstance]" if $ENV{DEBUG};
my $my_handlers = qq {
sub handle_start_$myinstance
{
&handle_start($myinstance, \@_);
}
sub handle_end_$myinstance
{
&handle_end($myinstance, \@_);
}
sub handle_char_$myinstance
{
&handle_char($myinstance, \@_);
}
\$XML::Node::parser = new XML::Parser(Handlers => { Start => \\& handle_start_$myinstance,
End => \\& handle_end_$myinstance,
Char => \\& handle_char_$myinstance } );
};
#carp "[[[[[[[[[[[[[[[[$my_handlers]]]]]]]]]]]]]]";
eval ($my_handlers);
$parser->parse(shift);
}
sub handle_start
{
my $myinstance = shift;
my $p = shift;
my $element = shift;
my $current_path = $selves[$myinstance]->{CURRENT_PATH} =
$selves[$myinstance]->{CURRENT_PATH} . ">" . $element;
my $current_tag = $selves[$myinstance]->{CURRENT_TAG} = $element;
my $attr;
my $value;
# carp("handle_start called [$myinstance] [$element] [$current_path]\n");
while (defined ($attr = shift ) ) {
if (! defined ($value = shift)) {
croak ("value for attribute [$attr] of element [$element] is not returned by XML::Parser\n");
}
# carp("Attribute [$attr] of element [$element] found with value [$value] attr_path:[$attr_path]\n");
my @array = split(/>/, $current_path);
my $current_relative_path = "$current_tag:$attr";
my $i;
if ($selves[$myinstance]->{ATTR_HANDLERS}->{$current_relative_path}) {
handle($p, $value, $selves[$myinstance]->{ATTR_HANDLERS}->{$current_relative_path});
}
for ($i=$#array-1;$i>=1;$i--)
{ # call all relative paths
$current_relative_path = $array[$i] . ">" . $current_relative_path;
if ($selves[$myinstance]->{ATTR_HANDLERS}->{$current_relative_path}) {
handle($p, $value, $selves[$myinstance]->{ATTR_HANDLERS}->{$current_relative_path});
}
}
my $attr_path = "$current_path:$attr";
if ($selves[$myinstance]->{ATTR_HANDLERS}->{$attr_path}) {
handle($p, $value, $selves[$myinstance]->{ATTR_HANDLERS}->{$attr_path});
}
}
my @array = split(/>/, $current_path);
my $current_relative_path = $current_tag;
my $i;
if ($selves[$myinstance]->{START_HANDLERS}->{$current_tag}) {
handle($p, $element, $selves[$myinstance]->{START_HANDLERS}->{$current_tag});
}
#carp("--Begin loop\n");
for ($i=$#array-1;$i>=1;$i--)
{ # call all relative paths
$current_relative_path = $array[$i] . ">" . $current_relative_path;
#carp("Array size is $#array, \$i is $i, current_relative_path is $current_relative_path\n");
if ($selves[$myinstance]->{START_HANDLERS}->{$current_relative_path}) {
handle($p, $element, $selves[$myinstance]->{START_HANDLERS}->{$current_relative_path});
}
}
#carp("--End loop\n");
if ($selves[$myinstance]->{START_HANDLERS}->{$current_path}) {
handle($p, $element, $selves[$myinstance]->{START_HANDLERS}->{$current_path});
}
}
sub handle_end
{
my $myinstance = shift;
my $p = shift;
my $element = shift;
my $current_path = $selves[$myinstance]->{CURRENT_PATH};
# carp("handle_end called [$myinstance] [$element]\n");
$selves[$myinstance]->{CURRENT_TAG} = $element;
my @array = split(/>/, $current_path);
my $current_relative_path = $element;
my $i;
if ($selves[$myinstance]->{END_HANDLERS}->{$selves[$myinstance]->{CURRENT_TAG}}) {
handle($p, $element, $selves[$myinstance]->{END_HANDLERS}->{$selves[$myinstance]->{CURRENT_TAG}});
}
for ($i=$#array-1;$i>=1;$i--)
{ # call all relative paths
$current_relative_path = $array[$i] . ">" . $current_relative_path;
#carp("Array size is $#array, \$i is $i, current_relative_path is $current_relative_path\n");
if ($selves[$myinstance]->{END_HANDLERS}->{$current_relative_path}) {
handle($p, $element, $selves[$myinstance]->{END_HANDLERS}->{$current_relative_path});
}
}
if ($selves[$myinstance]->{END_HANDLERS}->{$selves[$myinstance]->{CURRENT_PATH}}) {
handle($p, $element, $selves[$myinstance]->{END_HANDLERS}->{$selves[$myinstance]->{CURRENT_PATH}});
}
$selves[$myinstance]->{CURRENT_PATH} =~ /(.*)>/;
$selves[$myinstance]->{CURRENT_PATH} = $1;
$selves[$myinstance]->{CURRENT_TAG} = $';
if ($element ne $selves[$myinstance]->{CURRENT_TAG}) {
carp "start-tag <$selves[$myinstance]->{CURRENT_TAG}> doesn't match end-tag <$element>. Is this XML file well-formed?\n";
}
$selves[$myinstance]->{CURRENT_PATH} =~ /(.*)>/;
$selves[$myinstance]->{CURRENT_TAG} = $';
}
sub handle_char
{
my $myinstance = shift;
my $p = shift;
my $element = shift;
my $current_path = $selves[$myinstance]->{CURRENT_PATH};
# carp("handle_char called [$myinstance] [$element]\n");
my @array = split(/>/, $current_path);
my $current_relative_path = $element;
my $i;
if ($selves[$myinstance]->{CHAR_HANDLERS}->{$selves[$myinstance]->{CURRENT_TAG}}) {
handle($p, $element, $selves[$myinstance]->{CHAR_HANDLERS}->{$selves[$myinstance]->{CURRENT_TAG}});
}
for ($i=$#array-1;$i>=1;$i--)
{ # call all relative paths
$current_relative_path = $array[$i] . ">" . $current_relative_path;
if ($selves[$myinstance]->{CHAR_HANDLERS}->{$current_relative_path}) {
handle($p, $element, $selves[$myinstance]->{CHAR_HANDLERS}->{$current_relative_path});
}
}
if ($selves[$myinstance]->{CHAR_HANDLERS}->{$selves[$myinstance]->{CURRENT_PATH}}) {
handle($p, $element, $selves[$myinstance]->{CHAR_HANDLERS}->{$selves[$myinstance]->{CURRENT_PATH}});
}
}
sub handle
{
my $p = shift;
my $element = shift;
my $handler = shift;
my $handler_type = ref($handler);
if ($handler_type eq "CODE") {
&$handler($p,$element); # call the handler function
} elsif ($handler_type eq "SCALAR") {
# chomp($element);
# $element =~ /^(\s*)/;
# $element = $';
# $element =~ /(\s*)$/;
# $element = $`;
if (! defined $$handler) {
$$handler = "";
#carp ("XML::Node - SCALAR handler undefined when processing [$element]");
}
$$handler = $$handler . $element; #append the content to the handler variable
} else {
carp "XML::Node -unknown handler type [$handler_type]\n";
exit;
}
}
1;
|