annotate Lib/IMPL/DOM/XMLReader.pm @ 152:1e7f03414b65

DOM: schema improvements DOM: saving to XML::Writer feature
author wizard
date Thu, 23 Sep 2010 03:58:43 +0400
parents 7b14e0122b79
children 4267a2ac3d46
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;
152
1e7f03414b65 DOM: schema improvements
wizard
parents: 113
diff changeset
17 public _direct property SkipWhitespace => prop_get | owner_set;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
18 private _direct property _current => prop_all;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
19 private _direct property _text => prop_all;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
20 private _direct property _textHistory => prop_all;
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
23 sub Parse {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
24 my ($this,$in) = @_;
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 my $parser = new XML::Parser(
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
27 Handlers => {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
28 Start => sub {shift; goto &OnStart($this,@_)},
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
29 End => sub {shift; goto &OnEnd($this,@_)},
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
30 Char => sub {shift; goto &OnChar($this,@_)}
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
34 $parser->parse($in);
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 sub ParseFile {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
38 my ($this,$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 my $parser = new XML::Parser(
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
41 Handlers => {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
42 Start => sub {shift; unshift @_, $this; goto &_OnBegin;},
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
43 End => sub {shift; unshift @_, $this; goto &_OnEnd;},
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
44 Char => sub {shift; unshift @_, $this; goto &_OnChar;}
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
48 $parser->parsefile($in);
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 sub _OnBegin {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
52 my ($this,$element,%attrs) = @_;
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 push @{$this->{$_textHistory}},$this->{$_text};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
55 $this->{$_text} = "";
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
56 $this->{$_current} = $this->Navigator->NavigateCreate($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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
59 sub _OnEnd {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
60 my ($this,$element) = @_;
152
1e7f03414b65 DOM: schema improvements
wizard
parents: 113
diff changeset
61 $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
62 $this->{$_text} = pop @{$this->{$_textHistory}};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
63 $this->{$_current} = $this->Navigator->Back;
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
66 sub _OnChar {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
67 my ($this,$val) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
68 $this->{$_text} .= $val;
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
104
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
71 sub LoadDocument {
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
72 my ($self,$file,$schema) = @_;
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
73
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
74 my $parser;
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
75 if ($schema) {
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
76 $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
77 $parser = $self->new(
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
78 Navigator => IMPL::DOM::Navigator::Builder->new(
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
79 'IMPL::DOM::Document',
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
80 $schema
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 );
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
83 } else {
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
84 $parser = $self->new(
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
85 Navigator => IMPL::DOM::Navigator::SimpleBuilder->new()
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
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
89 $parser->ParseFile($file);
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
90 my $doc = $parser->Navigator->Document;
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
91 if ($schema) {
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
92 my @errors = $parser->Navigator->BuildErrors;
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
93 push @errors, $schema->Validate($doc);
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
94 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
95 }
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
96 return $doc;
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
97 }
196bf443b5e1 DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents: 103
diff changeset
98
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
99 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
100
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
101 __END__
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
102
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
103 =pod
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 =head1 SYNOPSIS
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 my $reader = new IMPL::DOM::XMLReader(Navigator => $DomBuilder);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
108 my $obj = $reader->parsefile("data.xml");
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 =head1 DESCRIPTION
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
115 Навигатор должен поодерживать методы C<NavigateCreate> и C<Back>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
116
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
117 =head1 METHODS
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
118
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
119 =over
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 =item C<CTOR(Naviagtor => $builder)>
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
125 =item C<$obj->Parse($in)>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
126
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
127 Строит документ. На вход получает либо xml строку, либо HANDLE.
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->ParseFile($fileName)>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
130
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
131 Строит документ из файла с именем C<$fileName>.
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 =back
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
134
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 37
diff changeset
135 =cut