annotate lib/IMPL/DOM/XMLReader.pm @ 416:cc2cf8c0edc2 ref20150831

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