source: main/trunk/release-kits/shared/linux/XML-Parser/64-bit/perl-5.10/XML/Parser/Expat.pm@ 29196

Last change on this file since 29196 was 29196, checked in by ak19, 10 years ago

Rearrange binary files to be 32-bit or 64-bit specific

File size: 33.2 KB
Line 
1package XML::Parser::Expat;
2
3require 5.004;
4
5use strict;
6use vars qw($VERSION @ISA %Handler_Setters %Encoding_Table @Encoding_Path
7 $have_File_Spec);
8use Carp;
9
10require DynaLoader;
11
12@ISA = qw(DynaLoader);
13$VERSION = "2.40";
14
15$have_File_Spec = $INC{'File/Spec.pm'} || do 'File/Spec.pm';
16
17%Encoding_Table = ();
18if ($have_File_Spec) {
19 @Encoding_Path = (grep(-d $_,
20 map(File::Spec->catdir($_, qw(XML Parser Encodings)),
21 @INC)),
22 File::Spec->curdir);
23}
24else {
25 @Encoding_Path = (grep(-d $_, map($_ . '/XML/Parser/Encodings', @INC)), '.');
26}
27
28
29bootstrap XML::Parser::Expat $VERSION;
30
31%Handler_Setters = (
32 Start => \&SetStartElementHandler,
33 End => \&SetEndElementHandler,
34 Char => \&SetCharacterDataHandler,
35 Proc => \&SetProcessingInstructionHandler,
36 Comment => \&SetCommentHandler,
37 CdataStart => \&SetStartCdataHandler,
38 CdataEnd => \&SetEndCdataHandler,
39 Default => \&SetDefaultHandler,
40 Unparsed => \&SetUnparsedEntityDeclHandler,
41 Notation => \&SetNotationDeclHandler,
42 ExternEnt => \&SetExternalEntityRefHandler,
43 ExternEntFin => \&SetExtEntFinishHandler,
44 Entity => \&SetEntityDeclHandler,
45 Element => \&SetElementDeclHandler,
46 Attlist => \&SetAttListDeclHandler,
47 Doctype => \&SetDoctypeHandler,
48 DoctypeFin => \&SetEndDoctypeHandler,
49 XMLDecl => \&SetXMLDeclHandler
50 );
51
52sub new {
53 my ($class, %args) = @_;
54 my $self = bless \%args, $_[0];
55 $args{_State_} = 0;
56 $args{Context} = [];
57 $args{Namespaces} ||= 0;
58 $args{ErrorMessage} ||= '';
59 if ($args{Namespaces}) {
60 $args{Namespace_Table} = {};
61 $args{Namespace_List} = [undef];
62 $args{Prefix_Table} = {};
63 $args{New_Prefixes} = [];
64 }
65 $args{_Setters} = \%Handler_Setters;
66 $args{Parser} = ParserCreate($self, $args{ProtocolEncoding},
67 $args{Namespaces});
68 $self;
69}
70
71sub load_encoding {
72 my ($file) = @_;
73
74 $file =~ s!([^/]+)$!\L$1\E!;
75 $file .= '.enc' unless $file =~ /\.enc$/;
76 unless ($file =~ m!^/!) {
77 foreach (@Encoding_Path) {
78 my $tmp = ($have_File_Spec
79 ? File::Spec->catfile($_, $file)
80 : "$_/$file");
81 if (-e $tmp) {
82 $file = $tmp;
83 last;
84 }
85 }
86 }
87
88 local(*ENC);
89 open(ENC, $file) or croak("Couldn't open encmap $file:\n$!\n");
90 binmode(ENC);
91 my $data;
92 my $br = sysread(ENC, $data, -s $file);
93 croak("Trouble reading $file:\n$!\n")
94 unless defined($br);
95 close(ENC);
96
97 my $name = LoadEncoding($data, $br);
98 croak("$file isn't an encmap file")
99 unless defined($name);
100
101 $name;
102} # End load_encoding
103
104sub setHandlers {
105 my ($self, @handler_pairs) = @_;
106
107 croak("Uneven number of arguments to setHandlers method")
108 if (int(@handler_pairs) & 1);
109
110 my @ret;
111
112 while (@handler_pairs) {
113 my $type = shift @handler_pairs;
114 my $handler = shift @handler_pairs;
115 croak "Handler for $type not a Code ref"
116 unless (! defined($handler) or ! $handler or ref($handler) eq 'CODE');
117
118 my $hndl = $self->{_Setters}->{$type};
119
120 unless (defined($hndl)) {
121 my @types = sort keys %{$self->{_Setters}};
122 croak("Unknown Expat handler type: $type\n Valid types: @types");
123 }
124
125 my $old = &$hndl($self->{Parser}, $handler);
126 push (@ret, $type, $old);
127 }
128
129 return @ret;
130}
131
132sub xpcroak
133 {
134 my ($self, $message) = @_;
135
136 my $eclines = $self->{ErrorContext};
137 my $line = GetCurrentLineNumber($_[0]->{Parser});
138 $message .= " at line $line";
139 $message .= ":\n" . $self->position_in_context($eclines)
140 if defined($eclines);
141 croak $message;
142}
143
144sub xpcarp {
145 my ($self, $message) = @_;
146
147 my $eclines = $self->{ErrorContext};
148 my $line = GetCurrentLineNumber($_[0]->{Parser});
149 $message .= " at line $line";
150 $message .= ":\n" . $self->position_in_context($eclines)
151 if defined($eclines);
152 carp $message;
153}
154
155sub default_current {
156 my $self = shift;
157 if ($self->{_State_} == 1) {
158 return DefaultCurrent($self->{Parser});
159 }
160}
161
162sub recognized_string {
163 my $self = shift;
164 if ($self->{_State_} == 1) {
165 return RecognizedString($self->{Parser});
166 }
167}
168
169sub original_string {
170 my $self = shift;
171 if ($self->{_State_} == 1) {
172 return OriginalString($self->{Parser});
173 }
174}
175
176sub current_line {
177 my $self = shift;
178 if ($self->{_State_} == 1) {
179 return GetCurrentLineNumber($self->{Parser});
180 }
181}
182
183sub current_column {
184 my $self = shift;
185 if ($self->{_State_} == 1) {
186 return GetCurrentColumnNumber($self->{Parser});
187 }
188}
189
190sub current_byte {
191 my $self = shift;
192 if ($self->{_State_} == 1) {
193 return GetCurrentByteIndex($self->{Parser});
194 }
195}
196
197sub base {
198 my ($self, $newbase) = @_;
199 my $p = $self->{Parser};
200 my $oldbase = GetBase($p);
201 SetBase($p, $newbase) if @_ > 1;
202 return $oldbase;
203}
204
205sub context {
206 my $ctx = $_[0]->{Context};
207 @$ctx;
208}
209
210sub current_element {
211 my ($self) = @_;
212 @{$self->{Context}} ? $self->{Context}->[-1] : undef;
213}
214
215sub in_element {
216 my ($self, $element) = @_;
217 @{$self->{Context}} ? $self->eq_name($self->{Context}->[-1], $element)
218 : undef;
219}
220
221sub within_element {
222 my ($self, $element) = @_;
223 my $cnt = 0;
224 foreach (@{$self->{Context}}) {
225 $cnt++ if $self->eq_name($_, $element);
226 }
227 return $cnt;
228}
229
230sub depth {
231 my ($self) = @_;
232 int(@{$self->{Context}});
233}
234
235sub element_index {
236 my ($self) = @_;
237
238 if ($self->{_State_} == 1) {
239 return ElementIndex($self->{Parser});
240 }
241}
242
243################
244# Namespace methods
245
246sub namespace {
247 my ($self, $name) = @_;
248 local($^W) = 0;
249 $self->{Namespace_List}->[int($name)];
250}
251
252sub eq_name {
253 my ($self, $nm1, $nm2) = @_;
254 local($^W) = 0;
255
256 int($nm1) == int($nm2) and $nm1 eq $nm2;
257}
258
259sub generate_ns_name {
260 my ($self, $name, $namespace) = @_;
261
262 $namespace ?
263 GenerateNSName($name, $namespace, $self->{Namespace_Table},
264 $self->{Namespace_List})
265 : $name;
266}
267
268sub new_ns_prefixes {
269 my ($self) = @_;
270 if ($self->{Namespaces}) {
271 return @{$self->{New_Prefixes}};
272 }
273 return ();
274}
275
276sub expand_ns_prefix {
277 my ($self, $prefix) = @_;
278
279 if ($self->{Namespaces}) {
280 my $stack = $self->{Prefix_Table}->{$prefix};
281 return (defined($stack) and @$stack) ? $stack->[-1] : undef;
282 }
283
284 return undef;
285}
286
287sub current_ns_prefixes {
288 my ($self) = @_;
289
290 if ($self->{Namespaces}) {
291 my %set = %{$self->{Prefix_Table}};
292
293 if (exists $set{'#default'} and not defined($set{'#default'}->[-1])) {
294 delete $set{'#default'};
295 }
296
297 return keys %set;
298 }
299
300 return ();
301}
302
303
304################################################################
305# Namespace declaration handlers
306#
307
308sub NamespaceStart {
309 my ($self, $prefix, $uri) = @_;
310
311 $prefix = '#default' unless defined $prefix;
312 my $stack = $self->{Prefix_Table}->{$prefix};
313
314 if (defined $stack) {
315 push(@$stack, $uri);
316 }
317 else {
318 $self->{Prefix_Table}->{$prefix} = [$uri];
319 }
320
321 # The New_Prefixes list gets emptied at end of startElement function
322 # in Expat.xs
323
324 push(@{$self->{New_Prefixes}}, $prefix);
325}
326
327sub NamespaceEnd {
328 my ($self, $prefix) = @_;
329
330 $prefix = '#default' unless defined $prefix;
331
332 my $stack = $self->{Prefix_Table}->{$prefix};
333 if (@$stack > 1) {
334 pop(@$stack);
335 }
336 else {
337 delete $self->{Prefix_Table}->{$prefix};
338 }
339}
340
341################
342
343sub specified_attr {
344 my $self = shift;
345
346 if ($self->{_State_} == 1) {
347 return GetSpecifiedAttributeCount($self->{Parser});
348 }
349}
350
351sub finish {
352 my ($self) = @_;
353 if ($self->{_State_} == 1) {
354 my $parser = $self->{Parser};
355 UnsetAllHandlers($parser);
356 }
357}
358
359sub position_in_context {
360 my ($self, $lines) = @_;
361 if ($self->{_State_} == 1) {
362 my $parser = $self->{Parser};
363 my ($string, $linepos) = PositionContext($parser, $lines);
364
365 return '' unless defined($string);
366
367 my $col = GetCurrentColumnNumber($parser);
368 my $ptr = ('=' x ($col - 1)) . '^' . "\n";
369 my $ret;
370 my $dosplit = $linepos < length($string);
371
372 $string .= "\n" unless $string =~ /\n$/;
373
374 if ($dosplit) {
375 $ret = substr($string, 0, $linepos) . $ptr
376 . substr($string, $linepos);
377 } else {
378 $ret = $string . $ptr;
379 }
380
381 return $ret;
382 }
383}
384
385sub xml_escape {
386 my $self = shift;
387 my $text = shift;
388
389 study $text;
390 $text =~ s/\&/\&amp;/g;
391 $text =~ s/</\&lt;/g;
392 foreach (@_) {
393 croak "xml_escape: '$_' isn't a single character" if length($_) > 1;
394
395 if ($_ eq '>') {
396 $text =~ s/>/\&gt;/g;
397 }
398 elsif ($_ eq '"') {
399 $text =~ s/\"/\&quot;/;
400 }
401 elsif ($_ eq "'") {
402 $text =~ s/\'/\&apos;/;
403 }
404 else {
405 my $rep = '&#' . sprintf('x%X', ord($_)) . ';';
406 if (/\W/) {
407 my $ptrn = "\\$_";
408 $text =~ s/$ptrn/$rep/g;
409 }
410 else {
411 $text =~ s/$_/$rep/g;
412 }
413 }
414 }
415 $text;
416}
417
418sub skip_until {
419 my $self = shift;
420 if ($self->{_State_} <= 1) {
421 SkipUntil($self->{Parser}, $_[0]);
422 }
423}
424
425sub release {
426 my $self = shift;
427 ParserRelease($self->{Parser});
428}
429
430sub DESTROY {
431 my $self = shift;
432 ParserFree($self->{Parser});
433}
434
435sub parse {
436 my $self = shift;
437 my $arg = shift;
438 croak "Parse already in progress (Expat)" if $self->{_State_};
439 $self->{_State_} = 1;
440 my $parser = $self->{Parser};
441 my $ioref;
442 my $result = 0;
443
444 if (defined $arg) {
445 if (ref($arg) and UNIVERSAL::isa($arg, 'IO::Handle')) {
446 $ioref = $arg;
447 } elsif (tied($arg)) {
448 my $class = ref($arg);
449 no strict 'refs';
450 $ioref = $arg if defined &{"${class}::TIEHANDLE"};
451 }
452 else {
453 require IO::Handle;
454 eval {
455 no strict 'refs';
456 $ioref = *{$arg}{IO} if defined *{$arg};
457 };
458 undef $@;
459 }
460 }
461
462 if (defined($ioref)) {
463 my $delim = $self->{Stream_Delimiter};
464 my $prev_rs;
465
466 $prev_rs = ref($ioref)->input_record_separator("\n$delim\n")
467 if defined($delim);
468
469 $result = ParseStream($parser, $ioref, $delim);
470
471 ref($ioref)->input_record_separator($prev_rs)
472 if defined($delim);
473 } else {
474 $result = ParseString($parser, $arg);
475 }
476
477 $self->{_State_} = 2;
478 $result or croak $self->{ErrorMessage};
479}
480
481sub parsestring {
482 my $self = shift;
483 $self->parse(@_);
484}
485
486sub parsefile {
487 my $self = shift;
488 croak "Parser has already been used" if $self->{_State_};
489 local(*FILE);
490 open(FILE, $_[0]) or croak "Couldn't open $_[0]:\n$!";
491 binmode(FILE);
492 my $ret = $self->parse(*FILE);
493 close(FILE);
494 $ret;
495}
496
497################################################################
498package #hide from PAUSE
499 XML::Parser::ContentModel;
500use overload '""' => \&asString, 'eq' => \&thiseq;
501
502sub EMPTY () {1}
503sub ANY () {2}
504sub MIXED () {3}
505sub NAME () {4}
506sub CHOICE () {5}
507sub SEQ () {6}
508
509
510sub isempty {
511 return $_[0]->{Type} == EMPTY;
512}
513
514sub isany {
515 return $_[0]->{Type} == ANY;
516}
517
518sub ismixed {
519 return $_[0]->{Type} == MIXED;
520}
521
522sub isname {
523 return $_[0]->{Type} == NAME;
524}
525
526sub name {
527 return $_[0]->{Tag};
528}
529
530sub ischoice {
531 return $_[0]->{Type} == CHOICE;
532}
533
534sub isseq {
535 return $_[0]->{Type} == SEQ;
536}
537
538sub quant {
539 return $_[0]->{Quant};
540}
541
542sub children {
543 my $children = $_[0]->{Children};
544 if (defined $children) {
545 return @$children;
546 }
547 return undef;
548}
549
550sub asString {
551 my ($self) = @_;
552 my $ret;
553
554 if ($self->{Type} == NAME) {
555 $ret = $self->{Tag};
556 }
557 elsif ($self->{Type} == EMPTY) {
558 return "EMPTY";
559 }
560 elsif ($self->{Type} == ANY) {
561 return "ANY";
562 }
563 elsif ($self->{Type} == MIXED) {
564 $ret = '(#PCDATA';
565 foreach (@{$self->{Children}}) {
566 $ret .= '|' . $_;
567 }
568 $ret .= ')';
569 }
570 else {
571 my $sep = $self->{Type} == CHOICE ? '|' : ',';
572 $ret = '(' . join($sep, map { $_->asString } @{$self->{Children}}) . ')';
573 }
574
575 $ret .= $self->{Quant} if $self->{Quant};
576 return $ret;
577}
578
579sub thiseq {
580 my $self = shift;
581
582 return $self->asString eq $_[0];
583}
584
585################################################################
586package #hide from PAUSE
587 XML::Parser::ExpatNB;
588
589use vars qw(@ISA);
590use Carp;
591
592@ISA = qw(XML::Parser::Expat);
593
594sub parse {
595 my $self = shift;
596 my $class = ref($self);
597 croak "parse method not supported in $class";
598}
599
600sub parsestring {
601 my $self = shift;
602 my $class = ref($self);
603 croak "parsestring method not supported in $class";
604}
605
606sub parsefile {
607 my $self = shift;
608 my $class = ref($self);
609 croak "parsefile method not supported in $class";
610}
611
612sub parse_more {
613 my ($self, $data) = @_;
614
615 $self->{_State_} = 1;
616 my $ret = XML::Parser::Expat::ParsePartial($self->{Parser}, $data);
617
618 croak $self->{ErrorMessage} unless $ret;
619}
620
621sub parse_done {
622 my $self = shift;
623
624 my $ret = XML::Parser::Expat::ParseDone($self->{Parser});
625 unless ($ret) {
626 my $msg = $self->{ErrorMessage};
627 $self->release;
628 croak $msg;
629 }
630
631 $self->{_State_} = 2;
632
633 my $result = $ret;
634 my @result = ();
635 my $final = $self->{FinalHandler};
636 if (defined $final) {
637 if (wantarray) {
638 @result = &$final($self);
639 }
640 else {
641 $result = &$final($self);
642 }
643 }
644
645 $self->release;
646
647 return unless defined wantarray;
648 return wantarray ? @result : $result;
649}
650
651################################################################
652
653package #hide from PAUSE
654 XML::Parser::Encinfo;
655
656sub DESTROY {
657 my $self = shift;
658 XML::Parser::Expat::FreeEncoding($self);
659}
660
6611;
662
663__END__
664
665=head1 NAME
666
667XML::Parser::Expat - Lowlevel access to James Clark's expat XML parser
668
669=head1 SYNOPSIS
670
671 use XML::Parser::Expat;
672
673 $parser = XML::Parser::Expat->new;
674 $parser->setHandlers('Start' => \&sh,
675 'End' => \&eh,
676 'Char' => \&ch);
677 open(FOO, '<', 'info.xml') or die "Couldn't open";
678 $parser->parse(*FOO);
679 close(FOO);
680 # $parser->parse('<foo id="me"> here <em>we</em> go </foo>');
681
682 sub sh
683 {
684 my ($p, $el, %atts) = @_;
685 $p->setHandlers('Char' => \&spec)
686 if ($el eq 'special');
687 ...
688 }
689
690 sub eh
691 {
692 my ($p, $el) = @_;
693 $p->setHandlers('Char' => \&ch) # Special elements won't contain
694 if ($el eq 'special'); # other special elements
695 ...
696 }
697
698=head1 DESCRIPTION
699
700This module provides an interface to James Clark's XML parser, expat. As in
701expat, a single instance of the parser can only parse one document. Calls
702to parsestring after the first for a given instance will die.
703
704Expat (and XML::Parser::Expat) are event based. As the parser recognizes
705parts of the document (say the start or end of an XML element), then any
706handlers registered for that type of an event are called with suitable
707parameters.
708
709=head1 METHODS
710
711=over 4
712
713=item new
714
715This is a class method, the constructor for XML::Parser::Expat. Options are
716passed as keyword value pairs. The recognized options are:
717
718=over 4
719
720=item * ProtocolEncoding
721
722The protocol encoding name. The default is none. The expat built-in
723encodings are: C<UTF-8>, C<ISO-8859-1>, C<UTF-16>, and C<US-ASCII>.
724Other encodings may be used if they have encoding maps in one of the
725directories in the @Encoding_Path list. Setting the protocol encoding
726overrides any encoding in the XML declaration.
727
728=item * Namespaces
729
730When this option is given with a true value, then the parser does namespace
731processing. By default, namespace processing is turned off. When it is
732turned on, the parser consumes I<xmlns> attributes and strips off prefixes
733from element and attributes names where those prefixes have a defined
734namespace. A name's namespace can be found using the L<"namespace"> method
735and two names can be checked for absolute equality with the L<"eq_name">
736method.
737
738=item * NoExpand
739
740Normally, the parser will try to expand references to entities defined in
741the internal subset. If this option is set to a true value, and a default
742handler is also set, then the default handler will be called when an
743entity reference is seen in text. This has no effect if a default handler
744has not been registered, and it has no effect on the expansion of entity
745references inside attribute values.
746
747=item * Stream_Delimiter
748
749This option takes a string value. When this string is found alone on a line
750while parsing from a stream, then the parse is ended as if it saw an end of
751file. The intended use is with a stream of xml documents in a MIME multipart
752format. The string should not contain a trailing newline.
753
754=item * ErrorContext
755
756When this option is defined, errors are reported in context. The value
757of ErrorContext should be the number of lines to show on either side of
758the line in which the error occurred.
759
760=item * ParseParamEnt
761
762Unless standalone is set to "yes" in the XML declaration, setting this to
763a true value allows the external DTD to be read, and parameter entities
764to be parsed and expanded.
765
766=item * Base
767
768The base to use for relative pathnames or URLs. This can also be done by
769using the base method.
770
771=back
772
773=item setHandlers(TYPE, HANDLER [, TYPE, HANDLER [...]])
774
775This method registers handlers for the various events. If no handlers are
776registered, then a call to parsestring or parsefile will only determine if
777the corresponding XML document is well formed (by returning without error.)
778This may be called from within a handler, after the parse has started.
779
780Setting a handler to something that evaluates to false unsets that
781handler.
782
783This method returns a list of type, handler pairs corresponding to the
784input. The handlers returned are the ones that were in effect before the
785call to setHandlers.
786
787The recognized events and the parameters passed to the corresponding
788handlers are:
789
790=over 4
791
792=item * Start (Parser, Element [, Attr, Val [,...]])
793
794This event is generated when an XML start tag is recognized. Parser is
795an XML::Parser::Expat instance. Element is the name of the XML element that
796is opened with the start tag. The Attr & Val pairs are generated for each
797attribute in the start tag.
798
799=item * End (Parser, Element)
800
801This event is generated when an XML end tag is recognized. Note that
802an XML empty tag (<foo/>) generates both a start and an end event.
803
804There is always a lower level start and end handler installed that wrap
805the corresponding callbacks. This is to handle the context mechanism.
806A consequence of this is that the default handler (see below) will not
807see a start tag or end tag unless the default_current method is called.
808
809=item * Char (Parser, String)
810
811This event is generated when non-markup is recognized. The non-markup
812sequence of characters is in String. A single non-markup sequence of
813characters may generate multiple calls to this handler. Whatever the
814encoding of the string in the original document, this is given to the
815handler in UTF-8.
816
817=item * Proc (Parser, Target, Data)
818
819This event is generated when a processing instruction is recognized.
820
821=item * Comment (Parser, String)
822
823This event is generated when a comment is recognized.
824
825=item * CdataStart (Parser)
826
827This is called at the start of a CDATA section.
828
829=item * CdataEnd (Parser)
830
831This is called at the end of a CDATA section.
832
833=item * Default (Parser, String)
834
835This is called for any characters that don't have a registered handler.
836This includes both characters that are part of markup for which no
837events are generated (markup declarations) and characters that
838could generate events, but for which no handler has been registered.
839
840Whatever the encoding in the original document, the string is returned to
841the handler in UTF-8.
842
843=item * Unparsed (Parser, Entity, Base, Sysid, Pubid, Notation)
844
845This is called for a declaration of an unparsed entity. Entity is the name
846of the entity. Base is the base to be used for resolving a relative URI.
847Sysid is the system id. Pubid is the public id. Notation is the notation
848name. Base and Pubid may be undefined.
849
850=item * Notation (Parser, Notation, Base, Sysid, Pubid)
851
852This is called for a declaration of notation. Notation is the notation name.
853Base is the base to be used for resolving a relative URI. Sysid is the system
854id. Pubid is the public id. Base, Sysid, and Pubid may all be undefined.
855
856=item * ExternEnt (Parser, Base, Sysid, Pubid)
857
858This is called when an external entity is referenced. Base is the base to be
859used for resolving a relative URI. Sysid is the system id. Pubid is the public
860id. Base, and Pubid may be undefined.
861
862This handler should either return a string, which represents the contents of
863the external entity, or return an open filehandle that can be read to obtain
864the contents of the external entity, or return undef, which indicates the
865external entity couldn't be found and will generate a parse error.
866
867If an open filehandle is returned, it must be returned as either a glob
868(*FOO) or as a reference to a glob (e.g. an instance of IO::Handle).
869
870=item * ExternEntFin (Parser)
871
872This is called after an external entity has been parsed. It allows
873applications to perform cleanup on actions performed in the above
874ExternEnt handler.
875
876=item * Entity (Parser, Name, Val, Sysid, Pubid, Ndata, IsParam)
877
878This is called when an entity is declared. For internal entities, the Val
879parameter will contain the value and the remaining three parameters will
880be undefined. For external entities, the Val parameter
881will be undefined, the Sysid parameter will have the system id, the Pubid
882parameter will have the public id if it was provided (it will be undefined
883otherwise), the Ndata parameter will contain the notation for unparsed
884entities. If this is a parameter entity declaration, then the IsParam
885parameter is true.
886
887Note that this handler and the Unparsed handler above overlap. If both are
888set, then this handler will not be called for unparsed entities.
889
890=item * Element (Parser, Name, Model)
891
892The element handler is called when an element declaration is found. Name is
893the element name, and Model is the content model as an
894XML::Parser::ContentModel object. See L<"XML::Parser::ContentModel Methods">
895for methods available for this class.
896
897=item * Attlist (Parser, Elname, Attname, Type, Default, Fixed)
898
899This handler is called for each attribute in an ATTLIST declaration.
900So an ATTLIST declaration that has multiple attributes
901will generate multiple calls to this handler. The Elname parameter is the
902name of the element with which the attribute is being associated. The Attname
903parameter is the name of the attribute. Type is the attribute type, given as
904a string. Default is the default value, which will either be "#REQUIRED",
905"#IMPLIED" or a quoted string (i.e. the returned string will begin and end
906with a quote character). If Fixed is true, then this is a fixed attribute.
907
908=item * Doctype (Parser, Name, Sysid, Pubid, Internal)
909
910This handler is called for DOCTYPE declarations. Name is the document type
911name. Sysid is the system id of the document type, if it was provided,
912otherwise it's undefined. Pubid is the public id of the document type,
913which will be undefined if no public id was given. Internal will be
914true or false, indicating whether or not the doctype declaration contains
915an internal subset.
916
917=item * DoctypeFin (Parser)
918
919This handler is called after parsing of the DOCTYPE declaration has finished,
920including any internal or external DTD declarations.
921
922=item * XMLDecl (Parser, Version, Encoding, Standalone)
923
924This handler is called for XML declarations. Version is a string containg
925the version. Encoding is either undefined or contains an encoding string.
926Standalone is either undefined, or true or false. Undefined indicates
927that no standalone parameter was given in the XML declaration. True or
928false indicates "yes" or "no" respectively.
929
930=back
931
932=item namespace(name)
933
934Return the URI of the namespace that the name belongs to. If the name doesn't
935belong to any namespace, an undef is returned. This is only valid on names
936received through the Start or End handlers from a single document, or through
937a call to the generate_ns_name method. In other words, don't use names
938generated from one instance of XML::Parser::Expat with other instances.
939
940=item eq_name(name1, name2)
941
942Return true if name1 and name2 are identical (i.e. same name and from
943the same namespace.) This is only meaningful if both names were obtained
944through the Start or End handlers from a single document, or through
945a call to the generate_ns_name method.
946
947=item generate_ns_name(name, namespace)
948
949Return a name, associated with a given namespace, good for using with the
950above 2 methods. The namespace argument should be the namespace URI, not
951a prefix.
952
953=item new_ns_prefixes
954
955When called from a start tag handler, returns namespace prefixes declared
956with this start tag. If called elsewere (or if there were no namespace
957prefixes declared), it returns an empty list. Setting of the default
958namespace is indicated with '#default' as a prefix.
959
960=item expand_ns_prefix(prefix)
961
962Return the uri to which the given prefix is currently bound. Returns
963undef if the prefix isn't currently bound. Use '#default' to find the
964current binding of the default namespace (if any).
965
966=item current_ns_prefixes
967
968Return a list of currently bound namespace prefixes. The order of the
969the prefixes in the list has no meaning. If the default namespace is
970currently bound, '#default' appears in the list.
971
972=item recognized_string
973
974Returns the string from the document that was recognized in order to call
975the current handler. For instance, when called from a start handler, it
976will give us the the start-tag string. The string is encoded in UTF-8.
977This method doesn't return a meaningful string inside declaration handlers.
978
979=item original_string
980
981Returns the verbatim string from the document that was recognized in
982order to call the current handler. The string is in the original document
983encoding. This method doesn't return a meaningful string inside declaration
984handlers.
985
986=item default_current
987
988When called from a handler, causes the sequence of characters that generated
989the corresponding event to be sent to the default handler (if one is
990registered). Use of this method is deprecated in favor the recognized_string
991method, which you can use without installing a default handler. This
992method doesn't deliver a meaningful string to the default handler when
993called from inside declaration handlers.
994
995=item xpcroak(message)
996
997Concatenate onto the given message the current line number within the
998XML document plus the message implied by ErrorContext. Then croak with
999the formed message.
1000
1001=item xpcarp(message)
1002
1003Concatenate onto the given message the current line number within the
1004XML document plus the message implied by ErrorContext. Then carp with
1005the formed message.
1006
1007=item current_line
1008
1009Returns the line number of the current position of the parse.
1010
1011=item current_column
1012
1013Returns the column number of the current position of the parse.
1014
1015=item current_byte
1016
1017Returns the current position of the parse.
1018
1019=item base([NEWBASE]);
1020
1021Returns the current value of the base for resolving relative URIs. If
1022NEWBASE is supplied, changes the base to that value.
1023
1024=item context
1025
1026Returns a list of element names that represent open elements, with the
1027last one being the innermost. Inside start and end tag handlers, this
1028will be the tag of the parent element.
1029
1030=item current_element
1031
1032Returns the name of the innermost currently opened element. Inside
1033start or end handlers, returns the parent of the element associated
1034with those tags.
1035
1036=item in_element(NAME)
1037
1038Returns true if NAME is equal to the name of the innermost currently opened
1039element. If namespace processing is being used and you want to check
1040against a name that may be in a namespace, then use the generate_ns_name
1041method to create the NAME argument.
1042
1043=item within_element(NAME)
1044
1045Returns the number of times the given name appears in the context list.
1046If namespace processing is being used and you want to check
1047against a name that may be in a namespace, then use the generate_ns_name
1048method to create the NAME argument.
1049
1050=item depth
1051
1052Returns the size of the context list.
1053
1054=item element_index
1055
1056Returns an integer that is the depth-first visit order of the current
1057element. This will be zero outside of the root element. For example,
1058this will return 1 when called from the start handler for the root element
1059start tag.
1060
1061=item skip_until(INDEX)
1062
1063INDEX is an integer that represents an element index. When this method
1064is called, all handlers are suspended until the start tag for an element
1065that has an index number equal to INDEX is seen. If a start handler has
1066been set, then this is the first tag that the start handler will see
1067after skip_until has been called.
1068
1069
1070=item position_in_context(LINES)
1071
1072Returns a string that shows the current parse position. LINES should be
1073an integer >= 0 that represents the number of lines on either side of the
1074current parse line to place into the returned string.
1075
1076=item xml_escape(TEXT [, CHAR [, CHAR ...]])
1077
1078Returns TEXT with markup characters turned into character entities. Any
1079additional characters provided as arguments are also turned into character
1080references where found in TEXT.
1081
1082=item parse (SOURCE)
1083
1084The SOURCE parameter should either be a string containing the whole XML
1085document, or it should be an open IO::Handle. Only a single document
1086may be parsed for a given instance of XML::Parser::Expat, so this will croak
1087if it's been called previously for this instance.
1088
1089=item parsestring(XML_DOC_STRING)
1090
1091Parses the given string as an XML document. Only a single document may be
1092parsed for a given instance of XML::Parser::Expat, so this will die if either
1093parsestring or parsefile has been called for this instance previously.
1094
1095This method is deprecated in favor of the parse method.
1096
1097=item parsefile(FILENAME)
1098
1099Parses the XML document in the given file. Will die if parsestring or
1100parsefile has been called previously for this instance.
1101
1102=item is_defaulted(ATTNAME)
1103
1104NO LONGER WORKS. To find out if an attribute is defaulted please use
1105the specified_attr method.
1106
1107=item specified_attr
1108
1109When the start handler receives lists of attributes and values, the
1110non-defaulted (i.e. explicitly specified) attributes occur in the list
1111first. This method returns the number of specified items in the list.
1112So if this number is equal to the length of the list, there were no
1113defaulted values. Otherwise the number points to the index of the
1114first defaulted attribute name.
1115
1116=item finish
1117
1118Unsets all handlers (including internal ones that set context), but expat
1119continues parsing to the end of the document or until it finds an error.
1120It should finish up a lot faster than with the handlers set.
1121
1122=item release
1123
1124There are data structures used by XML::Parser::Expat that have circular
1125references. This means that these structures will never be garbage
1126collected unless these references are explicitly broken. Calling this
1127method breaks those references (and makes the instance unusable.)
1128
1129Normally, higher level calls handle this for you, but if you are using
1130XML::Parser::Expat directly, then it's your responsibility to call it.
1131
1132=back
1133
1134=head2 XML::Parser::ContentModel Methods
1135
1136The element declaration handlers are passed objects of this class as the
1137content model of the element declaration. They also represent content
1138particles, components of a content model.
1139
1140When referred to as a string, these objects are automagicly converted to a
1141string representation of the model (or content particle).
1142
1143=over 4
1144
1145=item isempty
1146
1147This method returns true if the object is "EMPTY", false otherwise.
1148
1149=item isany
1150
1151This method returns true if the object is "ANY", false otherwise.
1152
1153=item ismixed
1154
1155This method returns true if the object is "(#PCDATA)" or "(#PCDATA|...)*",
1156false otherwise.
1157
1158=item isname
1159
1160This method returns if the object is an element name.
1161
1162=item ischoice
1163
1164This method returns true if the object is a choice of content particles.
1165
1166
1167=item isseq
1168
1169This method returns true if the object is a sequence of content particles.
1170
1171=item quant
1172
1173This method returns undef or a string representing the quantifier
1174('?', '*', '+') associated with the model or particle.
1175
1176=item children
1177
1178This method returns undef or (for mixed, choice, and sequence types)
1179an array of component content particles. There will always be at least
1180one component for choices and sequences, but for a mixed content model
1181of pure PCDATA, "(#PCDATA)", then an undef is returned.
1182
1183=back
1184
1185=head2 XML::Parser::ExpatNB Methods
1186
1187The class XML::Parser::ExpatNB is a subclass of XML::Parser::Expat used
1188for non-blocking access to the expat library. It does not support the parse,
1189parsestring, or parsefile methods, but it does have these additional methods:
1190
1191=over 4
1192
1193=item parse_more(DATA)
1194
1195Feed expat more text to munch on.
1196
1197=item parse_done
1198
1199Tell expat that it's gotten the whole document.
1200
1201=back
1202
1203=head1 FUNCTIONS
1204
1205=over 4
1206
1207=item XML::Parser::Expat::load_encoding(ENCODING)
1208
1209Load an external encoding. ENCODING is either the name of an encoding or
1210the name of a file. The basename is converted to lowercase and a '.enc'
1211extension is appended unless there's one already there. Then, unless
1212it's an absolute pathname (i.e. begins with '/'), the first file by that
1213name discovered in the @Encoding_Path path list is used.
1214
1215The encoding in the file is loaded and kept in the %Encoding_Table
1216table. Earlier encodings of the same name are replaced.
1217
1218This function is automaticly called by expat when it encounters an encoding
1219it doesn't know about. Expat shouldn't call this twice for the same
1220encoding name. The only reason users should use this function is to
1221explicitly load an encoding not contained in the @Encoding_Path list.
1222
1223=back
1224
1225=head1 AUTHORS
1226
1227Larry Wall <F<[email protected]>> wrote version 1.0.
1228
1229Clark Cooper <F<[email protected]>> picked up support, changed the API
1230for this version (2.x), provided documentation, and added some standard
1231package features.
1232
1233=cut
Note: See TracBrowser for help on using the repository browser.