|
1 #!/usr/bin/env python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 #based on the plcopen standard. |
|
6 # |
|
7 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 # |
|
9 #See COPYING file for copyrights details. |
|
10 # |
|
11 #This library is free software; you can redistribute it and/or |
|
12 #modify it under the terms of the GNU General Public |
|
13 #License as published by the Free Software Foundation; either |
|
14 #version 2.1 of the License, or (at your option) any later version. |
|
15 # |
|
16 #This library is distributed in the hope that it will be useful, |
|
17 #but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 #General Public License for more details. |
|
20 # |
|
21 #You should have received a copy of the GNU General Public |
|
22 #License along with this library; if not, write to the Free Software |
|
23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
|
25 from xml.dom import minidom |
|
26 from types import * |
|
27 import re, datetime |
|
28 from xmlclass import * |
|
29 |
|
30 def GenerateDictFacets(facets): |
|
31 return dict([(name, (None, False)) for name in facets]) |
|
32 |
|
33 def GenerateSimpleTypeXMLText(function): |
|
34 def generateXMLTextMethod(value, name = None, indent = 0): |
|
35 text = "" |
|
36 if name is not None: |
|
37 ind1, ind2 = getIndent(indent, name) |
|
38 text += ind1 + "<%s>"%name |
|
39 text += function(value) |
|
40 if name is not None: |
|
41 text += "</%s>\n"%name |
|
42 return text |
|
43 return generateXMLTextMethod |
|
44 |
|
45 def GenerateFloatXMLText(extra_values = []): |
|
46 def generateXMLTextMethod(value, name = None, indent = 0): |
|
47 text = "" |
|
48 if name is not None: |
|
49 ind1, ind2 = getIndent(indent, name) |
|
50 text += ind1 + "<%s>"%name |
|
51 if value in extra_values or value % 1 != 0 or isinstance(value, IntType): |
|
52 text += str(value) |
|
53 else: |
|
54 text += "%.0f"%value |
|
55 if name is not None: |
|
56 text += "</%s>\n"%name |
|
57 return text |
|
58 return generateXMLTextMethod |
|
59 |
|
60 DEFAULT_FACETS = GenerateDictFacets(["pattern", "whiteSpace", "enumeration"]) |
|
61 NUMBER_FACETS = GenerateDictFacets(DEFAULT_FACETS.keys() + ["maxInclusive", "maxExclusive", "minInclusive", "minExclusive"]) |
|
62 DECIMAL_FACETS = GenerateDictFacets(NUMBER_FACETS.keys() + ["totalDigits", "fractionDigits"]) |
|
63 STRING_FACETS = GenerateDictFacets(DEFAULT_FACETS.keys() + ["length", "minLength", "maxLength"]) |
|
64 |
|
65 ALL_FACETS = ["pattern", "whiteSpace", "enumeration", "maxInclusive", |
|
66 "maxExclusive", "minInclusive", "minExclusive", "totalDigits", |
|
67 "fractionDigits", "length", "minLength", "maxLength"] |
|
68 |
|
69 |
|
70 #------------------------------------------------------------------------------- |
|
71 # Structure reducing functions |
|
72 #------------------------------------------------------------------------------- |
|
73 |
|
74 |
|
75 # Documentation elements |
|
76 |
|
77 def ReduceAppInfo(factory, attributes, elements): |
|
78 return {"type" : "appinfo", "source" : attributes.get("source", None), |
|
79 "content" : "\n".join(elements)} |
|
80 |
|
81 |
|
82 def ReduceDocumentation(factory, attributes, elements): |
|
83 return {"type" : "documentation", "source" : attributes.get("source", None), |
|
84 "language" : attributes.get("lang", "any"), "content" : "\n".join(elements)} |
|
85 |
|
86 |
|
87 def ReduceAnnotation(factory, attributes, elements): |
|
88 annotations, children = factory.ReduceElements(elements) |
|
89 annotation = {"type" : "annotation", "appinfo" : [], "documentation" : {}} |
|
90 for child in children: |
|
91 if child["type"] == "appinfo": |
|
92 annotation["appinfo"].append((child["source"], child["content"])) |
|
93 elif child["type"] == "documentation": |
|
94 if child["source"] is not None: |
|
95 text = "(source : %(source)s):\n%(content)s\n\n"%child |
|
96 else: |
|
97 text = child["content"] + "\n\n" |
|
98 if child["language"] not in annotation["documentation"]: |
|
99 annotation["documentation"] = text |
|
100 else: |
|
101 annotation["documentation"] += text |
|
102 return annotation |
|
103 |
|
104 # Simple type elements |
|
105 |
|
106 def GenerateFacetReducing(facetname, canbefixed): |
|
107 def ReduceFacet(factory, attributes, elements): |
|
108 annotations, children = factory.ReduceElements(elements) |
|
109 if "value" in attributes: |
|
110 facet = {"type" : facetname, "value" : attributes["value"], "doc" : annotations} |
|
111 if canbefixed: |
|
112 facet["fixed"] = attributes.get("fixed", False) |
|
113 return facet |
|
114 raise ValueError, "A value must be defined for the \"%s\" facet!"%facetname |
|
115 return ReduceFacet |
|
116 |
|
117 |
|
118 def ReduceList(factory, attributes, elements): |
|
119 annotations, children = factory.ReduceElements(elements) |
|
120 list = {"type" : "list", "itemType" : attributes.get("itemType", None), "doc" : annotations} |
|
121 |
|
122 if len(children) > 0 and children[0]["type"] == SIMPLETYPE: |
|
123 if list["itemType"] is None: |
|
124 list["itemType"] = children[0] |
|
125 else: |
|
126 raise ValueError, "Only one base type can be defined for restriction!" |
|
127 if list["itemType"] is None: |
|
128 raise ValueError, "No base type has been defined for list!" |
|
129 return list |
|
130 |
|
131 |
|
132 def ReduceUnion(factory, attributes, elements): |
|
133 annotations, children = factory.ReduceElements(elements) |
|
134 union = {"type" : "union", "memberTypes" : attributes.get("memberTypes", []), "doc" : annotations} |
|
135 |
|
136 if child in children: |
|
137 if child["type"] == SIMPLETYPE: |
|
138 union["memberTypes"] = child |
|
139 if len(union["memberTypes"]) == 0: |
|
140 raise ValueError, "No base type has been defined for union!" |
|
141 return list |
|
142 |
|
143 |
|
144 def ReduceSimpleType(factory, attributes, elements): |
|
145 # Reduce all the simple type children |
|
146 annotations, children = factory.ReduceElements(elements) |
|
147 |
|
148 typeinfos = children[0] |
|
149 |
|
150 # Initialize type informations |
|
151 facets = {} |
|
152 simpleType = {"type" : SIMPLETYPE, "final" : attributes.get("final", []), "doc" : annotations} |
|
153 if "name" in attributes: |
|
154 simpleType["name"] = attributes["name"] |
|
155 |
|
156 if typeinfos["type"] == "restriction": |
|
157 # Search for base type definition |
|
158 if isinstance(typeinfos["base"], (StringType, UnicodeType)): |
|
159 basetypeinfos = factory.FindSchemaElement(typeinfos["base"], SIMPLETYPE) |
|
160 if basetypeinfos is None: |
|
161 raise "\"%s\" isn't defined!"%typeinfos["base"] |
|
162 else: |
|
163 basetypeinfos = typeinfos["base"] |
|
164 |
|
165 # Check that base type is a simple type |
|
166 if basetypeinfos["type"] != SIMPLETYPE: |
|
167 raise ValueError, "Base type given isn't a simpleType!" |
|
168 |
|
169 typeinfos["basename"] = basetypeinfos["basename"] |
|
170 |
|
171 # Check that derivation is allowed |
|
172 if "final" in basetypeinfos: |
|
173 if "#all" in basetypeinfos["final"]: |
|
174 raise ValueError, "Base type can't be derivated!" |
|
175 if "restriction" in basetypeinfos["final"]: |
|
176 raise ValueError, "Base type can't be derivated by restriction!" |
|
177 |
|
178 # Extract simple type facets |
|
179 for facet in typeinfos["facets"]: |
|
180 facettype = facet["type"] |
|
181 if facettype not in basetypeinfos["facets"]: |
|
182 raise ValueError, "\"%s\" facet can't be defined for \"%s\" type!"%(facettype, type) |
|
183 elif basetypeinfos["facets"][facettype][1]: |
|
184 raise ValueError, "\"%s\" facet is fixed on base type!"%facettype |
|
185 value = facet["value"] |
|
186 basevalue = basetypeinfos["facets"][facettype][0] |
|
187 if facettype == "enumeration": |
|
188 value = basetypeinfos["extract"](value, False) |
|
189 if len(facets) == 0: |
|
190 facets["enumeration"] = ([value], False) |
|
191 continue |
|
192 elif facets.keys() == ["enumeration"]: |
|
193 facets["enumeration"][0].append(value) |
|
194 continue |
|
195 else: |
|
196 raise ValueError, "\"enumeration\" facet can't be defined with another facet type!" |
|
197 elif "enumeration" in facets: |
|
198 raise ValueError, "\"enumeration\" facet can't be defined with another facet type!" |
|
199 elif facettype in facets: |
|
200 raise ValueError, "\"%s\" facet can't be defined two times!"%facettype |
|
201 elif facettype == "length": |
|
202 if "minLength" in facets: |
|
203 raise ValueError, "\"length\" and \"minLength\" facets can't be defined at the same time!" |
|
204 if "maxLength" in facets: |
|
205 raise ValueError, "\"length\" and \"maxLength\" facets can't be defined at the same time!" |
|
206 try: |
|
207 value = int(value) |
|
208 except: |
|
209 raise ValueError, "\"length\" must be an integer!" |
|
210 if value < 0: |
|
211 raise ValueError, "\"length\" can't be negative!" |
|
212 elif basevalue is not None and basevalue != value: |
|
213 raise ValueError, "\"length\" can't be different from \"length\" defined in base type!" |
|
214 elif facettype == "minLength": |
|
215 if "length" in facets: |
|
216 raise ValueError, "\"length\" and \"minLength\" facets can't be defined at the same time!" |
|
217 try: |
|
218 value = int(value) |
|
219 except: |
|
220 raise ValueError, "\"minLength\" must be an integer!" |
|
221 if value < 0: |
|
222 raise ValueError, "\"minLength\" can't be negative!" |
|
223 elif "maxLength" in facets and value > facets["maxLength"]: |
|
224 raise ValueError, "\"minLength\" must be lesser than or equal to \"maxLength\"!" |
|
225 elif basevalue is not None and basevalue < value: |
|
226 raise ValueError, "\"minLength\" can't be lesser than \"minLength\" defined in base type!" |
|
227 elif facettype == "maxLength": |
|
228 if "length" in facets: |
|
229 raise ValueError, "\"length\" and \"maxLength\" facets can't be defined at the same time!" |
|
230 try: |
|
231 value = int(value) |
|
232 except: |
|
233 raise ValueError, "\"maxLength\" must be an integer!" |
|
234 if value < 0: |
|
235 raise ValueError, "\"maxLength\" can't be negative!" |
|
236 elif "minLength" in facets and value < facets["minLength"]: |
|
237 raise ValueError, "\"minLength\" must be lesser than or equal to \"maxLength\"!" |
|
238 elif basevalue is not None and basevalue > value: |
|
239 raise ValueError, "\"maxLength\" can't be greater than \"maxLength\" defined in base type!" |
|
240 elif facettype == "minInclusive": |
|
241 if "minExclusive" in facets: |
|
242 raise ValueError, "\"minExclusive\" and \"minInclusive\" facets can't be defined at the same time!" |
|
243 value = basetypeinfos["extract"](facet["value"], False) |
|
244 if "maxInclusive" in facets and value > facets["maxInclusive"][0]: |
|
245 raise ValueError, "\"minInclusive\" must be lesser than or equal to \"maxInclusive\"!" |
|
246 elif "maxExclusive" in facets and value >= facets["maxExclusive"][0]: |
|
247 raise ValueError, "\"minInclusive\" must be lesser than \"maxExclusive\"!" |
|
248 elif facettype == "minExclusive": |
|
249 if "minInclusive" in facets: |
|
250 raise ValueError, "\"minExclusive\" and \"minInclusive\" facets can't be defined at the same time!" |
|
251 value = basetypeinfos["extract"](facet["value"], False) |
|
252 if "maxInclusive" in facets and value >= facets["maxInclusive"][0]: |
|
253 raise ValueError, "\"minExclusive\" must be lesser than \"maxInclusive\"!" |
|
254 elif "maxExclusive" in facets and value >= facets["maxExclusive"][0]: |
|
255 raise ValueError, "\"minExclusive\" must be lesser than \"maxExclusive\"!" |
|
256 elif facettype == "maxInclusive": |
|
257 if "maxExclusive" in facets: |
|
258 raise ValueError, "\"maxExclusive\" and \"maxInclusive\" facets can't be defined at the same time!" |
|
259 value = basetypeinfos["extract"](facet["value"], False) |
|
260 if "minInclusive" in facets and value < facets["minInclusive"][0]: |
|
261 raise ValueError, "\"minInclusive\" must be lesser than or equal to \"maxInclusive\"!" |
|
262 elif "minExclusive" in facets and value <= facets["minExclusive"][0]: |
|
263 raise ValueError, "\"minExclusive\" must be lesser than \"maxInclusive\"!" |
|
264 elif facettype == "maxExclusive": |
|
265 if "maxInclusive" in facets: |
|
266 raise ValueError, "\"maxExclusive\" and \"maxInclusive\" facets can't be defined at the same time!" |
|
267 value = basetypeinfos["extract"](facet["value"], False) |
|
268 if "minInclusive" in facets and value <= facets["minInclusive"][0]: |
|
269 raise ValueError, "\"minInclusive\" must be lesser than \"maxExclusive\"!" |
|
270 elif "minExclusive" in facets and value <= facets["minExclusive"][0]: |
|
271 raise ValueError, "\"minExclusive\" must be lesser than \"maxExclusive\"!" |
|
272 elif facettype == "whiteSpace": |
|
273 if basevalue == "collapse" and value in ["preserve", "replace"] or basevalue == "replace" and value == "preserve": |
|
274 raise ValueError, "\"whiteSpace\" is incompatible with \"whiteSpace\" defined in base type!" |
|
275 elif facettype == "totalDigits": |
|
276 if "fractionDigits" in facets and value <= facets["fractionDigits"][0]: |
|
277 raise ValueError, "\"fractionDigits\" must be lesser than or equal to \"totalDigits\"!" |
|
278 elif basevalue is not None and value > basevalue: |
|
279 raise ValueError, "\"totalDigits\" can't be greater than \"totalDigits\" defined in base type!" |
|
280 elif facettype == "fractionDigits": |
|
281 if "totalDigits" in facets and value <= facets["totalDigits"][0]: |
|
282 raise ValueError, "\"fractionDigits\" must be lesser than or equal to \"totalDigits\"!" |
|
283 elif basevalue is not None and value > basevalue: |
|
284 raise ValueError, "\"totalDigits\" can't be greater than \"totalDigits\" defined in base type!" |
|
285 facets[facettype] = (value, facet.get("fixed", False)) |
|
286 |
|
287 # Report not redefined facet from base type to new created type |
|
288 for facettype, facetvalue in basetypeinfos["facets"].items(): |
|
289 if facettype not in facets: |
|
290 facets[facettype] = facetvalue |
|
291 |
|
292 # Generate extract value for new created type |
|
293 def ExtractSimpleTypeValue(attr, extract = True): |
|
294 value = basetypeinfos["extract"](attr, extract) |
|
295 for facetname, (facetvalue, facetfixed) in facets.items(): |
|
296 if facetvalue is not None: |
|
297 if facetname == "enumeration" and value not in facetvalue: |
|
298 raise ValueError, "\"%s\" not in enumerated values"%value |
|
299 elif facetname == "length" and len(value) != facetvalue: |
|
300 raise ValueError, "value must have a length of %d"%facetvalue |
|
301 elif facetname == "minLength" and len(value) < facetvalue: |
|
302 raise ValueError, "value must have a length of %d at least"%facetvalue |
|
303 elif facetname == "maxLength" and len(value) > facetvalue: |
|
304 raise ValueError, "value must have a length of %d at most"%facetvalue |
|
305 elif facetname == "minInclusive" and value < facetvalue: |
|
306 raise ValueError, "value must be greater than or equal to %s"%str(facetvalue) |
|
307 elif facetname == "minExclusive" and value <= facetvalue: |
|
308 raise ValueError, "value must be greater than %s"%str(facetvalue) |
|
309 elif facetname == "maxInclusive" and value > facetvalue: |
|
310 raise ValueError, "value must be lesser than or equal to %s"%str(facetvalue) |
|
311 elif facetname == "maxExclusive" and value >= facetvalue: |
|
312 raise ValueError, "value must be lesser than %s"%str(facetvalue) |
|
313 elif facetname == "pattern": |
|
314 model = re.compile("(?:%s)?$"%facetvalue) |
|
315 result = model.match(value) |
|
316 if result is None: |
|
317 raise ValueError, "value doesn't follow the pattern %s"%facetvalue |
|
318 elif facetname == "whiteSpace": |
|
319 if facetvalue == "replace": |
|
320 value = GetNormalizedString(value, False) |
|
321 elif facetvalue == "collapse": |
|
322 value = GetToken(value, False) |
|
323 return value |
|
324 |
|
325 def CheckSimpleTypeValue(value): |
|
326 for facetname, (facetvalue, facetfixed) in facets.items(): |
|
327 if facetvalue is not None: |
|
328 if facetname == "enumeration" and value not in facetvalue: |
|
329 return False |
|
330 elif facetname == "length" and len(value) != facetvalue: |
|
331 return False |
|
332 elif facetname == "minLength" and len(value) < facetvalue: |
|
333 return False |
|
334 elif facetname == "maxLength" and len(value) > facetvalue: |
|
335 return False |
|
336 elif facetname == "minInclusive" and value < facetvalue: |
|
337 return False |
|
338 elif facetname == "minExclusive" and value <= facetvalue: |
|
339 return False |
|
340 elif facetname == "maxInclusive" and value > facetvalue: |
|
341 return False |
|
342 elif facetname == "maxExclusive" and value >= facetvalue: |
|
343 return False |
|
344 elif facetname == "pattern": |
|
345 model = re.compile("(?:%s)?$"%facetvalue) |
|
346 result = model.match(value) |
|
347 if result is None: |
|
348 raise ValueError, "value doesn't follow the pattern %s"%facetvalue |
|
349 return True |
|
350 |
|
351 def SimpleTypeInitialValue(): |
|
352 for facetname, (facetvalue, facetfixed) in facets.items(): |
|
353 if facetvalue is not None: |
|
354 if facetname == "enumeration": |
|
355 return facetvalue[0] |
|
356 elif facetname == "length": |
|
357 return " "*facetvalue |
|
358 elif facetname == "minLength": |
|
359 return " "*minLength |
|
360 elif facetname == "minInclusive" and facetvalue > 0: |
|
361 return facetvalue |
|
362 elif facetname == "minExclusive" and facetvalue >= 0: |
|
363 return facetvalue + 1 |
|
364 elif facetname == "maxInclusive" and facetvalue < 0: |
|
365 return facetvalue |
|
366 elif facetname == "maxExclusive" and facetvalue <= 0: |
|
367 return facetvalue - 1 |
|
368 return basetypeinfos["initial"]() |
|
369 |
|
370 GenerateSimpleType = basetypeinfos["generate"] |
|
371 |
|
372 elif typeinfos["type"] == "list": |
|
373 # Search for item type definition |
|
374 if isinstance(typeinfos["itemType"], (StringType, UnicodeType)): |
|
375 itemtypeinfos = factory.FindSchemaElement(typeinfos["itemType"], SIMPLETYPE) |
|
376 if itemtypeinfos is None: |
|
377 raise "\"%s\" isn't defined!"%typeinfos["itemType"] |
|
378 else: |
|
379 itemtypeinfos = typeinfos["itemType"] |
|
380 |
|
381 # Check that item type is a simple type |
|
382 if itemtypeinfos["type"] != SIMPLETYPE: |
|
383 raise ValueError, "Item type given isn't a simpleType!" |
|
384 |
|
385 # Check that derivation is allowed |
|
386 if "#all" in itemtypeinfos["final"]: |
|
387 raise ValueError, "Item type can't be derivated!" |
|
388 if "list" in itemtypeinfos["final"]: |
|
389 raise ValueError, "Item type can't be derivated by list!" |
|
390 |
|
391 # Generate extract value for new created type |
|
392 def ExtractSimpleTypeValue(attr, extract = True): |
|
393 values = [] |
|
394 for value in GetToken(attr, extract).split(" "): |
|
395 values.append(itemtypeinfos["extract"](value, False)) |
|
396 return values |
|
397 |
|
398 def CheckSimpleTypeValue(value): |
|
399 for value in GetToken(attr, extract).split(" "): |
|
400 result = itemtypeinfos["check"](value) |
|
401 if not result: |
|
402 return result |
|
403 return True |
|
404 |
|
405 SimpleTypeInitialValue = lambda: "" |
|
406 |
|
407 GenerateSimpleType = GenerateSimpleTypeXMLText(lambda x : " ".join(map(itemtypeinfos, values))) |
|
408 |
|
409 facets = GenerateDictFacets(["length", "maxLength", "minLength", "enumeration", "pattern"]) |
|
410 facet["whiteSpace"] = ("collapse", False) |
|
411 |
|
412 elif typeinfos["type"] == "union": |
|
413 # Search for member types definition |
|
414 membertypesinfos = [] |
|
415 for membertype in typeinfos["memberTypes"]: |
|
416 if isinstance(membertype, (StringType, UnicodeType)): |
|
417 infos = factory.FindSchemaElement(typeinfos["memberTypes"], SIMPLETYPE) |
|
418 if infos is None: |
|
419 raise "\"%s\" isn't defined!"%typeinfos["itemType"] |
|
420 else: |
|
421 infos = membertype |
|
422 |
|
423 # Check that member type is a simple type |
|
424 if infos["type"] != SIMPLETYPE: |
|
425 raise ValueError, "Member type given isn't a simpleType!" |
|
426 |
|
427 # Check that derivation is allowed |
|
428 if "#all" in infos["final"]: |
|
429 raise ValueError, "Item type can't be derivated!" |
|
430 if "union" in infos["final"]: |
|
431 raise ValueError, "Member type can't be derivated by union!" |
|
432 |
|
433 membertypesinfos.append(infos) |
|
434 |
|
435 # Generate extract value for new created type |
|
436 def ExtractSimpleTypeValue(attr, extract = True): |
|
437 if extract: |
|
438 value = GetAttributeValue(attr) |
|
439 else: |
|
440 value = attr |
|
441 for infos in membertypesinfos: |
|
442 try: |
|
443 return infos["extract"](attr, False) |
|
444 except: |
|
445 pass |
|
446 raise ValueError, "\"%s\" isn't valid for type defined for union!" |
|
447 |
|
448 def CheckSimpleTypeValue(value): |
|
449 for infos in membertypesinfos: |
|
450 result = infos["check"](value) |
|
451 if result: |
|
452 return result |
|
453 return False |
|
454 |
|
455 SimpleTypeInitialValue = membertypesinfos[0]["initial"] |
|
456 |
|
457 def GenerateSimpleTypeFunction(value): |
|
458 if isinstance(value, BooleanType): |
|
459 return {True : "true", False : "false"}[value] |
|
460 else: |
|
461 return str(value) |
|
462 GenerateSimpleType = GenerateSimpleTypeXMLText(GenerateSimpleTypeFunction) |
|
463 |
|
464 facets = GenerateDictFacets(["pattern", "enumeration"]) |
|
465 |
|
466 simpleType["facets"] = facets |
|
467 simpleType["extract"] = ExtractSimpleTypeValue |
|
468 simpleType["initial"] = SimpleTypeInitialValue |
|
469 simpleType["check"] = CheckSimpleTypeValue |
|
470 simpleType["generate"] = GenerateSimpleType |
|
471 return simpleType |
|
472 |
|
473 |
|
474 # Complex type |
|
475 |
|
476 def ExtractAttributes(factory, elements, base = None): |
|
477 if base is not None: |
|
478 basetypeinfos = factory.FindSchemaElement(base, COMPLEXTYPE) |
|
479 if isinstance(basetypeinfos, (UnicodeType, StringType)): |
|
480 attrnames = {} |
|
481 else: |
|
482 attrnames = dict(map(lambda x:(x["name"], True), basetypeinfos["attributes"])) |
|
483 else: |
|
484 attrnames = {} |
|
485 attrs = [] |
|
486 for element in elements: |
|
487 if element["type"] == ATTRIBUTE: |
|
488 if attrnames.get(element["name"], False): |
|
489 raise ValueError, "\"%s\" attribute has been defined two times!"%element["name"] |
|
490 else: |
|
491 attrnames[element["name"]] = True |
|
492 attrs.append(element) |
|
493 elif element["type"] == "attributeGroup": |
|
494 attrgroup = factory.FindSchemaElement(element["ref"], ATTRIBUTESGROUP) |
|
495 for attr in attrgroup["attributes"]: |
|
496 if attrnames.get(attr["name"], False): |
|
497 raise ValueError, "\"%s\" attribute has been defined two times!"%attr["name"] |
|
498 else: |
|
499 attrnames[attr["name"]] = True |
|
500 attrs.append(attr) |
|
501 elif element["type"] == "anyAttribute": |
|
502 raise ValueError, "\"anyAttribute\" element isn't supported yet!" |
|
503 return attrs |
|
504 |
|
505 |
|
506 def ReduceRestriction(factory, attributes, elements): |
|
507 annotations, children = factory.ReduceElements(elements) |
|
508 restriction = {"type" : "restriction", "base" : attributes.get("base", None), "facets" : [], "doc" : annotations} |
|
509 if len(children) > 0 and children[0]["type"] == SIMPLETYPE: |
|
510 if restriction["base"] is None: |
|
511 restriction["base"] = children.pop(0) |
|
512 else: |
|
513 raise ValueError, "Only one base type can be defined for restriction!" |
|
514 if restriction["base"] is None: |
|
515 raise ValueError, "No base type has been defined for restriction!" |
|
516 |
|
517 while len(children) > 0 and children[0]["type"] in ALL_FACETS: |
|
518 restriction["facets"].append(children.pop(0)) |
|
519 restriction["attributes"] = ExtractAttributes(factory, children) |
|
520 return restriction |
|
521 |
|
522 |
|
523 def ReduceExtension(factory, attributes, elements): |
|
524 annotations, children = factory.ReduceElements(elements) |
|
525 extension = {"type" : "extension", "attributes" : [], "elements" : [], "base" : attributes.get("base", None), "doc" : annotations} |
|
526 if len(children) > 0: |
|
527 if children[0]["type"] in ["group", "all", CHOICE, "sequence"]: |
|
528 group = children.pop(0) |
|
529 if group["type"] in ["all", "sequence"]: |
|
530 extension["elements"] = group["elements"] |
|
531 extension["order"] = group["order"] |
|
532 elif group["type"] == CHOICE: |
|
533 content = group.copy() |
|
534 content["name"] = "content" |
|
535 extension["elements"].append(content) |
|
536 elif group["type"] == "group": |
|
537 elmtgroup = factory.FindSchemaElement(child["ref"], ELEMENTSGROUP) |
|
538 if "elements" in elmtgroup: |
|
539 extension["elements"] = elmtgroup["elements"] |
|
540 extension["order"] = elmtgroup["order"] |
|
541 else: |
|
542 content = elmtgroup.copy() |
|
543 content["name"] = "content" |
|
544 extension["elements"].append(content) |
|
545 extension["attributes"] = ExtractAttributes(factory, children, extension["base"]) |
|
546 return extension |
|
547 |
|
548 |
|
549 def ReduceSimpleContent(factory, attributes, elements): |
|
550 annotations, children = factory.ReduceElements(elements) |
|
551 simpleContent = children[0].copy() |
|
552 simpleContent["type"] = "simpleContent" |
|
553 return simpleContent |
|
554 |
|
555 |
|
556 def ReduceComplexContent(factory, attributes, elements): |
|
557 annotations, children = factory.ReduceElements(elements) |
|
558 complexContent = children[0].copy() |
|
559 complexContent["type"] = "complexContent" |
|
560 return complexContent |
|
561 |
|
562 |
|
563 def ReduceComplexType(factory, attributes, elements): |
|
564 annotations, children = factory.ReduceElements(elements) |
|
565 |
|
566 if len(children) > 0: |
|
567 if children[0]["type"] in ["simpleContent", "complexContent"]: |
|
568 complexType = children[0].copy() |
|
569 complexType.update(attributes) |
|
570 complexType["type"] = COMPLEXTYPE |
|
571 return complexType |
|
572 elif children[0]["type"] in ["group", "all", CHOICE, "sequence"]: |
|
573 complexType = {"type" : COMPLEXTYPE, "elements" : [], "order" : True, "doc" : annotations} |
|
574 complexType.update(attributes) |
|
575 group = children.pop(0) |
|
576 if group["type"] in ["all", "sequence"]: |
|
577 if group["minOccurs"] == 0 or group["maxOccurs"] != 1: |
|
578 if len(group["elements"]) > 1: |
|
579 raise ValueError, "Not supported yet!" |
|
580 if group["minOccurs"] == 0: |
|
581 group["elements"][0]["minOccurs"] = group["minOccurs"] |
|
582 if group["maxOccurs"] != 1: |
|
583 group["elements"][0]["maxOccurs"] = group["maxOccurs"] |
|
584 complexType["elements"] = group["elements"] |
|
585 complexType["order"] = group["order"] |
|
586 elif group["type"] == CHOICE: |
|
587 content = group.copy() |
|
588 content["name"] = "content" |
|
589 complexType["elements"].append(content) |
|
590 elif group["type"] == "group": |
|
591 elmtgroup = factory.FindSchemaElement(child["ref"], ELEMENTSGROUP) |
|
592 if "elements" in elmtgroup: |
|
593 complexType["elements"] = elmtgroup["elements"] |
|
594 complexType["order"] = elmtgroup["order"] |
|
595 else: |
|
596 content = elmtgroup.copy() |
|
597 content["name"] = "content" |
|
598 complexType["elements"].append(content) |
|
599 else: |
|
600 complexType = {"elements" : [], "order" : True, "doc" : annotations} |
|
601 complexType.update(attributes) |
|
602 complexType["type"] = COMPLEXTYPE |
|
603 complexType["attributes"] = ExtractAttributes(factory, children) |
|
604 return complexType |
|
605 else: |
|
606 raise ValueError, "\"ComplexType\" can't be empty!" |
|
607 |
|
608 |
|
609 # Attribute elements |
|
610 |
|
611 def ReduceAnyAttribute(factory, attributes, elements): |
|
612 return {"type" : "anyAttribute"} |
|
613 |
|
614 |
|
615 def ReduceAttribute(factory, attributes, elements): |
|
616 annotations, children = factory.ReduceElements(elements) |
|
617 |
|
618 if "default" in attributes: |
|
619 if "fixed" in attributes: |
|
620 raise ValueError, "\"default\" and \"fixed\" can't be defined at the same time!" |
|
621 elif attributes.get("use", "optional") != "optional": |
|
622 raise ValueError, "if \"default\" present, \"use\" can only have the value \"optional\"!" |
|
623 |
|
624 attribute = {"type" : ATTRIBUTE, "attr_type" : attributes.get("type", None), "doc" : annotations} |
|
625 if len(children) > 0: |
|
626 if attribute["attr_type"] is None: |
|
627 attribute["attr_type"] = children[0] |
|
628 else: |
|
629 raise ValueError, "Only one type can be defined for attribute!" |
|
630 |
|
631 if "ref" in attributes: |
|
632 if "name" in attributes: |
|
633 raise ValueError, "\"ref\" and \"name\" can't be defined at the same time!" |
|
634 elif "form" in attributes: |
|
635 raise ValueError, "\"ref\" and \"form\" can't be defined at the same time!" |
|
636 elif attribute["attr_type"] is not None: |
|
637 raise ValueError, "if \"ref\" is present, no type can be defined!" |
|
638 elif attribute["attr_type"] is None: |
|
639 raise ValueError, "No type has been defined for attribute!" |
|
640 |
|
641 if "type" in attributes: |
|
642 tmp_attrs = attributes.copy() |
|
643 tmp_attrs.pop("type") |
|
644 attribute.update(tmp_attrs) |
|
645 else: |
|
646 attribute.update(attributes) |
|
647 return attribute |
|
648 |
|
649 |
|
650 def ReduceAttributeGroup(factory, attributes, elements): |
|
651 annotations, children = factory.ReduceElements(elements) |
|
652 if "ref" in attributes: |
|
653 return {"type" : "attributeGroup", "ref" : attributes["ref"], "doc" : annotations} |
|
654 else: |
|
655 return {"type" : ATTRIBUTESGROUP, "attributes" : ExtractAttributes(factory, children), "doc" : annotations} |
|
656 |
|
657 |
|
658 # Elements groups |
|
659 |
|
660 def ReduceAny(factory, attributes, elements): |
|
661 annotations, children = factory.ReduceElements(elements) |
|
662 |
|
663 any = {"type" : ANY, "doc" : annotations} |
|
664 any.update(attributes) |
|
665 return any |
|
666 |
|
667 def ReduceElement(factory, attributes, elements): |
|
668 if "default" in attributes and "fixed" in attributes: |
|
669 raise ValueError, "\"default\" and \"fixed\" can't be defined at the same time!" |
|
670 |
|
671 if "ref" in attributes: |
|
672 annotations, children = factory.ReduceElements(elements) |
|
673 |
|
674 for attr in ["name", "default", "fixed", "form", "block", "type"]: |
|
675 if attr in attributes: |
|
676 raise ValueError, "\"ref\" and \"%s\" can't be defined at the same time!"%attr |
|
677 if attributes.get("nillable", False): |
|
678 raise ValueError, "\"ref\" and \"nillable\" can't be defined at the same time!" |
|
679 if len(children) > 0: |
|
680 raise ValueError, "No type and no constraints can be defined where \"ref\" is defined!" |
|
681 |
|
682 infos = factory.FindSchemaElement(attributes["ref"], ELEMENT) |
|
683 if infos is not None: |
|
684 element = infos.copy() |
|
685 element["minOccurs"] = attributes["minOccurs"] |
|
686 element["maxOccurs"] = attributes["maxOccurs"] |
|
687 return element |
|
688 else: |
|
689 raise ValueError, "\"%s\" base type isn't defined or circular referenced!"%name |
|
690 |
|
691 elif "name" in attributes: |
|
692 annotations, children = factory.ReduceElements(elements) |
|
693 |
|
694 element = {"type" : ELEMENT, "elmt_type" : attributes.get("type", None), "doc" : annotations} |
|
695 if len(children) > 0: |
|
696 if element["elmt_type"] is None: |
|
697 element["elmt_type"] = children[0] |
|
698 else: |
|
699 raise ValueError, "Only one type can be defined for attribute!" |
|
700 elif element["elmt_type"] is None: |
|
701 element["elmt_type"] = "tag" |
|
702 element["type"] = TAG |
|
703 |
|
704 if "type" in attributes: |
|
705 tmp_attrs = attributes.copy() |
|
706 tmp_attrs.pop("type") |
|
707 element.update(tmp_attrs) |
|
708 else: |
|
709 element.update(attributes) |
|
710 return element |
|
711 else: |
|
712 raise ValueError, "\"Element\" must have at least a \"ref\" or a \"name\" defined!" |
|
713 |
|
714 def ReduceAll(factory, attributes, elements): |
|
715 annotations, children = factory.ReduceElements(elements) |
|
716 |
|
717 for child in children: |
|
718 if children["maxOccurs"] == "unbounded" or children["maxOccurs"] > 1: |
|
719 raise ValueError, "\"all\" item can't have \"maxOccurs\" attribute greater than 1!" |
|
720 |
|
721 return {"type" : "all", "elements" : children, "minOccurs" : attributes["minOccurs"], |
|
722 "maxOccurs" : attributes["maxOccurs"], "order" : False, "doc" : annotations} |
|
723 |
|
724 |
|
725 def ReduceChoice(factory, attributes, elements): |
|
726 annotations, children = factory.ReduceElements(elements) |
|
727 |
|
728 choices = [] |
|
729 for child in children: |
|
730 if child["type"] in [ELEMENT, ANY, TAG]: |
|
731 choices.append(child) |
|
732 elif child["type"] == "sequence": |
|
733 raise ValueError, "\"sequence\" in \"choice\" is not supported. Create instead a new complex type!" |
|
734 elif child["type"] == CHOICE: |
|
735 choices.extend(child["choices"]) |
|
736 elif child["type"] == "group": |
|
737 elmtgroup = factory.FindSchemaElement(child["ref"], ELEMENTSGROUP) |
|
738 if "choices" not in elmtgroup: |
|
739 raise ValueError, "Only group composed of \"choice\" can be referenced in \"choice\" element!" |
|
740 choices_tmp = [] |
|
741 for choice in elmtgroup["choices"]: |
|
742 if not isinstance(choice["elmt_type"], (UnicodeType, StringType)) and choice["elmt_type"]["type"] == COMPLEXTYPE: |
|
743 elmt_type = "%s_%s"%(elmtgroup["name"], choice["name"]) |
|
744 if factory.TargetNamespace is not None: |
|
745 elmt_type = "%s:%s"%(factory.TargetNamespace, elmt_type) |
|
746 new_choice = choice.copy() |
|
747 new_choice["elmt_type"] = elmt_type |
|
748 choices_tmp.append(new_choice) |
|
749 else: |
|
750 choices_tmp.append(choice) |
|
751 choices.extend(choices_tmp) |
|
752 |
|
753 return {"type" : CHOICE, "choices" : choices, "minOccurs" : attributes["minOccurs"], |
|
754 "maxOccurs" : attributes["maxOccurs"], "doc" : annotations} |
|
755 |
|
756 |
|
757 def ReduceSequence(factory, attributes, elements): |
|
758 annotations, children = factory.ReduceElements(elements) |
|
759 |
|
760 sequence = [] |
|
761 for child in children: |
|
762 if child["type"] in [ELEMENT, ANY, TAG]: |
|
763 sequence.append(child) |
|
764 elif child["type"] == CHOICE: |
|
765 content = child.copy() |
|
766 content["name"] = "content" |
|
767 sequence.append(content) |
|
768 elif child["type"] == "sequence": |
|
769 sequence.extend(child["elements"]) |
|
770 elif child["type"] == "group": |
|
771 elmtgroup = factory.FindSchemaElement(child["ref"], ELEMENTSGROUP) |
|
772 if "elements" not in elmtgroup or not elmtgroup["order"]: |
|
773 raise ValueError, "Only group composed of \"sequence\" can be referenced in \"sequence\" element!" |
|
774 elements_tmp = [] |
|
775 for element in elmtgroup["elements"]: |
|
776 if not isinstance(element["elmt_type"], (UnicodeType, StringType)) and element["elmt_type"]["type"] == COMPLEXTYPE: |
|
777 elmt_type = "%s_%s"%(elmtgroup["name"], element["name"]) |
|
778 if factory.TargetNamespace is not None: |
|
779 elmt_type = "%s:%s"%(factory.TargetNamespace, elmt_type) |
|
780 new_element = element.copy() |
|
781 new_element["elmt_type"] = elmt_type |
|
782 elements_tmp.append(new_element) |
|
783 else: |
|
784 elements_tmp.append(element) |
|
785 sequence.extend(elements_tmp) |
|
786 |
|
787 return {"type" : "sequence", "elements" : sequence, "minOccurs" : attributes["minOccurs"], |
|
788 "maxOccurs" : attributes["maxOccurs"], "order" : True, "doc" : annotations} |
|
789 |
|
790 |
|
791 def ReduceGroup(factory, attributes, elements): |
|
792 annotations, children = factory.ReduceElements(elements) |
|
793 |
|
794 if "ref" in attributes: |
|
795 return {"type" : "group", "ref" : attributes["ref"], "doc" : annotations} |
|
796 else: |
|
797 element = children[0] |
|
798 group = {"type" : ELEMENTSGROUP, "doc" : annotations} |
|
799 if element["type"] == CHOICE: |
|
800 group["choices"] = element["choices"] |
|
801 else: |
|
802 group.update({"elements" : element["elements"], "order" : group["order"]}) |
|
803 group.update(attributes) |
|
804 return group |
|
805 |
|
806 # Constraint elements |
|
807 |
|
808 def ReduceUnique(factory, attributes, elements): |
|
809 annotations, children = factory.ReduceElements(elements) |
|
810 raise ValueError, "\"unique\" element isn't supported yet!" |
|
811 |
|
812 def ReduceKey(factory, attributes, elements): |
|
813 annotations, children = factory.ReduceElements(elements) |
|
814 raise ValueError, "\"key\" element isn't supported yet!" |
|
815 |
|
816 def ReduceKeyRef(factory, attributes, elements): |
|
817 annotations, children = factory.ReduceElements(elements) |
|
818 raise ValueError, "\"keyref\" element isn't supported yet!" |
|
819 |
|
820 def ReduceSelector(factory, attributes, elements): |
|
821 annotations, children = factory.ReduceElements(elements) |
|
822 raise ValueError, "\"selector\" element isn't supported yet!" |
|
823 |
|
824 def ReduceField(factory, attributes, elements): |
|
825 annotations, children = factory.ReduceElements(elements) |
|
826 raise ValueError, "\"field\" element isn't supported yet!" |
|
827 |
|
828 |
|
829 # Inclusion elements |
|
830 |
|
831 def ReduceImport(factory, attributes, elements): |
|
832 annotations, children = factory.ReduceElements(elements) |
|
833 raise ValueError, "\"import\" element isn't supported yet!" |
|
834 |
|
835 def ReduceInclude(factory, attributes, elements): |
|
836 annotations, children = factory.ReduceElements(elements) |
|
837 raise ValueError, "\"include\" element isn't supported yet!" |
|
838 |
|
839 def ReduceRedefine(factory, attributes, elements): |
|
840 annotations, children = factory.ReduceElements(elements) |
|
841 raise ValueError, "\"redefine\" element isn't supported yet!" |
|
842 |
|
843 |
|
844 # Schema element |
|
845 |
|
846 def ReduceSchema(factory, attributes, elements): |
|
847 factory.AttributeFormDefault = attributes["attributeFormDefault"] |
|
848 factory.ElementFormDefault = attributes["elementFormDefault"] |
|
849 factory.BlockDefault = attributes["blockDefault"] |
|
850 factory.FinalDefault = attributes["finalDefault"] |
|
851 |
|
852 if "targetNamespace" in attributes: |
|
853 factory.TargetNamespace = factory.DefinedNamespaces.get(attributes["targetNamespace"], None) |
|
854 factory.Namespaces[factory.TargetNamespace] = {} |
|
855 |
|
856 annotations, children = factory.ReduceElements(elements, True) |
|
857 |
|
858 for child in children: |
|
859 if "name" in child: |
|
860 infos = factory.GetQualifiedNameInfos(child["name"], factory.TargetNamespace, True) |
|
861 if infos is None: |
|
862 factory.Namespaces[factory.TargetNamespace][child["name"]] = child |
|
863 elif not CompareSchema(infos, child): |
|
864 raise ValueError, "\"%s\" is defined twice in targetNamespace!"%child["name"] |
|
865 |
|
866 def CompareSchema(schema, reference): |
|
867 if isinstance(schema, ListType): |
|
868 if not isinstance(reference, ListType) or len(schema) != len(reference): |
|
869 return False |
|
870 for i, value in enumerate(schema): |
|
871 result = CompareSchema(value, reference[i]) |
|
872 if not result: |
|
873 return result |
|
874 return True |
|
875 elif isinstance(schema, DictType): |
|
876 if not isinstance(reference, DictType) or len(schema) != len(reference): |
|
877 return False |
|
878 for name, value in schema.items(): |
|
879 ref_value = reference.get(name, None) |
|
880 if ref_value is None: |
|
881 return False |
|
882 result = CompareSchema(value, ref_value) |
|
883 if not result: |
|
884 return result |
|
885 return True |
|
886 elif isinstance(schema, FunctionType): |
|
887 if not isinstance(reference, FunctionType) or schema.__name__ != reference.__name__: |
|
888 return False |
|
889 else: |
|
890 return True |
|
891 return schema == reference |
|
892 |
|
893 #------------------------------------------------------------------------------- |
|
894 # Base class for XSD schema extraction |
|
895 #------------------------------------------------------------------------------- |
|
896 |
|
897 |
|
898 class XSDClassFactory(ClassFactory): |
|
899 |
|
900 def __init__(self, document, debug = False): |
|
901 ClassFactory.__init__(self, document, debug) |
|
902 self.Namespaces["xml"] = { |
|
903 "lang" : { |
|
904 "type" : SYNTAXATTRIBUTE, |
|
905 "extract" : { |
|
906 "default" : GenerateEnumeratedExtraction("lang", LANGUAGES) |
|
907 } |
|
908 } |
|
909 } |
|
910 self.Namespaces["xsi"] = { |
|
911 "noNamespaceSchemaLocation" : { |
|
912 "type" : SYNTAXATTRIBUTE, |
|
913 "extract" : { |
|
914 "default" : NotSupportedYet("noNamespaceSchemaLocation") |
|
915 } |
|
916 }, |
|
917 "nil" : { |
|
918 "type" : SYNTAXATTRIBUTE, |
|
919 "extract" : { |
|
920 "default" : NotSupportedYet("nil") |
|
921 } |
|
922 }, |
|
923 "schemaLocation" : { |
|
924 "type" : SYNTAXATTRIBUTE, |
|
925 "extract" : { |
|
926 "default" : NotSupportedYet("schemaLocation") |
|
927 } |
|
928 }, |
|
929 "type" : { |
|
930 "type" : SYNTAXATTRIBUTE, |
|
931 "extract" : { |
|
932 "default" : NotSupportedYet("type") |
|
933 } |
|
934 } |
|
935 } |
|
936 |
|
937 def ParseSchema(self): |
|
938 schema = self.Document.childNodes[0] |
|
939 for qualified_name, attr in schema._attrs.items(): |
|
940 value = GetAttributeValue(attr) |
|
941 if value == "http://www.w3.org/2001/XMLSchema": |
|
942 namespace, name = DecomposeQualifiedName(qualified_name) |
|
943 if namespace == "xmlns": |
|
944 self.DefinedNamespaces["http://www.w3.org/2001/XMLSchema"] = name |
|
945 self.SchemaNamespace = name |
|
946 else: |
|
947 self.DefinedNamespaces["http://www.w3.org/2001/XMLSchema"] = self.SchemaNamespace |
|
948 self.Namespaces[self.SchemaNamespace] = XSD_NAMESPACE |
|
949 self.Schema = XSD_NAMESPACE["schema"]["extract"]["default"](self, schema) |
|
950 ReduceSchema(self, self.Schema[1], self.Schema[2]) |
|
951 |
|
952 def FindSchemaElement(self, element_name, element_type): |
|
953 namespace, name = DecomposeQualifiedName(element_name) |
|
954 element = self.GetQualifiedNameInfos(name, namespace, True) |
|
955 if element is None and namespace == self.TargetNamespace and name not in self.CurrentCompilations: |
|
956 self.CurrentCompilations.append(name) |
|
957 element = self.CreateSchemaElement(name, element_type) |
|
958 self.CurrentCompilations.pop(-1) |
|
959 if element is not None: |
|
960 self.Namespaces[self.TargetNamespace][name] = element |
|
961 if element is None: |
|
962 if name in self.CurrentCompilations: |
|
963 if self.Debug: |
|
964 print "Warning : \"%s\" is circular referenced!"%element_name |
|
965 return element_name |
|
966 else: |
|
967 raise ValueError, "\"%s\" isn't defined!"%element_name |
|
968 if element["type"] != element_type: |
|
969 raise ValueError, "\"%s\" isn't a group!"%element_name |
|
970 return element |
|
971 |
|
972 def CreateSchemaElement(self, element_name, element_type): |
|
973 for type, attributes, elements in self.Schema[2]: |
|
974 namespace, name = DecomposeQualifiedName(type) |
|
975 if "name" in attributes and attributes["name"] == element_name: |
|
976 element_infos = None |
|
977 if element_type == ATTRIBUTE and name == "attribute": |
|
978 element_infos = ReduceAttribute(self, attributes, elements) |
|
979 elif element_type == ELEMENT and name == "element": |
|
980 element_infos = ReduceElement(self, attributes, elements) |
|
981 elif element_type == ATTRIBUTESGROUP and name == "attributeGroup": |
|
982 element_infos = ReduceAttributeGroup(self, attributes, elements) |
|
983 elif element_type == ELEMENTSGROUP and name == "group": |
|
984 element_infos = ReduceGroup(self, attributes, elements) |
|
985 elif element_type == SIMPLETYPE and name == "simpleType": |
|
986 element_infos = ReduceSimpleType(self, attributes, elements) |
|
987 elif element_type == COMPLEXTYPE and name == "complexType": |
|
988 element_infos = ReduceComplexType(self, attributes, elements) |
|
989 if element_infos is not None: |
|
990 self.Namespaces[self.TargetNamespace][element_name] = element_infos |
|
991 return element_infos |
|
992 return None |
|
993 |
|
994 """ |
|
995 This function opens the xsd file and generate the classes from the xml tree |
|
996 """ |
|
997 def GenerateClassesFromXSD(filename, declare = False): |
|
998 xsdfile = open(filename, 'r') |
|
999 factory = XSDClassFactory(minidom.parse(xsdfile)) |
|
1000 xsdfile.close() |
|
1001 factory.ParseSchema() |
|
1002 return GenerateClasses(factory, declare) |
|
1003 |
|
1004 """ |
|
1005 This function generate the classes from the xsd given as a string |
|
1006 """ |
|
1007 def GenerateClassesFromXSDstring(xsdstring, declare = False): |
|
1008 factory = XSDClassFactory(minidom.parseString(xsdstring)) |
|
1009 factory.ParseSchema() |
|
1010 return GenerateClasses(factory, declare) |
|
1011 |
|
1012 |
|
1013 #------------------------------------------------------------------------------- |
|
1014 # XSD schema syntax elements |
|
1015 #------------------------------------------------------------------------------- |
|
1016 |
|
1017 XSD_NAMESPACE = { |
|
1018 |
|
1019 #------------------------------------------------------------------------------- |
|
1020 # Syntax elements definition |
|
1021 #------------------------------------------------------------------------------- |
|
1022 |
|
1023 "all" : {"struct" : """ |
|
1024 <all |
|
1025 id = ID |
|
1026 maxOccurs = 1 : 1 |
|
1027 minOccurs = (0 | 1) : 1 |
|
1028 {any attributes with non-schema namespace . . .}> |
|
1029 Content: (annotation?, element*) |
|
1030 </all>""", |
|
1031 "type" : SYNTAXELEMENT, |
|
1032 "extract" : { |
|
1033 "default" : GenerateElement("all", ["id", "maxOccurs", "minOccurs"], |
|
1034 re.compile("((?:annotation )?(?:element )*)")) |
|
1035 }, |
|
1036 "reduce" : ReduceAll |
|
1037 }, |
|
1038 |
|
1039 "annotation" : {"struct" : """ |
|
1040 <annotation |
|
1041 id = ID |
|
1042 {any attributes with non-schema namespace . . .}> |
|
1043 Content: (appinfo | documentation)* |
|
1044 </annotation>""", |
|
1045 "type" : SYNTAXELEMENT, |
|
1046 "extract" : { |
|
1047 "default" : GenerateElement("annotation", ["id"], |
|
1048 re.compile("((?:app_info |documentation )*)")) |
|
1049 }, |
|
1050 "reduce" : ReduceAnnotation |
|
1051 }, |
|
1052 |
|
1053 "any" : {"struct": """ |
|
1054 <any |
|
1055 id = ID |
|
1056 maxOccurs = (nonNegativeInteger | unbounded) : 1 |
|
1057 minOccurs = nonNegativeInteger : 1 |
|
1058 namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) ) : ##any |
|
1059 processContents = (lax | skip | strict) : strict |
|
1060 {any attributes with non-schema namespace . . .}> |
|
1061 Content: (annotation?) |
|
1062 </any>""", |
|
1063 "type" : SYNTAXELEMENT, |
|
1064 "extract" : { |
|
1065 "default" : GenerateElement("any", |
|
1066 ["id", "maxOccurs", "minOccurs", "namespace", "processContents"], |
|
1067 re.compile("((?:annotation )?(?:simpleType )*)")) |
|
1068 }, |
|
1069 "reduce" : ReduceAny |
|
1070 }, |
|
1071 |
|
1072 "anyAttribute" : {"struct" : """ |
|
1073 <anyAttribute |
|
1074 id = ID |
|
1075 namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) ) : ##any |
|
1076 processContents = (lax | skip | strict) : strict |
|
1077 {any attributes with non-schema namespace . . .}> |
|
1078 Content: (annotation?) |
|
1079 </anyAttribute>""", |
|
1080 "type" : SYNTAXELEMENT, |
|
1081 "extract" : { |
|
1082 "default" : GenerateElement("anyAttribute", |
|
1083 ["id", "namespace", "processContents"], ONLY_ANNOTATION) |
|
1084 }, |
|
1085 "reduce" : ReduceAnyAttribute |
|
1086 }, |
|
1087 |
|
1088 "appinfo" : {"struct" : """ |
|
1089 <appinfo |
|
1090 source = anyURI |
|
1091 {any attributes with non-schema namespace . . .}> |
|
1092 Content: ({any})* |
|
1093 </appinfo>""", |
|
1094 "type" : SYNTAXELEMENT, |
|
1095 "extract" : { |
|
1096 "default" : GenerateElement("appinfo", ["source"], re.compile("(.*)"), True) |
|
1097 }, |
|
1098 "reduce" : ReduceAppInfo |
|
1099 }, |
|
1100 |
|
1101 "attribute" : {"struct" : """ |
|
1102 <attribute |
|
1103 default = string |
|
1104 fixed = string |
|
1105 form = (qualified | unqualified) |
|
1106 id = ID |
|
1107 name = NCName |
|
1108 ref = QName |
|
1109 type = QName |
|
1110 use = (optional | prohibited | required) : optional |
|
1111 {any attributes with non-schema namespace . . .}> |
|
1112 Content: (annotation?, simpleType?) |
|
1113 </attribute>""", |
|
1114 "type" : SYNTAXELEMENT, |
|
1115 "extract" : { |
|
1116 "default" : GenerateElement("attribute", |
|
1117 ["default", "fixed", "form", "id", "name", "ref", "type", "use"], |
|
1118 re.compile("((?:annotation )?(?:simpleType )?)")), |
|
1119 "schema" : GenerateElement("attribute", |
|
1120 ["default", "fixed", "form", "id", "name", "type"], |
|
1121 re.compile("((?:annotation )?(?:simpleType )?)")) |
|
1122 }, |
|
1123 "reduce" : ReduceAttribute |
|
1124 }, |
|
1125 |
|
1126 "attributeGroup" : {"struct" : """ |
|
1127 <attributeGroup |
|
1128 id = ID |
|
1129 name = NCName |
|
1130 ref = QName |
|
1131 {any attributes with non-schema namespace . . .}> |
|
1132 Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?)) |
|
1133 </attributeGroup>""", |
|
1134 "type" : SYNTAXELEMENT, |
|
1135 "extract" : { |
|
1136 "default" : GenerateElement("attributeGroup", |
|
1137 ["id", "ref"], ONLY_ANNOTATION), |
|
1138 "schema" : GenerateElement("attributeGroup", |
|
1139 ["id", "name"], |
|
1140 re.compile("((?:annotation )?(?:(?:attribute |attributeGroup )*(?:anyAttribute )?))")) |
|
1141 }, |
|
1142 "reduce" : ReduceAttributeGroup |
|
1143 }, |
|
1144 |
|
1145 "choice" : {"struct" : """ |
|
1146 <choice |
|
1147 id = ID |
|
1148 maxOccurs = (nonNegativeInteger | unbounded) : 1 |
|
1149 minOccurs = nonNegativeInteger : 1 |
|
1150 {any attributes with non-schema namespace . . .}> |
|
1151 Content: (annotation?, (element | group | choice | sequence | any)*) |
|
1152 </choice>""", |
|
1153 "type" : SYNTAXELEMENT, |
|
1154 "extract" : { |
|
1155 "default" : GenerateElement("choice", ["id", "maxOccurs", "minOccurs"], |
|
1156 re.compile("((?:annotation )?(?:element |group |choice |sequence |any )*)")) |
|
1157 }, |
|
1158 "reduce" : ReduceChoice |
|
1159 }, |
|
1160 |
|
1161 "complexContent" : {"struct" : """ |
|
1162 <complexContent |
|
1163 id = ID |
|
1164 mixed = boolean |
|
1165 {any attributes with non-schema namespace . . .}> |
|
1166 Content: (annotation?, (restriction | extension)) |
|
1167 </complexContent>""", |
|
1168 "type" : SYNTAXELEMENT, |
|
1169 "extract" : { |
|
1170 "default" : GenerateElement("complexContent", ["id", "mixed"], |
|
1171 re.compile("((?:annotation )?(?:restriction |extension ))")) |
|
1172 }, |
|
1173 "reduce" : ReduceComplexContent |
|
1174 }, |
|
1175 |
|
1176 "complexType" : {"struct" : """ |
|
1177 <complexType |
|
1178 abstract = boolean : false |
|
1179 block = (#all | List of (extension | restriction)) |
|
1180 final = (#all | List of (extension | restriction)) |
|
1181 id = ID |
|
1182 mixed = boolean : false |
|
1183 name = NCName |
|
1184 {any attributes with non-schema namespace . . .}> |
|
1185 Content: (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))) |
|
1186 </complexType>""", |
|
1187 "type" : SYNTAXELEMENT, |
|
1188 "extract" : { |
|
1189 "default" : GenerateElement("complexType", |
|
1190 ["abstract", "block", "final", "id", "mixed", "name"], |
|
1191 re.compile("((?:annotation )?(?:simpleContent |complexContent |(?:(?:group |all |choice |sequence )?(?:(?:attribute |attributeGroup )*(?:anyAttribute )?))))")) |
|
1192 }, |
|
1193 "reduce" : ReduceComplexType |
|
1194 }, |
|
1195 |
|
1196 "documentation" : {"struct" : """ |
|
1197 <documentation |
|
1198 source = anyURI |
|
1199 xml:lang = language |
|
1200 {any attributes with non-schema namespace . . .}> |
|
1201 Content: ({any})* |
|
1202 </documentation>""", |
|
1203 "type" : SYNTAXELEMENT, |
|
1204 "extract" : { |
|
1205 "default" : GenerateElement("documentation", |
|
1206 ["source", "lang"], re.compile("(.*)"), True) |
|
1207 }, |
|
1208 "reduce" : ReduceDocumentation |
|
1209 }, |
|
1210 |
|
1211 "element" : {"struct" : """ |
|
1212 <element |
|
1213 abstract = boolean : false |
|
1214 block = (#all | List of (extension | restriction | substitution)) |
|
1215 default = string |
|
1216 final = (#all | List of (extension | restriction)) |
|
1217 fixed = string |
|
1218 form = (qualified | unqualified) |
|
1219 id = ID |
|
1220 maxOccurs = (nonNegativeInteger | unbounded) : 1 |
|
1221 minOccurs = nonNegativeInteger : 1 |
|
1222 name = NCName |
|
1223 nillable = boolean : false |
|
1224 ref = QName |
|
1225 substitutionGroup = QName |
|
1226 type = QName |
|
1227 {any attributes with non-schema namespace . . .}> |
|
1228 Content: (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*)) |
|
1229 </element>""", |
|
1230 "type" : SYNTAXELEMENT, |
|
1231 "extract" : { |
|
1232 "default" : GenerateElement("element", |
|
1233 ["abstract", "block", "default", "final", "fixed", "form", "id", "maxOccurs", "minOccurs", "name", "nillable", "ref", "substitutionGroup", "type"], |
|
1234 re.compile("((?:annotation )?(?:simpleType |complexType )?(?:unique |key |keyref )*)")), |
|
1235 "schema" : GenerateElement("element", |
|
1236 ["abstract", "block", "default", "final", "fixed", "form", "id", "name", "nillable", "substitutionGroup", "type"], |
|
1237 re.compile("((?:annotation )?(?:simpleType |complexType )?(?:unique |key |keyref )*)")) |
|
1238 }, |
|
1239 "reduce" : ReduceElement |
|
1240 }, |
|
1241 |
|
1242 "enumeration" : {"struct" : """ |
|
1243 <enumeration |
|
1244 id = ID |
|
1245 value = anySimpleType |
|
1246 {any attributes with non-schema namespace . . .}> |
|
1247 Content: (annotation?) |
|
1248 </enumeration>""", |
|
1249 "type" : SYNTAXELEMENT, |
|
1250 "extract" : { |
|
1251 "default" : GenerateElement("enumeration", ["id", "value"], ONLY_ANNOTATION) |
|
1252 }, |
|
1253 "reduce" : GenerateFacetReducing("enumeration", False) |
|
1254 }, |
|
1255 |
|
1256 "extension" : {"struct" : """ |
|
1257 <extension |
|
1258 base = QName |
|
1259 id = ID |
|
1260 {any attributes with non-schema namespace . . .}> |
|
1261 Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))) |
|
1262 </extension>""", |
|
1263 "type" : SYNTAXELEMENT, |
|
1264 "extract" : { |
|
1265 "default" : GenerateElement("extension", ["base", "id"], |
|
1266 re.compile("((?:annotation )?(?:(?:attribute |attributeGroup )*(?:anyAttribute )?))")), |
|
1267 "complexContent" : GenerateElement("extension", ["base", "id"], |
|
1268 re.compile("((?:annotation )?(?:group |all |choice |sequence )?(?:(?:attribute |attributeGroup )*(?:anyAttribute )?))")) |
|
1269 }, |
|
1270 "reduce" : ReduceExtension |
|
1271 }, |
|
1272 |
|
1273 "field" : {"struct" : """ |
|
1274 <field |
|
1275 id = ID |
|
1276 xpath = a subset of XPath expression, see below |
|
1277 {any attributes with non-schema namespace . . .}> |
|
1278 Content: (annotation?) |
|
1279 </field>""", |
|
1280 "type" : SYNTAXELEMENT, |
|
1281 "extract" : { |
|
1282 "default" : GenerateElement("field", ["id", "xpath"], ONLY_ANNOTATION) |
|
1283 }, |
|
1284 "reduce" : ReduceField |
|
1285 }, |
|
1286 |
|
1287 "fractionDigits" : {"struct" : """ |
|
1288 <fractionDigits |
|
1289 fixed = boolean : false |
|
1290 id = ID |
|
1291 value = nonNegativeInteger |
|
1292 {any attributes with non-schema namespace . . .}> |
|
1293 Content: (annotation?) |
|
1294 </fractionDigits>""", |
|
1295 "type" : SYNTAXELEMENT, |
|
1296 "extract" : { |
|
1297 "default" : GenerateElement("fractionDigits", |
|
1298 ["fixed", "id", "value"], ONLY_ANNOTATION) |
|
1299 }, |
|
1300 "reduce" : GenerateFacetReducing("fractionDigits", True) |
|
1301 }, |
|
1302 |
|
1303 "group" : {"struct" : """ |
|
1304 <group |
|
1305 id = ID |
|
1306 maxOccurs = (nonNegativeInteger | unbounded) : 1 |
|
1307 minOccurs = nonNegativeInteger : 1 |
|
1308 name = NCName |
|
1309 ref = QName |
|
1310 {any attributes with non-schema namespace . . .}> |
|
1311 Content: (annotation?, (all | choice | sequence)?) |
|
1312 </group>""", |
|
1313 "type" : SYNTAXELEMENT, |
|
1314 "extract" : { |
|
1315 "default" : GenerateElement("group", |
|
1316 ["id", "maxOccurs", "minOccurs", "ref"], |
|
1317 re.compile("((?:annotation )?(?:all |choice |sequence )?)")), |
|
1318 "schema" : GenerateElement("group", |
|
1319 ["id", "name"], |
|
1320 re.compile("((?:annotation )?(?:all |choice |sequence )?)")) |
|
1321 }, |
|
1322 "reduce" : ReduceGroup |
|
1323 }, |
|
1324 |
|
1325 "import" : {"struct" : """ |
|
1326 <import |
|
1327 id = ID |
|
1328 namespace = anyURI |
|
1329 schemaLocation = anyURI |
|
1330 {any attributes with non-schema namespace . . .}> |
|
1331 Content: (annotation?) |
|
1332 </import>""", |
|
1333 "type" : SYNTAXELEMENT, |
|
1334 "extract" : { |
|
1335 "default" : GenerateElement("import", |
|
1336 ["id", "namespace", "schemaLocation"], ONLY_ANNOTATION) |
|
1337 }, |
|
1338 "reduce" : ReduceImport |
|
1339 }, |
|
1340 |
|
1341 "include" : {"struct" : """ |
|
1342 <include |
|
1343 id = ID |
|
1344 schemaLocation = anyURI |
|
1345 {any attributes with non-schema namespace . . .}> |
|
1346 Content: (annotation?) |
|
1347 </include>""", |
|
1348 "type" : SYNTAXELEMENT, |
|
1349 "extract" : { |
|
1350 "default" : GenerateElement("include", |
|
1351 ["id", "schemaLocation"], ONLY_ANNOTATION) |
|
1352 }, |
|
1353 "reduce" : ReduceInclude |
|
1354 }, |
|
1355 |
|
1356 "key" : {"struct" : """ |
|
1357 <key |
|
1358 id = ID |
|
1359 name = NCName |
|
1360 {any attributes with non-schema namespace . . .}> |
|
1361 Content: (annotation?, (selector, field+)) |
|
1362 </key>""", |
|
1363 "type" : SYNTAXELEMENT, |
|
1364 "extract" : { |
|
1365 "default" : GenerateElement("key", ["id", "name"], |
|
1366 re.compile("((?:annotation )?(?:selector |(?:field )+))")) |
|
1367 }, |
|
1368 "reduce" : ReduceKey |
|
1369 }, |
|
1370 |
|
1371 "keyref" : {"struct" : """ |
|
1372 <keyref |
|
1373 id = ID |
|
1374 name = NCName |
|
1375 refer = QName |
|
1376 {any attributes with non-schema namespace . . .}> |
|
1377 Content: (annotation?, (selector, field+)) |
|
1378 </keyref>""", |
|
1379 "type" : SYNTAXELEMENT, |
|
1380 "extract" : { |
|
1381 "default" : GenerateElement("keyref", ["id", "name", "refer"], |
|
1382 re.compile("((?:annotation )?(?:selector |(?:field )+))")) |
|
1383 }, |
|
1384 "reduce" : ReduceKeyRef |
|
1385 }, |
|
1386 |
|
1387 "length" : {"struct" : """ |
|
1388 <length |
|
1389 fixed = boolean : false |
|
1390 id = ID |
|
1391 value = nonNegativeInteger |
|
1392 {any attributes with non-schema namespace . . .}> |
|
1393 Content: (annotation?) |
|
1394 </length>""", |
|
1395 "type" : SYNTAXELEMENT, |
|
1396 "extract" : { |
|
1397 "default" : GenerateElement("length", |
|
1398 ["fixed", "id", "value"], ONLY_ANNOTATION) |
|
1399 }, |
|
1400 "reduce" : GenerateFacetReducing("length", True) |
|
1401 }, |
|
1402 |
|
1403 "list" : {"struct" : """ |
|
1404 <list |
|
1405 id = ID |
|
1406 itemType = QName |
|
1407 {any attributes with non-schema namespace . . .}> |
|
1408 Content: (annotation?, simpleType?) |
|
1409 </list>""", |
|
1410 "type" : SYNTAXELEMENT, |
|
1411 "extract" : { |
|
1412 "default" : GenerateElement("list", ["id", "itemType"], |
|
1413 re.compile("((?:annotation )?(?:simpleType )?)$")) |
|
1414 }, |
|
1415 "reduce" : ReduceList |
|
1416 }, |
|
1417 |
|
1418 "maxExclusive" : {"struct" : """ |
|
1419 <maxInclusive |
|
1420 fixed = boolean : false |
|
1421 id = ID |
|
1422 value = anySimpleType |
|
1423 {any attributes with non-schema namespace . . .}> |
|
1424 Content: (annotation?) |
|
1425 </maxInclusive>""", |
|
1426 "type" : SYNTAXELEMENT, |
|
1427 "extract" : { |
|
1428 "default" : GenerateElement("maxExclusive", |
|
1429 ["fixed", "id", "value"], ONLY_ANNOTATION) |
|
1430 }, |
|
1431 "reduce" : GenerateFacetReducing("maxExclusive", True) |
|
1432 }, |
|
1433 |
|
1434 "maxInclusive" : {"struct" : """ |
|
1435 <maxExclusive |
|
1436 fixed = boolean : false |
|
1437 id = ID |
|
1438 value = anySimpleType |
|
1439 {any attributes with non-schema namespace . . .}> |
|
1440 Content: (annotation?) |
|
1441 </maxExclusive>""", |
|
1442 "type" : SYNTAXELEMENT, |
|
1443 "extract" : { |
|
1444 "default" : GenerateElement("maxInclusive", |
|
1445 ["fixed", "id", "value"], ONLY_ANNOTATION) |
|
1446 }, |
|
1447 "reduce" : GenerateFacetReducing("maxInclusive", True) |
|
1448 }, |
|
1449 |
|
1450 "maxLength" : {"struct" : """ |
|
1451 <maxLength |
|
1452 fixed = boolean : false |
|
1453 id = ID |
|
1454 value = nonNegativeInteger |
|
1455 {any attributes with non-schema namespace . . .}> |
|
1456 Content: (annotation?) |
|
1457 </maxLength>""", |
|
1458 "type" : SYNTAXELEMENT, |
|
1459 "extract" : { |
|
1460 "default" : GenerateElement("maxLength", |
|
1461 ["fixed", "id", "value"], ONLY_ANNOTATION) |
|
1462 }, |
|
1463 "reduce" : GenerateFacetReducing("maxLength", True) |
|
1464 }, |
|
1465 |
|
1466 "minExclusive" : {"struct" : """ |
|
1467 <minExclusive |
|
1468 fixed = boolean : false |
|
1469 id = ID |
|
1470 value = anySimpleType |
|
1471 {any attributes with non-schema namespace . . .}> |
|
1472 Content: (annotation?) |
|
1473 </minExclusive>""", |
|
1474 "type" : SYNTAXELEMENT, |
|
1475 "extract" : { |
|
1476 "default" : GenerateElement("minExclusive", |
|
1477 ["fixed", "id", "value"], ONLY_ANNOTATION) |
|
1478 }, |
|
1479 "reduce" : GenerateFacetReducing("minExclusive", True) |
|
1480 }, |
|
1481 |
|
1482 "minInclusive" : {"struct" : """ |
|
1483 <minInclusive |
|
1484 fixed = boolean : false |
|
1485 id = ID |
|
1486 value = anySimpleType |
|
1487 {any attributes with non-schema namespace . . .}> |
|
1488 Content: (annotation?) |
|
1489 </minInclusive>""", |
|
1490 "type" : SYNTAXELEMENT, |
|
1491 "extract" : { |
|
1492 "default" : GenerateElement("minInclusive", |
|
1493 ["fixed", "id", "value"], ONLY_ANNOTATION) |
|
1494 }, |
|
1495 "reduce" : GenerateFacetReducing("minInclusive", True) |
|
1496 }, |
|
1497 |
|
1498 "minLength" : {"struct" : """ |
|
1499 <minLength |
|
1500 fixed = boolean : false |
|
1501 id = ID |
|
1502 value = nonNegativeInteger |
|
1503 {any attributes with non-schema namespace . . .}> |
|
1504 Content: (annotation?) |
|
1505 </minLength>""", |
|
1506 "type" : SYNTAXELEMENT, |
|
1507 "extract" : { |
|
1508 "default" : GenerateElement("minLength", |
|
1509 ["fixed", "id", "value"], ONLY_ANNOTATION) |
|
1510 }, |
|
1511 "reduce" : GenerateFacetReducing("minLength", True) |
|
1512 }, |
|
1513 |
|
1514 "pattern" : {"struct" : """ |
|
1515 <pattern |
|
1516 id = ID |
|
1517 value = string |
|
1518 {any attributes with non-schema namespace . . .}> |
|
1519 Content: (annotation?) |
|
1520 </pattern>""", |
|
1521 "type" : SYNTAXELEMENT, |
|
1522 "extract" : { |
|
1523 "default" : GenerateElement("pattern", ["id", "value"], ONLY_ANNOTATION) |
|
1524 }, |
|
1525 "reduce" : GenerateFacetReducing("pattern", False) |
|
1526 }, |
|
1527 |
|
1528 "redefine" : {"struct" : """ |
|
1529 <redefine |
|
1530 id = ID |
|
1531 schemaLocation = anyURI |
|
1532 {any attributes with non-schema namespace . . .}> |
|
1533 Content: (annotation | (simpleType | complexType | group | attributeGroup))* |
|
1534 </redefine>""", |
|
1535 "type" : SYNTAXELEMENT, |
|
1536 "extract" : { |
|
1537 "default" : GenerateElement("refine", ["id", "schemaLocation"], |
|
1538 re.compile("((?:annotation |(?:simpleType |complexType |group |attributeGroup ))*)")) |
|
1539 }, |
|
1540 "reduce" : ReduceRedefine |
|
1541 }, |
|
1542 |
|
1543 "restriction" : {"struct" : """ |
|
1544 <restriction |
|
1545 base = QName |
|
1546 id = ID |
|
1547 {any attributes with non-schema namespace . . .}> |
|
1548 Content: (annotation?, (group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)) |
|
1549 </restriction>""", |
|
1550 "type" : SYNTAXELEMENT, |
|
1551 "extract" : { |
|
1552 "default" : GenerateElement("restriction", ["base", "id"], |
|
1553 re.compile("((?:annotation )?(?:(?:simpleType )?(?:(?:minExclusive |minInclusive |maxExclusive |maxInclusive |totalDigits |fractionDigits |length |minLength |maxLength |enumeration |whiteSpace |pattern )*)))")), |
|
1554 "simpleContent" : GenerateElement("restriction", ["base", "id"], |
|
1555 re.compile("((?:annotation )?(?:(?:simpleType )?(?:(?:minExclusive |minInclusive |maxExclusive |maxInclusive |totalDigits |fractionDigits |length |minLength |maxLength |enumeration |whiteSpace |pattern )*)?(?:(?:attribute |attributeGroup )*(?:anyAttribute )?)))")), |
|
1556 "complexContent" : GenerateElement("restriction", ["base", "id"], |
|
1557 re.compile("((?:annotation )?(?:(?:simpleType )?(?:group |all |choice |sequence )?(?:(?:attribute |attributeGroup )*(?:anyAttribute )?)))")), |
|
1558 }, |
|
1559 "reduce" : ReduceRestriction |
|
1560 }, |
|
1561 |
|
1562 "schema" : {"struct" : """ |
|
1563 <schema |
|
1564 attributeFormDefault = (qualified | unqualified) : unqualified |
|
1565 blockDefault = (#all | List of (extension | restriction | substitution)) : '' |
|
1566 elementFormDefault = (qualified | unqualified) : unqualified |
|
1567 finalDefault = (#all | List of (extension | restriction | list | union)) : '' |
|
1568 id = ID |
|
1569 targetNamespace = anyURI |
|
1570 version = token |
|
1571 xml:lang = language |
|
1572 {any attributes with non-schema namespace . . .}> |
|
1573 Content: ((include | import | redefine | annotation)*, (((simpleType | complexType | group | attributeGroup) | element | attribute | notation), annotation*)*) |
|
1574 </schema>""", |
|
1575 "type" : SYNTAXELEMENT, |
|
1576 "extract" : { |
|
1577 "default" : GenerateElement("schema", |
|
1578 ["attributeFormDefault", "blockDefault", "elementFormDefault", "finalDefault", "id", "targetNamespace", "version", "lang"], |
|
1579 re.compile("((?:include |import |redefine |annotation )*(?:(?:(?:simpleType |complexType |group |attributeGroup )|element |attribute |annotation )(?:annotation )*)*)")) |
|
1580 } |
|
1581 }, |
|
1582 |
|
1583 "selector" : {"struct" : """ |
|
1584 <selector |
|
1585 id = ID |
|
1586 xpath = a subset of XPath expression, see below |
|
1587 {any attributes with non-schema namespace . . .}> |
|
1588 Content: (annotation?) |
|
1589 </selector>""", |
|
1590 "type" : SYNTAXELEMENT, |
|
1591 "extract" : { |
|
1592 "default" : GenerateElement("selector", ["id", "xpath"], ONLY_ANNOTATION) |
|
1593 }, |
|
1594 "reduce" : ReduceSelector |
|
1595 }, |
|
1596 |
|
1597 "sequence" : {"struct" : """ |
|
1598 <sequence |
|
1599 id = ID |
|
1600 maxOccurs = (nonNegativeInteger | unbounded) : 1 |
|
1601 minOccurs = nonNegativeInteger : 1 |
|
1602 {any attributes with non-schema namespace . . .}> |
|
1603 Content: (annotation?, (element | group | choice | sequence | any)*) |
|
1604 </sequence>""", |
|
1605 "type" : SYNTAXELEMENT, |
|
1606 "extract" : { |
|
1607 "default" : GenerateElement("sequence", ["id", "maxOccurs", "minOccurs"], |
|
1608 re.compile("((?:annotation )?(?:element |group |choice |sequence |any )*)")) |
|
1609 }, |
|
1610 "reduce" : ReduceSequence |
|
1611 }, |
|
1612 |
|
1613 "simpleContent" : {"struct" : """ |
|
1614 <simpleContent |
|
1615 id = ID |
|
1616 {any attributes with non-schema namespace . . .}> |
|
1617 Content: (annotation?, (restriction | extension)) |
|
1618 </simpleContent>""", |
|
1619 "type" : SYNTAXELEMENT, |
|
1620 "extract" : { |
|
1621 "default" : GenerateElement("simpleContent", ["id"], |
|
1622 re.compile("((?:annotation )?(?:restriction |extension ))")) |
|
1623 }, |
|
1624 "reduce" : ReduceSimpleContent |
|
1625 }, |
|
1626 |
|
1627 "simpleType" : {"struct" : """ |
|
1628 <simpleType |
|
1629 final = (#all | List of (list | union | restriction)) |
|
1630 id = ID |
|
1631 name = NCName |
|
1632 {any attributes with non-schema namespace . . .}> |
|
1633 Content: (annotation?, (restriction | list | union)) |
|
1634 </simpleType>""", |
|
1635 "type" : SYNTAXELEMENT, |
|
1636 "extract" : { |
|
1637 "default" : GenerateElement("simpleType", ["final", "id", "name"], |
|
1638 re.compile("((?:annotation )?(?:restriction |list |union ))")) |
|
1639 }, |
|
1640 "reduce" : ReduceSimpleType |
|
1641 }, |
|
1642 |
|
1643 "totalDigits" : {"struct" : """ |
|
1644 <totalDigits |
|
1645 fixed = boolean : false |
|
1646 id = ID |
|
1647 value = positiveInteger |
|
1648 {any attributes with non-schema namespace . . .}> |
|
1649 Content: (annotation?) |
|
1650 </totalDigits>""", |
|
1651 "type" : SYNTAXELEMENT, |
|
1652 "extract" : { |
|
1653 "default" : GenerateElement("totalDigits", |
|
1654 ["fixed", "id", "value"], ONLY_ANNOTATION), |
|
1655 }, |
|
1656 "reduce" : GenerateFacetReducing("totalDigits", True) |
|
1657 }, |
|
1658 |
|
1659 "union" : {"struct" : """ |
|
1660 <union |
|
1661 id = ID |
|
1662 memberTypes = List of QName |
|
1663 {any attributes with non-schema namespace . . .}> |
|
1664 Content: (annotation?, simpleType*) |
|
1665 </union>""", |
|
1666 "type" : SYNTAXELEMENT, |
|
1667 "extract" : { |
|
1668 "default" : GenerateElement("union", ["id", "memberTypes"], |
|
1669 re.compile("((?:annotation )?(?:simpleType )*)")) |
|
1670 }, |
|
1671 "reduce" : ReduceUnion |
|
1672 }, |
|
1673 |
|
1674 "unique" : {"struct" : """ |
|
1675 <unique |
|
1676 id = ID |
|
1677 name = NCName |
|
1678 {any attributes with non-schema namespace . . .}> |
|
1679 Content: (annotation?, (selector, field+)) |
|
1680 </unique>""", |
|
1681 "type" : SYNTAXELEMENT, |
|
1682 "extract" : { |
|
1683 "default" : GenerateElement("unique", ["id", "name"], |
|
1684 re.compile("((?:annotation )?(?:selector |(?:field )+))")) |
|
1685 }, |
|
1686 "reduce" : ReduceUnique |
|
1687 }, |
|
1688 |
|
1689 "whiteSpace" : {"struct" : """ |
|
1690 <whiteSpace |
|
1691 fixed = boolean : false |
|
1692 id = ID |
|
1693 value = (collapse | preserve | replace) |
|
1694 {any attributes with non-schema namespace . . .}> |
|
1695 Content: (annotation?) |
|
1696 </whiteSpace>""", |
|
1697 "type" : SYNTAXELEMENT, |
|
1698 "extract" : { |
|
1699 "default" : GenerateElement("whiteSpace", |
|
1700 ["fixed", "id", "value"], ONLY_ANNOTATION) |
|
1701 }, |
|
1702 "reduce" : GenerateFacetReducing("whiteSpace", True) |
|
1703 }, |
|
1704 |
|
1705 #------------------------------------------------------------------------------- |
|
1706 # Syntax attributes definition |
|
1707 #------------------------------------------------------------------------------- |
|
1708 |
|
1709 "abstract" : { |
|
1710 "type" : SYNTAXATTRIBUTE, |
|
1711 "extract" : { |
|
1712 "default" : GetBoolean |
|
1713 }, |
|
1714 "default" : { |
|
1715 "default" : False |
|
1716 } |
|
1717 }, |
|
1718 |
|
1719 "attributeFormDefault" : { |
|
1720 "type" : SYNTAXATTRIBUTE, |
|
1721 "extract" : { |
|
1722 "default" : GenerateEnumeratedExtraction("member attributeFormDefault", ["qualified", "unqualified"]) |
|
1723 }, |
|
1724 "default" : { |
|
1725 "default" : "unqualified" |
|
1726 } |
|
1727 }, |
|
1728 |
|
1729 "base" : { |
|
1730 "type" : SYNTAXATTRIBUTE, |
|
1731 "extract" : { |
|
1732 "default" : GenerateModelNameExtraction("member base", QName_model) |
|
1733 } |
|
1734 }, |
|
1735 |
|
1736 "block" : { |
|
1737 "type" : SYNTAXATTRIBUTE, |
|
1738 "extract" : { |
|
1739 "default" : GenerateGetList("block", ["restriction", "extension", "substitution"]) |
|
1740 } |
|
1741 }, |
|
1742 |
|
1743 "blockDefault" : { |
|
1744 "type" : SYNTAXATTRIBUTE, |
|
1745 "extract" : { |
|
1746 "default" : GenerateGetList("block", ["restriction", "extension", "substitution"]) |
|
1747 }, |
|
1748 "default" : { |
|
1749 "default" : "" |
|
1750 } |
|
1751 }, |
|
1752 |
|
1753 "default" : { |
|
1754 "type" : SYNTAXATTRIBUTE, |
|
1755 "extract" : { |
|
1756 "default" : GetAttributeValue |
|
1757 } |
|
1758 }, |
|
1759 |
|
1760 "elementFormDefault" : { |
|
1761 "type" : SYNTAXATTRIBUTE, |
|
1762 "extract" : { |
|
1763 "default" : GenerateEnumeratedExtraction("member elementFormDefault", ["qualified", "unqualified"]) |
|
1764 }, |
|
1765 "default" : { |
|
1766 "default" : "unqualified" |
|
1767 } |
|
1768 }, |
|
1769 |
|
1770 "final" : { |
|
1771 "type" : SYNTAXATTRIBUTE, |
|
1772 "extract" : { |
|
1773 "default" : GenerateGetList("final", ["restriction", "extension", "substitution"]), |
|
1774 "simpleType" : GenerateGetList("final", ["list", "union", "restriction"]) |
|
1775 } |
|
1776 }, |
|
1777 |
|
1778 "finalDefault" : { |
|
1779 "type" : SYNTAXATTRIBUTE, |
|
1780 "extract" : { |
|
1781 "default" : GenerateGetList("finalDefault", ["restriction", "extension", "list", "union"]) |
|
1782 }, |
|
1783 "default" : { |
|
1784 "default" : "" |
|
1785 } |
|
1786 }, |
|
1787 |
|
1788 "fixed" : { |
|
1789 "type" : SYNTAXATTRIBUTE, |
|
1790 "extract" : { |
|
1791 "default" : GetBoolean, |
|
1792 "attribute" : GetAttributeValue, |
|
1793 "element" : GetAttributeValue |
|
1794 }, |
|
1795 "default" : { |
|
1796 "default" : False, |
|
1797 "attribute" : None, |
|
1798 "element" : None |
|
1799 } |
|
1800 }, |
|
1801 |
|
1802 "form" : { |
|
1803 "type" : SYNTAXATTRIBUTE, |
|
1804 "extract" : { |
|
1805 "default" : GenerateEnumeratedExtraction("member form", ["qualified", "unqualified"]) |
|
1806 } |
|
1807 }, |
|
1808 |
|
1809 "id" : { |
|
1810 "type" : SYNTAXATTRIBUTE, |
|
1811 "extract" : { |
|
1812 "default" : GenerateModelNameExtraction("member id", NCName_model) |
|
1813 } |
|
1814 }, |
|
1815 |
|
1816 "itemType" : { |
|
1817 "type" : SYNTAXATTRIBUTE, |
|
1818 "extract" : { |
|
1819 "default" : GenerateModelNameExtraction("member itemType", QName_model) |
|
1820 } |
|
1821 }, |
|
1822 |
|
1823 "memberTypes" : { |
|
1824 "type" : SYNTAXATTRIBUTE, |
|
1825 "extract" : { |
|
1826 "default" : GenerateModelNameListExtraction("member memberTypes", QNames_model) |
|
1827 }, |
|
1828 }, |
|
1829 |
|
1830 "maxOccurs" : { |
|
1831 "type" : SYNTAXATTRIBUTE, |
|
1832 "extract" : { |
|
1833 "default" : GenerateLimitExtraction(), |
|
1834 "all" : GenerateLimitExtraction(1, 1, False) |
|
1835 }, |
|
1836 "default" : { |
|
1837 "default" : 1 |
|
1838 } |
|
1839 }, |
|
1840 |
|
1841 "minOccurs" : { |
|
1842 "type" : SYNTAXATTRIBUTE, |
|
1843 "extract" : { |
|
1844 "default" : GenerateLimitExtraction(unbounded = False), |
|
1845 "all" : GenerateLimitExtraction(0, 1, False) |
|
1846 }, |
|
1847 "default" : { |
|
1848 "default" : 1 |
|
1849 } |
|
1850 }, |
|
1851 |
|
1852 "mixed" : { |
|
1853 "type" : SYNTAXATTRIBUTE, |
|
1854 "extract" : { |
|
1855 "default" : GetBoolean |
|
1856 }, |
|
1857 "default" : { |
|
1858 "default" : None, |
|
1859 "complexType" : False |
|
1860 } |
|
1861 }, |
|
1862 |
|
1863 "name" : { |
|
1864 "type" : SYNTAXATTRIBUTE, |
|
1865 "extract" : { |
|
1866 "default" : GenerateModelNameExtraction("member name", NCName_model) |
|
1867 } |
|
1868 }, |
|
1869 |
|
1870 "namespace" : { |
|
1871 "type" : SYNTAXATTRIBUTE, |
|
1872 "extract" : { |
|
1873 "default" : GenerateModelNameExtraction("member namespace", URI_model), |
|
1874 "any" : GetNamespaces |
|
1875 }, |
|
1876 "default" : { |
|
1877 "default" : None, |
|
1878 "any" : "##any" |
|
1879 } |
|
1880 }, |
|
1881 |
|
1882 "nillable" : { |
|
1883 "type" : SYNTAXATTRIBUTE, |
|
1884 "extract" : { |
|
1885 "default" : GetBoolean |
|
1886 }, |
|
1887 "default" : { |
|
1888 "default" : False |
|
1889 } |
|
1890 }, |
|
1891 |
|
1892 "processContents" : { |
|
1893 "type" : SYNTAXATTRIBUTE, |
|
1894 "extract" : { |
|
1895 "default" : GenerateEnumeratedExtraction("member processContents", ["lax", "skip", "strict"]) |
|
1896 }, |
|
1897 "default" : { |
|
1898 "default" : "strict" |
|
1899 } |
|
1900 }, |
|
1901 |
|
1902 "ref" : { |
|
1903 "type" : SYNTAXATTRIBUTE, |
|
1904 "extract" : { |
|
1905 "default" : GenerateModelNameExtraction("member ref", QName_model) |
|
1906 } |
|
1907 }, |
|
1908 |
|
1909 "refer" : { |
|
1910 "type" : SYNTAXATTRIBUTE, |
|
1911 "extract" : { |
|
1912 "default" : GenerateModelNameExtraction("member refer", QName_model) |
|
1913 } |
|
1914 }, |
|
1915 |
|
1916 "schemaLocation" : { |
|
1917 "type" : SYNTAXATTRIBUTE, |
|
1918 "extract" : { |
|
1919 "default" : GenerateModelNameExtraction("member schemaLocation", URI_model) |
|
1920 } |
|
1921 }, |
|
1922 |
|
1923 "source" : { |
|
1924 "type" : SYNTAXATTRIBUTE, |
|
1925 "extract" : { |
|
1926 "default" : GenerateModelNameExtraction("member source", URI_model) |
|
1927 } |
|
1928 }, |
|
1929 |
|
1930 "substitutionGroup" : { |
|
1931 "type" : SYNTAXATTRIBUTE, |
|
1932 "extract" : { |
|
1933 "default" : GenerateModelNameExtraction("member substitutionGroup", QName_model) |
|
1934 } |
|
1935 }, |
|
1936 |
|
1937 "targetNamespace" : { |
|
1938 "type" : SYNTAXATTRIBUTE, |
|
1939 "extract" : { |
|
1940 "default" : GenerateModelNameExtraction("member targetNamespace", URI_model) |
|
1941 } |
|
1942 }, |
|
1943 |
|
1944 "type" : { |
|
1945 "type" : SYNTAXATTRIBUTE, |
|
1946 "extract" : { |
|
1947 "default" : GenerateModelNameExtraction("member type", QName_model) |
|
1948 } |
|
1949 }, |
|
1950 |
|
1951 "use" : { |
|
1952 "type" : SYNTAXATTRIBUTE, |
|
1953 "extract" : { |
|
1954 "default" : GenerateEnumeratedExtraction("member usage", ["required", "optional", "prohibited"]) |
|
1955 }, |
|
1956 "default" : { |
|
1957 "default" : "optional" |
|
1958 } |
|
1959 }, |
|
1960 |
|
1961 "value" : { |
|
1962 "type" : SYNTAXATTRIBUTE, |
|
1963 "extract" : { |
|
1964 "default" : GetAttributeValue, |
|
1965 "fractionDigits" : GenerateIntegerExtraction(minInclusive=0), |
|
1966 "length" : GenerateIntegerExtraction(minInclusive=0), |
|
1967 "maxLength" : GenerateIntegerExtraction(minInclusive=0), |
|
1968 "minLength" : GenerateIntegerExtraction(minInclusive=0), |
|
1969 "totalDigits" : GenerateIntegerExtraction(minExclusive=0), |
|
1970 "whiteSpace" : GenerateEnumeratedExtraction("value", ["collapse", "preserve", "replace"]) |
|
1971 } |
|
1972 }, |
|
1973 |
|
1974 "version" : { |
|
1975 "type" : SYNTAXATTRIBUTE, |
|
1976 "extract" : { |
|
1977 "default" : GetToken |
|
1978 } |
|
1979 }, |
|
1980 |
|
1981 "xpath" : { |
|
1982 "type" : SYNTAXATTRIBUTE, |
|
1983 "extract" : { |
|
1984 "default" : NotSupportedYet("xpath") |
|
1985 } |
|
1986 }, |
|
1987 |
|
1988 #------------------------------------------------------------------------------- |
|
1989 # Simple types definition |
|
1990 #------------------------------------------------------------------------------- |
|
1991 |
|
1992 "string" : { |
|
1993 "type" : SIMPLETYPE, |
|
1994 "basename" : "string", |
|
1995 "extract" : GetAttributeValue, |
|
1996 "facets" : STRING_FACETS, |
|
1997 "generate" : GenerateSimpleTypeXMLText(lambda x : x), |
|
1998 "initial" : lambda: "", |
|
1999 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2000 }, |
|
2001 |
|
2002 "normalizedString" : { |
|
2003 "type" : SIMPLETYPE, |
|
2004 "basename" : "normalizedString", |
|
2005 "extract" : GetNormalizedString, |
|
2006 "facets" : STRING_FACETS, |
|
2007 "generate" : GenerateSimpleTypeXMLText(lambda x : x), |
|
2008 "initial" : lambda: "", |
|
2009 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2010 }, |
|
2011 |
|
2012 "token" : { |
|
2013 "type" : SIMPLETYPE, |
|
2014 "basename" : "token", |
|
2015 "extract" : GetToken, |
|
2016 "facets" : STRING_FACETS, |
|
2017 "generate" : GenerateSimpleTypeXMLText(lambda x : x), |
|
2018 "initial" : lambda: "", |
|
2019 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2020 }, |
|
2021 |
|
2022 "base64Binary" : { |
|
2023 "type" : SIMPLETYPE, |
|
2024 "basename" : "base64Binary", |
|
2025 "extract" : NotSupportedYet("base64Binary"), |
|
2026 "facets" : STRING_FACETS, |
|
2027 "generate" : GenerateSimpleTypeXMLText(str), |
|
2028 "initial" : lambda: 0, |
|
2029 "check" : lambda x: isinstance(x, IntType) |
|
2030 }, |
|
2031 |
|
2032 "hexBinary" : { |
|
2033 "type" : SIMPLETYPE, |
|
2034 "basename" : "hexBinary", |
|
2035 "extract" : GetHexInteger, |
|
2036 "facets" : STRING_FACETS, |
|
2037 "generate" : GenerateSimpleTypeXMLText(lambda x : "%X"%x), |
|
2038 "initial" : lambda: 0, |
|
2039 "check" : lambda x: isinstance(x, IntType) |
|
2040 }, |
|
2041 |
|
2042 "integer" : { |
|
2043 "type" : SIMPLETYPE, |
|
2044 "basename" : "integer", |
|
2045 "extract" : GenerateIntegerExtraction(), |
|
2046 "facets" : DECIMAL_FACETS, |
|
2047 "generate" : GenerateSimpleTypeXMLText(str), |
|
2048 "initial" : lambda: 0, |
|
2049 "check" : lambda x: isinstance(x, IntType) |
|
2050 }, |
|
2051 |
|
2052 "positiveInteger" : { |
|
2053 "type" : SIMPLETYPE, |
|
2054 "basename" : "positiveInteger", |
|
2055 "extract" : GenerateIntegerExtraction(minExclusive=0), |
|
2056 "facets" : DECIMAL_FACETS, |
|
2057 "generate" : GenerateSimpleTypeXMLText(str), |
|
2058 "initial" : lambda: 0, |
|
2059 "check" : lambda x: isinstance(x, IntType) |
|
2060 }, |
|
2061 |
|
2062 "negativeInteger" : { |
|
2063 "type" : SIMPLETYPE, |
|
2064 "basename" : "negativeInteger", |
|
2065 "extract" : GenerateIntegerExtraction(maxExclusive=0), |
|
2066 "facets" : DECIMAL_FACETS, |
|
2067 "generate" : GenerateSimpleTypeXMLText(str), |
|
2068 "initial" : lambda: 0, |
|
2069 "check" : lambda x: isinstance(x, IntType) |
|
2070 }, |
|
2071 |
|
2072 "nonNegativeInteger" : { |
|
2073 "type" : SIMPLETYPE, |
|
2074 "basename" : "nonNegativeInteger", |
|
2075 "extract" : GenerateIntegerExtraction(minInclusive=0), |
|
2076 "facets" : DECIMAL_FACETS, |
|
2077 "generate" : GenerateSimpleTypeXMLText(str), |
|
2078 "initial" : lambda: 0, |
|
2079 "check" : lambda x: isinstance(x, IntType) |
|
2080 }, |
|
2081 |
|
2082 "nonPositiveInteger" : { |
|
2083 "type" : SIMPLETYPE, |
|
2084 "basename" : "nonPositiveInteger", |
|
2085 "extract" : GenerateIntegerExtraction(maxInclusive=0), |
|
2086 "facets" : DECIMAL_FACETS, |
|
2087 "generate" : GenerateSimpleTypeXMLText(str), |
|
2088 "initial" : lambda: 0, |
|
2089 "check" : lambda x: isinstance(x, IntType) |
|
2090 }, |
|
2091 |
|
2092 "long" : { |
|
2093 "type" : SIMPLETYPE, |
|
2094 "basename" : "long", |
|
2095 "extract" : GenerateIntegerExtraction(minInclusive=-2**63,maxExclusive=2**63), |
|
2096 "facets" : DECIMAL_FACETS, |
|
2097 "generate" : GenerateSimpleTypeXMLText(str), |
|
2098 "initial" : lambda: 0, |
|
2099 "check" : lambda x: isinstance(x, IntType) |
|
2100 }, |
|
2101 |
|
2102 "unsignedLong" : { |
|
2103 "type" : SIMPLETYPE, |
|
2104 "basename" : "unsignedLong", |
|
2105 "extract" : GenerateIntegerExtraction(minInclusive=0,maxExclusive=2**64), |
|
2106 "facets" : DECIMAL_FACETS, |
|
2107 "generate" : GenerateSimpleTypeXMLText(str), |
|
2108 "initial" : lambda: 0, |
|
2109 "check" : lambda x: isinstance(x, IntType) |
|
2110 }, |
|
2111 |
|
2112 "int" : { |
|
2113 "type" : SIMPLETYPE, |
|
2114 "basename" : "int", |
|
2115 "extract" : GenerateIntegerExtraction(minInclusive=-2**31,maxExclusive=2**31), |
|
2116 "facets" : DECIMAL_FACETS, |
|
2117 "generate" : GenerateSimpleTypeXMLText(str), |
|
2118 "initial" : lambda: 0, |
|
2119 "check" : lambda x: isinstance(x, IntType) |
|
2120 }, |
|
2121 |
|
2122 "unsignedInt" : { |
|
2123 "type" : SIMPLETYPE, |
|
2124 "basename" : "unsignedInt", |
|
2125 "extract" : GenerateIntegerExtraction(minInclusive=0,maxExclusive=2**32), |
|
2126 "facets" : DECIMAL_FACETS, |
|
2127 "generate" : GenerateSimpleTypeXMLText(str), |
|
2128 "initial" : lambda: 0, |
|
2129 "check" : lambda x: isinstance(x, IntType) |
|
2130 }, |
|
2131 |
|
2132 "short" : { |
|
2133 "type" : SIMPLETYPE, |
|
2134 "basename" : "short", |
|
2135 "extract" : GenerateIntegerExtraction(minInclusive=-2**15,maxExclusive=2**15), |
|
2136 "facets" : DECIMAL_FACETS, |
|
2137 "generate" : GenerateSimpleTypeXMLText(str), |
|
2138 "initial" : lambda: 0, |
|
2139 "check" : lambda x: isinstance(x, IntType) |
|
2140 }, |
|
2141 |
|
2142 "unsignedShort" : { |
|
2143 "type" : SIMPLETYPE, |
|
2144 "basename" : "unsignedShort", |
|
2145 "extract" : GenerateIntegerExtraction(minInclusive=0,maxExclusive=2**16), |
|
2146 "facets" : DECIMAL_FACETS, |
|
2147 "generate" : GenerateSimpleTypeXMLText(str), |
|
2148 "initial" : lambda: 0, |
|
2149 "check" : lambda x: isinstance(x, IntType) |
|
2150 }, |
|
2151 |
|
2152 "byte" : { |
|
2153 "type" : SIMPLETYPE, |
|
2154 "basename" : "byte", |
|
2155 "extract" : GenerateIntegerExtraction(minInclusive=-2**7,maxExclusive=2**7), |
|
2156 "facets" : DECIMAL_FACETS, |
|
2157 "generate" : GenerateSimpleTypeXMLText(str), |
|
2158 "initial" : lambda: 0, |
|
2159 "check" : lambda x: isinstance(x, IntType) |
|
2160 }, |
|
2161 |
|
2162 "unsignedByte" : { |
|
2163 "type" : SIMPLETYPE, |
|
2164 "basename" : "unsignedByte", |
|
2165 "extract" : GenerateIntegerExtraction(minInclusive=0,maxExclusive=2**8), |
|
2166 "facets" : DECIMAL_FACETS, |
|
2167 "generate" : GenerateSimpleTypeXMLText(str), |
|
2168 "initial" : lambda: 0, |
|
2169 "check" : lambda x: isinstance(x, IntType) |
|
2170 }, |
|
2171 |
|
2172 "decimal" : { |
|
2173 "type" : SIMPLETYPE, |
|
2174 "basename" : "decimal", |
|
2175 "extract" : GenerateFloatExtraction("decimal"), |
|
2176 "facets" : DECIMAL_FACETS, |
|
2177 "generate" : GenerateFloatXMLText(), |
|
2178 "initial" : lambda: 0., |
|
2179 "check" : lambda x: isinstance(x, (IntType, FloatType)) |
|
2180 }, |
|
2181 |
|
2182 "float" : { |
|
2183 "type" : SIMPLETYPE, |
|
2184 "basename" : "float", |
|
2185 "extract" : GenerateFloatExtraction("float", ["INF", "-INF", "NaN"]), |
|
2186 "facets" : NUMBER_FACETS, |
|
2187 "generate" : GenerateFloatXMLText(["INF", "-INF", "NaN"]), |
|
2188 "initial" : lambda: 0., |
|
2189 "check" : lambda x: {"INF" : True, "-INF" : True, "NaN" : True}.get(x, isinstance(x, (IntType, FloatType))) |
|
2190 }, |
|
2191 |
|
2192 "double" : { |
|
2193 "type" : SIMPLETYPE, |
|
2194 "basename" : "double", |
|
2195 "extract" : GenerateFloatExtraction("double", ["INF", "-INF", "NaN"]), |
|
2196 "facets" : NUMBER_FACETS, |
|
2197 "generate" : GenerateFloatXMLText(["INF", "-INF", "NaN"]), |
|
2198 "initial" : lambda: 0., |
|
2199 "check" : lambda x: {"INF" : True, "-INF" : True, "NaN" : True}.get(x, isinstance(x, (IntType, FloatType))) |
|
2200 }, |
|
2201 |
|
2202 "boolean" : { |
|
2203 "type" : SIMPLETYPE, |
|
2204 "basename" : "boolean", |
|
2205 "extract" : GetBoolean, |
|
2206 "facets" : ["pattern", "whiteSpace"], |
|
2207 "generate" : GenerateSimpleTypeXMLText(lambda x:{True : "true", False : "false"}[x]), |
|
2208 "initial" : lambda: False, |
|
2209 "check" : lambda x: isinstance(x, BooleanType) |
|
2210 }, |
|
2211 |
|
2212 "duration" : { |
|
2213 "type" : SIMPLETYPE, |
|
2214 "basename" : "duration", |
|
2215 "extract" : NotSupportedYet("duration"), |
|
2216 "facets" : NUMBER_FACETS, |
|
2217 "generate" : GenerateSimpleTypeXMLText(str), |
|
2218 "initial" : lambda: "", |
|
2219 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2220 }, |
|
2221 |
|
2222 "dateTime" : { |
|
2223 "type" : SIMPLETYPE, |
|
2224 "basename" : "dateTime", |
|
2225 "extract" : GetDateTime, |
|
2226 "facets" : NUMBER_FACETS, |
|
2227 "generate" : GenerateSimpleTypeXMLText(str), |
|
2228 "initial" : lambda: datetime.datetime(1,1,1,0,0,0,0), |
|
2229 "check" : lambda x: isinstance(x, datetime.datetime) |
|
2230 }, |
|
2231 |
|
2232 "date" : { |
|
2233 "type" : SIMPLETYPE, |
|
2234 "basename" : "date", |
|
2235 "extract" : GetDate, |
|
2236 "facets" : NUMBER_FACETS, |
|
2237 "generate" : GenerateSimpleTypeXMLText(str), |
|
2238 "initial" : lambda: datetime.date(1,1,1), |
|
2239 "check" : lambda x: isinstance(x, datetime.date) |
|
2240 }, |
|
2241 |
|
2242 "time" : { |
|
2243 "type" : SIMPLETYPE, |
|
2244 "basename" : "time", |
|
2245 "extract" : GetTime, |
|
2246 "facets" : NUMBER_FACETS, |
|
2247 "generate" : GenerateSimpleTypeXMLText(str), |
|
2248 "initial" : lambda: datetime.time(0,0,0,0), |
|
2249 "check" : lambda x: isinstance(x, datetime.time) |
|
2250 }, |
|
2251 |
|
2252 "gYear" : { |
|
2253 "type" : SIMPLETYPE, |
|
2254 "basename" : "gYear", |
|
2255 "extract" : NotSupportedYet("gYear"), |
|
2256 "facets" : NUMBER_FACETS, |
|
2257 "generate" : GenerateSimpleTypeXMLText(str), |
|
2258 "initial" : lambda: "", |
|
2259 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2260 }, |
|
2261 |
|
2262 "gYearMonth" : { |
|
2263 "type" : SIMPLETYPE, |
|
2264 "basename" : "gYearMonth", |
|
2265 "extract" : NotSupportedYet("gYearMonth"), |
|
2266 "facets" : NUMBER_FACETS, |
|
2267 "generate" : GenerateSimpleTypeXMLText(str), |
|
2268 "initial" : lambda: "", |
|
2269 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2270 }, |
|
2271 |
|
2272 "gMonth" : { |
|
2273 "type" : SIMPLETYPE, |
|
2274 "basename" : "gMonth", |
|
2275 "extract" : NotSupportedYet("gMonth"), |
|
2276 "facets" : NUMBER_FACETS, |
|
2277 "generate" : GenerateSimpleTypeXMLText(str), |
|
2278 "initial" : lambda: "", |
|
2279 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2280 }, |
|
2281 |
|
2282 "gMonthDay" : { |
|
2283 "type" : SIMPLETYPE, |
|
2284 "basename" : "gMonthDay", |
|
2285 "extract" : NotSupportedYet("gMonthDay"), |
|
2286 "facets" : NUMBER_FACETS, |
|
2287 "generate" : GenerateSimpleTypeXMLText(str), |
|
2288 "initial" : lambda: "", |
|
2289 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2290 }, |
|
2291 |
|
2292 "gDay" : { |
|
2293 "type" : SIMPLETYPE, |
|
2294 "basename" : "gDay", |
|
2295 "extract" : NotSupportedYet("gDay"), |
|
2296 "facets" : NUMBER_FACETS, |
|
2297 "generate" : GenerateSimpleTypeXMLText(str), |
|
2298 "initial" : lambda: "", |
|
2299 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2300 }, |
|
2301 |
|
2302 "Name" : { |
|
2303 "type" : SIMPLETYPE, |
|
2304 "basename" : "Name", |
|
2305 "extract" : GenerateModelNameExtraction("Name", Name_model), |
|
2306 "facets" : STRING_FACETS, |
|
2307 "generate" : GenerateSimpleTypeXMLText(lambda x: x), |
|
2308 "initial" : lambda: "", |
|
2309 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2310 }, |
|
2311 |
|
2312 "QName" : { |
|
2313 "type" : SIMPLETYPE, |
|
2314 "basename" : "QName", |
|
2315 "extract" : GenerateModelNameExtraction("QName", QName_model), |
|
2316 "facets" : STRING_FACETS, |
|
2317 "generate" : GenerateSimpleTypeXMLText(lambda x: x), |
|
2318 "initial" : lambda: "", |
|
2319 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2320 }, |
|
2321 |
|
2322 "NCName" : { |
|
2323 "type" : SIMPLETYPE, |
|
2324 "basename" : "NCName", |
|
2325 "extract" : GenerateModelNameExtraction("NCName", NCName_model), |
|
2326 "facets" : STRING_FACETS, |
|
2327 "generate" : GenerateSimpleTypeXMLText(lambda x: x), |
|
2328 "initial" : lambda: "", |
|
2329 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2330 }, |
|
2331 |
|
2332 "anyURI" : { |
|
2333 "type" : SIMPLETYPE, |
|
2334 "basename" : "anyURI", |
|
2335 "extract" : GenerateModelNameExtraction("anyURI", URI_model), |
|
2336 "facets" : STRING_FACETS, |
|
2337 "generate" : GenerateSimpleTypeXMLText(lambda x: x), |
|
2338 "initial" : lambda: "", |
|
2339 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2340 }, |
|
2341 |
|
2342 "language" : { |
|
2343 "type" : SIMPLETYPE, |
|
2344 "basename" : "language", |
|
2345 "extract" : GenerateEnumeratedExtraction("language", LANGUAGES), |
|
2346 "facets" : STRING_FACETS, |
|
2347 "generate" : GenerateSimpleTypeXMLText(lambda x: x), |
|
2348 "initial" : lambda: "en", |
|
2349 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2350 }, |
|
2351 |
|
2352 "ID" : { |
|
2353 "type" : SIMPLETYPE, |
|
2354 "basename" : "ID", |
|
2355 "extract" : GenerateModelNameExtraction("ID", Name_model), |
|
2356 "facets" : STRING_FACETS, |
|
2357 "generate" : GenerateSimpleTypeXMLText(lambda x: x), |
|
2358 "initial" : lambda: "", |
|
2359 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2360 }, |
|
2361 |
|
2362 "IDREF" : { |
|
2363 "type" : SIMPLETYPE, |
|
2364 "basename" : "IDREF", |
|
2365 "extract" : GenerateModelNameExtraction("IDREF", Name_model), |
|
2366 "facets" : STRING_FACETS, |
|
2367 "generate" : GenerateSimpleTypeXMLText(lambda x: x), |
|
2368 "initial" : lambda: "", |
|
2369 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2370 }, |
|
2371 |
|
2372 "IDREFS" : { |
|
2373 "type" : SIMPLETYPE, |
|
2374 "basename" : "IDREFS", |
|
2375 "extract" : GenerateModelNameExtraction("IDREFS", Names_model), |
|
2376 "facets" : STRING_FACETS, |
|
2377 "generate" : GenerateSimpleTypeXMLText(lambda x: x), |
|
2378 "initial" : lambda: "", |
|
2379 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2380 }, |
|
2381 |
|
2382 "ENTITY" : { |
|
2383 "type" : SIMPLETYPE, |
|
2384 "basename" : "ENTITY", |
|
2385 "extract" : GenerateModelNameExtraction("ENTITY", Name_model), |
|
2386 "facets" : STRING_FACETS, |
|
2387 "generate" : GenerateSimpleTypeXMLText(lambda x: x), |
|
2388 "initial" : lambda: "", |
|
2389 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2390 }, |
|
2391 |
|
2392 "ENTITIES" : { |
|
2393 "type" : SIMPLETYPE, |
|
2394 "basename" : "ENTITIES", |
|
2395 "extract" : GenerateModelNameExtraction("ENTITIES", Names_model), |
|
2396 "facets" : STRING_FACETS, |
|
2397 "generate" : GenerateSimpleTypeXMLText(lambda x: x), |
|
2398 "initial" : lambda: "", |
|
2399 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2400 }, |
|
2401 |
|
2402 "NOTATION" : { |
|
2403 "type" : SIMPLETYPE, |
|
2404 "basename" : "NOTATION", |
|
2405 "extract" : GenerateModelNameExtraction("NOTATION", Name_model), |
|
2406 "facets" : STRING_FACETS, |
|
2407 "generate" : GenerateSimpleTypeXMLText(lambda x: x), |
|
2408 "initial" : lambda: "", |
|
2409 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2410 }, |
|
2411 |
|
2412 "NMTOKEN" : { |
|
2413 "type" : SIMPLETYPE, |
|
2414 "basename" : "NMTOKEN", |
|
2415 "extract" : GenerateModelNameExtraction("NMTOKEN", NMToken_model), |
|
2416 "facets" : STRING_FACETS, |
|
2417 "generate" : GenerateSimpleTypeXMLText(lambda x: x), |
|
2418 "initial" : lambda: "", |
|
2419 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2420 }, |
|
2421 |
|
2422 "NMTOKENS" : { |
|
2423 "type" : SIMPLETYPE, |
|
2424 "basename" : "NMTOKENS", |
|
2425 "extract" : GenerateModelNameExtraction("NMTOKENS", NMTokens_model), |
|
2426 "facets" : STRING_FACETS, |
|
2427 "generate" : GenerateSimpleTypeXMLText(lambda x: x), |
|
2428 "initial" : lambda: "", |
|
2429 "check" : lambda x: isinstance(x, (StringType, UnicodeType)) |
|
2430 }, |
|
2431 |
|
2432 # Complex Types |
|
2433 "anyType" : {"type" : COMPLEXTYPE, "extract" : lambda x:None}, |
|
2434 } |
|
2435 |
|
2436 if __name__ == '__main__': |
|
2437 classes = GenerateClassesFromXSD("test.xsd") |
|
2438 |
|
2439 # Code for test of test.xsd |
|
2440 xmlfile = open("po.xml", 'r') |
|
2441 tree = minidom.parse(xmlfile) |
|
2442 xmlfile.close() |
|
2443 test = classes["PurchaseOrderType"]() |
|
2444 for child in tree.childNodes: |
|
2445 if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "purchaseOrder": |
|
2446 test.loadXMLTree(child) |
|
2447 test.items.item[0].setquantity(2) |
|
2448 testfile = open("test.xml", 'w') |
|
2449 testfile.write("<?xml version=\"1.0\"?>\n") |
|
2450 testfile.write(test.generateXMLText("purchaseOrder")) |
|
2451 testfile.close() |