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