Mercurial > pub > Impl
annotate Lib/IMPL/DOM/Schema.pm @ 214:4683002758aa
sync
author | sergey |
---|---|
date | Mon, 06 Aug 2012 17:27:47 +0400 |
parents | 4d0e1962161c |
children | 6d8092d8ce1b |
rev | line source |
---|---|
49 | 1 package IMPL::DOM::Schema; |
2 use strict; | |
3 use warnings; | |
4 | |
5 require IMPL::DOM::Schema::ComplexNode; | |
6 require IMPL::DOM::Schema::ComplexType; | |
7 require IMPL::DOM::Schema::SimpleNode; | |
8 require IMPL::DOM::Schema::SimpleType; | |
9 require IMPL::DOM::Schema::Node; | |
10 require IMPL::DOM::Schema::AnyNode; | |
11 require IMPL::DOM::Schema::NodeList; | |
12 require IMPL::DOM::Schema::NodeSet; | |
13 require IMPL::DOM::Schema::Property; | |
14 require IMPL::DOM::Schema::SwitchNode; | |
101 | 15 require IMPL::DOM::Schema::Validator; |
16 require IMPL::DOM::Navigator::Builder; | |
17 require IMPL::DOM::XMLReader; | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
18 require IMPL::DOM::Schema::InflateFactory; |
49 | 19 |
166 | 20 use parent qw(IMPL::DOM::Document); |
49 | 21 use IMPL::Class::Property; |
22 use IMPL::Class::Property::Direct; | |
101 | 23 use File::Spec; |
49 | 24 |
25 our %CTOR = ( | |
26 'IMPL::DOM::Document' => sub { nodeName => 'schema' } | |
27 ); | |
28 | |
29 BEGIN { | |
30 private _direct property _TypesMap => prop_all; | |
101 | 31 public _direct property baseDir => prop_all; |
49 | 32 public _direct property BaseSchemas => prop_get | owner_set; |
33 } | |
34 | |
35 sub resolveType { | |
36 $_[0]->{$_TypesMap}->{$_[1]}; | |
37 } | |
38 | |
102 | 39 sub CTOR { |
194 | 40 my ($this,%args) = @_; |
41 | |
42 $this->{$baseDir} = ($args{baseDir} || '.'); | |
102 | 43 } |
44 | |
49 | 45 sub Create { |
46 my ($this,$nodeName,$class,$refArgs) = @_; | |
47 | |
100 | 48 die new IMPL::Exception('Invalid node class') unless $class->isa('IMPL::DOM::Node'); |
49 | 49 |
101 | 50 if ($class->isa('IMPL::DOM::Schema::Validator')) { |
194 | 51 $class = "IMPL::DOM::Schema::Validator::$nodeName"; |
52 unless (eval {$class->can('new')}) { | |
53 eval "require $class; 1;"; | |
54 my $e = $@; | |
55 die new IMPL::Exception("Invalid validator",$class,$e) if $e; | |
56 } | |
101 | 57 } |
58 | |
59 return $this->SUPER::Create($nodeName,$class,$refArgs); | |
49 | 60 } |
61 | |
62 sub Process { | |
63 my ($this) = @_; | |
64 | |
101 | 65 # process instructions |
66 $this->Include($_) foreach map $_->nodeProperty('source'), $this->selectNodes('Include'); | |
67 | |
68 # build types map | |
49 | 69 $this->{$_TypesMap} = { map { $_->type, $_ } $this->selectNodes(sub { $_[0]->nodeName eq 'ComplexType' || $_[0]->nodeName eq 'SimpleType' } ) }; |
70 } | |
71 | |
101 | 72 sub Include { |
194 | 73 my ($this,$file) = @_; |
74 | |
75 my $schema = $this->LoadSchema(File::Spec->catfile($this->baseDir, $file)); | |
76 | |
77 $this->appendRange( $schema->childNodes ); | |
101 | 78 } |
79 | |
80 sub LoadSchema { | |
194 | 81 my ($this,$file) = @_; |
82 | |
83 $file = File::Spec->rel2abs($file); | |
84 | |
85 my $class = ref $this || $this; | |
86 | |
87 my $reader = new IMPL::DOM::XMLReader( | |
88 Navigator => new IMPL::DOM::Navigator::Builder( | |
89 $class, | |
90 $class->MetaSchema | |
91 ), | |
92 SkipWhitespace => 1 | |
93 ); | |
94 | |
95 $reader->ParseFile($file); | |
96 | |
97 my $schema = $reader->Navigator->Document; | |
98 | |
99 my ($vol,$dir) = File::Spec->splitpath($file); | |
100 | |
101 $schema->baseDir($dir); | |
102 | |
103 my @errors = $class->MetaSchema->Validate($schema); | |
104 | |
105 die new IMPL::Exception("Schema is invalid",$file,map( $_->Message, @errors ) ) if @errors; | |
106 | |
107 $schema->Process; | |
108 | |
109 return $schema; | |
101 | 110 } |
111 | |
49 | 112 sub Validate { |
113 my ($this,$node) = @_; | |
114 | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
115 if ( my ($schemaNode) = $this->selectNodes(sub { $_->isa('IMPL::DOM::Schema::Node') and $_[0]->name eq $node->nodeName })) { |
49 | 116 $schemaNode->Validate($node); |
117 } else { | |
126 | 118 return new IMPL::DOM::Schema::ValidationError(Node => $node, Message=> "A specified document (%Node.nodeName%) doesn't match the schema"); |
49 | 119 } |
120 } | |
121 | |
122 my $schema; | |
123 | |
124 sub MetaSchema { | |
125 | |
126 return $schema if $schema; | |
127 | |
128 $schema = new IMPL::DOM::Schema; | |
129 | |
130 $schema->appendRange( | |
131 IMPL::DOM::Schema::ComplexNode->new(name => 'schema')->appendRange( | |
132 IMPL::DOM::Schema::NodeSet->new()->appendRange( | |
133 IMPL::DOM::Schema::Node->new(name => 'ComplexNode', type => 'ComplexNode', minOccur => 0, maxOccur=>'unbounded'), | |
134 IMPL::DOM::Schema::Node->new(name => 'ComplexType', type => 'ComplexType', minOccur => 0, maxOccur=>'unbounded'), | |
135 IMPL::DOM::Schema::Node->new(name => 'SimpleNode', type => 'SimpleNode', minOccur => 0, maxOccur=>'unbounded'), | |
136 IMPL::DOM::Schema::Node->new(name => 'SimpleType', type => 'SimpleType', minOccur => 0, maxOccur=>'unbounded'), | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
137 IMPL::DOM::Schema::Node->new(name => 'Node', type => 'Node', minOccur => 0, maxOccur=>'unbounded'), |
49 | 138 IMPL::DOM::Schema::SimpleNode->new(name => 'Include', minOccur => 0, maxOccur=>'unbounded')->appendRange( |
139 IMPL::DOM::Schema::Property->new(name => 'source') | |
140 ) | |
141 ), | |
142 ), | |
143 IMPL::DOM::Schema::ComplexType->new(type => 'NodeSet', nativeType => 'IMPL::DOM::Schema::NodeSet')->appendRange( | |
144 IMPL::DOM::Schema::NodeSet->new()->appendRange( | |
145 IMPL::DOM::Schema::Node->new(name => 'ComplexNode', type => 'ComplexNode', minOccur => 0, maxOccur=>'unbounded'), | |
146 IMPL::DOM::Schema::Node->new(name => 'SimpleNode', type => 'SimpleNode', minOccur => 0, maxOccur=>'unbounded'), | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
147 IMPL::DOM::Schema::Node->new(name => 'Node', type=>'Node', minOccur => 0, maxOccur=>'unbounded'), |
49 | 148 IMPL::DOM::Schema::SwitchNode->new(minOccur => 0, maxOccur => 1)->appendRange( |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
149 IMPL::DOM::Schema::Node->new(name => 'AnyNode', type => 'AnyNode'), |
49 | 150 IMPL::DOM::Schema::Node->new(name => 'SwitchNode',type => 'SwitchNode') |
151 ) | |
152 ) | |
153 ), | |
154 IMPL::DOM::Schema::ComplexType->new(type => 'SwitchNode', nativeType => 'IMPL::DOM::Schema::SwitchNode')->appendRange( | |
155 IMPL::DOM::Schema::NodeSet->new()->appendRange( | |
156 IMPL::DOM::Schema::Node->new(name => 'ComplexNode', type=>'ComplexNode', minOccur => 0, maxOccur=>'unbounded'), | |
157 IMPL::DOM::Schema::Node->new(name => 'SimpleNode', type=>'SimpleNode', minOccur => 0, maxOccur=>'unbounded'), | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
158 IMPL::DOM::Schema::Node->new(name => 'Node', type=>'Node', minOccur => 0, maxOccur=>'unbounded'), |
49 | 159 ) |
160 ), | |
161 IMPL::DOM::Schema::ComplexType->new(type => 'NodeList', nativeType => 'IMPL::DOM::Schema::NodeList')->appendRange( | |
162 IMPL::DOM::Schema::NodeSet->new()->appendRange( | |
163 IMPL::DOM::Schema::Node->new(name => 'ComplexNode', type => 'ComplexNode', minOccur => 0, maxOccur=>'unbounded'), | |
164 IMPL::DOM::Schema::Node->new(name => 'SimpleNode', type => 'SimpleNode', minOccur => 0, maxOccur=>'unbounded'), | |
165 IMPL::DOM::Schema::Node->new(name => 'SwitchNode',type => 'SwitchNode', minOccur => 0, maxOccur=>'unbounded'), | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
166 IMPL::DOM::Schema::Node->new(name => 'Node', type => 'Node', minOccur => 0, maxOccur=>'unbounded'), |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
167 IMPL::DOM::Schema::Node->new(name => 'AnyNode', type => 'AnyNode', minOccur => 0, maxOccur=>'unbounded'), |
49 | 168 ) |
169 ), | |
170 IMPL::DOM::Schema::ComplexType->new(type => 'ComplexType', nativeType => 'IMPL::DOM::Schema::ComplexType')->appendRange( | |
171 IMPL::DOM::Schema::NodeList->new()->appendRange( | |
172 IMPL::DOM::Schema::SwitchNode->new()->appendRange( | |
173 IMPL::DOM::Schema::Node->new(name => 'NodeSet', type => 'NodeSet'), | |
174 IMPL::DOM::Schema::Node->new(name => 'NodeList',type => 'NodeList'), | |
175 ), | |
102 | 176 IMPL::DOM::Schema::Node->new(name => 'Property', type=>'Property', maxOccur=>'unbounded', minOccur=>0), |
49 | 177 IMPL::DOM::Schema::AnyNode->new(maxOccur => 'unbounded', minOccur => 0, type=>'Validator') |
178 ), | |
179 new IMPL::DOM::Schema::Property(name => 'type') | |
180 ), | |
181 IMPL::DOM::Schema::ComplexType->new(type => 'ComplexNode', nativeType => 'IMPL::DOM::Schema::ComplexNode')->appendRange( | |
182 IMPL::DOM::Schema::NodeList->new()->appendRange( | |
183 IMPL::DOM::Schema::SwitchNode->new()->appendRange( | |
184 IMPL::DOM::Schema::Node->new(name => 'NodeSet', type => 'NodeSet'), | |
185 IMPL::DOM::Schema::Node->new(name => 'NodeList',type => 'NodeList'), | |
186 ), | |
102 | 187 IMPL::DOM::Schema::Node->new(name => 'Property', type=>'Property', maxOccur=>'unbounded', minOccur=>0), |
49 | 188 IMPL::DOM::Schema::AnyNode->new(maxOccur => 'unbounded', minOccur => 0, type=>'Validator') |
189 ), | |
190 new IMPL::DOM::Schema::Property(name => 'name') | |
191 ), | |
192 IMPL::DOM::Schema::ComplexType->new(type => 'SimpleType', nativeType => 'IMPL::DOM::Schema::SimpleType')->appendRange( | |
102 | 193 IMPL::DOM::Schema::NodeList->new()->appendRange( |
194 | 194 IMPL::DOM::Schema::Node->new(name => 'Property', type=>'Property', maxOccur=>'unbounded', minOccur=>0), |
49 | 195 IMPL::DOM::Schema::AnyNode->new(maxOccur => 'unbounded', minOccur => 0, type=>'Validator') |
196 ), | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
197 new IMPL::DOM::Schema::Property(name => 'type'), |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
198 new IMPL::DOM::Schema::Property(name => 'inflator', optional => 1, inflator => 'IMPL::DOM::Schema::InflateFactory') |
49 | 199 ), |
200 IMPL::DOM::Schema::ComplexType->new(type => 'SimpleNode', nativeType => 'IMPL::DOM::Schema::SimpleNode')->appendRange( | |
102 | 201 IMPL::DOM::Schema::NodeList->new()->appendRange( |
194 | 202 IMPL::DOM::Schema::Node->new(name => 'Property', type=>'Property', maxOccur=>'unbounded', minOccur=>0), |
49 | 203 IMPL::DOM::Schema::AnyNode->new(maxOccur => 'unbounded', minOccur => 0, type=>'Validator') |
204 ), | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
205 new IMPL::DOM::Schema::Property(name => 'name'), |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
206 new IMPL::DOM::Schema::Property(name => 'inflator', optional => 1, inflator => 'IMPL::DOM::Schema::InflateFactory') |
49 | 207 ), |
208 IMPL::DOM::Schema::ComplexType->new(type => 'Validator', nativeType => 'IMPL::DOM::Schema::Validator')->appendRange( | |
209 IMPL::DOM::Schema::NodeList->new()->appendRange( | |
210 IMPL::DOM::Schema::AnyNode->new(maxOccur => 'unbounded', minOccur => 0) | |
100 | 211 ) |
102 | 212 ), |
213 IMPL::DOM::Schema::ComplexType->new(type => 'Property', nativeType => 'IMPL::DOM::Schema::Property' )->appendRange( | |
194 | 214 IMPL::DOM::Schema::NodeList->new()->appendRange( |
215 IMPL::DOM::Schema::AnyNode->new(maxOccur => 'unbounded', minOccur => 0) | |
216 ), | |
217 IMPL::DOM::Schema::Property->new(name => 'name'), | |
218 new IMPL::DOM::Schema::Property(name => 'inflator', optional => 1, inflator => 'IMPL::DOM::Schema::InflateFactory') | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
219 ), |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
220 IMPL::DOM::Schema::SimpleType->new(type => 'Node', nativeType => 'IMPL::DOM::Schema::Node')->appendRange( |
194 | 221 IMPL::DOM::Schema::Property->new(name => 'name'), |
222 IMPL::DOM::Schema::Property->new(name => 'type') | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
223 ), |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
224 IMPL::DOM::Schema::SimpleType->new(type => 'AnyNode', nativeType => 'IMPL::DOM::Schema::AnyNode') |
49 | 225 ); |
226 | |
227 $schema->Process; | |
228 | |
229 return $schema; | |
230 } | |
231 | |
232 1; | |
233 | |
234 __END__ | |
235 | |
236 =pod | |
237 | |
98 | 238 =head1 NAME |
239 | |
180 | 240 C<IMPL::DOM::Schema> - Схема документа. |
98 | 241 |
49 | 242 =head1 DESCRIPTION |
243 | |
166 | 244 C<use parent qw(IMPL::DOM::Document)> |
98 | 245 |
180 | 246 DOM схема - это документ, состоящий из определенных узлов, описывающая структуру |
247 других документов. | |
49 | 248 |
249 =head1 METHODS | |
250 | |
251 =over | |
252 | |
253 =item C<< $obj->Process() >> | |
254 | |
180 | 255 Обновляет таблицу типов из содержимого. |
49 | 256 |
257 =item C<< $obj->ResolveType($typeName) >> | |
258 | |
180 | 259 Возвращает схему типа c именем C<$typeName>. |
49 | 260 |
261 =back | |
262 | |
263 =head1 META SCHEMA | |
264 | |
180 | 265 Схема для описания схемы, эта схема используется для постороения других схем, выглядит приблизительно так |
49 | 266 |
98 | 267 =begin code xml |
268 | |
49 | 269 <schema> |
270 <ComplexNode name="schema"> | |
271 <NodeSet> | |
272 <Node minOcuur="0" maxOccur="unbounded" name="ComplexNode" type="ComplexNode"/> | |
273 <Node minOcuur="0" maxOccur="unbounded" name="SimpleNode" type="SimpleNode"/> | |
274 <Node minOcuur="0" maxOccur="unbounded" name="ComplexType" type="ComplexType"/> | |
275 <Node minOcuur="0" maxOccur="unbounded" name="SimpleType" type="SimpleType"/> | |
276 <SimpleNode minOcuur="0" maxOccur="unbounded" name="Node"/> | |
277 <SimpleNode minOcuur="0" maxOccur="unbounded" name="Include"/> | |
278 </NodeSet> | |
279 </ComplexNode> | |
280 | |
281 <ComplexType type="NodeContainer"> | |
282 <NodeSet> | |
283 <Node minOcuur="0" maxOccur="unbounded" name="ComplexNode" type="ComplexNode"/> | |
284 <Node minOcuur="0" maxOccur="unbounded" name="SimpleNode" type="SimpleNode"/> | |
285 <SimpleNode minOcuur="0" maxOccur="unbounded" name="Node"/> | |
286 </NodeSet> | |
287 </ComplexType> | |
288 | |
289 <ComplexType type="ComplexType"> | |
290 <NodeList> | |
291 <Node name="NodeSet" type="NodeContainer" minOcuur=0/> | |
292 <Node name="NodeList" type="NodeContainer" minOccur=0/> | |
293 <AnyNode minOccur="0" maxOccur="unbounded" type="Validator"/> | |
294 </NodeList> | |
295 </ComplexType> | |
296 | |
297 <ComplexType type="ComplexNode"> | |
298 <NodeList> | |
299 <Node name="NodeSet" type="NodeContainer" minOcuur=0/> | |
300 <Node name="NodeList" type="NodeContainer" minOccur=0/> | |
301 <AnyNode minOccur="0" maxOccur="unbounded" type="Validator"/> | |
302 </NodeList> | |
303 </ComplexType> | |
304 | |
305 <ComplexType type="SimpleNode"> | |
306 <NodeSet> | |
307 <AnyNode minOccur=0 maxOccur="unbounded" type="Validator"/> | |
308 </NodeSet> | |
309 </ComplexType> | |
310 | |
311 <ComplexType type="SimpleType"> | |
312 <NodeSet> | |
313 <AnyNode minOccur=0 maxOccur="unbounded" type="Validator"/> | |
314 </NodeSet> | |
315 </ComplexType> | |
316 | |
317 <ComplexType type="Validator"> | |
318 <NodeSet> | |
319 <AnyNode minOccur=0 maxOccur="unbounded"/> | |
320 </NodeSet> | |
321 </ComplexType> | |
322 | |
323 </schema> | |
324 | |
98 | 325 =end code xml |
326 | |
49 | 327 =cut |