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