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