<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># $Id: Reader.pm,v 1.1.2.1 2004/04/20 20:09:48 pajas Exp $
#
# This is free software, you may use it and distribute it under the same terms as
# Perl itself.
#
# Copyright 2001-2003 AxKit.com Ltd., 2002-2006 Christian Glahn, 2006-2009 Petr Pajas
#
#
package XML::LibXML::Reader;

use XML::LibXML;
use Carp;
use strict;
use warnings;

use vars qw ($VERSION);
$VERSION = "2.0108"; # VERSION TEMPLATE: DO NOT CHANGE

use 5.008_000;

BEGIN {
  UNIVERSAL::can('XML::LibXML::Reader','_newForFile') or
      croak("Cannot use XML::LibXML::Reader module - ".
	    "your libxml2 is compiled without reader support!");
}

use base qw(Exporter);
use constant {
    XML_READER_TYPE_NONE =&gt; 0,
    XML_READER_TYPE_ELEMENT =&gt; 1,
    XML_READER_TYPE_ATTRIBUTE =&gt; 2,
    XML_READER_TYPE_TEXT =&gt; 3,
    XML_READER_TYPE_CDATA =&gt; 4,
    XML_READER_TYPE_ENTITY_REFERENCE =&gt; 5,
    XML_READER_TYPE_ENTITY =&gt; 6,
    XML_READER_TYPE_PROCESSING_INSTRUCTION =&gt; 7,
    XML_READER_TYPE_COMMENT =&gt; 8,
    XML_READER_TYPE_DOCUMENT =&gt; 9,
    XML_READER_TYPE_DOCUMENT_TYPE =&gt; 10,
    XML_READER_TYPE_DOCUMENT_FRAGMENT =&gt; 11,
    XML_READER_TYPE_NOTATION =&gt; 12,
    XML_READER_TYPE_WHITESPACE =&gt; 13,
    XML_READER_TYPE_SIGNIFICANT_WHITESPACE =&gt; 14,
    XML_READER_TYPE_END_ELEMENT =&gt; 15,
    XML_READER_TYPE_END_ENTITY =&gt; 16,
    XML_READER_TYPE_XML_DECLARATION =&gt; 17,

    XML_READER_NONE      =&gt; -1,
    XML_READER_START     =&gt;  0,
    XML_READER_ELEMENT   =&gt;  1,
    XML_READER_END       =&gt;  2,
    XML_READER_EMPTY     =&gt;  3,
    XML_READER_BACKTRACK =&gt;  4,
    XML_READER_DONE      =&gt;  5,
    XML_READER_ERROR     =&gt;  6
};
use vars qw( @EXPORT @EXPORT_OK %EXPORT_TAGS );

sub CLONE_SKIP { 1 }

BEGIN {

%EXPORT_TAGS = (
  types =&gt;
  [qw(
    XML_READER_TYPE_NONE
    XML_READER_TYPE_ELEMENT
    XML_READER_TYPE_ATTRIBUTE
    XML_READER_TYPE_TEXT
    XML_READER_TYPE_CDATA
    XML_READER_TYPE_ENTITY_REFERENCE
    XML_READER_TYPE_ENTITY
    XML_READER_TYPE_PROCESSING_INSTRUCTION
    XML_READER_TYPE_COMMENT
    XML_READER_TYPE_DOCUMENT
    XML_READER_TYPE_DOCUMENT_TYPE
    XML_READER_TYPE_DOCUMENT_FRAGMENT
    XML_READER_TYPE_NOTATION
    XML_READER_TYPE_WHITESPACE
    XML_READER_TYPE_SIGNIFICANT_WHITESPACE
    XML_READER_TYPE_END_ELEMENT
    XML_READER_TYPE_END_ENTITY
    XML_READER_TYPE_XML_DECLARATION
    )],
  states =&gt;
  [qw(
    XML_READER_NONE
    XML_READER_START
    XML_READER_ELEMENT
    XML_READER_END
    XML_READER_EMPTY
    XML_READER_BACKTRACK
    XML_READER_DONE
    XML_READER_ERROR
   )]
);
@EXPORT    = (@{$EXPORT_TAGS{types}},@{$EXPORT_TAGS{states}});
@EXPORT_OK = @EXPORT;
$EXPORT_TAGS{all}=\@EXPORT_OK;
}

