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