Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1045 lines
30 KiB

  1. package XML::Parser::Expat;
  2. require 5.004;
  3. use strict;
  4. use vars qw($VERSION @ISA %Handler_Setters %Encoding_Table @Encoding_Path
  5. $have_File_Spec);
  6. use Carp;
  7. use IO::Handle;
  8. require DynaLoader;
  9. @ISA = qw(DynaLoader);
  10. $VERSION = "2.27" ;
  11. $have_File_Spec = do 'File/Spec.pm';
  12. %Encoding_Table = ();
  13. if ($have_File_Spec) {
  14. @Encoding_Path = (grep(-d $_,
  15. map(File::Spec->catdir($_, qw(XML Parser Encodings)),
  16. @INC)),
  17. File::Spec->curdir);
  18. }
  19. else {
  20. @Encoding_Path = (grep(-d $_, map($_ . '/XML/Parser/Encodings', @INC)), '.');
  21. }
  22. bootstrap XML::Parser::Expat $VERSION;
  23. %Handler_Setters = (
  24. Start => \&SetStartElementHandler,
  25. End => \&SetEndElementHandler,
  26. Char => \&SetCharacterDataHandler,
  27. Proc => \&SetProcessingInstructionHandler,
  28. Comment => \&SetCommentHandler,
  29. CdataStart => \&SetStartCdataHandler,
  30. CdataEnd => \&SetEndCdataHandler,
  31. Default => \&SetDefaultHandler,
  32. Unparsed => \&SetUnparsedEntityDeclHandler,
  33. Notation => \&SetNotationDeclHandler,
  34. ExternEnt => \&SetExternalEntityRefHandler,
  35. Entity => \&SetEntityDeclHandler,
  36. Element => \&SetElementDeclHandler,
  37. Attlist => \&SetAttListDeclHandler,
  38. Doctype => \&SetDoctypeHandler,
  39. XMLDecl => \&SetXMLDeclHandler
  40. );
  41. sub new {
  42. my ($class, %args) = @_;
  43. my $self = bless \%args, $_[0];
  44. $args{Used} = 0;
  45. $args{Context} = [];
  46. $args{Namespaces} ||= 0;
  47. $args{ErrorMessage} ||= '';
  48. if ($args{Namespaces}) {
  49. $args{Namespace_Table} = {};
  50. $args{Namespace_List} = [undef];
  51. $args{Prefix_Table} = {};
  52. $args{New_Prefixes} = [];
  53. }
  54. $args{_Setters} = \%Handler_Setters;
  55. $args{Parser} = ParserCreate($self, $args{ProtocolEncoding},
  56. $args{Namespaces});
  57. $self;
  58. }
  59. sub load_encoding {
  60. my ($file) = @_;
  61. $file =~ s!([^/]+)$!\L$1\E!;
  62. $file .= '.enc' unless $file =~ /\.enc$/;
  63. unless ($file =~ m!^/!) {
  64. foreach (@Encoding_Path) {
  65. my $tmp = ($have_File_Spec
  66. ? File::Spec->catfile($_, $file)
  67. : "$_/$file");
  68. if (-e $tmp) {
  69. $file = $tmp;
  70. last;
  71. }
  72. }
  73. }
  74. local(*ENC);
  75. open(ENC, $file) or croak("Couldn't open encmap $file:\n$!\n");
  76. binmode(ENC);
  77. my $data;
  78. my $br = sysread(ENC, $data, -s $file);
  79. croak("Trouble reading $file:\n$!\n")
  80. unless defined($br);
  81. close(ENC);
  82. my $name = LoadEncoding($data, $br);
  83. croak("$file isn't an encmap file")
  84. unless defined($name);
  85. $name;
  86. } # End load_encoding
  87. sub setHandlers {
  88. my ($self, @handler_pairs) = @_;
  89. croak("Uneven number of arguments to setHandlers method")
  90. if (int(@handler_pairs) & 1);
  91. my @ret;
  92. while (@handler_pairs) {
  93. my $type = shift @handler_pairs;
  94. my $handler = shift @handler_pairs;
  95. croak "Handler for $type not a Code ref"
  96. unless (! defined($handler) or ! $handler or ref($handler) eq 'CODE');
  97. my $hndl = $self->{_Setters}->{$type};
  98. unless (defined($hndl)) {
  99. my @types = sort keys %{$self->{_Setters}};
  100. croak("Unknown Expat handler type: $type\n Valid types: @types");
  101. }
  102. my $old = &$hndl($self->{Parser}, $handler);
  103. push (@ret, $type, $old);
  104. }
  105. return @ret;
  106. }
  107. sub xpcroak {
  108. my ($self, $message) = @_;
  109. my $eclines = $self->{ErrorContext};
  110. my $line = GetCurrentLineNumber($_[0]->{Parser});
  111. $message .= " at line $line";
  112. $message .= ":\n" . $self->position_in_context($eclines)
  113. if defined($eclines);
  114. croak $message;
  115. }
  116. sub xpcarp {
  117. my ($self, $message) = @_;
  118. my $eclines = $self->{ErrorContext};
  119. my $line = GetCurrentLineNumber($_[0]->{Parser});
  120. $message .= " at line $line";
  121. $message .= ":\n" . $self->position_in_context($eclines)
  122. if defined($eclines);
  123. carp $message;
  124. }
  125. sub default_current {
  126. my $self = shift;
  127. DefaultCurrent($self->{Parser});
  128. }
  129. sub recognized_string {
  130. my $self = shift;
  131. RecognizedString($self->{Parser});
  132. }
  133. sub original_string {
  134. my $self = shift;
  135. OriginalString($self->{Parser});
  136. }
  137. sub current_line {
  138. GetCurrentLineNumber($_[0]->{Parser});
  139. }
  140. sub current_column {
  141. GetCurrentColumnNumber($_[0]->{Parser});
  142. }
  143. sub current_byte {
  144. GetCurrentByteIndex($_[0]->{Parser});
  145. }
  146. sub base {
  147. my ($self, $newbase) = @_;
  148. my $p = $self->{Parser};
  149. my $oldbase = GetBase($p);
  150. SetBase($p, $newbase) if @_ > 1;
  151. $oldbase;
  152. }
  153. sub context {
  154. my $ctx = $_[0]->{Context};
  155. @$ctx;
  156. }
  157. sub current_element {
  158. my ($self) = @_;
  159. @{$self->{Context}} ? $self->{Context}->[-1] : undef;
  160. }
  161. sub in_element {
  162. my ($self, $element) = @_;
  163. @{$self->{Context}} ? $self->eq_name($self->{Context}->[-1], $element)
  164. : undef;
  165. }
  166. sub within_element {
  167. my ($self, $element) = @_;
  168. my $cnt = 0;
  169. foreach (@{$self->{Context}}) {
  170. $cnt++ if $self->eq_name($_, $element);
  171. }
  172. return $cnt;
  173. }
  174. sub depth {
  175. my ($self) = @_;
  176. int(@{$self->{Context}});
  177. }
  178. sub element_index {
  179. my ($self) = @_;
  180. ElementIndex($self->{Parser});
  181. }
  182. ################
  183. # Namespace methods
  184. sub namespace {
  185. my ($self, $name) = @_;
  186. local($^W) = 0;
  187. $self->{Namespace_List}->[int($name)];
  188. }
  189. sub eq_name {
  190. my ($self, $nm1, $nm2) = @_;
  191. local($^W) = 0;
  192. int($nm1) == int($nm2) and $nm1 eq $nm2;
  193. }
  194. sub generate_ns_name {
  195. my ($self, $name, $namespace) = @_;
  196. $namespace ?
  197. GenerateNSName($name, $namespace, $self->{Namespace_Table},
  198. $self->{Namespace_List})
  199. : $name;
  200. }
  201. sub new_ns_prefixes {
  202. my ($self) = @_;
  203. if ($self->{Namespaces}) {
  204. return @{$self->{New_Prefixes}};
  205. }
  206. return ();
  207. }
  208. sub expand_ns_prefix {
  209. my ($self, $prefix) = @_;
  210. if ($self->{Namespaces}) {
  211. my $stack = $self->{Prefix_Table}->{$prefix};
  212. return (defined($stack) and @$stack) ? $stack->[-1] : undef;
  213. }
  214. return undef;
  215. }
  216. sub current_ns_prefixes {
  217. my ($self) = @_;
  218. if ($self->{Namespaces}) {
  219. my %set = %{$self->{Prefix_Table}};
  220. if (exists $set{'#default'} and not defined($set{'#default'}->[-1])) {
  221. delete $set{'#default'};
  222. }
  223. return keys %set;
  224. }
  225. return ();
  226. }
  227. ################################################################
  228. # Namespace declaration handlers
  229. #
  230. sub NamespaceStart {
  231. my ($self, $prefix, $uri) = @_;
  232. $prefix = '#default' unless defined $prefix;
  233. my $stack = $self->{Prefix_Table}->{$prefix};
  234. if (defined $stack) {
  235. push(@$stack, $uri);
  236. }
  237. else {
  238. $self->{Prefix_Table}->{$prefix} = [$uri];
  239. }
  240. # The New_Prefixes list gets emptied at end of startElement function
  241. # in Expat.xs
  242. push(@{$self->{New_Prefixes}}, $prefix);
  243. }
  244. sub NamespaceEnd {
  245. my ($self, $prefix) = @_;
  246. $prefix = '#default' unless defined $prefix;
  247. my $stack = $self->{Prefix_Table}->{$prefix};
  248. if (@$stack > 1) {
  249. pop(@$stack);
  250. }
  251. else {
  252. delete $self->{Prefix_Table}->{$prefix};
  253. }
  254. }
  255. ################
  256. sub is_defaulted {
  257. my ($self, $attname) = @_;
  258. warn <<'End_of_Warning;';
  259. The is_defaulted method no longer works.
  260. Please use the specified_attr method instead. The is_defaulted method will
  261. be removed altogether in a future version.
  262. End_of_Warning;
  263. return 0;
  264. }
  265. sub specified_attr {
  266. my $self = shift;
  267. GetSpecifiedAttributeCount($self->{Parser});
  268. }
  269. sub finish {
  270. my ($self) = @_;
  271. my $parser = $self->{Parser};
  272. UnsetAllHandlers($parser);
  273. }
  274. sub position_in_context {
  275. my ($self, $lines) = @_;
  276. my $parser = $self->{Parser};
  277. my ($string, $linepos) = PositionContext($parser, $lines);
  278. return '' unless defined($string);
  279. my $col = GetCurrentColumnNumber($parser);
  280. my $ptr = ('=' x ($col - 1)) . '^' . "\n";
  281. my $ret;
  282. my $dosplit = $linepos < length($string);
  283. $string .= "\n" unless $string =~ /\n$/;
  284. if ($dosplit) {
  285. $ret = substr($string, 0, $linepos) . $ptr
  286. . substr($string, $linepos);
  287. } else {
  288. $ret = $string . $ptr;
  289. }
  290. $ret;
  291. }
  292. sub xml_escape {
  293. my $self = shift;
  294. my $text = shift;
  295. study $text;
  296. $text =~ s/\&/\&amp;/g;
  297. $text =~ s/</\&lt;/g;
  298. foreach (@_) {
  299. if ($_ eq '>') {
  300. $text =~ s/>/\&gt;/g;
  301. }
  302. elsif ($_ eq '"') {
  303. $text =~ s/\"/\&quot;/;
  304. }
  305. elsif ($_ eq "'") {
  306. $text =~ s/\'/\&apos;/;
  307. }
  308. else {
  309. my $rep = '&#' . sprintf('x%X', ord($_)) . ';';
  310. $text =~ s/$_/$rep/g;
  311. }
  312. }
  313. $text;
  314. }
  315. sub skip_until {
  316. my $self = shift;
  317. SkipUntil($self->{Parser}, $_[0]);
  318. }
  319. sub release {
  320. my $self = shift;
  321. ParserRelease($self->{Parser});
  322. }
  323. sub DESTROY {
  324. my $self = shift;
  325. ParserFree($self->{Parser});
  326. }
  327. sub parse {
  328. my $self = shift;
  329. my $arg = shift;
  330. croak "Parse already in progress (Expat)" if $self->{Used};
  331. $self->{Used} = 1;
  332. my $parser = $self->{Parser};
  333. my $ioref;
  334. my $result = 0;
  335. if (defined $arg) {
  336. if (ref($arg) and UNIVERSAL::isa($arg, 'IO::Handler')) {
  337. $ioref = $arg;
  338. } else {
  339. eval {
  340. $ioref = *{$arg}{IO};
  341. };
  342. }
  343. }
  344. if (defined($ioref)) {
  345. my $delim = $self->{Stream_Delimiter};
  346. my $prev_rs;
  347. $prev_rs = ref($ioref)->input_record_separator("\n$delim\n")
  348. if defined($delim);
  349. $result = ParseStream($parser, $ioref, $delim);
  350. ref($ioref)->input_record_separator($prev_rs)
  351. if defined($delim);
  352. } else {
  353. $result = ParseString($parser, $arg);
  354. }
  355. $result or croak $self->{ErrorMessage};
  356. }
  357. sub parsestring {
  358. my $self = shift;
  359. $self->parse(@_);
  360. }
  361. sub parsefile {
  362. my $self = shift;
  363. croak "Parser has already been used" if $self->{Used};
  364. local(*FILE);
  365. open(FILE, $_[0]) or croak "Couldn't open $_[0]:\n$!";
  366. binmode(FILE);
  367. my $ret = $self->parse(*FILE);
  368. close(FILE);
  369. $ret;
  370. }
  371. ################################################################
  372. package XML::Parser::ExpatNB;
  373. use vars qw(@ISA);
  374. use Carp;
  375. @ISA = qw(XML::Parser::Expat);
  376. sub parse {
  377. my $self = shift;
  378. my $class = ref($self);
  379. croak "parse method not supported in $class";
  380. }
  381. sub parsestring {
  382. my $self = shift;
  383. my $class = ref($self);
  384. croak "parsestring method not supported in $class";
  385. }
  386. sub parsefile {
  387. my $self = shift;
  388. my $class = ref($self);
  389. croak "parsefile method not supported in $class";
  390. }
  391. sub parse_more {
  392. my ($self, $data) = @_;
  393. my $ret = XML::Parser::Expat::ParsePartial($self->{Parser}, $data);
  394. croak $self->{ErrorMessage} unless $ret;
  395. }
  396. sub parse_done {
  397. my $self = shift;
  398. my $ret = XML::Parser::Expat::ParseDone($self->{Parser});
  399. unless ($ret) {
  400. my $msg = $self->{ErrorMessage};
  401. $self->release;
  402. croak $msg;
  403. }
  404. my $result = $ret;
  405. my @result = ();
  406. my $final = $self->{FinalHandler};
  407. if (defined $final) {
  408. if (wantarray) {
  409. @result = &$final($self);
  410. }
  411. else {
  412. $result = &$final($self);
  413. }
  414. }
  415. $self->release;
  416. return unless defined wantarray;
  417. return wantarray ? @result : $result;
  418. }
  419. ################################################################
  420. package XML::Parser::Encinfo;
  421. sub DESTROY {
  422. my $self = shift;
  423. XML::Parser::Expat::FreeEncoding($self);
  424. }
  425. 1;
  426. __END__
  427. =head1 NAME
  428. XML::Parser::Expat - Lowlevel access to James Clark's expat XML parser
  429. =head1 SYNOPSIS
  430. use XML::Parser::Expat;
  431. $parser = new XML::Parser::Expat;
  432. $parser->setHandlers('Start' => \&sh,
  433. 'End' => \&eh,
  434. 'Char' => \&ch);
  435. open(FOO, 'info.xml') or die "Couldn't open";
  436. $parser->parse(*FOO);
  437. close(FOO);
  438. # $parser->parse('<foo id="me"> here <em>we</em> go </foo>');
  439. sub sh
  440. {
  441. my ($p, $el, %atts) = @_;
  442. $p->setHandlers('Char' => \&spec)
  443. if ($el eq 'special');
  444. ...
  445. }
  446. sub eh
  447. {
  448. my ($p, $el) = @_;
  449. $p->setHandlers('Char' => \&ch) # Special elements won't contain
  450. if ($el eq 'special'); # other special elements
  451. ...
  452. }
  453. =head1 DESCRIPTION
  454. This module provides an interface to James Clark's XML parser, expat. As in
  455. expat, a single instance of the parser can only parse one document. Calls
  456. to parsestring after the first for a given instance will die.
  457. Expat (and XML::Parser::Expat) are event based. As the parser recognizes
  458. parts of the document (say the start or end of an XML element), then any
  459. handlers registered for that type of an event are called with suitable
  460. parameters.
  461. =head1 METHODS
  462. =over 4
  463. =item new
  464. This is a class method, the constructor for XML::Parser::Expat. Options are
  465. passed as keyword value pairs. The recognized options are:
  466. =over 4
  467. =item * ProtocolEncoding
  468. The protocol encoding name. The default is none. The expat built-in
  469. encodings are: C<UTF-8>, C<ISO-8859-1>, C<UTF-16>, and C<US-ASCII>.
  470. Other encodings may be used if they have encoding maps in one of the
  471. directories in the @Encoding_Path list. Setting the protocol encoding
  472. overrides any encoding in the XML declaration.
  473. =item * Namespaces
  474. When this option is given with a true value, then the parser does namespace
  475. processing. By default, namespace processing is turned off. When it is
  476. turned on, the parser consumes I<xmlns> attributes and strips off prefixes
  477. from element and attributes names where those prefixes have a defined
  478. namespace. A name's namespace can be found using the L<"namespace"> method
  479. and two names can be checked for absolute equality with the L<"eq_name">
  480. method.
  481. =item * NoExpand
  482. Normally, the parser will try to expand references to entities defined in
  483. the internal subset. If this option is set to a true value, and a default
  484. handler is also set, then the default handler will be called when an
  485. entity reference is seen in text. This has no effect if a default handler
  486. has not been registered, and it has no effect on the expansion of entity
  487. references inside attribute values.
  488. =item * Stream_Delimiter
  489. This option takes a string value. When this string is found alone on a line
  490. while parsing from a stream, then the parse is ended as if it saw an end of
  491. file. The intended use is with a stream of xml documents in a MIME multipart
  492. format. The string should not contain a trailing newline.
  493. =item * ErrorContext
  494. When this option is defined, errors are reported in context. The value
  495. of ErrorContext should be the number of lines to show on either side of
  496. the line in which the error occurred.
  497. =item * ParseParamEnt
  498. Unless standalone is set to "yes" in the XML declaration, setting this to
  499. a true value allows the external DTD to be read, and parameter entities
  500. to be parsed and expanded.
  501. =item * Base
  502. The base to use for relative pathnames or URLs. This can also be done by
  503. using the base method.
  504. =back
  505. =item setHandlers(TYPE, HANDLER [, TYPE, HANDLER [...]])
  506. This method registers handlers for the various events. If no handlers are
  507. registered, then a call to parsestring or parsefile will only determine if
  508. the corresponding XML document is well formed (by returning without error.)
  509. This may be called from within a handler, after the parse has started.
  510. Setting a handler to something that evaluates to false unsets that
  511. handler.
  512. This method returns a list of type, handler pairs corresponding to the
  513. input. The handlers returned are the ones that were in effect before the
  514. call to setHandlers.
  515. The recognized events and the parameters passed to the corresponding
  516. handlers are:
  517. =over 4
  518. =item * Start (Parser, Element [, Attr, Val [,...]])
  519. This event is generated when an XML start tag is recognized. Parser is
  520. an XML::Parser::Expat instance. Element is the name of the XML element that
  521. is opened with the start tag. The Attr & Val pairs are generated for each
  522. attribute in the start tag.
  523. =item * End (Parser, Element)
  524. This event is generated when an XML end tag is recognized. Note that
  525. an XML empty tag (<foo/>) generates both a start and an end event.
  526. There is always a lower level start and end handler installed that wrap
  527. the corresponding callbacks. This is to handle the context mechanism.
  528. A consequence of this is that the default handler (see below) will not
  529. see a start tag or end tag unless the default_current method is called.
  530. =item * Char (Parser, String)
  531. This event is generated when non-markup is recognized. The non-markup
  532. sequence of characters is in String. A single non-markup sequence of
  533. characters may generate multiple calls to this handler. Whatever the
  534. encoding of the string in the original document, this is given to the
  535. handler in UTF-8.
  536. =item * Proc (Parser, Target, Data)
  537. This event is generated when a processing instruction is recognized.
  538. =item * Comment (Parser, String)
  539. This event is generated when a comment is recognized.
  540. =item * CdataStart (Parser)
  541. This is called at the start of a CDATA section.
  542. =item * CdataEnd (Parser)
  543. This is called at the end of a CDATA section.
  544. =item * Default (Parser, String)
  545. This is called for any characters that don't have a registered handler.
  546. This includes both characters that are part of markup for which no
  547. events are generated (markup declarations) and characters that
  548. could generate events, but for which no handler has been registered.
  549. Whatever the encoding in the original document, the string is returned to
  550. the handler in UTF-8.
  551. =item * Unparsed (Parser, Entity, Base, Sysid, Pubid, Notation)
  552. This is called for a declaration of an unparsed entity. Entity is the name
  553. of the entity. Base is the base to be used for resolving a relative URI.
  554. Sysid is the system id. Pubid is the public id. Notation is the notation
  555. name. Base and Pubid may be undefined.
  556. =item * Notation (Parser, Notation, Base, Sysid, Pubid)
  557. This is called for a declaration of notation. Notation is the notation name.
  558. Base is the base to be used for resolving a relative URI. Sysid is the system
  559. id. Pubid is the public id. Base, Sysid, and Pubid may all be undefined.
  560. =item * ExternEnt (Parser, Base, Sysid, Pubid)
  561. This is called when an external entity is referenced. Base is the base to be
  562. used for resolving a relative URI. Sysid is the system id. Pubid is the public
  563. id. Base, and Pubid may be undefined.
  564. This handler should either return a string, which represents the contents of
  565. the external entity, or return an open filehandle that can be read to obtain
  566. the contents of the external entity, or return undef, which indicates the
  567. external entity couldn't be found and will generate a parse error.
  568. If an open filehandle is returned, it must be returned as either a glob
  569. (*FOO) or as a reference to a glob (e.g. an instance of IO::Handle). The
  570. parser will close the filehandle after using it.
  571. =item * Entity (Parser, Name, Val, Sysid, Pubid, Ndata)
  572. This is called when an entity is declared. For internal entities, the Val
  573. parameter will contain the value and the remaining three parameters will
  574. be undefined. For external entities, the Val parameter
  575. will be undefined, the Sysid parameter will have the system id, the Pubid
  576. parameter will have the public id if it was provided (it will be undefined
  577. otherwise), the Ndata parameter will contain the notation for unparsed
  578. entities. If this is a parameter entity declaration, then a '%' will be
  579. prefixed to the name.
  580. Note that this handler and the Unparsed handler above overlap. If both are
  581. set, then this handler will not be called for unparsed entities.
  582. =item * Element (Parser, Name, Model)
  583. The element handler is called when an element declaration is found. Name is
  584. the element name, and Model is the content model as a string.
  585. =item * Attlist (Parser, Elname, Attname, Type, Default, Fixed)
  586. This handler is called for each attribute in an ATTLIST declaration.
  587. So an ATTLIST declaration that has multiple attributes
  588. will generate multiple calls to this handler. The Elname parameter is the
  589. name of the element with which the attribute is being associated. The Attname
  590. parameter is the name of the attribute. Type is the attribute type, given as
  591. a string. Default is the default value, which will either be "#REQUIRED",
  592. "#IMPLIED" or a quoted string (i.e. the returned string will begin and end
  593. with a quote character). If Fixed is true, then this is a fixed attribute.
  594. =item * Doctype (Parser, Name, Sysid, Pubid, Internal)
  595. This handler is called for DOCTYPE declarations. Name is the document type
  596. name. Sysid is the system id of the document type, if it was provided,
  597. otherwise it's undefined. Pubid is the public id of the document type,
  598. which will be undefined if no public id was given. Internal is the internal
  599. subset, given as a string. If there was no internal subset, it will be
  600. undefined. Internal will contain all whitespace, comments, processing
  601. instructions, and declarations seen in the internal subset. The declarations
  602. will be there whether or not they have been processed by another handler
  603. (except for unparsed entities processed by the Unparsed handler). However,
  604. comments and processing instructions will not appear if they've been processed
  605. by their respective handlers.
  606. =item * XMLDecl (Parser, Version, Encoding, Standalone)
  607. This handler is called for xml declarations. Version is a string containg
  608. the version. Encoding is either undefined or contains an encoding string.
  609. Standalone will be either true, false, or undefined if the standalone attribute
  610. is yes, no, or not made respectively.
  611. =back
  612. =item namespace(name)
  613. Return the URI of the namespace that the name belongs to. If the name doesn't
  614. belong to any namespace, an undef is returned. This is only valid on names
  615. received through the Start or End handlers from a single document, or through
  616. a call to the generate_ns_name method. In other words, don't use names
  617. generated from one instance of XML::Parser::Expat with other instances.
  618. =item eq_name(name1, name2)
  619. Return true if name1 and name2 are identical (i.e. same name and from
  620. the same namespace.) This is only meaningful if both names were obtained
  621. through the Start or End handlers from a single document, or through
  622. a call to the generate_ns_name method.
  623. =item generate_ns_name(name, namespace)
  624. Return a name, associated with a given namespace, good for using with the
  625. above 2 methods. The namespace argument should be the namespace URI, not
  626. a prefix.
  627. =item new_ns_prefixes
  628. When called from a start tag handler, returns namespace prefixes declared
  629. with this start tag. If called elsewere (or if there were no namespace
  630. prefixes declared), it returns an empty list. Setting of the default
  631. namespace is indicated with '#default' as a prefix.
  632. =item expand_ns_prefix(prefix)
  633. Return the uri to which the given prefix is currently bound. Returns
  634. undef if the prefix isn't currently bound. Use '#default' to find the
  635. current binding of the default namespace (if any).
  636. =item current_ns_prefixes
  637. Return a list of currently bound namespace prefixes. The order of the
  638. the prefixes in the list has no meaning. If the default namespace is
  639. currently bound, '#default' appears in the list.
  640. =item recognized_string
  641. Returns the string from the document that was recognized in order to call
  642. the current handler. For instance, when called from a start handler, it
  643. will give us the the start-tag string. The string is encoded in UTF-8.
  644. =item original_string
  645. Returns the verbatim string from the document that was recognized in
  646. order to call the current handler. The string is in the original document
  647. encoding.
  648. =item default_current
  649. When called from a handler, causes the sequence of characters that generated
  650. the corresponding event to be sent to the default handler (if one is
  651. registered). Use of this method is deprecated in favor the recognized_string
  652. method, which you can use without installing a default handler.
  653. =item xpcroak(message)
  654. Concatenate onto the given message the current line number within the
  655. XML document plus the message implied by ErrorContext. Then croak with
  656. the formed message.
  657. =item xpcarp(message)
  658. Concatenate onto the given message the current line number within the
  659. XML document plus the message implied by ErrorContext. Then carp with
  660. the formed message.
  661. =item current_line
  662. Returns the line number of the current position of the parse.
  663. =item current_column
  664. Returns the column number of the current position of the parse.
  665. =item current_byte
  666. Returns the current position of the parse.
  667. =item base([NEWBASE]);
  668. Returns the current value of the base for resolving relative URIs. If
  669. NEWBASE is supplied, changes the base to that value.
  670. =item context
  671. Returns a list of element names that represent open elements, with the
  672. last one being the innermost. Inside start and end tag handlers, this
  673. will be the tag of the parent element.
  674. =item current_element
  675. Returns the name of the innermost currently opened element. Inside
  676. start or end handlers, returns the parent of the element associated
  677. with those tags.
  678. =item in_element(NAME)
  679. Returns true if NAME is equal to the name of the innermost currently opened
  680. element. If namespace processing is being used and you want to check
  681. against a name that may be in a namespace, then use the generate_ns_name
  682. method to create the NAME argument.
  683. =item within_element(NAME)
  684. Returns the number of times the given name appears in the context list.
  685. If namespace processing is being used and you want to check
  686. against a name that may be in a namespace, then use the generate_ns_name
  687. method to create the NAME argument.
  688. =item depth
  689. Returns the size of the context list.
  690. =item element_index
  691. Returns an integer that is the depth-first visit order of the current
  692. element. This will be zero outside of the root element. For example,
  693. this will return 1 when called from the start handler for the root element
  694. start tag.
  695. =item skip_until(INDEX)
  696. INDEX is an integer that represents an element index. When this method
  697. is called, all handlers are suspended until the start tag for an element
  698. that has an index number equal to INDEX is seen. If a start handler has
  699. been set, then this is the first tag that the start handler will see
  700. after skip_until has been called.
  701. =item position_in_context(LINES)
  702. Returns a string that shows the current parse position. LINES should be
  703. an integer >= 0 that represents the number of lines on either side of the
  704. current parse line to place into the returned string.
  705. =item xml_escape(TEXT [, CHAR [, CHAR ...]])
  706. Returns TEXT with markup characters turned into character entities. Any
  707. additional characters provided as arguments are also turned into character
  708. references where found in TEXT.
  709. =item parse (SOURCE)
  710. The SOURCE parameter should either be a string containing the whole XML
  711. document, or it should be an open IO::Handle. Only a single document
  712. may be parsed for a given instance of XML::Parser::Expat, so this will croak
  713. if it's been called previously for this instance.
  714. =item parsestring(XML_DOC_STRING)
  715. Parses the given string as an XML document. Only a single document may be
  716. parsed for a given instance of XML::Parser::Expat, so this will die if either
  717. parsestring or parsefile has been called for this instance previously.
  718. This method is deprecated in favor of the parse method.
  719. =item parsefile(FILENAME)
  720. Parses the XML document in the given file. Will die if parsestring or
  721. parsefile has been called previously for this instance.
  722. =item is_defaulted(ATTNAME)
  723. NO LONGER WORKS. To find out if an attribute is defaulted please use
  724. the specified_attr method.
  725. =item specified_attr
  726. When the start handler receives lists of attributes and values, the
  727. non-defaulted (i.e. explicitly specified) attributes occur in the list
  728. first. This method returns the number of specified items in the list.
  729. So if this number is equal to the length of the list, there were no
  730. defaulted values. Otherwise the number points to the index of the
  731. first defaulted attribute name.
  732. =item finish
  733. Unsets all handlers (including internal ones that set context), but expat
  734. continues parsing to the end of the document or until it finds an error.
  735. It should finish up a lot faster than with the handlers set.
  736. =item release
  737. There are data structures used by XML::Parser::Expat that have circular
  738. references. This means that these structures will never be garbage
  739. collected unless these references are explicitly broken. Calling this
  740. method breaks those references (and makes the instance unusable.)
  741. Normally, higher level calls handle this for you, but if you are using
  742. XML::Parser::Expat directly, then it's your responsibility to call it.
  743. =back
  744. =head2 XML::Parser::ExpatNB Methods
  745. The class XML::Parser::ExpatNB is a subclass of XML::Parser::Expat used
  746. for non-blocking access to the expat library. It does not support the parse,
  747. parsestring, or parsefile methods, but it does have these additional methods:
  748. =over 4
  749. =item parse_more(DATA)
  750. Feed expat more text to munch on.
  751. =item parse_done
  752. Tell expat that it's gotten the whole document.
  753. =back
  754. =head1 FUNCTIONS
  755. =over 4
  756. =item XML::Parser::Expat::load_encoding(ENCODING)
  757. Load an external encoding. ENCODING is either the name of an encoding or
  758. the name of a file. The basename is converted to lowercase and a '.enc'
  759. extension is appended unless there's one already there. Then, unless
  760. it's an absolute pathname (i.e. begins with '/'), the first file by that
  761. name discovered in the @Encoding_Path path list is used.
  762. The encoding in the file is loaded and kept in the %Encoding_Table
  763. table. Earlier encodings of the same name are replaced.
  764. This function is automaticly called by expat when it encounters an encoding
  765. it doesn't know about. Expat shouldn't call this twice for the same
  766. encoding name. The only reason users should use this function is to
  767. explicitly load an encoding not contained in the @Encoding_Path list.
  768. =back
  769. =head1 AUTHORS
  770. Larry Wall <F<[email protected]>> wrote version 1.0.
  771. Clark Cooper <F<[email protected]>> picked up support, changed the API
  772. for this version (2.x), provided documentation, and added some standard
  773. package features.
  774. =cut