annotate Lib/IMPL/DOM/XMLReader.pm @ 358:248f95c1762a

added simple XML Sax parser
author sergey
date Mon, 18 Nov 2013 01:25:35 +0400
parents 4ddb27ff4a0b
children 010ceafd0c5a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
1 package IMPL::DOM::XMLReader;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
2 use strict;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
3 use warnings;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
4
166
4267a2ac3d46 Added Class::Template,
wizard
parents: 152
diff changeset
5 use parent qw(IMPL::Object IMPL::Object::Autofill);
263
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
6
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
7 use IMPL::Class::Property;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
8 use XML::Parser;
263
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
9
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
10 use IMPL::require {
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
11 Schema => 'IMPL::DOM::Schema', # IMPL::DOM::Schema references IMPL::DOM::XML::Reader
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
12 Builder => 'IMPL::DOM::Navigator::Builder',
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
13 SimpleBuilder => 'IMPL::DOM::Navigator::SimpleBuilder'
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
14 };
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
15
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
16 __PACKAGE__->PassThroughArgs;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
17
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
18 BEGIN {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
19 public _direct property Navigator => prop_get | owner_set;
152
1e7f03414b65 DOM: schema improvements
wizard
parents: 113
diff changeset
20 public _direct property SkipWhitespace => prop_get | owner_set;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
21 private _direct property _current => prop_all;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
22 private _direct property _text => prop_all;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
23 private _direct property _textHistory => prop_all;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
24 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
25
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
26 sub Parse {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
27 my ($this,$in) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
28
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
29 my $parser = new XML::Parser(
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
30 Handlers => {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
31 Start => sub {shift; goto &OnStart($this,@_)},
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
32 End => sub {shift; goto &OnEnd($this,@_)},
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
33 Char => sub {shift; goto &OnChar($this,@_)}
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
34 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
35 );
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
36
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
37 $parser->parse($in);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
38 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
39
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
40 sub ParseFile {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
41 my ($this,$in) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
42
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
43 my $parser = new XML::Parser(
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
44 Handlers => {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
45 Start => sub {shift; unshift @_, $this; goto &_OnBegin;},
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
46 End => sub {shift; unshift @_, $this; goto &_OnEnd;},
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
47 Char => sub {shift; unshift @_, $this; goto &_OnChar;}
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
48 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
49 );
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
50
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
51 $parser->parsefile($in);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
52 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
53
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
54 sub _OnBegin {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
55 my ($this,$element,%attrs) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
56
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
57 push @{$this->{$_textHistory}},$this->{$_text};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
58 $this->{$_text} = "";
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
59 $this->{$_current} = $this->Navigator->NavigateCreate($element,%attrs);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
60 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
61
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
62 sub _OnEnd {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
63 my ($this,$element) = @_;
152
1e7f03414b65 DOM: schema improvements
wizard
parents: 113
diff changeset
64 $this->{$_current}->nodeValue($this->Navigator->inflateValue( $this->{$_text} ) ) if length $this->{$_text} and (not $this->{$SkipWhitespace} or $this->{$_text} =~ /\S/);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
65 $this->{$_text} = pop @{$this->{$_textHistory}};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
66 $this->{$_current} = $this->Navigator->Back;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
67 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
68
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
69 sub _OnChar {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
70 my ($this,$val) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
71 $this->{$_text} .= $val;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
72 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
73
104
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
74 sub LoadDocument {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
75 my ($self,$file,$schema) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
76
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
77 my $parser;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
78 if ($schema) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
79 $schema = IMPL::DOM::Schema->LoadSchema($schema) if not ref $schema;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
80 $parser = $self->new(
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
81 Navigator => IMPL::DOM::Navigator::Builder->new(
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
82 'IMPL::DOM::Document',
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
83 $schema
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
84 )
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
85 );
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
86 } else {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
87 $parser = $self->new(
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
88 Navigator => IMPL::DOM::Navigator::SimpleBuilder->new()
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
89 );
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
90 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
91
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
92 $parser->ParseFile($file);
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
93 my $doc = $parser->Navigator->Document;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
94 if ($schema) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
95 my @errors = $parser->Navigator->BuildErrors;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
96 push @errors, $schema->Validate($doc);
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
97 die new IMPL::Exception("Loaded document doesn't match the schema", @errors) if @errors;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
98 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
99 return $doc;
104
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
100 }
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
101
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
102 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
103
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
104 __END__
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
105
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
106 =pod
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
107
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
108 =head1 SYNOPSIS
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
109
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
110 my $reader = new IMPL::DOM::XMLReader(Navigator => $DomBuilder);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
111 my $obj = $reader->parsefile("data.xml");
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
112
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
113 =head1 DESCRIPTION
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
114
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
115 Простой класс, использующий навигатор для постороения документа. В зависимости от
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
116 используемого навигатора может быть получен различный результат.
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
117
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
118 Навигатор должен поодерживать методы C<NavigateCreate> и C<Back>
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
119
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
120 =head1 METHODS
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
121
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
122 =over
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
123
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
124 =item C<CTOR(Naviagtor => $builder)>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
125
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
126 Создает новый экземпляр парсера, с указанным навигатором для построения документа
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
127
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
128 =item C<$obj->Parse($in)>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
129
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
130 Строит документ. На вход получает либо xml строку, либо HANDLE.
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
131
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
132 =item C<$obj->ParseFile($fileName)>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
133
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
134 Строит документ из файла с именем C<$fileName>.
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
135
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
136 =back
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
137
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
138 =cut