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