1 # $Id: Reader.pm,v 1.11 2005/10/14 20:31:20 matt Exp $ |
|
2 |
|
3 package XML::SAX::PurePerl::Reader; |
|
4 |
|
5 use strict; |
|
6 use XML::SAX::PurePerl::Reader::URI; |
|
7 use XML::SAX::PurePerl::Productions qw( $SingleChar $Letter $NameChar ); |
|
8 use Exporter (); |
|
9 |
|
10 use vars qw(@ISA @EXPORT_OK); |
|
11 @ISA = qw(Exporter); |
|
12 @EXPORT_OK = qw( |
|
13 EOF |
|
14 BUFFER |
|
15 LINE |
|
16 COLUMN |
|
17 ENCODING |
|
18 XML_VERSION |
|
19 ); |
|
20 |
|
21 use constant EOF => 0; |
|
22 use constant BUFFER => 1; |
|
23 use constant LINE => 2; |
|
24 use constant COLUMN => 3; |
|
25 use constant ENCODING => 4; |
|
26 use constant SYSTEM_ID => 5; |
|
27 use constant PUBLIC_ID => 6; |
|
28 use constant XML_VERSION => 7; |
|
29 |
|
30 require XML::SAX::PurePerl::Reader::Stream; |
|
31 require XML::SAX::PurePerl::Reader::String; |
|
32 |
|
33 if ($] >= 5.007002) { |
|
34 require XML::SAX::PurePerl::Reader::UnicodeExt; |
|
35 } |
|
36 else { |
|
37 require XML::SAX::PurePerl::Reader::NoUnicodeExt; |
|
38 } |
|
39 |
|
40 sub new { |
|
41 my $class = shift; |
|
42 my $thing = shift; |
|
43 |
|
44 # try to figure if this $thing is a handle of some sort |
|
45 if (ref($thing) && UNIVERSAL::isa($thing, 'IO::Handle')) { |
|
46 return XML::SAX::PurePerl::Reader::Stream->new($thing)->init; |
|
47 } |
|
48 my $ioref; |
|
49 if (tied($thing)) { |
|
50 my $class = ref($thing); |
|
51 no strict 'refs'; |
|
52 $ioref = $thing if defined &{"${class}::TIEHANDLE"}; |
|
53 } |
|
54 else { |
|
55 eval { |
|
56 $ioref = *{$thing}{IO}; |
|
57 }; |
|
58 undef $@; |
|
59 } |
|
60 if ($ioref) { |
|
61 return XML::SAX::PurePerl::Reader::Stream->new($thing)->init; |
|
62 } |
|
63 |
|
64 if ($thing =~ /</) { |
|
65 # assume it's a string |
|
66 return XML::SAX::PurePerl::Reader::String->new($thing)->init; |
|
67 } |
|
68 |
|
69 # assume it is a uri |
|
70 return XML::SAX::PurePerl::Reader::URI->new($thing)->init; |
|
71 } |
|
72 |
|
73 sub init { |
|
74 my $self = shift; |
|
75 $self->[LINE] = 1; |
|
76 $self->[COLUMN] = 1; |
|
77 $self->read_more; |
|
78 return $self; |
|
79 } |
|
80 |
|
81 sub data { |
|
82 my ($self, $min_length) = (@_, 1); |
|
83 if (length($self->[BUFFER]) < $min_length) { |
|
84 $self->read_more; |
|
85 } |
|
86 return $self->[BUFFER]; |
|
87 } |
|
88 |
|
89 sub match { |
|
90 my ($self, $char) = @_; |
|
91 my $data = $self->data; |
|
92 if (substr($data, 0, 1) eq $char) { |
|
93 $self->move_along(1); |
|
94 return 1; |
|
95 } |
|
96 return 0; |
|
97 } |
|
98 |
|
99 sub public_id { |
|
100 my $self = shift; |
|
101 @_ and $self->[PUBLIC_ID] = shift; |
|
102 $self->[PUBLIC_ID]; |
|
103 } |
|
104 |
|
105 sub system_id { |
|
106 my $self = shift; |
|
107 @_ and $self->[SYSTEM_ID] = shift; |
|
108 $self->[SYSTEM_ID]; |
|
109 } |
|
110 |
|
111 sub line { |
|
112 shift->[LINE]; |
|
113 } |
|
114 |
|
115 sub column { |
|
116 shift->[COLUMN]; |
|
117 } |
|
118 |
|
119 sub get_encoding { |
|
120 my $self = shift; |
|
121 return $self->[ENCODING]; |
|
122 } |
|
123 |
|
124 sub get_xml_version { |
|
125 my $self = shift; |
|
126 return $self->[XML_VERSION]; |
|
127 } |
|
128 |
|
129 1; |
|
130 |
|
131 __END__ |
|
132 |
|
133 =head1 NAME |
|
134 |
|
135 XML::Parser::PurePerl::Reader - Abstract Reader factory class |
|
136 |
|
137 =cut |
|