Commit 913c9a6c authored by Andrey Shevchuk's avatar Andrey Shevchuk

removed old XML::Parser::Expat

parent 736b6dc6
# XML::Parser
#
# Copyright (c) 1998-2000 Larry Wall and Clark Cooper
# 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::Parser;
use strict;
our ( $VERSION, $LWP_load_failed );
use Carp;
BEGIN {
require XML::Parser::Expat;
$VERSION = '2.46';
die "Parser.pm and Expat.pm versions don't match"
unless $VERSION eq $XML::Parser::Expat::VERSION;
}
$LWP_load_failed = 0;
sub new {
my ( $class, %args ) = @_;
my $style = $args{Style};
my $nonexopt = $args{Non_Expat_Options} ||= {};
$nonexopt->{Style} = 1;
$nonexopt->{Non_Expat_Options} = 1;
$nonexopt->{Handlers} = 1;
$nonexopt->{_HNDL_TYPES} = 1;
$nonexopt->{NoLWP} = 1;
$args{_HNDL_TYPES} = {%XML::Parser::Expat::Handler_Setters};
$args{_HNDL_TYPES}->{Init} = 1;
$args{_HNDL_TYPES}->{Final} = 1;
$args{Handlers} ||= {};
my $handlers = $args{Handlers};
if ( defined($style) ) {
my $stylepkg = $style;
if ( $stylepkg !~ /::/ ) {
$stylepkg = "\u$style";
eval {
my $fullpkg = "XML::Parser::Style::$stylepkg";
my $stylefile = $fullpkg;
$stylefile =~ s/::/\//g;
require "$stylefile.pm";
$stylepkg = $fullpkg;
};
if ($@) {
# fallback to old behaviour
$stylepkg = "XML::Parser::$stylepkg";
}
}
foreach my $htype ( keys %{ $args{_HNDL_TYPES} } ) {
# Handlers explicitly given override
# handlers from the Style package
unless ( defined( $handlers->{$htype} ) ) {
# A handler in the style package must either have
# exactly the right case as the type name or a
# completely lower case version of it.
my $hname = "${stylepkg}::$htype";
if ( defined(&$hname) ) {
$handlers->{$htype} = \&$hname;
next;
}
$hname = "${stylepkg}::\L$htype";
if ( defined(&$hname) ) {
$handlers->{$htype} = \&$hname;
next;
}
}
}
}
unless ( defined( $handlers->{ExternEnt} )
or defined( $handlers->{ExternEntFin} ) ) {
if ( $args{NoLWP} or $LWP_load_failed ) {
$handlers->{ExternEnt} = \&file_ext_ent_handler;
$handlers->{ExternEntFin} = \&file_ext_ent_cleanup;
}
else {
# The following just bootstraps the real LWP external entity
# handler
$handlers->{ExternEnt} = \&initial_ext_ent_handler;
# No cleanup function available until LWPExternEnt.pl loaded
}
}
$args{Pkg} ||= caller;
bless \%args, $class;
} # End of new
sub setHandlers {
my ( $self, @handler_pairs ) = @_;
croak('Uneven number of arguments to setHandlers method')
if ( int(@handler_pairs) & 1 );
my @ret;
while (@handler_pairs) {
my $type = shift @handler_pairs;
my $handler = shift @handler_pairs;
unless ( defined( $self->{_HNDL_TYPES}->{$type} ) ) {
my @types = sort keys %{ $self->{_HNDL_TYPES} };
croak("Unknown Parser handler type: $type\n Valid types: @types");
}
push( @ret, $type, $self->{Handlers}->{$type} );
$self->{Handlers}->{$type} = $handler;
}
return @ret;
}
sub parse_start {
my $self = shift;
my @expat_options = ();
my ( $key, $val );
while ( ( $key, $val ) = each %{$self} ) {
push( @expat_options, $key, $val )
unless exists $self->{Non_Expat_Options}->{$key};
}
my %handlers = %{ $self->{Handlers} };
my $init = delete $handlers{Init};
my $final = delete $handlers{Final};
my $expatnb = XML::Parser::ExpatNB->new( @expat_options, @_ );
$expatnb->setHandlers(%handlers);
&$init($expatnb)
if defined($init);
$expatnb->{_State_} = 1;
$expatnb->{FinalHandler} = $final
if defined($final);
return $expatnb;
}
sub parse {
my $self = shift;
my $arg = shift;
my @expat_options = ();
my ( $key, $val );
while ( ( $key, $val ) = each %{$self} ) {
push( @expat_options, $key, $val )
unless exists $self->{Non_Expat_Options}->{$key};
}
my $expat = XML::Parser::Expat->new( @expat_options, @_ );
my %handlers = %{ $self->{Handlers} };
my $init = delete $handlers{Init};
my $final = delete $handlers{Final};
$expat->setHandlers(%handlers);
if ( $self->{Base} ) {
$expat->base( $self->{Base} );
}
&$init($expat)
if defined($init);
my @result = ();
my $result;
eval { $result = $expat->parse($arg); };
my $err = $@;
if ($err) {
$expat->release;
die $err;
}
if ( $result and defined($final) ) {
if (wantarray) {
@result = &$final($expat);
}
else {
$result = &$final($expat);
}
}
$expat->release;
return unless defined wantarray;
return wantarray ? @result : $result;
}
sub parsestring {
my $self = shift;
$self->parse(@_);
}
sub parsefile {
my $self = shift;
my $file = shift;
open( my $fh, '<', $file ) or croak "Couldn't open $file:\n$!";
binmode($fh);
my @ret;
my $ret;
$self->{Base} = $file;
if (wantarray) {
eval { @ret = $self->parse( $fh, @_ ); };
}
else {
eval { $ret = $self->parse( $fh, @_ ); };
}
my $err = $@;
close($fh);
die $err if $err;
return unless defined wantarray;
return wantarray ? @ret : $ret;
}
sub initial_ext_ent_handler {
# This just bootstraps in the real lwp_ext_ent_handler which
# also loads the URI and LWP modules.
unless ($LWP_load_failed) {
local ($^W) = 0;
my $stat = eval { require('XML/Parser/LWPExternEnt.pl'); };
if ($stat) {
$_[0]->setHandlers(
ExternEnt => \&lwp_ext_ent_handler,
ExternEntFin => \&lwp_ext_ent_cleanup
);
goto &lwp_ext_ent_handler;
}
# Failed to load lwp handler, act as if NoLWP
$LWP_load_failed = 1;
my $cmsg = "Couldn't load LWP based external entity handler\n" . "Switching to file-based external entity handler\n" . " (To avoid this message, use NoLWP option to XML::Parser)\n";
warn($cmsg);
}
$_[0]->setHandlers(
ExternEnt => \&file_ext_ent_handler,
ExternEntFin => \&file_ext_ent_cleanup
);
goto &file_ext_ent_handler;
}
sub file_ext_ent_handler {
my ( $xp, $base, $path ) = @_;
# Prepend base only for relative paths
if ( defined($base)
and not( $path =~ m!^(?:[\\/]|\w+:)! ) ) {
my $newpath = $base;
$newpath =~ s![^\\/:]*$!$path!;
$path = $newpath;
}
if ( $path =~ /^\s*[|>+]/
or $path =~ /\|\s*$/ ) {
$xp->{ErrorMessage} .= "System ID ($path) contains Perl IO control characters";
return undef;
}
require IO::File;
my $fh = IO::File->new($path);
unless ( defined $fh ) {
$xp->{ErrorMessage} .= "Failed to open $path:\n$!";
return undef;
}
$xp->{_BaseStack} ||= [];
$xp->{_FhStack} ||= [];
push( @{ $xp->{_BaseStack} }, $base );
push( @{ $xp->{_FhStack} }, $fh );
$xp->base($path);
return $fh;
}
sub file_ext_ent_cleanup {
my ($xp) = @_;
my $fh = pop( @{ $xp->{_FhStack} } );
$fh->close;
my $base = pop( @{ $xp->{_BaseStack} } );
$xp->base($base);
}
1;
__END__
=head1 NAME
XML::Parser - A perl module for parsing XML documents
=head1 SYNOPSIS
use XML::Parser;
$p1 = XML::Parser->new(Style => 'Debug');
$p1->parsefile('REC-xml-19980210.xml');
$p1->parse('<foo id="me">Hello World</foo>');
# Alternative
$p2 = XML::Parser->new(Handlers => {Start => \&handle_start,
End => \&handle_end,
Char => \&handle_char});
$p2->parse($socket);
# Another alternative
$p3 = XML::Parser->new(ErrorContext => 2);
$p3->setHandlers(Char => \&text,
Default => \&other);
open(my $fh, 'xmlgenerator |');
$p3->parse($foo, ProtocolEncoding => 'ISO-8859-1');
close($foo);
$p3->parsefile('junk.xml', ErrorContext => 3);
=begin man
.ds PI
=end man
=head1 DESCRIPTION
This module provides ways to parse XML documents. It is built on top of
L<XML::Parser::Expat>, which is a lower level interface to James Clark's
expat library. Each call to one of the parsing methods creates a new
instance of XML::Parser::Expat which is then used to parse the document.
Expat options may be provided when the XML::Parser object is created.
These options are then passed on to the Expat object on each parse call.
They can also be given as extra arguments to the parse methods, in which
case they override options given at XML::Parser creation time.
The behavior of the parser is controlled either by C<L</STYLES>> and/or
C<L</HANDLERS>> options, or by L</setHandlers> method. These all provide
mechanisms for XML::Parser to set the handlers needed by XML::Parser::Expat.
If neither C<Style> nor C<Handlers> are specified, then parsing just
checks the document for being well-formed.
When underlying handlers get called, they receive as their first parameter
the I<Expat> object, not the Parser object.
=head1 METHODS
=over 4
=item new
This is a class method, the constructor for XML::Parser. Options are passed
as keyword value pairs. Recognized options are:
=over 4
=item * Style
This option provides an easy way to create a given style of parser. The
built in styles are: L<"Debug">, L<"Subs">, L<"Tree">, L<"Objects">,
and L<"Stream">. These are all defined in separate packages under
C<XML::Parser::Style::*>, and you can find further documentation for
each style both below, and in those packages.
Custom styles can be provided by giving a full package name containing
at least one '::'. This package should then have subs defined for each
handler it wishes to have installed. See L<"STYLES"> below
for a discussion of each built in style.
=item * Handlers
When provided, this option should be an anonymous hash containing as
keys the type of handler and as values a sub reference to handle that
type of event. All the handlers get passed as their 1st parameter the
instance of expat that is parsing the document. Further details on
handlers can be found in L<"HANDLERS">. Any handler set here
overrides the corresponding handler set with the Style option.
=item * Pkg
Some styles will refer to subs defined in this package. If not provided,
it defaults to the package which called the constructor.
=item * ErrorContext
This is an Expat option. When this option is defined, errors are reported
in context. The value should be the number of lines to show on either side
of the line in which the error occurred.
=item * ProtocolEncoding
This is an Expat option. This sets the protocol encoding name. It defaults
to none. The built-in encodings are: C<UTF-8>, C<ISO-8859-1>, C<UTF-16>, and
C<US-ASCII>. Other encodings may be used if they have encoding maps in one
of the directories in the @Encoding_Path list. Check L<"ENCODINGS"> for
more information on encoding maps. Setting the protocol encoding overrides
any encoding in the XML declaration.
=item * Namespaces
This is an Expat option. If this is set to a true value, then namespace
processing is done during the parse. See L<XML::Parser::Expat/"Namespaces">
for further discussion of namespace processing.
=item * NoExpand
This is an Expat option. Normally, the parser will try to expand references
to entities defined in the internal subset. If this option is set to a true
value, and a default handler is also set, then the default handler will be
called when an entity reference is seen in text. This has no effect if a
default handler has not been registered, and it has no effect on the expansion
of entity references inside attribute values.
=item * Stream_Delimiter
This is an Expat option. It takes a string value. When this string is found
alone on a line while parsing from a stream, then the parse is ended as if it
saw an end of file. The intended use is with a stream of xml documents in a
MIME multipart format. The string should not contain a trailing newline.
=item * ParseParamEnt
This is an Expat option. Unless standalone is set to "yes" in the XML
declaration, setting this to a true value allows the external DTD to be read,
and parameter entities to be parsed and expanded.
=item * NoLWP
This option has no effect if the ExternEnt or ExternEntFin handlers are
directly set. Otherwise, if true, it forces the use of a file based external
entity handler.
=item * Non_Expat_Options
If provided, this should be an anonymous hash whose keys are options that
shouldn't be passed to Expat. This should only be of concern to those
subclassing XML::Parser.
=back
=item setHandlers(TYPE, HANDLER [, TYPE, HANDLER [...]])
This method registers handlers for various parser events. It overrides any
previous handlers registered through the Style or Handler options or through
earlier calls to setHandlers. By providing a false or undefined value as
the handler, the existing handler can be unset.
This method returns a list of type, handler pairs corresponding to the
input. The handlers returned are the ones that were in effect prior to
the call.
See a description of the handler types in L<"HANDLERS">.
=item parse(SOURCE [, OPT => OPT_VALUE [...]])
The SOURCE parameter should either be a string containing the whole XML
document, or it should be an open IO::Handle. Constructor options to
XML::Parser::Expat given as keyword-value pairs may follow the SOURCE
parameter. These override, for this call, any options or attributes passed
through from the XML::Parser instance.
A die call is thrown if a parse error occurs. Otherwise it will return 1
or whatever is returned from the B<Final> handler, if one is installed.
In other words, what parse may return depends on the style.
=item parsestring
This is just an alias for parse for backwards compatibility.
=item parsefile(FILE [, OPT => OPT_VALUE [...]])
Open FILE for reading, then call parse with the open handle. The file
is closed no matter how parse returns. Returns what parse returns.
=item parse_start([ OPT => OPT_VALUE [...]])
Create and return a new instance of XML::Parser::ExpatNB. Constructor
options may be provided. If an init handler has been provided, it is
called before returning the ExpatNB object. Documents are parsed by
making incremental calls to the parse_more method of this object, which
takes a string. A single call to the parse_done method of this object,
which takes no arguments, indicates that the document is finished.
If there is a final handler installed, it is executed by the parse_done
method before returning and the parse_done method returns whatever is
returned by the final handler.
=back
=head1 HANDLERS
Expat is an event based parser. As the parser recognizes parts of the
document (say the start or end tag for an XML element), then any handlers
registered for that type of an event are called with suitable parameters.
All handlers receive an instance of XML::Parser::Expat as their first
argument. See L<XML::Parser::Expat/"METHODS"> for a discussion of the
methods that can be called on this object.
=head2 Init (Expat)
This is called just before the parsing of the document starts.
=head2 Final (Expat)
This is called just after parsing has finished, but only if no errors
occurred during the parse. Parse returns what this returns.
=head2 Start (Expat, Element [, Attr, Val [,...]])
This event is generated when an XML start tag is recognized. Element is the
name of the XML element type that is opened with the start tag. The Attr &
Val pairs are generated for each attribute in the start tag.
=head2 End (Expat, Element)
This event is generated when an XML end tag is recognized. Note that
an XML empty tag (<foo/>) generates both a start and an end event.
=head2 Char (Expat, String)
This event is generated when non-markup is recognized. The non-markup
sequence of characters is in String. A single non-markup sequence of
characters may generate multiple calls to this handler. Whatever the
encoding of the string in the original document, this is given to the
handler in UTF-8.
=head2 Proc (Expat, Target, Data)
This event is generated when a processing instruction is recognized.
=head2 Comment (Expat, Data)
This event is generated when a comment is recognized.
=head2 CdataStart (Expat)
This is called at the start of a CDATA section.
=head2 CdataEnd (Expat)
This is called at the end of a CDATA section.
=head2 Default (Expat, String)
This is called for any characters that don't have a registered handler.
This includes both characters that are part of markup for which no
events are generated (markup declarations) and characters that
could generate events, but for which no handler has been registered.
Whatever the encoding in the original document, the string is returned to
the handler in UTF-8.
=head2 Unparsed (Expat, Entity, Base, Sysid, Pubid, Notation)
This is called for a declaration of an unparsed entity. Entity is the name
of the entity. Base is the base to be used for resolving a relative URI.
Sysid is the system id. Pubid is the public id. Notation is the notation
name. Base and Pubid may be undefined.
=head2 Notation (Expat, Notation, Base, Sysid, Pubid)
This is called for a declaration of notation. Notation is the notation name.
Base is the base to be used for resolving a relative URI. Sysid is the system
id. Pubid is the public id. Base, Sysid, and Pubid may all be undefined.
=head2 ExternEnt (Expat, Base, Sysid, Pubid)
This is called when an external entity is referenced. Base is the base to be
used for resolving a relative URI. Sysid is the system id. Pubid is the public
id. Base, and Pubid may be undefined.
This handler should either return a string, which represents the contents of
the external entity, or return an open filehandle that can be read to obtain
the contents of the external entity, or return undef, which indicates the
external entity couldn't be found and will generate a parse error.
If an open filehandle is returned, it must be returned as either a glob
(*FOO) or as a reference to a glob (e.g. an instance of IO::Handle).
A default handler is installed for this event. The default handler is
XML::Parser::lwp_ext_ent_handler unless the NoLWP option was provided with
a true value, otherwise XML::Parser::file_ext_ent_handler is the default
handler for external entities. Even without the NoLWP option, if the
URI or LWP modules are missing, the file based handler ends up being used
after giving a warning on the first external entity reference.
The LWP external entity handler will use proxies defined in the environment
(http_proxy, ftp_proxy, etc.).
Please note that the LWP external entity handler reads the entire
entity into a string and returns it, where as the file handler opens a
filehandle.
Also note that the file external entity handler will likely choke on
absolute URIs or file names that don't fit the conventions of the local
operating system.
The expat base method can be used to set a basename for
relative pathnames. If no basename is given, or if the basename is itself
a relative name, then it is relative to the current working directory.
=head2 ExternEntFin (Expat)
This is called after parsing an external entity. It's not called unless
an ExternEnt handler is also set. There is a default handler installed
that pairs with the default ExternEnt handler.
If you're going to install your own ExternEnt handler, then you should
set (or unset) this handler too.
=head2 Entity (Expat, Name, Val, Sysid, Pubid, Ndata, IsParam)
This is called when an entity is declared. For internal entities, the Val
parameter will contain the value and the remaining three parameters will be
undefined. For external entities, the Val parameter will be undefined, the
Sysid parameter will have the system id, the Pubid parameter will have the
public id if it was provided (it will be undefined otherwise), the Ndata
parameter will contain the notation for unparsed entities. If this is a
parameter entity declaration, then the IsParam parameter is true.
Note that this handler and the Unparsed handler above overlap. If both are
set, then this handler will not be called for unparsed entities.
=head2 Element (Expat, Name, Model)
The element handler is called when an element declaration is found. Name
is the element name, and Model is the content model as an XML::Parser::Content
object. See L<XML::Parser::Expat/"XML::Parser::ContentModel Methods">
for methods available for this class.
=head2 Attlist (Expat, Elname, Attname, Type, Default, Fixed)
This handler is called for each attribute in an ATTLIST declaration.
So an ATTLIST declaration that has multiple attributes will generate multiple
calls to this handler. The Elname parameter is the name of the element with
which the attribute is being associated. The Attname parameter is the name
of the attribute. Type is the attribute type, given as a string. Default is
the default value, which will either be "#REQUIRED", "#IMPLIED" or a quoted
string (i.e. the returned string will begin and end with a quote character).
If Fixed is true, then this is a fixed attribute.
=head2 Doctype (Expat, Name, Sysid, Pubid, Internal)
This handler is called for DOCTYPE declarations. Name is the document type
name. Sysid is the system id of the document type, if it was provided,
otherwise it's undefined. Pubid is the public id of the document type,
which will be undefined if no public id was given. Internal is the internal
subset, given as a string. If there was no internal subset, it will be
undefined. Internal will contain all whitespace, comments, processing
instructions, and declarations seen in the internal subset. The declarations
will be there whether or not they have been processed by another handler
(except for unparsed entities processed by the Unparsed handler). However,
comments and processing instructions will not appear if they've been processed
by their respective handlers.
=head2 * DoctypeFin (Parser)
This handler is called after parsing of the DOCTYPE declaration has finished,
including any internal or external DTD declarations.
=head2 XMLDecl (Expat, Version, Encoding, Standalone)
This handler is called for xml declarations. Version is a string containing
the version. Encoding is either undefined or contains an encoding string.
Standalone will be either true, false, or undefined if the standalone attribute
is yes, no, or not made respectively.
=head1 STYLES
=head2 Debug
This just prints out the document in outline form. Nothing special is
returned by parse.
=head2 Subs
Each time an element starts, a sub by that name in the package specified
by the Pkg option is called with the same parameters that the Start
handler gets called with.
Each time an element ends, a sub with that name appended with an underscore
("_"), is called with the same parameters that the End handler gets called
with.
Nothing special is returned by parse.
=head2 Tree
Parse will return a parse tree for the document. Each node in the tree
takes the form of a tag, content pair. Text nodes are represented with
a pseudo-tag of "0" and the string that is their content. For elements,
the content is an array reference. The first item in the array is a
(possibly empty) hash reference containing attributes. The remainder of
the array is a sequence of tag-content pairs representing the content
of the element.
So for example 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 root document "foo", has 3 children: a "head" element, a "bar"
element and the text "do". After the empty attribute hash, these are
represented in it's contents by 3 tag-content pairs.
=head2 Objects
This is similar to the Tree style, except that a hash object is created for
each element. The corresponding object will be in the class whose name
is created by appending "::" and the element name to the package set with
the Pkg option. Non-markup text will be in the ::Characters class. The
contents of the corresponding object will be in an anonymous array that
is the value of the Kids property for that object.
=head2 Stream
This style also uses the Pkg package. If none of the subs that this
style looks for is there, then the effect of parsing with this style is
to print a canonical copy of the document without comments or declarations.
All the subs receive as their 1st parameter the Expat instance for the
document they're parsing.
It looks for the following routines:
=over 4
=item * StartDocument
Called at the start of the parse .
=item * StartTag
Called for every start tag with a second parameter of the element type. The $_
variable will contain a copy of the tag and the %_ variable will contain
attribute values supplied for that element.
=item * EndTag
Called for every end tag with a second parameter of the element type. The $_
variable will contain a copy of the end tag.
=item * Text
Called just before start or end tags with accumulated non-markup text in
the $_ variable.
=item * PI
Called for processing instructions. The $_ variable will contain a copy of
the PI and the target and data are sent as 2nd and 3rd parameters
respectively.
=item * EndDocument
Called at conclusion of the parse.
=back
=head1 ENCODINGS
XML documents may be encoded in character sets other than Unicode as
long as they may be mapped into the Unicode character set. Expat has
further restrictions on encodings. Read the xmlparse.h header file in
the expat distribution to see details on these restrictions.
Expat has built-in encodings for: C<UTF-8>, C<ISO-8859-1>, C<UTF-16>, and
C<US-ASCII>. Encodings are set either through the XML declaration
encoding attribute or through the ProtocolEncoding option to XML::Parser
or XML::Parser::Expat.
For encodings other than the built-ins, expat calls the function
load_encoding in the Expat package with the encoding name. This function
looks for a file in the path list @XML::Parser::Expat::Encoding_Path, that
matches the lower-cased name with a '.enc' extension. The first one it
finds, it loads.
If you wish to build your own encoding maps, check out the XML::Encoding
module from CPAN.
=head1 AUTHORS
Larry Wall <F<larry@wall.org>> wrote version 1.0.
Clark Cooper <F<coopercc@netheaven.com>> picked up support, changed the API
for this version (2.x), provided documentation,
and added some standard package features.
Matt Sergeant <F<matt@sergeant.org>> is now maintaining XML::Parser
=cut
Mapping files for Japanese encodings
1998 12/25
Fuji Xerox Information Systems
MURATA Makoto
1. Overview
This version of XML::Parser and XML::Encoding does not come with map files for
the charset "Shift_JIS" and the charset "euc-jp". Unfortunately, each of these
charsets has more than one mapping. None of these mappings are
considered as authoritative.
Therefore, we have come to believe that it is dangerous to provide map files
for these charsets. Rather, we introduce several private charsets and map
files for these private charsets. If IANA, Unicode Consoritum, and JIS
eventually reach a consensus, we will be able to provide map files for
"Shift_JIS" and "euc-jp".
2. Different mappings from existing charsets to Unicode
1) Different mappings in JIS X0221 and Unicode
The mapping between JIS X0208:1990 and Unicode 1.1 and the mapping
between JIS X0212:1990 and Unicode 1.1 are published from Unicode
consortium. They are available at
ftp://ftp.unicode.org/Public/MAPPINGS/EASTASIA/JIS/JIS0208.TXT and
ftp://ftp.unicode.org/Public/MAPPINGS/EASTASIA/JIS/JIS0212.TXT,
respectively.) These mapping files have a note as below:
# The kanji mappings are a normative part of ISO/IEC 10646. The
# non-kanji mappings are provisional, pending definition of
# official mappings by Japanese standards bodies.
Unfortunately, the non-kanji mappings in the Japanese standard for ISO 10646/1,
namely JIS X 0221:1995, is different from the Unicode Consortium mapping since
0x213D of JIS X 0208 is mapped to U+2014 (em dash) rather than U+2015
(horizontal bar). Furthermore, JIS X 0221 clearly says that the mapping is
informational and non-normative. As a result, some companies (e.g., Microsoft and
Apple) have introduced slightly different mappings. Therefore, neither the
Unicode consortium mapping nor the JIS X 0221 mapping are considered as
authoritative.
2) Shift-JIS
This charset is especially problematic, since its definition has been unclear
since its inception.
The current registration of the charset "Shift_JIS" is as below:
>Name: Shift_JIS (preferred MIME name)
>MIBenum: 17
>Source: A Microsoft code that extends csHalfWidthKatakana to include
> kanji by adding a second byte when the value of the first
> byte is in the ranges 81-9F or E0-EF.
>Alias: MS_Kanji
>Alias: csShiftJIS
First, this does not reference to the mapping "Shift-JIS to Unicode"
published by the Unicode consortium (available at
ftp://ftp.unicode.org/Public/MAPPINGS/EASTASIA/JIS/SHIFTJIS.TXT).
Second, "kanji" in this registration can be interepreted in different ways.
Does this "kanji" reference to JIS X0208:1978, JIS X0208:1983, or JIS
X0208:1990(== JIS X0208:1997)? These three standards are *incompatible* with
each other. Moreover, we can even argue that "kanji" refers to JIS X0212 or
ideographic characters in other countries.
Third, each company has extended Shift JIS. For example, Microsoft introduced
OEM extensions (NEC extensionsand IBM extensions).
Forth, Shift JIS uses JIS X0201, which is almost upper-compatible with US-ASCII
but is not quite. 5C and 7E of JIS X 0201 are different from backslash and
tilde, respectively. However, many programming languages (e.g., Java)
ignore this difference and assumes that 5C and 7E of Shift JIS are backslash
and tilde.
3. Proposed charsets and mappings
As a tentative solution, we introduce two private charsets for EUC-JP and four
priviate charsets for Shift JIS.
1) EUC-JP
We have two charsets, namely "x-eucjp-unicode" and "x-eucjp-jisx0221". Their
difference is only one code point. The mapping for the former is based
on the Unicode Consortium mapping, while the latter is based on the JIS X0221
mapping.
2) Shift JIS
We have four charsets, namely x-sjis-unicode, x-sjis-jisx0221,
x-sjis-jdk117, and x-sjis-cp932.
The mapping for the charset x-sjis-unicode is the one published by the Unicode
consortium. The mapping for x-sjis-jisx0221 is almost equivalent to
x-sjis-unicode, but 0x213D of JIS X 0208 is mapped to U+2014 (em dash) rather
than U+2015. The charset x-sjis-jdk117 is again almost equivalent to
x-sjis-unicode, but 0x5C and 0x7E of JIS X0201 are mapped to backslash and
tilde.
The charset x-sjis-cp932 is used by Microsoft Windows, and its mapping is
published from the Unicode Consortium (available at:
ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP932.txt). The
coded character set for this charset includes NEC-extensions and
IBM-extensions. 0x5C and 0x7E of JIS X0201 are mapped to backslash and tilde;
0x213D is mapped to U+2015; and 0x2140, 0x2141, 0x2142, and 0x215E of JIS X
0208 are mapped to compatibility characters.
Makoto
Fuji Xerox Information Systems
Tel: +81-44-812-7230 Fax: +81-44-812-7231
E-mail: murata@apsdc.ksp.fujixerox.co.jp
This directory contains binary encoding maps for some selected encodings.
If they are placed in a directory listed in @XML::Parser::Expat::Encoding_Path,
then they are automatically loaded by the XML::Parser::Expat::load_encoding
function as needed. Otherwise you may load what you need directly by
explicitly calling this function.
These maps were generated by a perl script that comes with the module
XML::Encoding, compile_encoding, from XML formatted encoding maps that
are distributed with that module. These XML encoding maps were generated
in turn with a different script, domap, from mapping information contained
on the Unicode version 2.0 CD-ROM. This CD-ROM comes with the Unicode
Standard reference manual and can be ordered from the Unicode Consortium
at http://www.unicode.org. The identical information is available on the
internet at ftp://ftp.unicode.org/Public/MAPPINGS.
See the encoding.h header in the Expat sub-directory for a description of
the structure of these files.
Clark Cooper
December 12, 1998
================================================================
Contributed maps
This distribution contains four contributed encodings from MURATA Makoto
<murata@apsdc.ksp.fujixerox.co.jp> that are variations on the encoding
commonly called Shift_JIS:
x-sjis-cp932.enc
x-sjis-jdk117.enc
x-sjis-jisx0221.enc
x-sjis-unicode.enc (This is the same encoding as the shift_jis.enc that
was distributed with this module in version 2.17)
Please read his message (Japanese_Encodings.msg) about why these are here
and why I've removed the shift_jis.enc encoding.
We also have two contributed encodings that are variations of the EUC-JP
encoding from Yoshida Masato <yoshidam@inse.co.jp>:
x-euc-jp-jisx0221.enc
x-euc-jp-unicode.enc
The comments that MURATA Makoto made in his message apply to these
encodings too.
KangChan Lee <dolphin@comeng.chungnam.ac.kr> supplied the euc-kr encoding.
Clark Cooper
December 26, 1998
package XML::Parser::Expat;
use strict;
#use warnings; No warnings numeric??
use XSLoader;
use Carp;
our $VERSION = '2.46';
our ( %Encoding_Table, @Encoding_Path, $have_File_Spec );
use File::Spec ();
%Encoding_Table = ();
if ($have_File_Spec) {
@Encoding_Path = (
grep( -d $_,
map( File::Spec->catdir( $_, qw(XML Parser Encodings) ),
@INC ) ),
File::Spec->curdir
);
}
else {
@Encoding_Path = ( grep( -d $_, map( $_ . '/XML/Parser/Encodings', @INC ) ), '.' );
}
XSLoader::load( 'XML::Parser::Expat', $VERSION );
our %Handler_Setters = (
Start => \&SetStartElementHandler,
End => \&SetEndElementHandler,
Char => \&SetCharacterDataHandler,
Proc => \&SetProcessingInstructionHandler,
Comment => \&SetCommentHandler,
CdataStart => \&SetStartCdataHandler,
CdataEnd => \&SetEndCdataHandler,
Default => \&SetDefaultHandler,
Unparsed => \&SetUnparsedEntityDeclHandler,
Notation => \&SetNotationDeclHandler,
ExternEnt => \&SetExternalEntityRefHandler,
ExternEntFin => \&SetExtEntFinishHandler,
Entity => \&SetEntityDeclHandler,
Element => \&SetElementDeclHandler,
Attlist => \&SetAttListDeclHandler,
Doctype => \&SetDoctypeHandler,
DoctypeFin => \&SetEndDoctypeHandler,
XMLDecl => \&SetXMLDeclHandler
);
sub new {
my ( $class, %args ) = @_;
my $self = bless \%args, $_[0];
$args{_State_} = 0;
$args{Context} = [];
$args{Namespaces} ||= 0;
$args{ErrorMessage} ||= '';
if ( $args{Namespaces} ) {
$args{Namespace_Table} = {};
$args{Namespace_List} = [undef];
$args{Prefix_Table} = {};
$args{New_Prefixes} = [];
}
$args{_Setters} = \%Handler_Setters;
$args{Parser} = ParserCreate(
$self, $args{ProtocolEncoding},
$args{Namespaces}
);
$self;
}
sub load_encoding {
my ($file) = @_;
$file =~ s!([^/]+)$!\L$1\E!;
$file .= '.enc' unless $file =~ /\.enc$/;
unless ( $file =~ m!^/! ) {
foreach (@Encoding_Path) {
my $tmp = (
$have_File_Spec
? File::Spec->catfile( $_, $file )
: "$_/$file"
);
if ( -e $tmp ) {
$file = $tmp;
last;
}
}
}
open( my $fh, '<', $file ) or croak("Couldn't open encmap $file:\n$!\n");
binmode($fh);
my $data;
my $br = sysread( $fh, $data, -s $file );
croak("Trouble reading $file:\n$!\n")
unless defined($br);
close($fh);
my $name = LoadEncoding( $data, $br );
croak("$file isn't an encmap file")
unless defined($name);
$name;
} # End load_encoding
sub setHandlers {
my ( $self, @handler_pairs ) = @_;
croak("Uneven number of arguments to setHandlers method")
if ( int(@handler_pairs) & 1 );
my @ret;
while (@handler_pairs) {
my $type = shift @handler_pairs;
my $handler = shift @handler_pairs;
croak 'Handler for $type not a Code ref'
unless ( !defined($handler) or !$handler or ref($handler) eq 'CODE' );
my $hndl = $self->{_Setters}->{$type};
unless ( defined($hndl) ) {
my @types = sort keys %{ $self->{_Setters} };
croak("Unknown Expat handler type: $type\n Valid types: @types");
}
my $old = &$hndl( $self->{Parser}, $handler );
push( @ret, $type, $old );
}
return @ret;
}
sub xpcroak {
my ( $self, $message ) = @_;
my $eclines = $self->{ErrorContext};
my $line = GetCurrentLineNumber( $_[0]->{Parser} );
$message .= " at line $line";
$message .= ":\n" . $self->position_in_context($eclines)
if defined($eclines);
croak $message;
}
sub xpcarp {
my ( $self, $message ) = @_;
my $eclines = $self->{ErrorContext};
my $line = GetCurrentLineNumber( $_[0]->{Parser} );
$message .= ' at line $line';
$message .= ":\n" . $self->position_in_context($eclines)
if defined($eclines);
carp $message;
}
sub default_current {
my $self = shift;
if ( $self->{_State_} == 1 ) {
return DefaultCurrent( $self->{Parser} );
}
}
sub recognized_string {
my $self = shift;
if ( $self->{_State_} == 1 ) {
return RecognizedString( $self->{Parser} );
}
}
sub original_string {
my $self = shift;
if ( $self->{_State_} == 1 ) {
return OriginalString( $self->{Parser} );
}
}
sub current_line {
my $self = shift;
if ( $self->{_State_} == 1 ) {
return GetCurrentLineNumber( $self->{Parser} );
}
}
sub current_column {
my $self = shift;
if ( $self->{_State_} == 1 ) {
return GetCurrentColumnNumber( $self->{Parser} );
}
}
sub current_byte {
my $self = shift;
if ( $self->{_State_} == 1 ) {
return GetCurrentByteIndex( $self->{Parser} );
}
}
sub base {
my ( $self, $newbase ) = @_;
my $p = $self->{Parser};
my $oldbase = GetBase($p);
SetBase( $p, $newbase ) if @_ > 1;
return $oldbase;
}
sub context {
my $ctx = $_[0]->{Context};
@$ctx;
}
sub current_element {
my ($self) = @_;
@{ $self->{Context} } ? $self->{Context}->[-1] : undef;
}
sub in_element {
my ( $self, $element ) = @_;
@{ $self->{Context} }
? $self->eq_name( $self->{Context}->[-1], $element )
: undef;
}
sub within_element {
my ( $self, $element ) = @_;
my $cnt = 0;
foreach ( @{ $self->{Context} } ) {
$cnt++ if $self->eq_name( $_, $element );
}
return $cnt;
}
sub depth {
my ($self) = @_;
int( @{ $self->{Context} } );
}
sub element_index {
my ($self) = @_;
if ( $self->{_State_} == 1 ) {
return ElementIndex( $self->{Parser} );
}
}
################
# Namespace methods
sub namespace {
my ( $self, $name ) = @_;
local ($^W) = 0;
$self->{Namespace_List}->[ int($name) ];
}
sub eq_name {
my ( $self, $nm1, $nm2 ) = @_;
local ($^W) = 0;
int($nm1) == int($nm2) and $nm1 eq $nm2;
}
sub generate_ns_name {
my ( $self, $name, $namespace ) = @_;
$namespace
? GenerateNSName(
$name, $namespace, $self->{Namespace_Table},
$self->{Namespace_List}
)
: $name;
}
sub new_ns_prefixes {
my ($self) = @_;
if ( $self->{Namespaces} ) {
return @{ $self->{New_Prefixes} };
}
return ();
}
sub expand_ns_prefix {
my ( $self, $prefix ) = @_;
if ( $self->{Namespaces} ) {
my $stack = $self->{Prefix_Table}->{$prefix};
return ( defined($stack) and @$stack ) ? $stack->[-1] : undef;
}
return undef;
}
sub current_ns_prefixes {
my ($self) = @_;
if ( $self->{Namespaces} ) {
my %set = %{ $self->{Prefix_Table} };
if ( exists $set{'#default'} and not defined( $set{'#default'}->[-1] ) ) {
delete $set{'#default'};
}
return keys %set;
}
return ();
}
################################################################
# Namespace declaration handlers
#
sub NamespaceStart {
my ( $self, $prefix, $uri ) = @_;
$prefix = '#default' unless defined $prefix;
my $stack = $self->{Prefix_Table}->{$prefix};
if ( defined $stack ) {
push( @$stack, $uri );
}
else {
$self->{Prefix_Table}->{$prefix} = [$uri];
}
# The New_Prefixes list gets emptied at end of startElement function
# in Expat.xs
push( @{ $self->{New_Prefixes} }, $prefix );
}
sub NamespaceEnd {
my ( $self, $prefix ) = @_;
$prefix = '#default' unless defined $prefix;
my $stack = $self->{Prefix_Table}->{$prefix};
if ( @$stack > 1 ) {
pop(@$stack);
}
else {
delete $self->{Prefix_Table}->{$prefix};
}
}
################
sub specified_attr {
my $self = shift;
if ( $self->{_State_} == 1 ) {
return GetSpecifiedAttributeCount( $self->{Parser} );
}
}
sub finish {
my ($self) = @_;
if ( $self->{_State_} == 1 ) {
my $parser = $self->{Parser};
UnsetAllHandlers($parser);
}
}
sub position_in_context {
my ( $self, $lines ) = @_;
if ( $self->{_State_} == 1 ) {
my $parser = $self->{Parser};
my ( $string, $linepos ) = PositionContext( $parser, $lines );
return '' unless defined($string);
my $col = GetCurrentColumnNumber($parser);
my $ptr = ( '=' x ( $col - 1 ) ) . '^' . "\n";
my $ret;
my $dosplit = $linepos < length($string);
$string .= "\n" unless $string =~ /\n$/;
if ($dosplit) {
$ret = substr( $string, 0, $linepos ) . $ptr . substr( $string, $linepos );
}
else {
$ret = $string . $ptr;
}
return $ret;
}
}
sub xml_escape {
my $self = shift;
my $text = shift;
study $text;
$text =~ s/\&/\&amp;/g;
$text =~ s/</\&lt;/g;
foreach (@_) {
croak "xml_escape: '$_' isn't a single character" if length($_) > 1;
if ( $_ eq '>' ) {
$text =~ s/>/\&gt;/g;
}
elsif ( $_ eq '"' ) {
$text =~ s/\"/\&quot;/;
}
elsif ( $_ eq "'" ) {
$text =~ s/\'/\&apos;/;
}
else {
my $rep = '&#' . sprintf( 'x%X', ord($_) ) . ';';
if (/\W/) {
my $ptrn = "\\$_";
$text =~ s/$ptrn/$rep/g;
}
else {
$text =~ s/$_/$rep/g;
}
}
}
$text;
}
sub skip_until {
my $self = shift;
if ( $self->{_State_} <= 1 ) {
SkipUntil( $self->{Parser}, $_[0] );
}
}
sub release {
my $self = shift;
ParserRelease( $self->{Parser} );
}
sub DESTROY {
my $self = shift;
ParserFree( $self->{Parser} );
}
sub parse {
my $self = shift;
my $arg = shift;
croak 'Parse already in progress (Expat)' if $self->{_State_};
$self->{_State_} = 1;
my $parser = $self->{Parser};
my $ioref;
my $result = 0;
if ( defined $arg ) {
local *@;
if ( ref($arg) and UNIVERSAL::isa( $arg, 'IO::Handle' ) ) {
$ioref = $arg;
}
elsif ( $] < 5.008 and defined tied($arg) ) {
require IO::Handle;
$ioref = $arg;
}
else {
require IO::Handle;
eval {
no strict 'refs';
$ioref = *{$arg}{IO} if defined *{$arg};
};
if ( ref($ioref) eq 'FileHandle' ) {
#for perl 5.10.x and possibly earlier, see t/file_open_scalar.t
require FileHandle;
}
}
}
if ( defined($ioref) ) {
my $delim = $self->{Stream_Delimiter};
my $prev_rs;
my $ioclass = ref $ioref;
$ioclass = 'IO::Handle' if !length $ioclass;
$prev_rs = $ioclass->input_record_separator("\n$delim\n")
if defined($delim);
$result = ParseStream( $parser, $ioref, $delim );
$ioclass->input_record_separator($prev_rs)
if defined($delim);
}
else {
$result = ParseString( $parser, $arg );
}
$self->{_State_} = 2;
$result or croak $self->{ErrorMessage};
}
sub parsestring {
my $self = shift;
$self->parse(@_);
}
sub parsefile {
my $self = shift;
croak 'Parser has already been used' if $self->{_State_};
open( my $fh, '<', $_[0] ) or croak "Couldn't open $_[0]:\n$!";
binmode($fh);
my $ret = $self->parse($fh);
close($fh);
$ret;
}
################################################################
package #hide from PAUSE
XML::Parser::ContentModel;
use overload '""' => \&asString, 'eq' => \&thiseq;
sub EMPTY () { 1 }
sub ANY () { 2 }
sub MIXED () { 3 }
sub NAME () { 4 }
sub CHOICE () { 5 }
sub SEQ () { 6 }
sub isempty {
return $_[0]->{Type} == EMPTY;
}
sub isany {
return $_[0]->{Type} == ANY;
}
sub ismixed {
return $_[0]->{Type} == MIXED;
}
sub isname {
return $_[0]->{Type} == NAME;
}
sub name {
return $_[0]->{Tag};
}
sub ischoice {
return $_[0]->{Type} == CHOICE;
}
sub isseq {
return $_[0]->{Type} == SEQ;
}
sub quant {
return $_[0]->{Quant};
}
sub children {
my $children = $_[0]->{Children};
if ( defined $children ) {
return @$children;
}
return undef;
}
sub asString {
my ($self) = @_;
my $ret;
if ( $self->{Type} == NAME ) {
$ret = $self->{Tag};
}
elsif ( $self->{Type} == EMPTY ) {
return 'EMPTY';
}
elsif ( $self->{Type} == ANY ) {
return 'ANY';
}
elsif ( $self->{Type} == MIXED ) {
$ret = '(#PCDATA';
foreach ( @{ $self->{Children} } ) {
$ret .= '|' . $_;
}
$ret .= ')';
}
else {
my $sep = $self->{Type} == CHOICE ? '|' : ',';
$ret = '(' . join( $sep, map { $_->asString } @{ $self->{Children} } ) . ')';
}
$ret .= $self->{Quant} if $self->{Quant};
return $ret;
}
sub thiseq {
my $self = shift;
return $self->asString eq $_[0];
}
################################################################
package #hide from PAUSE
XML::Parser::ExpatNB;
use Carp;
our @ISA = qw(XML::Parser::Expat);
sub parse {
my $self = shift;
my $class = ref($self);
croak "parse method not supported in $class";
}
sub parsestring {
my $self = shift;
my $class = ref($self);
croak "parsestring method not supported in $class";
}
sub parsefile {
my $self = shift;
my $class = ref($self);
croak "parsefile method not supported in $class";
}
sub parse_more {
my ( $self, $data ) = @_;
$self->{_State_} = 1;
my $ret = XML::Parser::Expat::ParsePartial( $self->{Parser}, $data );
croak $self->{ErrorMessage} unless $ret;
}
sub parse_done {
my $self = shift;
my $ret = XML::Parser::Expat::ParseDone( $self->{Parser} );
unless ($ret) {
my $msg = $self->{ErrorMessage};
$self->release;
croak $msg;
}
$self->{_State_} = 2;
my $result = $ret;
my @result = ();
my $final = $self->{FinalHandler};
if ( defined $final ) {
if (wantarray) {
@result = &$final($self);
}
else {
$result = &$final($self);
}
}
$self->release;
return unless defined wantarray;
return wantarray ? @result : $result;
}
################################################################
package #hide from PAUSE
XML::Parser::Encinfo;
sub DESTROY {
my $self = shift;
XML::Parser::Expat::FreeEncoding($self);
}
1;
__END__
=head1 NAME
XML::Parser::Expat - Lowlevel access to James Clark's expat XML parser
=head1 SYNOPSIS
use XML::Parser::Expat;
$parser = XML::Parser::Expat->new;
$parser->setHandlers('Start' => \&sh,
'End' => \&eh,
'Char' => \&ch);
open(my $fh, '<', 'info.xml') or die "Couldn't open";
$parser->parse($fh);
close($fh);
# $parser->parse('<foo id="me"> here <em>we</em> go </foo>');
sub sh
{
my ($p, $el, %atts) = @_;
$p->setHandlers('Char' => \&spec)
if ($el eq 'special');
...
}
sub eh
{
my ($p, $el) = @_;
$p->setHandlers('Char' => \&ch) # Special elements won't contain
if ($el eq 'special'); # other special elements
...
}
=head1 DESCRIPTION
This module provides an interface to James Clark's XML parser, expat. As in
expat, a single instance of the parser can only parse one document. Calls
to parsestring after the first for a given instance will die.
Expat (and XML::Parser::Expat) are event based. As the parser recognizes
parts of the document (say the start or end of an XML element), then any
handlers registered for that type of an event are called with suitable
parameters.
=head1 METHODS
=over 4
=item new
This is a class method, the constructor for XML::Parser::Expat. Options are
passed as keyword value pairs. The recognized options are:
=over 4
=item * ProtocolEncoding
The protocol encoding name. The default is none. The expat built-in
encodings are: C<UTF-8>, C<ISO-8859-1>, C<UTF-16>, and C<US-ASCII>.
Other encodings may be used if they have encoding maps in one of the
directories in the @Encoding_Path list. Setting the protocol encoding
overrides any encoding in the XML declaration.
=item * Namespaces
When this option is given with a true value, then the parser does namespace
processing. By default, namespace processing is turned off. When it is
turned on, the parser consumes I<xmlns> attributes and strips off prefixes
from element and attributes names where those prefixes have a defined
namespace. A name's namespace can be found using the L<"namespace"> method
and two names can be checked for absolute equality with the L<"eq_name">
method.
=item * NoExpand
Normally, the parser will try to expand references to entities defined in
the internal subset. If this option is set to a true value, and a default
handler is also set, then the default handler will be called when an
entity reference is seen in text. This has no effect if a default handler
has not been registered, and it has no effect on the expansion of entity
references inside attribute values.
=item * Stream_Delimiter
This option takes a string value. When this string is found alone on a line
while parsing from a stream, then the parse is ended as if it saw an end of
file. The intended use is with a stream of xml documents in a MIME multipart
format. The string should not contain a trailing newline.
=item * ErrorContext
When this option is defined, errors are reported in context. The value
of ErrorContext should be the number of lines to show on either side of
the line in which the error occurred.
=item * ParseParamEnt
Unless standalone is set to "yes" in the XML declaration, setting this to
a true value allows the external DTD to be read, and parameter entities
to be parsed and expanded.
=item * Base
The base to use for relative pathnames or URLs. This can also be done by
using the base method.
=back
=item setHandlers(TYPE, HANDLER [, TYPE, HANDLER [...]])
This method registers handlers for the various events. If no handlers are
registered, then a call to parsestring or parsefile will only determine if
the corresponding XML document is well formed (by returning without error.)
This may be called from within a handler, after the parse has started.
Setting a handler to something that evaluates to false unsets that
handler.
This method returns a list of type, handler pairs corresponding to the
input. The handlers returned are the ones that were in effect before the
call to setHandlers.
The recognized events and the parameters passed to the corresponding
handlers are:
=over 4
=item * Start (Parser, Element [, Attr, Val [,...]])
This event is generated when an XML start tag is recognized. Parser is
an XML::Parser::Expat instance. Element is the name of the XML element that
is opened with the start tag. The Attr & Val pairs are generated for each
attribute in the start tag.
=item * End (Parser, Element)
This event is generated when an XML end tag is recognized. Note that
an XML empty tag (<foo/>) generates both a start and an end event.
There is always a lower level start and end handler installed that wrap
the corresponding callbacks. This is to handle the context mechanism.
A consequence of this is that the default handler (see below) will not
see a start tag or end tag unless the default_current method is called.
=item * Char (Parser, String)
This event is generated when non-markup is recognized. The non-markup
sequence of characters is in String. A single non-markup sequence of
characters may generate multiple calls to this handler. Whatever the
encoding of the string in the original document, this is given to the
handler in UTF-8.
=item * Proc (Parser, Target, Data)
This event is generated when a processing instruction is recognized.
=item * Comment (Parser, String)
This event is generated when a comment is recognized.
=item * CdataStart (Parser)
This is called at the start of a CDATA section.
=item * CdataEnd (Parser)
This is called at the end of a CDATA section.
=item * Default (Parser, String)
This is called for any characters that don't have a registered handler.
This includes both characters that are part of markup for which no
events are generated (markup declarations) and characters that
could generate events, but for which no handler has been registered.
Whatever the encoding in the original document, the string is returned to
the handler in UTF-8.
=item * Unparsed (Parser, Entity, Base, Sysid, Pubid, Notation)
This is called for a declaration of an unparsed entity. Entity is the name
of the entity. Base is the base to be used for resolving a relative URI.
Sysid is the system id. Pubid is the public id. Notation is the notation
name. Base and Pubid may be undefined.
=item * Notation (Parser, Notation, Base, Sysid, Pubid)
This is called for a declaration of notation. Notation is the notation name.
Base is the base to be used for resolving a relative URI. Sysid is the system
id. Pubid is the public id. Base, Sysid, and Pubid may all be undefined.
=item * ExternEnt (Parser, Base, Sysid, Pubid)
This is called when an external entity is referenced. Base is the base to be
used for resolving a relative URI. Sysid is the system id. Pubid is the public
id. Base, and Pubid may be undefined.
This handler should either return a string, which represents the contents of
the external entity, or return an open filehandle that can be read to obtain
the contents of the external entity, or return undef, which indicates the
external entity couldn't be found and will generate a parse error.
If an open filehandle is returned, it must be returned as either a glob
(*FOO) or as a reference to a glob (e.g. an instance of IO::Handle).
=item * ExternEntFin (Parser)
This is called after an external entity has been parsed. It allows
applications to perform cleanup on actions performed in the above
ExternEnt handler.
=item * Entity (Parser, Name, Val, Sysid, Pubid, Ndata, IsParam)
This is called when an entity is declared. For internal entities, the Val
parameter will contain the value and the remaining three parameters will
be undefined. For external entities, the Val parameter
will be undefined, the Sysid parameter will have the system id, the Pubid
parameter will have the public id if it was provided (it will be undefined
otherwise), the Ndata parameter will contain the notation for unparsed
entities. If this is a parameter entity declaration, then the IsParam
parameter is true.
Note that this handler and the Unparsed handler above overlap. If both are
set, then this handler will not be called for unparsed entities.
=item * Element (Parser, Name, Model)
The element handler is called when an element declaration is found. Name is
the element name, and Model is the content model as an
XML::Parser::ContentModel object. See L<"XML::Parser::ContentModel Methods">
for methods available for this class.
=item * Attlist (Parser, Elname, Attname, Type, Default, Fixed)
This handler is called for each attribute in an ATTLIST declaration.
So an ATTLIST declaration that has multiple attributes
will generate multiple calls to this handler. The Elname parameter is the
name of the element with which the attribute is being associated. The Attname
parameter is the name of the attribute. Type is the attribute type, given as
a string. Default is the default value, which will either be "#REQUIRED",
"#IMPLIED" or a quoted string (i.e. the returned string will begin and end
with a quote character). If Fixed is true, then this is a fixed attribute.
=item * Doctype (Parser, Name, Sysid, Pubid, Internal)
This handler is called for DOCTYPE declarations. Name is the document type
name. Sysid is the system id of the document type, if it was provided,
otherwise it's undefined. Pubid is the public id of the document type,
which will be undefined if no public id was given. Internal will be
true or false, indicating whether or not the doctype declaration contains
an internal subset.
=item * DoctypeFin (Parser)
This handler is called after parsing of the DOCTYPE declaration has finished,
including any internal or external DTD declarations.
=item * XMLDecl (Parser, Version, Encoding, Standalone)
This handler is called for XML declarations. Version is a string containing
the version. Encoding is either undefined or contains an encoding string.
Standalone is either undefined, or true or false. Undefined indicates
that no standalone parameter was given in the XML declaration. True or
false indicates "yes" or "no" respectively.
=back
=item namespace(name)
Return the URI of the namespace that the name belongs to. If the name doesn't
belong to any namespace, an undef is returned. This is only valid on names
received through the Start or End handlers from a single document, or through
a call to the generate_ns_name method. In other words, don't use names
generated from one instance of XML::Parser::Expat with other instances.
=item eq_name(name1, name2)
Return true if name1 and name2 are identical (i.e. same name and from
the same namespace.) This is only meaningful if both names were obtained
through the Start or End handlers from a single document, or through
a call to the generate_ns_name method.
=item generate_ns_name(name, namespace)
Return a name, associated with a given namespace, good for using with the
above 2 methods. The namespace argument should be the namespace URI, not
a prefix.
=item new_ns_prefixes
When called from a start tag handler, returns namespace prefixes declared
with this start tag. If called elsewhere (or if there were no namespace
prefixes declared), it returns an empty list. Setting of the default
namespace is indicated with '#default' as a prefix.
=item expand_ns_prefix(prefix)
Return the uri to which the given prefix is currently bound. Returns
undef if the prefix isn't currently bound. Use '#default' to find the
current binding of the default namespace (if any).
=item current_ns_prefixes
Return a list of currently bound namespace prefixes. The order of the
the prefixes in the list has no meaning. If the default namespace is
currently bound, '#default' appears in the list.
=item recognized_string
Returns the string from the document that was recognized in order to call
the current handler. For instance, when called from a start handler, it
will give us the start-tag string. The string is encoded in UTF-8.
This method doesn't return a meaningful string inside declaration handlers.
=item original_string
Returns the verbatim string from the document that was recognized in
order to call the current handler. The string is in the original document
encoding. This method doesn't return a meaningful string inside declaration
handlers.
=item default_current
When called from a handler, causes the sequence of characters that generated
the corresponding event to be sent to the default handler (if one is
registered). Use of this method is deprecated in favor the recognized_string
method, which you can use without installing a default handler. This
method doesn't deliver a meaningful string to the default handler when
called from inside declaration handlers.
=item xpcroak(message)
Concatenate onto the given message the current line number within the
XML document plus the message implied by ErrorContext. Then croak with
the formed message.
=item xpcarp(message)
Concatenate onto the given message the current line number within the
XML document plus the message implied by ErrorContext. Then carp with
the formed message.
=item current_line
Returns the line number of the current position of the parse.
=item current_column
Returns the column number of the current position of the parse.
=item current_byte
Returns the current position of the parse.
=item base([NEWBASE]);
Returns the current value of the base for resolving relative URIs. If
NEWBASE is supplied, changes the base to that value.
=item context
Returns a list of element names that represent open elements, with the
last one being the innermost. Inside start and end tag handlers, this
will be the tag of the parent element.
=item current_element
Returns the name of the innermost currently opened element. Inside
start or end handlers, returns the parent of the element associated
with those tags.
=item in_element(NAME)
Returns true if NAME is equal to the name of the innermost currently opened
element. If namespace processing is being used and you want to check
against a name that may be in a namespace, then use the generate_ns_name
method to create the NAME argument.
=item within_element(NAME)
Returns the number of times the given name appears in the context list.
If namespace processing is being used and you want to check
against a name that may be in a namespace, then use the generate_ns_name
method to create the NAME argument.
=item depth
Returns the size of the context list.
=item element_index
Returns an integer that is the depth-first visit order of the current
element. This will be zero outside of the root element. For example,
this will return 1 when called from the start handler for the root element
start tag.
=item skip_until(INDEX)
INDEX is an integer that represents an element index. When this method
is called, all handlers are suspended until the start tag for an element
that has an index number equal to INDEX is seen. If a start handler has
been set, then this is the first tag that the start handler will see
after skip_until has been called.
=item position_in_context(LINES)
Returns a string that shows the current parse position. LINES should be
an integer >= 0 that represents the number of lines on either side of the
current parse line to place into the returned string.
=item xml_escape(TEXT [, CHAR [, CHAR ...]])
Returns TEXT with markup characters turned into character entities. Any
additional characters provided as arguments are also turned into character
references where found in TEXT.
=item parse (SOURCE)
The SOURCE parameter should either be a string containing the whole XML
document, or it should be an open IO::Handle. Only a single document
may be parsed for a given instance of XML::Parser::Expat, so this will croak
if it's been called previously for this instance.
=item parsestring(XML_DOC_STRING)
Parses the given string as an XML document. Only a single document may be
parsed for a given instance of XML::Parser::Expat, so this will die if either
parsestring or parsefile has been called for this instance previously.
This method is deprecated in favor of the parse method.
=item parsefile(FILENAME)
Parses the XML document in the given file. Will die if parsestring or
parsefile has been called previously for this instance.
=item is_defaulted(ATTNAME)
NO LONGER WORKS. To find out if an attribute is defaulted please use
the specified_attr method.
=item specified_attr
When the start handler receives lists of attributes and values, the
non-defaulted (i.e. explicitly specified) attributes occur in the list
first. This method returns the number of specified items in the list.
So if this number is equal to the length of the list, there were no
defaulted values. Otherwise the number points to the index of the
first defaulted attribute name.
=item finish
Unsets all handlers (including internal ones that set context), but expat
continues parsing to the end of the document or until it finds an error.
It should finish up a lot faster than with the handlers set.
=item release
There are data structures used by XML::Parser::Expat that have circular
references. This means that these structures will never be garbage
collected unless these references are explicitly broken. Calling this
method breaks those references (and makes the instance unusable.)
Normally, higher level calls handle this for you, but if you are using
XML::Parser::Expat directly, then it's your responsibility to call it.
=back
=head2 XML::Parser::ContentModel Methods
The element declaration handlers are passed objects of this class as the
content model of the element declaration. They also represent content
particles, components of a content model.
When referred to as a string, these objects are automagicly converted to a
string representation of the model (or content particle).
=over 4
=item isempty
This method returns true if the object is "EMPTY", false otherwise.
=item isany
This method returns true if the object is "ANY", false otherwise.
=item ismixed
This method returns true if the object is "(#PCDATA)" or "(#PCDATA|...)*",
false otherwise.
=item isname
This method returns if the object is an element name.
=item ischoice
This method returns true if the object is a choice of content particles.
=item isseq
This method returns true if the object is a sequence of content particles.
=item quant
This method returns undef or a string representing the quantifier
('?', '*', '+') associated with the model or particle.
=item children
This method returns undef or (for mixed, choice, and sequence types)
an array of component content particles. There will always be at least
one component for choices and sequences, but for a mixed content model
of pure PCDATA, "(#PCDATA)", then an undef is returned.
=back
=head2 XML::Parser::ExpatNB Methods
The class XML::Parser::ExpatNB is a subclass of XML::Parser::Expat used
for non-blocking access to the expat library. It does not support the parse,
parsestring, or parsefile methods, but it does have these additional methods:
=over 4
=item parse_more(DATA)
Feed expat more text to munch on.
=item parse_done
Tell expat that it's gotten the whole document.
=back
=head1 FUNCTIONS
=over 4
=item XML::Parser::Expat::load_encoding(ENCODING)
Load an external encoding. ENCODING is either the name of an encoding or
the name of a file. The basename is converted to lowercase and a '.enc'
extension is appended unless there's one already there. Then, unless
it's an absolute pathname (i.e. begins with '/'), the first file by that
name discovered in the @Encoding_Path path list is used.
The encoding in the file is loaded and kept in the %Encoding_Table
table. Earlier encodings of the same name are replaced.
This function is automatically called by expat when it encounters an encoding
it doesn't know about. Expat shouldn't call this twice for the same
encoding name. The only reason users should use this function is to
explicitly load an encoding not contained in the @Encoding_Path list.
=back
=head1 AUTHORS
Larry Wall <F<larry@wall.org>> wrote version 1.0.
Clark Cooper <F<coopercc@netheaven.com>> picked up support, changed the API
for this version (2.x), provided documentation, and added some standard
package features.
=cut
# LWPExternEnt.pl
#
# Copyright (c) 2000 Clark Cooper
# 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::Parser;
use URI;
use URI::file;
use LWP::UserAgent;
##
## Note that this external entity handler reads the entire entity into
## memory, so it will choke on huge ones. It would be really nice if
## LWP::UserAgent optionally returned us an IO::Handle.
##
sub lwp_ext_ent_handler {
my ($xp, $base, $sys) = @_; # We don't use public id
my $uri;
if (defined $base) {
# Base may have been set by parsefile, which is agnostic about
# whether its a file or URI.
my $base_uri = new URI($base);
unless (defined $base_uri->scheme) {
$base_uri = URI->new_abs($base_uri, URI::file->cwd);
}
$uri = URI->new_abs($sys, $base_uri);
}
else {
$uri = new URI($sys);
unless (defined $uri->scheme) {
$uri = URI->new_abs($uri, URI::file->cwd);
}
}
my $ua = $xp->{_lwpagent};
unless (defined $ua) {
$ua = $xp->{_lwpagent} = new LWP::UserAgent();
$ua->env_proxy();
}
my $req = new HTTP::Request('GET', $uri);
my $res = $ua->request($req);
if ($res->is_error) {
$xp->{ErrorMessage} .= "\n" . $res->status_line . " $uri";
return undef;
}
$xp->{_BaseStack} ||= [];
push(@{$xp->{_BaseStack}}, $base);
$xp->base($uri);
return $res->content;
} # End lwp_ext_ent_handler
sub lwp_ext_ent_cleanup {
my ($xp) = @_;
$xp->base(pop(@{$xp->{_BaseStack}}));
} # End lwp_ext_ent_cleanup
1;
# ======================================================================
#
# Copyright (C) 2000-2007 Paul Kulchenko (paulclinger@yahoo.com)
# Copyright (C) 2008 Martin Kutter (martin.kutter@fen-net.de)
# XML::Parser::Lite is free software; you can redistribute it
# and/or modify it under the same terms as Perl itself.
#
# ======================================================================
package XML::Parser::Lite;
use 5.006;
use strict;
use warnings;
our $VERSION = '0.722';
sub new {
my $class = shift;
return $class if ref $class;
my $self = bless {} => $class;
my %parameters = @_;
$self->setHandlers(); # clear first
$self->setHandlers(%{$parameters{Handlers} || {}});
return $self;
}
sub setHandlers {
my $self = shift;
# allow symbolic refs, avoid "subroutine redefined" warnings
no strict 'refs'; ## no critic
no warnings qw(redefine);
# clear all handlers if called without parameters
if (not @_) {
for (qw(Start End Char Final Init Comment Doctype XMLDecl)) {
*$_ = sub {}
}
}
# we could use each here, too...
while (@_) {
my($name, $func) = splice(@_, 0, 2);
*$name = defined $func
? $func
: sub {}
}
return $self;
}
sub _regexp {
my $patch = shift || '';
my $package = __PACKAGE__;
# This parser is based on "shallow parser" http://www.cs.sfu.ca/~cameron/REX.html
# Robert D. Cameron "REX: XML Shallow Parsing with Regular Expressions",
# Technical Report TR 1998-17, School of Computing Science, Simon Fraser University, November, 1998.
# Copyright (c) 1998, Robert D. Cameron.
# The following code may be freely used and distributed provided that
# this copyright and citation notice remains intact and that modifications
# or additions are clearly identified.
# Modifications may be tracked on XML::Parser::Lite's source code repository at
# https://github.com/redhotpenguin/perl-XML-Parser-Lite
#
use re 'eval';
my $TextSE = "[^<]+";
my $UntilHyphen = "[^-]*-";
my $Until2Hyphens = "([^-]*)-(?:[^-]$[^-]*-)*-";
#my $CommentCE = "$Until2Hyphens(?{${package}::comment(\$2)})>?";
my $CommentCE = "(.+)--(?{${package}::comment(\$2)})>?";
# my $Until2Hyphens = "$UntilHyphen(?:[^-]$UntilHyphen)*-";
# my $CommentCE = "$Until2Hyphens>?";
my $UntilRSBs = "[^\\]]*](?:[^\\]]+])*]+";
my $CDATA_CE = "$UntilRSBs(?:[^\\]>]$UntilRSBs)*>";
my $S = "[ \\n\\t\\r]+";
my $NameStrt = "[A-Za-z_:]|[^\\x00-\\x7F]";
my $NameChar = "[A-Za-z0-9_:.-]|[^\\x00-\\x7F]";
my $Name = "(?:$NameStrt)(?:$NameChar)*";
my $QuoteSE = "\"[^\"]*\"|'[^']*'";
my $DT_IdentSE = "$Name(?:$S(?:$Name|$QuoteSE))*";
# my $DT_IdentSE = "$S$Name(?:$S(?:$Name|$QuoteSE))*";
my $MarkupDeclCE = "(?:[^\\]\"'><]+|$QuoteSE)*>";
my $S1 = "[\\n\\r\\t ]";
my $UntilQMs = "[^?]*\\?";
my $PI_Tail = "\\?>|$S1$UntilQMs(?:[^>?]$UntilQMs)*";
my $DT_ItemSE = "<(?:!(?:--$Until2Hyphens>|[^-]$MarkupDeclCE)|\\?$Name(?:$PI_Tail>))|%$Name;|$S";
my $DocTypeCE = "$S($DT_IdentSE(?:$S)?(?:\\[(?:$DT_ItemSE)*](?:$S)?)?)>(?{${package}::_doctype(\$3)})";
# my $PI_Tail = "\\?>|$S1$UntilQMs(?:[^>?]$UntilQMs)*>";
# my $DT_ItemSE = "<(?:!(?:--$Until2Hyphens>|[^-]$MarkupDeclCE)|\\?$Name(?:$PI_Tail))|%$Name;|$S";
# my $DocTypeCE = "$DT_IdentSE(?:$S)?(?:\\[(?:$DT_ItemSE)*](?:$S)?)?>?";
my $DeclCE = "--(?:$CommentCE)?|\\[CDATA\\[(?:$CDATA_CE)?|DOCTYPE(?:$DocTypeCE)?";
# my $PI_CE = "$Name(?:$PI_Tail)?";
my $PI_CE = "($Name(?:$PI_Tail))>(?{${package}::_xmldecl(\$5)})";
# these expressions were modified for backtracking and events
# my $EndTagCE = "($Name)(?{${package}::_end(\$2)})(?:$S)?>";
my $EndTagCE = "($Name)(?{${package}::_end(\$6)})(?:$S)?>";
my $AttValSE = "\"([^<\"]*)\"|'([^<']*)'";
# my $ElemTagCE = "($Name)(?:$S($Name)(?:$S)?=(?:$S)?(?:$AttValSE)(?{[\@{\$^R||[]},\$4=>defined\$5?\$5:\$6]}))*(?:$S)?(/)?>(?{${package}::_start( \$3,\@{\$^R||[]})})(?{\${7} and ${package}::_end(\$3)})";
my $ElemTagCE = "($Name)"
. "(?:$S($Name)(?:$S)?=(?:$S)?(?:$AttValSE)"
. "(?{[\@{\$^R||[]},\$8=>defined\$9?\$9:\$10]}))*(?:$S)?(/)?>"
. "(?{${package}::_start(\$7,\@{\$^R||[]})})(?{\$11 and ${package}::_end(\$7)})";
my $MarkupSPE = "<(?:!(?:$DeclCE)?|\\?(?:$PI_CE)?|/(?:$EndTagCE)?|(?:$ElemTagCE)?)";
# Next expression is under "black magic".
# Ideally it should be '($TextSE)(?{${package}::char(\$1)})|$MarkupSPE',
# but it doesn't work under Perl 5.005 and only magic with
# (?:....)?? solved the problem.
# I would appreciate if someone let me know what is the right thing to do
# and what's the reason for all this magic.
# Seems like a problem related to (?:....)? rather than to ?{} feature.
# Tests are in t/31-xmlparserlite.t if you decide to play with it.
#"(?{[]})(?:($TextSE)(?{${package}::_char(\$1)}))$patch|$MarkupSPE";
"(?:($TextSE)(?{${package}::_char(\$1)}))$patch|$MarkupSPE";
}
setHandlers();
# Try 5.6 and 5.10 regex first
my $REGEXP = _regexp('??');
sub _parse_re {
use re "eval";
undef $^R;
no strict 'refs'; ## no critic
1 while $_[0] =~ m{$REGEXP}go
};
# fixup regex if it does not work...
{
if (not eval { _parse_re('<soap:foo xmlns:soap="foo">bar</soap:foo>'); 1; } ) {
$REGEXP = _regexp();
local $^W;
*_parse_re = sub {
use re "eval";
undef $^R;
1 while $_[0] =~ m{$REGEXP}go
};
}
}
sub parse {
_init();
_parse_re($_[1]);
_final();
}
my(@stack, $level);
sub _init {
@stack = ();
$level = 0;
Init(__PACKAGE__, @_);
}
sub _final {
die "not properly closed tag '$stack[-1]'\n" if @stack;
die "no element found\n" unless $level;
Final(__PACKAGE__, @_)
}
sub _start {
die "multiple roots, wrong element '$_[0]'\n" if $level++ && !@stack;
push(@stack, $_[0]);
my $r=Start(__PACKAGE__, @_);
return ref($r) eq 'ARRAY' ? $r : undef;
}
sub _char {
Char(__PACKAGE__, $_[0]), return if @stack;
# check for junk before or after element
# can't use split or regexp due to limitations in ?{} implementation,
# will iterate with loop, but we'll do it no more than two times, so
# it shouldn't affect performance
for (my $i=0; $i < length $_[0]; $i++) {
die "junk '$_[0]' @{[$level ? 'after' : 'before']} XML element\n"
if index("\n\r\t ", substr($_[0],$i,1)) < 0; # or should '< $[' be there
}
}
sub _end {
no warnings qw(uninitialized);
pop(@stack) eq $_[0] or die "mismatched tag '$_[0]'\n";
my $r=End(__PACKAGE__, $_[0]);
return ref($r) eq 'ARRAY' ? $r : undef;
}
sub comment {
my $r=Comment(__PACKAGE__, $_[0]);
return ref($r) eq 'ARRAY' ? $r : undef;
}
sub end {
pop(@stack) eq $_[0] or die "mismatched tag '$_[0]'\n";
my $r=End(__PACKAGE__, $_[0]);
return ref($r) eq 'ARRAY' ? $r : undef;
}
sub _doctype {
my $r=Doctype(__PACKAGE__, $_[0]);
return ref($r) eq 'ARRAY' ? $r : undef;
}
sub _xmldecl {
XMLDecl(__PACKAGE__, $_[0]);
}
# ======================================================================
1;
__END__
=head1 NAME
XML::Parser::Lite - Lightweight pure-perl XML Parser (based on regexps)
=head1 SYNOPSIS
use XML::Parser::Lite;
$p1 = new XML::Parser::Lite;
$p1->setHandlers(
Start => sub { shift; print "start: @_\n" },
Char => sub { shift; print "char: @_\n" },
End => sub { shift; print "end: @_\n" },
);
$p1->parse('<foo id="me">Hello World!</foo>');
$p2 = new XML::Parser::Lite
Handlers => {
Start => sub { shift; print "start: @_\n" },
Char => sub { shift; print "char: @_\n" },
End => sub { shift; print "end: @_\n" },
}
;
$p2->parse('<foo id="me">Hello <bar>cruel</bar> World!</foo>');
=head1 DESCRIPTION
This module implements an XML parser with a interface similar to
L<XML::Parser>. Though not all callbacks are supported, you should be able to
use it in the same way you use XML::Parser. Due to using experimental regexp
features it'll work only on Perl 5.6 and above and may behave differently on
different platforms.
Note that you cannot use regular expressions or split in callbacks. This is
due to a limitation of perl's regular expression implementation (which is
not re-entrant).
=head1 SUBROUTINES/METHODS
=head2 new
Constructor.
The new() method returns the object called on when called as object method.
This behaviour was inherited from L<SOAP::Lite>,
which XML::Parser::Lite was split out from.
This means that the following effectively is
a no-op if $obj is a object:
$obj = $obj->new();
New accepts a single named parameter, C<Handlers> with a hash ref as value:
my $parser = XML::Parser::Lite->new(
Handlers => {
Start => sub { shift; print "start: @_\n" },
Char => sub { shift; print "char: @_\n" },
End => sub { shift; print "end: @_\n" },
}
);
The handlers given will be passed to setHandlers.
=head2 setHandlers
Sets (or resets) the parsing handlers. Accepts a hash with the handler names
and handler code references as parameters. Passing C<undef> instead of a
code reference replaces the handler by a no-op.
The following handlers can be set:
Init
Start
Char
End
Final
All other handlers are ignored.
Calling setHandlers without parameters resets all handlers to no-ops.
=head2 parse
Parses the XML given. In contrast to L<XML::Parser|XML::Parser>'s parse
method, parse() only parses strings.
=head1 Handler methods
=head2 Init
Called before parsing starts. You should perform any necessary initializations
in Init.
=head2 Start
Called at the start of each XML node. See L<XML::Parser> for details.
=head2 Char
Called for each character sequence. May be called multiple times for the
characters contained in an XML node (even for every single character).
Your implementation has to make sure that it captures all characters.
=head2 End
Called at the end of each XML node. See L<XML::Parser> for details
=head2 Comment
See L<XML::Parser> for details
=head2 XMLDecl
See L<XML::Parser> for details
=head2 Doctype
See L<XML::Parser> for details
=head2 Final
Called at the end of the parsing process. You should perform any necessary
cleanup here.
=head1 SEE ALSO
L<XML::Parser> - a full-blown XML Parser, on which XML::Parser::Lite is based.
Requires a C compiler and the I<expat> XML parser.
L<XML::Parser::LiteCopy> - a fork in L<XML::Parser::Lite::Tree>.
L<YAX> - another pure-perl module for XML parsing.
L<XML::Parser::REX> - another module that parses XML with regular expressions.
=head1 COPYRIGHT
Copyright (C) 2000-2007 Paul Kulchenko. All rights reserved.
Copyright (C) 2008 Martin Kutter. All rights reserved.
Copyright (C) 2013-2015 Fred Moyer. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
This parser is based on "shallow parser"
L<http://www.cs.sfu.ca/~cameron/REX.html>
Copyright (c) 1998, Robert D. Cameron.
=head1 AUTHORS
Paul Kulchenko (paulclinger@yahoo.com)
Martin Kutter (martin.kutter@fen-net.de)
Fred Moyer (fred@redhotpenguin.com)
Additional handlers supplied by Adam Leggett.
=head1 CONTRIBUTORS
David Steinbrunner (dsteinbrunner@pobox.com)
Neil Bowers (neil@bowers.com)
Paul Cochrane (paul@liekut.de)
=cut
# $Id: Debug.pm,v 1.1 2003-07-27 16:07:49 matt Exp $
package XML::Parser::Style::Debug;
use strict;
sub Start {
my $expat = shift;
my $tag = shift;
print STDERR "@{$expat->{Context}} \\\\ (@_)\n";
}
sub End {
my $expat = shift;
my $tag = shift;
print STDERR "@{$expat->{Context}} //\n";
}
sub Char {
my $expat = shift;
my $text = shift;
$text =~ s/([\x80-\xff])/sprintf "#x%X;", ord $1/eg;
$text =~ s/([\t\n])/sprintf "#%d;", ord $1/eg;
print STDERR "@{$expat->{Context}} || $text\n";
}
sub Proc {
my $expat = shift;
my $target = shift;
my $text = shift;
my @foo = @{ $expat->{Context} };
print STDERR "@foo $target($text)\n";
}
1;
__END__
=head1 NAME
XML::Parser::Style::Debug - Debug style for XML::Parser
=head1 SYNOPSIS
use XML::Parser;
my $p = XML::Parser->new(Style => 'Debug');
$p->parsefile('foo.xml');
=head1 DESCRIPTION
This just prints out the document in outline form to STDERR. Nothing special is
returned by parse.
=cut
# $Id: Objects.pm,v 1.1 2003-08-18 20:20:51 matt Exp $
package XML::Parser::Style::Objects;
use strict;
sub Init {
my $expat = shift;
$expat->{Lists} = [];
$expat->{Curlist} = $expat->{Tree} = [];
}
sub Start {
my $expat = shift;
my $tag = shift;
my $newlist = [];
my $class = "${$expat}{Pkg}::$tag";
my $newobj = bless { @_, Kids => $newlist }, $class;
push @{ $expat->{Lists} }, $expat->{Curlist};
push @{ $expat->{Curlist} }, $newobj;
$expat->{Curlist} = $newlist;
}
sub End {
my $expat = shift;
my $tag = shift;
$expat->{Curlist} = pop @{ $expat->{Lists} };
}
sub Char {
my $expat = shift;
my $text = shift;
my $class = "${$expat}{Pkg}::Characters";
my $clist = $expat->{Curlist};
my $pos = $#$clist;
if ( $pos >= 0 and ref( $clist->[$pos] ) eq $class ) {
$clist->[$pos]->{Text} .= $text;
}
else {
push @$clist, bless { Text => $text }, $class;
}
}
sub Final {
my $expat = shift;
delete $expat->{Curlist};
delete $expat->{Lists};
$expat->{Tree};
}
1;
__END__
=head1 NAME
XML::Parser::Style::Objects - Objects styler parser
=head1 SYNOPSIS
use XML::Parser;
my $p = XML::Parser->new(Style => 'Objects', Pkg => 'MyNode');
my $tree = $p->parsefile('foo.xml');
=head1 DESCRIPTION
This module implements XML::Parser's Objects style parser.
This is similar to the Tree style, except that a hash object is created for
each element. The corresponding object will be in the class whose name
is created by appending "::" and the element name to the package set with
the Pkg option. Non-markup text will be in the ::Characters class. The
contents of the corresponding object will be in an anonymous array that
is the value of the Kids property for that object.
=head1 SEE ALSO
L<XML::Parser::Style::Tree>
=cut
# $Id: Stream.pm,v 1.1 2003-07-27 16:07:49 matt Exp $
package XML::Parser::Style::Stream;
use strict;
# This style invented by Tim Bray <tbray@textuality.com>
sub Init {
no strict 'refs';
my $expat = shift;
$expat->{Text} = '';
my $sub = $expat->{Pkg} . "::StartDocument";
&$sub($expat)
if defined(&$sub);
}
sub Start {
no strict 'refs';
my $expat = shift;
my $type = shift;
doText($expat);
$_ = "<$type";
%_ = @_;
while (@_) {
$_ .= ' ' . shift() . '="' . shift() . '"';
}
$_ .= '>';
my $sub = $expat->{Pkg} . "::StartTag";
if ( defined(&$sub) ) {
&$sub( $expat, $type );
}
else {
print;
}
}
sub End {
no strict 'refs';
my $expat = shift;
my $type = shift;
# Set right context for Text handler
push( @{ $expat->{Context} }, $type );
doText($expat);
pop( @{ $expat->{Context} } );
$_ = "</$type>";
my $sub = $expat->{Pkg} . "::EndTag";
if ( defined(&$sub) ) {
&$sub( $expat, $type );
}
else {
print;
}
}
sub Char {
my $expat = shift;
$expat->{Text} .= shift;
}
sub Proc {
no strict 'refs';
my $expat = shift;
my $target = shift;
my $text = shift;
doText($expat);
$_ = "<?$target $text?>";
my $sub = $expat->{Pkg} . "::PI";
if ( defined(&$sub) ) {
&$sub( $expat, $target, $text );
}
else {
print;
}
}
sub Final {
no strict 'refs';
my $expat = shift;
my $sub = $expat->{Pkg} . "::EndDocument";
&$sub($expat)
if defined(&$sub);
}
sub doText {
no strict 'refs';
my $expat = shift;
$_ = $expat->{Text};
if ( length($_) ) {
my $sub = $expat->{Pkg} . "::Text";
if ( defined(&$sub) ) {
&$sub($expat);
}
else {
print;
}
$expat->{Text} = '';
}
}
1;
__END__
=head1 NAME
XML::Parser::Style::Stream - Stream style for XML::Parser
=head1 SYNOPSIS
use XML::Parser;
my $p = XML::Parser->new(Style => 'Stream', Pkg => 'MySubs');
$p->parsefile('foo.xml');
{
package MySubs;
sub StartTag {
my ($e, $name) = @_;
# do something with start tags
}
sub EndTag {
my ($e, $name) = @_;
# do something with end tags
}
sub Characters {
my ($e, $data) = @_;
# do something with text nodes
}
}
=head1 DESCRIPTION
This style uses the Pkg option to find subs in a given package to call for each event.
If none of the subs that this
style looks for is there, then the effect of parsing with this style is
to print a canonical copy of the document without comments or declarations.
All the subs receive as their 1st parameter the Expat instance for the
document they're parsing.
It looks for the following routines:
=over 4
=item * StartDocument
Called at the start of the parse .
=item * StartTag
Called for every start tag with a second parameter of the element type. The $_
variable will contain a copy of the tag and the %_ variable will contain
attribute values supplied for that element.
=item * EndTag
Called for every end tag with a second parameter of the element type. The $_
variable will contain a copy of the end tag.
=item * Text
Called just before start or end tags with accumulated non-markup text in
the $_ variable.
=item * PI
Called for processing instructions. The $_ variable will contain a copy of
the PI and the target and data are sent as 2nd and 3rd parameters
respectively.
=item * EndDocument
Called at conclusion of the parse.
=back
=cut
# $Id: Subs.pm,v 1.1 2003-07-27 16:07:49 matt Exp $
package XML::Parser::Style::Subs;
sub Start {
no strict 'refs';
my $expat = shift;
my $tag = shift;
my $sub = $expat->{Pkg} . "::$tag";
eval { &$sub( $expat, $tag, @_ ) };
}
sub End {
no strict 'refs';
my $expat = shift;
my $tag = shift;
my $sub = $expat->{Pkg} . "::${tag}_";
eval { &$sub( $expat, $tag ) };
}
1;
__END__
=head1 NAME
XML::Parser::Style::Subs - glue for handling element callbacks
=head1 SYNOPSIS
use XML::Parser;
my $p = XML::Parser->new(Style => 'Subs', Pkg => 'MySubs');
$p->parsefile('foo.xml');
{
package MySubs;
sub foo {
# start of foo tag
}
sub foo_ {
# end of foo tag
}
}
=head1 DESCRIPTION
Each time an element starts, a sub by that name in the package specified
by the Pkg option is called with the same parameters that the Start
handler gets called with.
Each time an element ends, a sub with that name appended with an underscore
("_"), is called with the same parameters that the End handler gets called
with.
Nothing special is returned by parse.
=cut
# $Id: Tree.pm,v 1.2 2003-07-31 07:54:51 matt Exp $
package XML::Parser::Style::Tree;
$XML::Parser::Built_In_Styles{Tree} = 1;
sub Init {
my $expat = shift;
$expat->{Lists} = [];
$expat->{Curlist} = $expat->{Tree} = [];
}
sub Start {
my $expat = shift;
my $tag = shift;
my $newlist = [ {@_} ];
push @{ $expat->{Lists} }, $expat->{Curlist};
push @{ $expat->{Curlist} }, $tag => $newlist;
$expat->{Curlist} = $newlist;
}
sub End {
my $expat = shift;
my $tag = shift;
$expat->{Curlist} = pop @{ $expat->{Lists} };
}
sub Char {
my $expat = shift;
my $text = shift;
my $clist = $expat->{Curlist};
my $pos = $#$clist;
if ( $pos > 0 and $clist->[ $pos - 1 ] eq '0' ) {
$clist->[$pos] .= $text;
}
else {
push @$clist, 0 => $text;
}
}
sub Final {
my $expat = shift;
delete $expat->{Curlist};
delete $expat->{Lists};
$expat->{Tree};
}
1;
__END__
=head1 NAME
XML::Parser::Style::Tree - Tree style parser
=head1 SYNOPSIS
use XML::Parser;
my $p = XML::Parser->new(Style => 'Tree');
my $tree = $p->parsefile('foo.xml');
=head1 DESCRIPTION
This module implements XML::Parser's Tree style parser.
When parsing a document, C<parse()> will return a parse tree for the
document. Each node in the tree
takes the form of a tag, content pair. Text nodes are represented with
a pseudo-tag of "0" and the string that is their content. For elements,
the content is an array reference. The first item in the array is a
(possibly empty) hash reference containing attributes. The remainder of
the array is a sequence of tag-content pairs representing the content
of the element.
So for example 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 root document "foo", has 3 children: a "head" element, a "bar"
element and the text "do". After the empty attribute hash, these are
represented in it's contents by 3 tag-content pairs.
=cut
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment