407
|
1 package IMPL::DOM::XMLReader;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
411
|
5 use XML::Parser;
|
407
|
6
|
411
|
7 use IMPL::declare {
|
|
8 require => {
|
|
9 Schema => 'IMPL::DOM::Schema', # IMPL::DOM::Schema references IMPL::DOM::XML::Reader
|
|
10 Builder => 'IMPL::DOM::Navigator::Builder',
|
|
11 SimpleBuilder => 'IMPL::DOM::Navigator::SimpleBuilder'
|
|
12 },
|
|
13 base => [
|
|
14 'IMPL::Object' => undef
|
|
15 ],
|
|
16 props => [
|
|
17 Navigator => '*r',
|
|
18 SkipWhitespace => '*r',
|
|
19 _current => '*rw',
|
|
20 _text => '*rw',
|
|
21 _textHistory => '*rw'
|
|
22 ]
|
|
23 };
|
407
|
24
|
|
25 use IMPL::require {
|
411
|
26
|
407
|
27 };
|
|
28
|
411
|
29 sub CTOR {
|
|
30 my ($this, %params) = @_;
|
|
31
|
|
32 $this->{$Navigator} = $params{Navigator} if $params{Navigator};
|
|
33 $this->{$SkipWhitespace} = $params{SkipWhitespace} if $params{SkipWhitespace};
|
407
|
34 }
|
|
35
|
|
36 sub Parse {
|
|
37 my ($this,$in) = @_;
|
|
38
|
|
39 my $parser = new XML::Parser(
|
|
40 Handlers => {
|
|
41 Start => sub {shift; goto &OnStart($this,@_)},
|
|
42 End => sub {shift; goto &OnEnd($this,@_)},
|
|
43 Char => sub {shift; goto &OnChar($this,@_)}
|
|
44 }
|
|
45 );
|
|
46
|
|
47 $parser->parse($in);
|
|
48 }
|
|
49
|
|
50 sub ParseFile {
|
|
51 my ($this,$in) = @_;
|
|
52
|
|
53 my $parser = new XML::Parser(
|
|
54 Handlers => {
|
|
55 Start => sub {shift; unshift @_, $this; goto &_OnBegin;},
|
|
56 End => sub {shift; unshift @_, $this; goto &_OnEnd;},
|
|
57 Char => sub {shift; unshift @_, $this; goto &_OnChar;}
|
|
58 }
|
|
59 );
|
|
60
|
|
61 $parser->parsefile($in);
|
|
62 }
|
|
63
|
|
64 sub _OnBegin {
|
|
65 my ($this,$element,%attrs) = @_;
|
|
66
|
|
67 push @{$this->{$_textHistory}},$this->{$_text};
|
|
68 $this->{$_text} = "";
|
|
69 $this->{$_current} = $this->Navigator->NavigateCreate($element,%attrs);
|
|
70 }
|
|
71
|
|
72 sub _OnEnd {
|
|
73 my ($this,$element) = @_;
|
|
74 $this->{$_current}->nodeValue($this->{$_text}) if length $this->{$_text} and (not $this->{$SkipWhitespace} or $this->{$_text} =~ /\S/);
|
|
75 $this->{$_text} = pop @{$this->{$_textHistory}};
|
|
76 $this->{$_current} = $this->Navigator->Back;
|
|
77 }
|
|
78
|
|
79 sub _OnChar {
|
|
80 my ($this,$val) = @_;
|
|
81 $this->{$_text} .= $val;
|
|
82 }
|
|
83
|
|
84 sub LoadDocument {
|
|
85 my ($self,$file,$schema) = @_;
|
|
86
|
|
87 my $parser;
|
|
88 if ($schema) {
|
|
89 $schema = IMPL::DOM::Schema->LoadSchema($schema) if not ref $schema;
|
|
90 $parser = $self->new(
|
|
91 Navigator => IMPL::DOM::Navigator::Builder->new(
|
|
92 'IMPL::DOM::Document',
|
|
93 $schema
|
|
94 )
|
|
95 );
|
|
96 } else {
|
|
97 $parser = $self->new(
|
|
98 Navigator => IMPL::DOM::Navigator::SimpleBuilder->new()
|
|
99 );
|
|
100 }
|
|
101
|
|
102 $parser->ParseFile($file);
|
|
103 my $doc = $parser->Navigator->Document;
|
|
104 my @errors;
|
|
105 if ($schema) {
|
|
106 push @errors, $schema->Validate($doc);
|
|
107 }
|
|
108
|
|
109 if (wantarray) {
|
|
110 return $doc,\@errors;
|
|
111 } else {
|
|
112 die new IMPL::Exception("Loaded document doesn't match the schema", @errors) if @errors;
|
|
113 return $doc;
|
|
114 }
|
|
115 }
|
|
116
|
|
117 1;
|
|
118
|
|
119 __END__
|
|
120
|
|
121 =pod
|
|
122
|
|
123 =head1 SYNOPSIS
|
|
124
|
|
125 my $reader = new IMPL::DOM::XMLReader(Navigator => $DomBuilder);
|
|
126 my $obj = $reader->parsefile("data.xml");
|
|
127
|
|
128 =head1 DESCRIPTION
|
|
129
|
|
130 Простой класс, использующий навигатор для постороения документа. В зависимости от
|
|
131 используемого навигатора может быть получен различный результат.
|
|
132
|
|
133 Навигатор должен поодерживать методы C<NavigateCreate> и C<Back>
|
|
134
|
|
135 =head1 METHODS
|
|
136
|
|
137 =over
|
|
138
|
|
139 =item C<CTOR(Naviagtor => $builder)>
|
|
140
|
|
141 Создает новый экземпляр парсера, с указанным навигатором для построения документа
|
|
142
|
|
143 =item C<$obj->Parse($in)>
|
|
144
|
|
145 Строит документ. На вход получает либо xml строку, либо HANDLE.
|
|
146
|
|
147 =item C<$obj->ParseFile($fileName)>
|
|
148
|
|
149 Строит документ из файла с именем C<$fileName>.
|
|
150
|
|
151 =back
|
|
152
|
|
153 =cut
|