/usr/share/perl5/XML/ESISParser.pm is in libxml-perl 0.08-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 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 | #
# Copyright (C) 1999 Ken MacLeod
# See the file COPYING for distribution terms.
#
# $Id: ESISParser.pm,v 1.9 2000/03/02 20:18:09 kmacleod Exp $
#
use strict;
use IO::File;
use UNIVERSAL;
package XML::ESISParser;
use vars qw{ $VERSION $NSGMLS_sgml $NSGMLS_FLAGS_sgml $NSGMLS_ENV_sgml
$NSGMLS_xml $NSGMLS_FLAGS_xml $NSGMLS_ENV_xml
$XML_DECL };
# will be substituted by make-rel script
$VERSION = "0.08";
$NSGMLS_sgml = 'nsgmls';
$NSGMLS_FLAGS_sgml = '-oentity -oempty -onotation-sysid -oincluded -oline -E0';
$NSGMLS_ENV_sgml = '';
$NSGMLS_xml = 'nsgmls';
$XML_DECL = '/usr/lib/sgml/declaration/xml.decl';
$NSGMLS_FLAGS_xml = '-oentity -oempty -onotation-sysid -oline -oincluded -wxml -E0 ';
$NSGMLS_ENV_xml = 'SP_CHARSET_FIXED=YES SP_ENCODING=XML';
sub new {
my $type = shift;
return bless { @_ }, $type;
}
sub parse {
my $self = shift;
die "XML::ESISParser: parser instance ($self) already parsing\n"
if (defined $self->{ParseOptions});
# If there's one arg and it has no ref, it's a string
my $args;
if (scalar (@_) == 1 && !ref($_[0])) {
$args = { Source => { String => shift } };
} else {
$args = (scalar (@_) == 1) ? shift : { @_ };
}
my $parse_options = { %$self, %$args };
$self->{ParseOptions} = $parse_options;
# ensure that we have at least one source
if (!defined $parse_options->{Source}
|| !(defined $parse_options->{Source}{String}
|| defined $parse_options->{Source}{ByteStream}
|| defined $parse_options->{Source}{SystemId}
|| defined $parse_options->{Source}{ESISStream})) {
die "XML::ESISParser: no source defined for parse\n";
}
# assign default Handler to any undefined handlers
if (defined $parse_options->{Handler}) {
$parse_options->{DocumentHandler} = $parse_options->{Handler}
if (!defined $parse_options->{DocumentHandler});
$parse_options->{DTDHandler} = $parse_options->{Handler}
if (!defined $parse_options->{DTDHandler});
$parse_options->{ErrorHandler} = $parse_options->{Handler}
if (!defined $parse_options->{ErrorHandler});
}
# create the NSGMLS command
my ($nsgmls_command, $nsgmls, $nsgmls_flags);
if (defined $parse_options->{NSGMLSCommand}) {
$nsgmls_command = $parse_options->{NSGMLSCommand};
} elsif (defined $parse_options->{IsSGML}
&& $parse_options->{IsSGML}) {
my $declaration = (defined $parse_options->{Declaration})
? " " . $parse_options->{Declaration} : "";
$nsgmls = $parse_options->{NSGMLS} = $NSGMLS_sgml;
$nsgmls_flags = $parse_options->{NSGMLS_FLAGS} = $NSGMLS_FLAGS_sgml;
$nsgmls_command = $parse_options->{NSGMLS_COMMAND} = "$nsgmls $nsgmls_flags $declaration";
} else {
my $declaration = (defined $parse_options->{Declaration})
? $parse_options->{Declaration} : $XML_DECL;
$nsgmls = $parse_options->{NSGMLS} = $NSGMLS_xml;
$nsgmls_flags = $parse_options->{NSGMLS_FLAGS} = $NSGMLS_FLAGS_xml;
$nsgmls_command = $parse_options->{NSGMLS_COMMAND} = "$NSGMLS_ENV_xml $nsgmls $nsgmls_flags $declaration";
}
my $result;
if (defined $self->{ParseOptions}{Source}{ESISStream}) {
# read ESIS stream directly
my $system_id = (defined $self->{ParseOptions}{Source}{SystemId})
? "\`$self->{ParseOptions}{Source}{SystemId}'" : 'ESIS Stream';
eval { $result = $self->parse_fh ($self->{ParseOptions}{Source}{ESISStream}) };
my $retval = $@;
if ($retval) {
die "XML::ESISParser::parse: unable to parse \`$system_id'\n$retval";
}
} elsif (defined $self->{ParseOptions}{Source}{ByteStream}) {
# call nsgmls using file handle
# FIXME special case stdin?
# For ByteStreams (Perl file handles) we create a sub-process
# that we feed the XML/SGML document and we get back the ESIS
# stream
my $retval;
my $system_id = (defined $self->{ParseOptions}{Source}{SystemId})
? "\`$self->{ParseOptions}{Source}{SystemId}'" : 'Byte Stream';
my ($pid) = open (ESIS, "-|");
if ($pid == 0) {
# 20% speed increase if grep swipes implieds (only 8% if
# we do it in `parse_fh'). XXX use a C routine or patch SP
open (SGML, "| $nsgmls_command 2>&1 | egrep -v '^A.* IMPLIED\$'")
or die "XML::ESISParser::parse: can't run \`$nsgmls' on \`$system_id'\n";
$self->{ParseOptions}{Source}{ByteStream}->print (*SGML);
close (SGML)
or die "XML::ESISParser::parse: can't run \`$nsgmls' on \`$system_id'\n";
exit 0;
} else {
eval { $result = $self->parse_fh (*ESIS) };
$retval = $@;
wait; # clean up that process
}
close (ESIS);
$self->{ParseOptions}{Source}{ByteStream}->close ();
if ($retval) {
die "XML::ESISParser::parse: unable to parse \`$system_id'\n$retval";
}
} elsif (defined $self->{ParseOptions}{Source}{String}) {
# call nsgmls with a literal string
} elsif (defined $self->{ParseOptions}{Source}{SystemId}) {
# if SystemId is a file, call nsgmls with file name
# otherwise, open stream on SystemId and do ByteStream
# FIXME this only handles file SystemIds right now
# 20% speed increase if grep swipes implieds (only 8% if
# we do it in `parse'). XXX use a C routine or patch SP
my $system_id = $self->{ParseOptions}{Source}{SystemId};
my ($fh) = IO::File->new
("$nsgmls_command '$system_id' 2>&1 | egrep -v '^A.* IMPLIED\$' |");
die "XML::ESISParser::parse: can't run \`$nsgmls' on \`$system_id'\n"
if (!defined $fh);
eval { $result = $self->parse_fh ($fh) };
my $retval = $@;
close ($fh);
if ($retval) {
die "XML::ESISParser::parse: unable to parse \`$system_id'\n$retval";
}
}
# clean up parser instance
delete $self->{ParseOptions};
delete $self->{DocumentHandler};
delete $self->{DTDHandler};
delete $self->{ErrorHandler};
return $result;
}
#
# Parse the `ESIS' information coming from `file'
#
sub parse_fh {
my ($self, $file) = @_;
my (@attributes, @properties, $files);
my $doc_h = $self->{ParseOptions}{DocumentHandler};
my $dtd_h = $self->{ParseOptions}{DTDHandler};
my $err_h = $self->{ParseOptions}{ErrorHandler};
# we cache these most commonly used `can()' calls
my $can_start_element = $doc_h->can('start_element');
my $can_end_element = $doc_h->can('end_element');
my $can_characters = $doc_h->can('characters');
my $can_record_end = $doc_h->can('record_end');
my $line = 0;
$doc_h->start_document( { } )
if ($doc_h->can('start_document'));
# 30% speed improvement by breaking the encapsulation
my ($is_filehandle) = (ref ($file) eq "FileHandle"
|| ref ($file) eq "IO::File");
while ($_ = ($is_filehandle ? <$file> : $file->getline())) {
chop;
if (/^A/) { # attribute
# Note: the output of `nsgmls' is `grep -v'ed to get rid of
# IMPLIED attributes, if we do it here we only get an 8%
# speed boost
my ($name, $type, $value) = split (/\s/, $', 3);
push (@attributes, $name => $value);
next;
}
if (/^\(/) { # start element
# break the encapsulation for an 8% boost
if ($#attributes >= 0) {
push (@properties, Attributes => { @attributes });
}
$doc_h->start_element ({ Name => $', @properties })
if ($can_start_element);
@properties = (); @attributes = ();
next;
}
if (/^\)/) { # end element
$doc_h->end_element ({ Name => $' })
if ($can_end_element);
next;
}
if (/^L/) { # line number
$line = $';
next;
}
if (/^-/) { # data (including sdata entities)
# This section is derived from David Megginson's SGMLSpm
my $sdata_flag = 0;
my $out = '';
my $data = $';
while ($data =~ /\\(\\|n|\||[0-7]{1,3})/) {
$out .= $`;
$data = $';
if ($1 eq '|') {
# beginning or end of SDATA
if ("$out" ne '') {
if ($sdata_flag) {
$doc_h->internal_entity_ref({ Name => $self->{'internal_entities_by_value'}{$out} })
if ($doc_h->can('internal_entity_ref'));
} else {
$doc_h->characters({ Data => $out })
if ($can_characters);
}
$out = '';
}
$sdata_flag = !$sdata_flag;
} elsif ($1 eq 'n') {
# record end
if ("$out" ne '') {
if ($sdata_flag) {
$doc_h->internal_entity_ref({ Name => $self->{'internal_entities_by_value'}{$out} })
if ($doc_h->can('internal_entity_ref'));
} else {
$doc_h->characters({ Data => $out })
if ($can_characters);
}
$out = '';
}
if ($can_record_end) {
$doc_h->record_end( { } );
} else {
$doc_h->characters({ Data => "\n" })
if ($can_characters);
}
} elsif ($1 eq '\\') {
$out .= '\\';
} else {
$out .= chr(oct($1));
}
}
$out .= $data;
if ("$out" ne '') {
if ($sdata_flag) {
$doc_h->internal_entity_ref({ Name => $self->{'internal_entities_by_value'}{$out} })
if ($doc_h->can('internal_entity_ref'));
} else {
$doc_h->characters({ Data => $out })
if ($can_characters);
}
}
next;
}
if (/^s/) { # sysid
push (@properties, SystemId => $');
next;
}
if (/^p/) { # pubid
push (@properties, PublicId => $');
next;
}
if (/^f/) { # file
if (!defined $files) {
$files = $';
} elsif (!ref $files) {
$files = [ $files, $' ];
} else {
push (@$files, $');
}
next;
}
if (/^E/) { # external entity definition
my ($entity_data) = $';
$entity_data =~ /^(\S+) (\S+) (\S+)$/
or die "XML::ESISParser::parse_fh: bad external entity event data: $entity_data\n";
my ($name,$type,$notation) = ($1,$2,$3);
if (defined $files) {
push (@properties, GeneratedId => $files);
}
$dtd_h->external_entity_decl ({ Name => $name, Type => $type,
Notation => $notation, @properties })
if ($dtd_h->can('external_entity_decl'));
@properties = (); undef $files;
next;
}
if (/^I/) { # internal entity definition
my ($name, $type, $value) = split (/\s/, $', 3);
$self->{'internal_entities_by_value'}{$value} = $name;
$dtd_h->internal_entity_decl ({ Name => $name, Type => $type,
Value => $value })
if ($dtd_h->can('internal_entity_decl'));
next;
}
if (/^&/) { # external entity reference
my ($name) = $';
$doc_h->external_entity_ref({ Name => $name })
if ($doc_h->can('external_entity_ref'));
next;
}
if (/^\?/) { # processing instruction (PI)
my ($data) = $';
if ($self->{ParseOptions}{IsSGML}) {
$doc_h->processing_instruction({ Data => $data })
if ($doc_h->can('processing_instruction'));
} else {
my ($target, $pi_data) = split (/\s+/, $data, 2);
$doc_h->processing_instruction({ Target => $target, Data => $pi_data })
if ($doc_h->can('processing_instruction'));
}
next;
}
if (/^N/) { # notation definition
my ($name) = $';
if (defined $files) {
push (@properties, GeneratedId => $files);
}
$dtd_h->notation_decl ({ Name => $name, @properties })
if ($dtd_h->can('notation_decl'));
@properties = (); undef $files;
next;
}
if (/^S/) { # subdoc definition
my ($name) = $';
if (defined $files) {
push (@properties, GeneratedId => $files);
}
$dtd_h->subdoc_entity_decl ({ Name => $name, @properties })
if ($dtd_h->can('subdoc_entity_decl'));
@properties = (); undef $files;
next;
}
if (/^T/) { # external SGML text entity definition
my ($name) = $';
if (defined $files) {
push (@properties, GeneratedId => $files);
}
$dtd_h->external_sgml_entity_decl ({ Name => $name, @properties })
if ($dtd_h->can('external_sgml_entity_decl'));
@properties = (); undef $files;
next;
}
if (/^D/) { # data attribute
# FIXME
my $message = "XML::ESISParser: can't handle data attributes yet\n";
if ($err_h->can('error')) {
$err_h->error ({ Message => $message });
} else {
die "$message";
}
next;
}
if (/^D/) { # link attribute
# FIXME
my $message = "XML::ESISParser: can't handle link attributes yet\n";
if ($err_h->can('error')) {
$err_h->error ({ Message => $message });
} else {
die "$message";
}
next;
}
if (/^{/) { # subdoc start
my ($name) = $';
$doc_h->start_subdoc ({ Name => $name })
if ($doc_h->can('start_subdoc'));
next;
}
if (/^}/) { # subdoc end
my ($name) = $';
$doc_h->end_subdoc ({ Name => $name })
if ($doc_h->can('end_subdoc'));
next;
}
if (/^#/) { # appinfo
my ($text) = $';
$doc_h->appinfo ({ Text => $text })
if ($doc_h->can('appinfo'));
next;
}
if (/^i/) { # next element is an included subelement
push (@properties, IncludedSubelement => 1);
next;
}
if (/^e/) { # next element is declared empty
push (@properties, Empty => 1);
next;
}
if (/^C/) { # conforming
$doc_h->conforming({})
if ($doc_h->can('conforming'));
next;
}
if (/^$self->{ParseOptions}{NSGMLS}:/) { # `nsgmls' error
my $message = $_;
if ($err_h->can('error')) {
$err_h->error ({ Message => $message });
} else {
die "$message\n";
}
next;
}
my ($op) = substr ($_, 0, 1);
my $message = "XML::ESISParser::parse_fh: ESIS command character \`$op' not recognized when reading line \`$_' around line $line ($.)";
if ($err_h->can('error')) {
$err_h->error ({ Message => $message });
} else {
die "$message";
}
}
if ($doc_h->can('end_document')) {
return $doc_h->end_document({});
} else {
return ();
}
}
1;
__END__
=head1 NAME
XML::ESISParser - Perl SAX parser using nsgmls
=head1 SYNOPSIS
use XML::ESISParser;
$parser = XML::ESISParser->new( [OPTIONS] );
$result = $parser->parse( [OPTIONS] );
$result = $parser->parse($string);
=head1 DESCRIPTION
C<XML::ESISParser> is a Perl SAX parser using the `nsgmls' command of
James Clark's SGML Parser (SP), a validating XML and SGML parser.
This man page summarizes the specific options, handlers, and
properties supported by C<XML::ESISParser>; please refer to the Perl
SAX standard in `C<SAX.pod>' for general usage information.
C<XML::ESISParser> defaults to parsing XML and has an option for
parsing SGML.
`C<nsgmls>' source, and binaries for some platforms, is available from
<http://www.jclark.com/>. `C<nsgmls>' is included in both the SP and
Jade packages.
=head1 METHODS
=over 4
=item new
Creates a new parser object. Default options for parsing, described
below, are passed as key-value pairs or as a single hash. Options may
be changed directly in the parser object unless stated otherwise.
Options passed to `C<parse()>' override the default options in the
parser object for the duration of the parse.
=back
=head1 OPTIONS
The following options are supported by C<XML::ESISParser>:
Handler default handler to receive events
DocumentHandler handler to receive document events
DTDHandler handler to receive DTD events
ErrorHandler handler to receive error events
Source hash containing the input source for parsing
IsSGML the document to be parsed is in SGML
If no handlers are provided then all events will be silently ignored.
If a single string argument is passed to the `C<parse()>' method, it
is treated as if a `C<Source>' option was given with a `C<String>'
parameter.
The `C<Source>' hash may contain the following parameters:
ByteStream The raw byte stream (file handle) containing the
document.
String A string containing the document.
SystemId The system identifier (URI) of the document.
If more than one of `C<ByteStream>', `C<String>', or `C<SystemId>',
then preference is given first to `C<ByteStream>', then `C<String>',
then `C<SystemId>'.
=head1 HANDLERS
The following handlers and properties are supported by
C<XML::ESISParser>:
=head2 DocumentHandler methods
=over 4
=item start_document
Receive notification of the beginning of a document.
No properties defined.
=item end_document
Receive notification of the end of a document.
No properties defined.
=item start_element
Receive notification of the beginning of an element.
Name The element type name.
Attributes A hash containing the attributes attached to the
element, if any.
IncludedSubelement This element is an included subelement.
Empty This element is declared empty.
The `C<Attributes>' hash contains only string values. The `C<Empty>'
flag is not set for an element that merely has no content, it is set
only if the DTD declares it empty.
BETA: Attribute values currently do not expand SData entities into
entity objects, they are still in the system data notation used by
nsgmls (inside `|'). A future version of XML::ESISParser will also
convert other types of attributes into their respective objects,
currently just their notation or entity names are given.
=item end_element
Receive notification of the end of an element.
Name The element type name.
=item characters
Receive notification of character data.
Data The characters from the document.
=item record_end
Receive notification of a record end sequence. XML applications
should convert this to a new-line.
=item processing_instruction
Receive notification of a processing instruction.
Target The processing instruction target in XML.
Data The processing instruction data, if any.
=item internal_entity_ref
Receive notification of a system data (SData) internal entity
reference.
Name The name of the internal entity reference.
=item external_entity_ref
Receive notification of a external entity reference.
Name The name of the external entity reference.
=item start_subdoc
Receive notification of the start of a sub document.
Name The name of the external entity reference.
=item end_subdoc
Receive notification of the end of a sub document.
Name The name of the external entity reference.
=item conforming
Receive notification that the document just parsed conforms to it's
document type declaration (DTD).
No properties defined.
=back
=head2 DTDHandler methods
=over 4
=item external_entity_decl
Receive notification of an external entity declaration.
Name The entity's entity name.
Type The entity's type (CDATA, NDATA, etc.)
SystemId The entity's system identifier.
PublicId The entity's public identifier, if any.
GeneratedId Generated system identifiers, if any.
=item internal_entity_decl
Receive notification of an internal entity declaration.
Name The entity's entity name.
Type The entity's type (CDATA, NDATA, etc.)
Value The entity's character value.
=item notation_decl
Receive notification of a notation declaration.
Name The notation's name.
SystemId The notation's system identifier.
PublicId The notation's public identifier, if any.
GeneratedId Generated system identifiers, if any.
=item subdoc_entity_decl
Receive notification of a subdocument entity declaration.
Name The entity's entity name.
SystemId The entity's system identifier.
PublicId The entity's public identifier, if any.
GeneratedId Generated system identifiers, if any.
=item external_sgml_entity_decl
Receive notification of an external SGML-entity declaration.
Name The entity's entity name.
SystemId The entity's system identifier.
PublicId The entity's public identifier, if any.
GeneratedId Generated system identifiers, if any.
=back
=head1 AUTHOR
Ken MacLeod, ken@bitsko.slc.ut.us
=head1 SEE ALSO
perl(1), PerlSAX.pod(3)
Extensible Markup Language (XML) <http://www.w3c.org/XML/>
SAX 1.0: The Simple API for XML <http://www.megginson.com/SAX/>
SGML Parser (SP) <http://www.jclark.com/sp/>
=cut
|