our %_preserve_flag;

{
  my %props = (
    load_ext_dtd =&gt; 1,		 # load the external subset
    complete_attributes =&gt; 2,	 # default DTD attributes
    validation =&gt; 3,		 # validate with the DTD
    expand_entities =&gt; 4,	 # substitute entities
  );
  sub getParserProp {
    my ($self, $name) = @_;
    my $prop = $props{$name};
    return undef unless defined $prop;
    return $self-&gt;_getParserProp($prop);
  }
  sub setParserProp {
    my $self = shift;
    my %args = map { ref($_) eq 'HASH' ? (%$_) : $_ } @_;
    my ($key, $value);
    while (($key,$value) = each %args) {
      my $prop = $props{ $key };
      $self-&gt;_setParserProp($prop,$value);
    }
    return;
  }

  my (%string_pool,%rng_pool,%xsd_pool); # used to preserve data passed to the reader
  sub new {
    my ($class) = shift;
    my %args = map { ref($_) eq 'HASH' ? (%$_) : $_ } @_;
    my $encoding = $args{encoding};
    my $URI = $args{URI};
    $URI="$URI" if defined $URI; # stringify in case it is an URI object
    my $options = XML::LibXML-&gt;_parser_options(\%args);

    my $self = undef;
    if ( defined $args{location} ) {
      $self = $class-&gt;_newForFile( $args{location}, $encoding, $options );
    }
    elsif ( defined $args{string} ) {
      $self = $class-&gt;_newForString( $args{string}, $URI, $encoding, $options );
      $string_pool{$self} = \$args{string};
    }
    elsif ( defined $args{IO} ) {
      $self = $class-&gt;_newForIO( $args{IO}, $URI, $encoding, $options  );
    }
    elsif ( defined $args{DOM} ) {
      croak("DOM must be a XML::LibXML::Document node")
	unless UNIVERSAL::isa($args{DOM}, 'XML::LibXML::Document');
      $self = $class-&gt;_newForDOM( $args{DOM} );
    }
    elsif ( defined $args{FD} ) {
      my $fd = fileno($args{FD});
      $self = $class-&gt;_newForFd( $fd, $URI, $encoding, $options  );
    }
    else {
      croak("XML::LibXML::Reader-&gt;new: specify location, string, IO, DOM, or FD");
    }
    if ($args{RelaxNG}) {
      if (ref($args{RelaxNG})) {
	$rng_pool{$self} = \$args{RelaxNG};
	$self-&gt;_setRelaxNG($args{RelaxNG});
      } else {
	$self-&gt;_setRelaxNGFile($args{RelaxNG});
      }
    }
    if ($args{Schema}) {
      if (ref($args{Schema})) {
	$xsd_pool{$self} = \$args{Schema};
	$self-&gt;_setXSD($args{Schema});
      } else {
	$self-&gt;_setXSDFile($args{Schema});
      }
    }
    return $self;
  }
  sub DESTROY {
    my $self = shift;
    delete $string_pool{$self};
    delete $rng_pool{$self};
    delete $xsd_pool{$self};
    $self-&gt;_DESTROY;
  }
}
sub close {
    my ($reader) = @_;
    # _close return -1 on failure, 0 on success
    # perl close returns 0 on failure, 1 on success
    return $reader-&gt;_close == 0 ? 1 : 0;
}

sub preservePattern {
  my $reader=shift;
  my ($pattern,$ns_map)=@_;
  if (ref($ns_map) eq 'HASH') {
    # translate prefix=&gt;URL hash to a (URL,prefix) list
    $reader-&gt;_preservePattern($pattern,[reverse %$ns_map]);
  } else {
    $reader-&gt;_preservePattern(@_);
  }
}

sub nodePath {
  my $reader=shift;
  my $path = $reader-&gt;_nodePath;
  $path=~s/\[\d+\]//g; # make /foo[1]/bar[1] just /foo/bar, since
                       # sibling count in the buffered fragment is
                       # basically random and generally misleading
  return $path;
}

1;
__END__
</pre></body></html>