Mercurial > pub > Impl
annotate Lib/IMPL/DOM/Schema.pm @ 388:648dfaf642e0
DOM refactoring,
removed inflators from DOM Schema,
DOM validation - in progress
author | cin |
---|---|
date | Tue, 11 Feb 2014 20:22:01 +0400 |
parents | 2f16f13b000c |
children | 5aff94ba842f |
rev | line source |
---|---|
49 | 1 package IMPL::DOM::Schema; |
2 use strict; | |
3 use warnings; | |
4 | |
381
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
5 use File::Spec; |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
6 use IMPL::Const qw(:prop); |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
7 use IMPL::declare { |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
8 require => { |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
9 ComplexNode => 'IMPL::DOM::Schema::ComplexNode', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
10 ComplexType => 'IMPL::DOM::Schema::ComplexType', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
11 SimpleNode => 'IMPL::DOM::Schema::SimpleNode', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
12 SimpleType => 'IMPL::DOM::Schema::SimpleType', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
13 Node => 'IMPL::DOM::Schema::Node', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
14 AnyNode => 'IMPL::DOM::Schema::AnyNode', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
15 NodeList => 'IMPL::DOM::Schema::NodeList', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
16 NodeSet => 'IMPL::DOM::Schema::NodeSet', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
17 Property => 'IMPL::DOM::Schema::Property', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
18 SwitchNode => 'IMPL::DOM::Schema::SwitchNode', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
19 Validator => 'IMPL::DOM::Schema::Validator', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
20 Builder => 'IMPL::DOM::Navigator::Builder', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
21 XMLReader => 'IMPL::DOM::XMLReader', # XMLReader references Schema |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
22 InflateFactory => 'IMPL::DOM::Schema::InflateFactory', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
23 Loader => 'IMPL::Code::Loader', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
24 StringMap => 'IMPL::Resources::StringLocaleMap' |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
25 }, |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
26 base => [ |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
27 'IMPL::DOM::Document' => sub { |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
28 nodeName => 'schema' |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
29 } |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
30 ], |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
31 props => [ |
383 | 32 _typesMap => PROP_RW | PROP_DIRECT, |
381
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
33 baseDir => PROP_RW | PROP_DIRECT, |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
34 schemaName => PROP_RW | PROP_DIRECT, |
383 | 35 baseSchemas => PROP_RO | PROP_LIST | PROP_DIRECT, |
381
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
36 stringMap => { |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
37 get => '_getStringMap', |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
38 direct => 1 |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
39 } |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
40 ] |
230 | 41 }; |
49 | 42 |
230 | 43 my $validatorLoader = Loader->new(prefix => Validator, verifyNames => 1); |
44 | |
250 | 45 #TODO rename and remove |
49 | 46 sub resolveType { |
383 | 47 goto &ResolveType; |
49 | 48 } |
49 | |
102 | 50 sub CTOR { |
194 | 51 my ($this,%args) = @_; |
52 | |
53 $this->{$baseDir} = ($args{baseDir} || '.'); | |
102 | 54 } |
55 | |
250 | 56 # compat |
57 sub ResolveType { | |
383 | 58 my ($this,$typeName) = @_; |
59 | |
60 my $type = $this->{$_typesMap}{$typeName}; | |
61 return $type if $type; | |
62 | |
63 foreach my $base ($this->baseSchemas) { | |
64 last if $type = $base->resolveType($typeName); | |
65 } | |
66 | |
67 die IMPL::KeyNotFoundException->new($typeName) | |
68 unless $type; | |
69 return $this->{$_typesMap}{$typeName} = $type; | |
250 | 70 } |
71 | |
49 | 72 sub Create { |
73 my ($this,$nodeName,$class,$refArgs) = @_; | |
74 | |
100 | 75 die new IMPL::Exception('Invalid node class') unless $class->isa('IMPL::DOM::Node'); |
49 | 76 |
101 | 77 if ($class->isa('IMPL::DOM::Schema::Validator')) { |
230 | 78 $class = $validatorLoader->GetFullName($nodeName); |
194 | 79 unless (eval {$class->can('new')}) { |
232 | 80 eval { |
81 $validatorLoader->Require($nodeName); | |
82 }; | |
83 my $e = $@; | |
194 | 84 die new IMPL::Exception("Invalid validator",$class,$e) if $e; |
85 } | |
101 | 86 } |
87 | |
88 return $this->SUPER::Create($nodeName,$class,$refArgs); | |
49 | 89 } |
90 | |
381
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
91 sub _getStringMap { |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
92 my ($this) = @_; |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
93 |
382 | 94 return $this->{$stringMap} |
383 | 95 if $this->{$stringMap}; |
382 | 96 |
97 my $map = StringMap->new(); | |
98 if ($this->baseDir and $this->schemaName) { | |
99 | |
100 $map->paths( File::Spec->catdir($this->baseDir,'locale') ); | |
383 | 101 $map->name( $this->schemaName ); |
382 | 102 } |
381
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
103 |
382 | 104 return $this->{$stringMap} = $map; |
381
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
105 } |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
106 |
49 | 107 sub Process { |
108 my ($this) = @_; | |
109 | |
101 | 110 # process instructions |
111 $this->Include($_) foreach map $_->nodeProperty('source'), $this->selectNodes('Include'); | |
112 | |
113 # build types map | |
383 | 114 $this->{$_typesMap} = { map { $_->type, $_ } $this->selectNodes(sub { $_[0]->nodeName eq 'ComplexType' || $_[0]->nodeName eq 'SimpleType' } ) }; |
49 | 115 } |
116 | |
101 | 117 sub Include { |
194 | 118 my ($this,$file) = @_; |
119 | |
120 my $schema = $this->LoadSchema(File::Spec->catfile($this->baseDir, $file)); | |
121 | |
383 | 122 $this->baseSchemas->Append( $schema ); |
101 | 123 } |
124 | |
125 sub LoadSchema { | |
194 | 126 my ($this,$file) = @_; |
127 | |
128 $file = File::Spec->rel2abs($file); | |
129 | |
130 my $class = ref $this || $this; | |
131 | |
232 | 132 my $reader = XMLReader->new( |
230 | 133 Navigator => Builder->new( |
194 | 134 $class, |
135 $class->MetaSchema | |
136 ), | |
137 SkipWhitespace => 1 | |
138 ); | |
139 | |
140 $reader->ParseFile($file); | |
141 | |
142 my $schema = $reader->Navigator->Document; | |
143 | |
381
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
144 my ($vol,$dir,$name) = File::Spec->splitpath($file); |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
145 |
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
146 $name =~ s/\.xml$//; |
194 | 147 |
148 $schema->baseDir($dir); | |
381
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
278
diff
changeset
|
149 $schema->schemaName($name); |
194 | 150 |
151 my @errors = $class->MetaSchema->Validate($schema); | |
152 | |
245 | 153 die new IMPL::Exception("Schema is invalid",$file,map( $_->message, @errors ) ) if @errors; |
194 | 154 |
155 $schema->Process; | |
156 | |
157 return $schema; | |
101 | 158 } |
159 | |
49 | 160 sub Validate { |
161 my ($this,$node) = @_; | |
162 | |
230 | 163 if ( my ($schemaNode) = $this->selectNodes(sub { $_->isa(Node) and $_[0]->name eq $node->nodeName })) { |
49 | 164 $schemaNode->Validate($node); |
165 } else { | |
266
89179bb8c388
*corrected TTView to handle plain (and undefined) values
cin
parents:
263
diff
changeset
|
166 return new IMPL::DOM::Schema::ValidationError(node => $node, message=> "A specified document (%Node.nodeName%) doesn't match the schema"); |
49 | 167 } |
168 } | |
169 | |
170 my $schema; | |
171 | |
172 sub MetaSchema { | |
173 | |
174 return $schema if $schema; | |
175 | |
232 | 176 $schema = __PACKAGE__->new(); |
49 | 177 |
178 $schema->appendRange( | |
230 | 179 ComplexNode->new(name => 'schema')->appendRange( |
180 NodeSet->new()->appendRange( | |
181 Node->new(name => 'ComplexNode', type => 'ComplexNode', minOccur => 0, maxOccur=>'unbounded'), | |
182 Node->new(name => 'ComplexType', type => 'ComplexType', minOccur => 0, maxOccur=>'unbounded'), | |
183 Node->new(name => 'SimpleNode', type => 'SimpleNode', minOccur => 0, maxOccur=>'unbounded'), | |
184 Node->new(name => 'SimpleType', type => 'SimpleType', minOccur => 0, maxOccur=>'unbounded'), | |
185 Node->new(name => 'Node', type => 'Node', minOccur => 0, maxOccur=>'unbounded'), | |
186 SimpleNode->new(name => 'Include', minOccur => 0, maxOccur=>'unbounded')->appendRange( | |
187 Property->new(name => 'source') | |
49 | 188 ) |
189 ), | |
190 ), | |
230 | 191 ComplexType->new(type => 'NodeSet', nativeType => 'IMPL::DOM::Schema::NodeSet')->appendRange( |
192 NodeSet->new()->appendRange( | |
193 Node->new(name => 'ComplexNode', type => 'ComplexNode', minOccur => 0, maxOccur=>'unbounded'), | |
194 Node->new(name => 'SimpleNode', type => 'SimpleNode', minOccur => 0, maxOccur=>'unbounded'), | |
195 Node->new(name => 'Node', type=>'Node', minOccur => 0, maxOccur=>'unbounded'), | |
196 SwitchNode->new(minOccur => 0, maxOccur => 1)->appendRange( | |
197 Node->new(name => 'AnyNode', type => 'AnyNode'), | |
198 Node->new(name => 'SwitchNode',type => 'SwitchNode') | |
49 | 199 ) |
200 ) | |
201 ), | |
230 | 202 ComplexType->new(type => 'SwitchNode', nativeType => 'IMPL::DOM::Schema::SwitchNode')->appendRange( |
203 NodeSet->new()->appendRange( | |
204 Node->new(name => 'ComplexNode', type=>'ComplexNode', minOccur => 0, maxOccur=>'unbounded'), | |
205 Node->new(name => 'SimpleNode', type=>'SimpleNode', minOccur => 0, maxOccur=>'unbounded'), | |
206 Node->new(name => 'Node', type=>'Node', minOccur => 0, maxOccur=>'unbounded'), | |
49 | 207 ) |
208 ), | |
230 | 209 ComplexType->new(type => 'NodeList', nativeType => 'IMPL::DOM::Schema::NodeList')->appendRange( |
210 NodeSet->new()->appendRange( | |
211 Node->new(name => 'ComplexNode', type => 'ComplexNode', minOccur => 0, maxOccur=>'unbounded'), | |
212 Node->new(name => 'SimpleNode', type => 'SimpleNode', minOccur => 0, maxOccur=>'unbounded'), | |
213 Node->new(name => 'SwitchNode',type => 'SwitchNode', minOccur => 0, maxOccur=>'unbounded'), | |
214 Node->new(name => 'Node', type => 'Node', minOccur => 0, maxOccur=>'unbounded'), | |
215 Node->new(name => 'AnyNode', type => 'AnyNode', minOccur => 0, maxOccur=>'unbounded'), | |
49 | 216 ) |
217 ), | |
230 | 218 ComplexType->new(type => 'ComplexType', nativeType => 'IMPL::DOM::Schema::ComplexType')->appendRange( |
219 NodeList->new()->appendRange( | |
220 SwitchNode->new()->appendRange( | |
221 Node->new(name => 'NodeSet', type => 'NodeSet'), | |
222 Node->new(name => 'NodeList',type => 'NodeList'), | |
49 | 223 ), |
230 | 224 Node->new(name => 'Property', type=>'Property', maxOccur=>'unbounded', minOccur=>0), |
225 AnyNode->new(maxOccur => 'unbounded', minOccur => 0, type=>'Validator') | |
49 | 226 ), |
230 | 227 Property->new(name => 'type') |
49 | 228 ), |
230 | 229 ComplexType->new(type => 'ComplexNode', nativeType => 'IMPL::DOM::Schema::ComplexNode')->appendRange( |
230 NodeList->new()->appendRange( | |
231 SwitchNode->new()->appendRange( | |
232 Node->new(name => 'NodeSet', type => 'NodeSet'), | |
233 Node->new(name => 'NodeList',type => 'NodeList'), | |
49 | 234 ), |
230 | 235 Node->new(name => 'Property', type=>'Property', maxOccur=>'unbounded', minOccur=>0), |
236 AnyNode->new(maxOccur => 'unbounded', minOccur => 0, type=>'Validator') | |
49 | 237 ), |
230 | 238 Property->new(name => 'name') |
49 | 239 ), |
230 | 240 ComplexType->new(type => 'SimpleType', nativeType => 'IMPL::DOM::Schema::SimpleType')->appendRange( |
241 NodeList->new()->appendRange( | |
242 Node->new(name => 'Property', type=>'Property', maxOccur=>'unbounded', minOccur=>0), | |
243 AnyNode->new(maxOccur => 'unbounded', minOccur => 0, type=>'Validator') | |
49 | 244 ), |
230 | 245 Property->new(name => 'type'), |
232 | 246 Property->new(name => 'inflator', optional => 1, inflator => 'IMPL::DOM::Schema::InflateFactory') |
49 | 247 ), |
230 | 248 ComplexType->new(type => 'SimpleNode', nativeType => 'IMPL::DOM::Schema::SimpleNode')->appendRange( |
249 NodeList->new()->appendRange( | |
250 Node->new(name => 'Property', type=>'Property', maxOccur=>'unbounded', minOccur=>0), | |
251 AnyNode->new(maxOccur => 'unbounded', minOccur => 0, type=>'Validator') | |
49 | 252 ), |
230 | 253 Property->new(name => 'name'), |
254 Property->new(name => 'inflator', optional => 1, inflator => 'IMPL::DOM::Schema::InflateFactory') | |
49 | 255 ), |
230 | 256 ComplexType->new(type => 'Validator', nativeType => 'IMPL::DOM::Schema::Validator')->appendRange( |
257 NodeList->new()->appendRange( | |
258 AnyNode->new(maxOccur => 'unbounded', minOccur => 0) | |
100 | 259 ) |
102 | 260 ), |
230 | 261 ComplexType->new(type => 'Property', nativeType => 'IMPL::DOM::Schema::Property' )->appendRange( |
262 NodeList->new()->appendRange( | |
263 AnyNode->new(maxOccur => 'unbounded', minOccur => 0) | |
194 | 264 ), |
230 | 265 Property->new(name => 'name'), |
266 Property->new(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
|
267 ), |
230 | 268 SimpleType->new(type => 'Node', nativeType => 'IMPL::DOM::Schema::Node')->appendRange( |
269 Property->new(name => 'name'), | |
270 Property->new(name => 'type') | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
271 ), |
230 | 272 SimpleType->new(type => 'AnyNode', nativeType => 'IMPL::DOM::Schema::AnyNode') |
49 | 273 ); |
274 | |
275 $schema->Process; | |
276 | |
277 return $schema; | |
278 } | |
279 | |
280 1; | |
281 | |
282 __END__ | |
283 | |
284 =pod | |
285 | |
98 | 286 =head1 NAME |
287 | |
180 | 288 C<IMPL::DOM::Schema> - Схема документа. |
98 | 289 |
49 | 290 =head1 DESCRIPTION |
291 | |
166 | 292 C<use parent qw(IMPL::DOM::Document)> |
98 | 293 |
180 | 294 DOM схема - это документ, состоящий из определенных узлов, описывающая структуру |
295 других документов. | |
49 | 296 |
297 =head1 METHODS | |
298 | |
299 =over | |
300 | |
301 =item C<< $obj->Process() >> | |
302 | |
180 | 303 Обновляет таблицу типов из содержимого. |
49 | 304 |
305 =item C<< $obj->ResolveType($typeName) >> | |
306 | |
180 | 307 Возвращает схему типа c именем C<$typeName>. |
49 | 308 |
309 =back | |
310 | |
311 =head1 META SCHEMA | |
312 | |
180 | 313 Схема для описания схемы, эта схема используется для постороения других схем, выглядит приблизительно так |
49 | 314 |
98 | 315 =begin code xml |
316 | |
49 | 317 <schema> |
318 <ComplexNode name="schema"> | |
319 <NodeSet> | |
320 <Node minOcuur="0" maxOccur="unbounded" name="ComplexNode" type="ComplexNode"/> | |
321 <Node minOcuur="0" maxOccur="unbounded" name="SimpleNode" type="SimpleNode"/> | |
322 <Node minOcuur="0" maxOccur="unbounded" name="ComplexType" type="ComplexType"/> | |
323 <Node minOcuur="0" maxOccur="unbounded" name="SimpleType" type="SimpleType"/> | |
324 <SimpleNode minOcuur="0" maxOccur="unbounded" name="Node"/> | |
325 <SimpleNode minOcuur="0" maxOccur="unbounded" name="Include"/> | |
326 </NodeSet> | |
327 </ComplexNode> | |
328 | |
329 <ComplexType type="NodeContainer"> | |
330 <NodeSet> | |
331 <Node minOcuur="0" maxOccur="unbounded" name="ComplexNode" type="ComplexNode"/> | |
332 <Node minOcuur="0" maxOccur="unbounded" name="SimpleNode" type="SimpleNode"/> | |
333 <SimpleNode minOcuur="0" maxOccur="unbounded" name="Node"/> | |
334 </NodeSet> | |
335 </ComplexType> | |
336 | |
337 <ComplexType type="ComplexType"> | |
338 <NodeList> | |
339 <Node name="NodeSet" type="NodeContainer" minOcuur=0/> | |
340 <Node name="NodeList" type="NodeContainer" minOccur=0/> | |
341 <AnyNode minOccur="0" maxOccur="unbounded" type="Validator"/> | |
342 </NodeList> | |
343 </ComplexType> | |
344 | |
345 <ComplexType type="ComplexNode"> | |
346 <NodeList> | |
347 <Node name="NodeSet" type="NodeContainer" minOcuur=0/> | |
348 <Node name="NodeList" type="NodeContainer" minOccur=0/> | |
349 <AnyNode minOccur="0" maxOccur="unbounded" type="Validator"/> | |
350 </NodeList> | |
351 </ComplexType> | |
352 | |
353 <ComplexType type="SimpleNode"> | |
354 <NodeSet> | |
355 <AnyNode minOccur=0 maxOccur="unbounded" type="Validator"/> | |
356 </NodeSet> | |
357 </ComplexType> | |
358 | |
359 <ComplexType type="SimpleType"> | |
360 <NodeSet> | |
361 <AnyNode minOccur=0 maxOccur="unbounded" type="Validator"/> | |
362 </NodeSet> | |
363 </ComplexType> | |
364 | |
365 <ComplexType type="Validator"> | |
366 <NodeSet> | |
367 <AnyNode minOccur=0 maxOccur="unbounded"/> | |
368 </NodeSet> | |
369 </ComplexType> | |
370 | |
371 </schema> | |
372 | |
98 | 373 =end code xml |
374 | |
49 | 375 =cut |