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