annotate Lib/IMPL/DOM/XMLReader.pm @ 263:0f59b2de72af

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