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