annotate Lib/IMPL/DOM/XMLReader.pm @ 125:a4b0a819bbda

Small fixes in IMPL::DOM::Schema
author wizard
date Thu, 10 Jun 2010 17:43:51 +0400
parents 7b14e0122b79
children 1e7f03414b65
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
5 use base qw(IMPL::Object IMPL::Object::Autofill);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
6 use IMPL::Class::Property;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
7 use IMPL::Class::Property::Direct;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
8 use XML::Parser;
104
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
9 require IMPL::DOM::Schema;
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
10 require IMPL::DOM::Navigator::Builder;
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
11 require IMPL::DOM::Navigator::SimpleBuilder;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
12
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
13 __PACKAGE__->PassThroughArgs;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
14
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
15 BEGIN {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
16 public _direct property Navigator => prop_get | owner_set;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
17 private _direct property _current => prop_all;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
18 private _direct property _text => prop_all;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
19 private _direct property _textHistory => prop_all;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
20 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
21
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
22 sub Parse {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
23 my ($this,$in) = @_;
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 my $parser = new XML::Parser(
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
26 Handlers => {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
27 Start => sub {shift; goto &OnStart($this,@_)},
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
28 End => sub {shift; goto &OnEnd($this,@_)},
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
29 Char => sub {shift; goto &OnChar($this,@_)}
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
30 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
31 );
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
32
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
33 $parser->parse($in);
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 sub ParseFile {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
37 my ($this,$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 my $parser = new XML::Parser(
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
40 Handlers => {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
41 Start => sub {shift; unshift @_, $this; goto &_OnBegin;},
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
42 End => sub {shift; unshift @_, $this; goto &_OnEnd;},
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
43 Char => sub {shift; unshift @_, $this; goto &_OnChar;}
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
44 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
45 );
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
46
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
47 $parser->parsefile($in);
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 sub _OnBegin {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
51 my ($this,$element,%attrs) = @_;
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 push @{$this->{$_textHistory}},$this->{$_text};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
54 $this->{$_text} = "";
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
55 $this->{$_current} = $this->Navigator->NavigateCreate($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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
58 sub _OnEnd {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
59 my ($this,$element) = @_;
113
7b14e0122b79 Updated PostToDOM transformation
wizard
parents: 104
diff changeset
60 $this->{$_current}->nodeValue($this->Navigator->inflateValue( $this->{$_text} ) ) if length $this->{$_text};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
61 $this->{$_text} = pop @{$this->{$_textHistory}};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
62 $this->{$_current} = $this->Navigator->Back;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
63 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
64
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
65 sub _OnChar {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
66 my ($this,$val) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
67 $this->{$_text} .= $val;
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
104
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
70 sub LoadDocument {
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
71 my ($self,$file,$schema) = @_;
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
72
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
73 my $parser;
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
74 if ($schema) {
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
75 $schema = IMPL::DOM::Schema->LoadSchema($schema) if not ref $schema;
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
76 $parser = $self->new(
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
77 Navigator => IMPL::DOM::Navigator::Builder->new(
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
78 'IMPL::DOM::Document',
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
79 $schema
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
80 )
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
81 );
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
82 } else {
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
83 $parser = $self->new(
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
84 Navigator => IMPL::DOM::Navigator::SimpleBuilder->new()
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
85 );
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
86 }
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
87
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
88 $parser->ParseFile($file);
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
89 my $doc = $parser->Navigator->Document;
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
90 if ($schema) {
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
91 my @errors = $parser->Navigator->BuildErrors;
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
92 push @errors, $schema->Validate($doc);
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
93 die new IMPL::Exception("Loaded document doesn't match the schema", @errors) if @errors;
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
94 }
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
95 return $doc;
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
96 }
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
97
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
98 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
99
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
100 __END__
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
101
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
102 =pod
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 =head1 SYNOPSIS
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 my $reader = new IMPL::DOM::XMLReader(Navigator => $DomBuilder);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
107 my $obj = $reader->parsefile("data.xml");
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 DESCRIPTION
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 Простой класс, использующий навигатор для постороения документа. В зависимости от
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
114 Навигатор должен поодерживать методы C<NavigateCreate> и C<Back>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
115
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
116 =head1 METHODS
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
117
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
118 =over
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 =item C<CTOR(Naviagtor => $builder)>
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 Создает новый экземпляр парсера, с указанным навигатором для построения документа
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<$obj->Parse($in)>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
125
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
126 Строит документ. На вход получает либо xml строку, либо HANDLE.
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->ParseFile($fileName)>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
129
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
130 Строит документ из файла с именем C<$fileName>.
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 =back
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
133
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
134 =cut