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