xmlclass/xsdschema.py
changeset 1763 bcc07ff2362c
parent 1762 fcc406143e5b
child 1765 ccf59c1f0b45
equal deleted inserted replaced
1762:fcc406143e5b 1763:bcc07ff2362c
   105         elif child["type"] == "documentation":
   105         elif child["type"] == "documentation":
   106             if child["source"] is not None:
   106             if child["source"] is not None:
   107                 text = "(source: %(source)s):\n%(content)s\n\n" % child
   107                 text = "(source: %(source)s):\n%(content)s\n\n" % child
   108             else:
   108             else:
   109                 text = child["content"] + "\n\n"
   109                 text = child["content"] + "\n\n"
   110             if not annotation["documentation"].has_key(child["language"]):
   110             if not child["language"] in annotation["documentation"]:
   111                 annotation["documentation"] = text
   111                 annotation["documentation"] = text
   112             else:
   112             else:
   113                 annotation["documentation"] += text
   113                 annotation["documentation"] += text
   114     return annotation
   114     return annotation
   115 
   115 
   117 
   117 
   118 
   118 
   119 def GenerateFacetReducing(facetname, canbefixed):
   119 def GenerateFacetReducing(facetname, canbefixed):
   120     def ReduceFacet(factory, attributes, elements):
   120     def ReduceFacet(factory, attributes, elements):
   121         annotations, children = factory.ReduceElements(elements)
   121         annotations, children = factory.ReduceElements(elements)
   122         if attributes.has_key("value"):
   122         if "value" in attributes:
   123             facet = {"type": facetname, "value": attributes["value"], "doc": annotations}
   123             facet = {"type": facetname, "value": attributes["value"], "doc": annotations}
   124             if canbefixed:
   124             if canbefixed:
   125                 facet["fixed"] = attributes.get("fixed", False)
   125                 facet["fixed"] = attributes.get("fixed", False)
   126             return facet
   126             return facet
   127         raise ValueError("A value must be defined for the \"%s\" facet!" % facetname)
   127         raise ValueError("A value must be defined for the \"%s\" facet!" % facetname)
   156 
   156 
   157 def CreateSimpleType(factory, attributes, typeinfos):
   157 def CreateSimpleType(factory, attributes, typeinfos):
   158     # Initialize type informations
   158     # Initialize type informations
   159     facets = {}
   159     facets = {}
   160     simpleType = {"type": SIMPLETYPE, "final": attributes.get("final", [])}
   160     simpleType = {"type": SIMPLETYPE, "final": attributes.get("final", [])}
   161     if attributes.has_key("name"):
   161     if "name" in attributes:
   162         simpleType["name"] = attributes["name"]
   162         simpleType["name"] = attributes["name"]
   163 
   163 
   164     if typeinfos["type"] in ["restriction", "extension"]:
   164     if typeinfos["type"] in ["restriction", "extension"]:
   165         # Search for base type definition
   165         # Search for base type definition
   166         if isinstance(typeinfos["base"], (StringType, UnicodeType)):
   166         if isinstance(typeinfos["base"], (StringType, UnicodeType)):
   175             raise ValueError("Base type given isn't a simpleType!")
   175             raise ValueError("Base type given isn't a simpleType!")
   176 
   176 
   177         simpleType["basename"] = basetypeinfos["basename"]
   177         simpleType["basename"] = basetypeinfos["basename"]
   178 
   178 
   179         # Check that derivation is allowed
   179         # Check that derivation is allowed
   180         if basetypeinfos.has_key("final"):
   180         if "final" in basetypeinfos:
   181             if "#all" in basetypeinfos["final"]:
   181             if "#all" in basetypeinfos["final"]:
   182                 raise ValueError("Base type can't be derivated!")
   182                 raise ValueError("Base type can't be derivated!")
   183             if "restriction" in basetypeinfos["final"] and typeinfos["type"] == "restriction":
   183             if "restriction" in basetypeinfos["final"] and typeinfos["type"] == "restriction":
   184                 raise ValueError("Base type can't be derivated by restriction!")
   184                 raise ValueError("Base type can't be derivated by restriction!")
   185 
   185 
   186         # Extract simple type facets
   186         # Extract simple type facets
   187         for facet in typeinfos.get("facets", []):
   187         for facet in typeinfos.get("facets", []):
   188             facettype = facet["type"]
   188             facettype = facet["type"]
   189             if not basetypeinfos["facets"].has_key(facettype):
   189             if not facettype in basetypeinfos["facets"]:
   190                 raise ValueError("\"%s\" facet can't be defined for \"%s\" type!" % (facettype, type))
   190                 raise ValueError("\"%s\" facet can't be defined for \"%s\" type!" % (facettype, type))
   191             elif basetypeinfos["facets"][facettype][1]:
   191             elif basetypeinfos["facets"][facettype][1]:
   192                 raise ValueError("\"%s\" facet is fixed on base type!" % facettype)
   192                 raise ValueError("\"%s\" facet is fixed on base type!" % facettype)
   193             value = facet["value"]
   193             value = facet["value"]
   194             basevalue = basetypeinfos["facets"][facettype][0]
   194             basevalue = basetypeinfos["facets"][facettype][0]
   200                 elif facets.keys() == [facettype]:
   200                 elif facets.keys() == [facettype]:
   201                     facets[facettype][0].append(value)
   201                     facets[facettype][0].append(value)
   202                     continue
   202                     continue
   203                 else:
   203                 else:
   204                     raise ValueError("\"%s\" facet can't be defined with another facet type!" % facettype)
   204                     raise ValueError("\"%s\" facet can't be defined with another facet type!" % facettype)
   205             elif facets.has_key("enumeration"):
   205             elif "enumeration" in facets:
   206                 raise ValueError("\"enumeration\" facet can't be defined with another facet type!")
   206                 raise ValueError("\"enumeration\" facet can't be defined with another facet type!")
   207             elif facets.has_key("pattern"):
   207             elif "pattern" in facets:
   208                 raise ValueError("\"pattern\" facet can't be defined with another facet type!")
   208                 raise ValueError("\"pattern\" facet can't be defined with another facet type!")
   209             elif facets.has_key(facettype):
   209             elif facettype in facets:
   210                 raise ValueError("\"%s\" facet can't be defined two times!" % facettype)
   210                 raise ValueError("\"%s\" facet can't be defined two times!" % facettype)
   211             elif facettype == "length":
   211             elif facettype == "length":
   212                 if facets.has_key("minLength"):
   212                 if "minLength" in facets:
   213                     raise ValueError("\"length\" and \"minLength\" facets can't be defined at the same time!")
   213                     raise ValueError("\"length\" and \"minLength\" facets can't be defined at the same time!")
   214                 if facets.has_key("maxLength"):
   214                 if "maxLength" in facets:
   215                     raise ValueError("\"length\" and \"maxLength\" facets can't be defined at the same time!")
   215                     raise ValueError("\"length\" and \"maxLength\" facets can't be defined at the same time!")
   216                 try:
   216                 try:
   217                     value = int(value)
   217                     value = int(value)
   218                 except:
   218                 except:
   219                     raise ValueError("\"length\" must be an integer!")
   219                     raise ValueError("\"length\" must be an integer!")
   220                 if value < 0:
   220                 if value < 0:
   221                     raise ValueError("\"length\" can't be negative!")
   221                     raise ValueError("\"length\" can't be negative!")
   222                 elif basevalue is not None and basevalue != value:
   222                 elif basevalue is not None and basevalue != value:
   223                     raise ValueError("\"length\" can't be different from \"length\" defined in base type!")
   223                     raise ValueError("\"length\" can't be different from \"length\" defined in base type!")
   224             elif facettype == "minLength":
   224             elif facettype == "minLength":
   225                 if facets.has_key("length"):
   225                 if "length" in facets:
   226                     raise ValueError("\"length\" and \"minLength\" facets can't be defined at the same time!")
   226                     raise ValueError("\"length\" and \"minLength\" facets can't be defined at the same time!")
   227                 try:
   227                 try:
   228                     value = int(value)
   228                     value = int(value)
   229                 except:
   229                 except:
   230                     raise ValueError("\"minLength\" must be an integer!")
   230                     raise ValueError("\"minLength\" must be an integer!")
   231                 if value < 0:
   231                 if value < 0:
   232                     raise ValueError("\"minLength\" can't be negative!")
   232                     raise ValueError("\"minLength\" can't be negative!")
   233                 elif facets.has_key("maxLength") and value > facets["maxLength"]:
   233                 elif "maxLength" in facets and value > facets["maxLength"]:
   234                     raise ValueError("\"minLength\" must be lesser than or equal to \"maxLength\"!")
   234                     raise ValueError("\"minLength\" must be lesser than or equal to \"maxLength\"!")
   235                 elif basevalue is not None and basevalue < value:
   235                 elif basevalue is not None and basevalue < value:
   236                     raise ValueError("\"minLength\" can't be lesser than \"minLength\" defined in base type!")
   236                     raise ValueError("\"minLength\" can't be lesser than \"minLength\" defined in base type!")
   237             elif facettype == "maxLength":
   237             elif facettype == "maxLength":
   238                 if facets.has_key("length"):
   238                 if "length" in facets:
   239                     raise ValueError("\"length\" and \"maxLength\" facets can't be defined at the same time!")
   239                     raise ValueError("\"length\" and \"maxLength\" facets can't be defined at the same time!")
   240                 try:
   240                 try:
   241                     value = int(value)
   241                     value = int(value)
   242                 except:
   242                 except:
   243                     raise ValueError("\"maxLength\" must be an integer!")
   243                     raise ValueError("\"maxLength\" must be an integer!")
   244                 if value < 0:
   244                 if value < 0:
   245                     raise ValueError("\"maxLength\" can't be negative!")
   245                     raise ValueError("\"maxLength\" can't be negative!")
   246                 elif facets.has_key("minLength") and value < facets["minLength"]:
   246                 elif "minLength" in facets and value < facets["minLength"]:
   247                     raise ValueError("\"minLength\" must be lesser than or equal to \"maxLength\"!")
   247                     raise ValueError("\"minLength\" must be lesser than or equal to \"maxLength\"!")
   248                 elif basevalue is not None and basevalue > value:
   248                 elif basevalue is not None and basevalue > value:
   249                     raise ValueError("\"maxLength\" can't be greater than \"maxLength\" defined in base type!")
   249                     raise ValueError("\"maxLength\" can't be greater than \"maxLength\" defined in base type!")
   250             elif facettype == "minInclusive":
   250             elif facettype == "minInclusive":
   251                 if facets.has_key("minExclusive"):
   251                 if "minExclusive" in facets:
   252                     raise ValueError("\"minExclusive\" and \"minInclusive\" facets can't be defined at the same time!")
   252                     raise ValueError("\"minExclusive\" and \"minInclusive\" facets can't be defined at the same time!")
   253                 value = basetypeinfos["extract"](facet["value"], False)
   253                 value = basetypeinfos["extract"](facet["value"], False)
   254                 if facets.has_key("maxInclusive") and value > facets["maxInclusive"][0]:
   254                 if "maxInclusive" in facets and value > facets["maxInclusive"][0]:
   255                     raise ValueError("\"minInclusive\" must be lesser than or equal to \"maxInclusive\"!")
   255                     raise ValueError("\"minInclusive\" must be lesser than or equal to \"maxInclusive\"!")
   256                 elif facets.has_key("maxExclusive") and value >= facets["maxExclusive"][0]:
   256                 elif "maxExclusive" in facets and value >= facets["maxExclusive"][0]:
   257                     raise ValueError("\"minInclusive\" must be lesser than \"maxExclusive\"!")
   257                     raise ValueError("\"minInclusive\" must be lesser than \"maxExclusive\"!")
   258             elif facettype == "minExclusive":
   258             elif facettype == "minExclusive":
   259                 if facets.has_key("minInclusive"):
   259                 if "minInclusive" in facets:
   260                     raise ValueError("\"minExclusive\" and \"minInclusive\" facets can't be defined at the same time!")
   260                     raise ValueError("\"minExclusive\" and \"minInclusive\" facets can't be defined at the same time!")
   261                 value = basetypeinfos["extract"](facet["value"], False)
   261                 value = basetypeinfos["extract"](facet["value"], False)
   262                 if facets.has_key("maxInclusive") and value >= facets["maxInclusive"][0]:
   262                 if "maxInclusive" in facets and value >= facets["maxInclusive"][0]:
   263                     raise ValueError("\"minExclusive\" must be lesser than \"maxInclusive\"!")
   263                     raise ValueError("\"minExclusive\" must be lesser than \"maxInclusive\"!")
   264                 elif facets.has_key("maxExclusive") and value >= facets["maxExclusive"][0]:
   264                 elif "maxExclusive" in facets and value >= facets["maxExclusive"][0]:
   265                     raise ValueError("\"minExclusive\" must be lesser than \"maxExclusive\"!")
   265                     raise ValueError("\"minExclusive\" must be lesser than \"maxExclusive\"!")
   266             elif facettype == "maxInclusive":
   266             elif facettype == "maxInclusive":
   267                 if facets.has_key("maxExclusive"):
   267                 if "maxExclusive" in facets:
   268                     raise ValueError("\"maxExclusive\" and \"maxInclusive\" facets can't be defined at the same time!")
   268                     raise ValueError("\"maxExclusive\" and \"maxInclusive\" facets can't be defined at the same time!")
   269                 value = basetypeinfos["extract"](facet["value"], False)
   269                 value = basetypeinfos["extract"](facet["value"], False)
   270                 if facets.has_key("minInclusive") and value < facets["minInclusive"][0]:
   270                 if "minInclusive" in facets and value < facets["minInclusive"][0]:
   271                     raise ValueError("\"minInclusive\" must be lesser than or equal to \"maxInclusive\"!")
   271                     raise ValueError("\"minInclusive\" must be lesser than or equal to \"maxInclusive\"!")
   272                 elif facets.has_key("minExclusive") and value <= facets["minExclusive"][0]:
   272                 elif "minExclusive" in facets and value <= facets["minExclusive"][0]:
   273                     raise ValueError("\"minExclusive\" must be lesser than \"maxInclusive\"!")
   273                     raise ValueError("\"minExclusive\" must be lesser than \"maxInclusive\"!")
   274             elif facettype == "maxExclusive":
   274             elif facettype == "maxExclusive":
   275                 if facets.has_key("maxInclusive"):
   275                 if "maxInclusive" in facets:
   276                     raise ValueError("\"maxExclusive\" and \"maxInclusive\" facets can't be defined at the same time!")
   276                     raise ValueError("\"maxExclusive\" and \"maxInclusive\" facets can't be defined at the same time!")
   277                 value = basetypeinfos["extract"](facet["value"], False)
   277                 value = basetypeinfos["extract"](facet["value"], False)
   278                 if facets.has_key("minInclusive") and value <= facets["minInclusive"][0]:
   278                 if "minInclusive" in facets and value <= facets["minInclusive"][0]:
   279                     raise ValueError("\"minInclusive\" must be lesser than \"maxExclusive\"!")
   279                     raise ValueError("\"minInclusive\" must be lesser than \"maxExclusive\"!")
   280                 elif facets.has_key("minExclusive") and value <= facets["minExclusive"][0]:
   280                 elif "minExclusive" in facets and value <= facets["minExclusive"][0]:
   281                     raise ValueError("\"minExclusive\" must be lesser than \"maxExclusive\"!")
   281                     raise ValueError("\"minExclusive\" must be lesser than \"maxExclusive\"!")
   282             elif facettype == "whiteSpace":
   282             elif facettype == "whiteSpace":
   283                 if basevalue == "collapse" and value in ["preserve", "replace"] or basevalue == "replace" and value == "preserve":
   283                 if basevalue == "collapse" and value in ["preserve", "replace"] or basevalue == "replace" and value == "preserve":
   284                     raise ValueError("\"whiteSpace\" is incompatible with \"whiteSpace\" defined in base type!")
   284                     raise ValueError("\"whiteSpace\" is incompatible with \"whiteSpace\" defined in base type!")
   285             elif facettype == "totalDigits":
   285             elif facettype == "totalDigits":
   286                 if facets.has_key("fractionDigits") and value <= facets["fractionDigits"][0]:
   286                 if "fractionDigits" in facets and value <= facets["fractionDigits"][0]:
   287                     raise ValueError("\"fractionDigits\" must be lesser than or equal to \"totalDigits\"!")
   287                     raise ValueError("\"fractionDigits\" must be lesser than or equal to \"totalDigits\"!")
   288                 elif basevalue is not None and value > basevalue:
   288                 elif basevalue is not None and value > basevalue:
   289                     raise ValueError("\"totalDigits\" can't be greater than \"totalDigits\" defined in base type!")
   289                     raise ValueError("\"totalDigits\" can't be greater than \"totalDigits\" defined in base type!")
   290             elif facettype == "fractionDigits":
   290             elif facettype == "fractionDigits":
   291                 if facets.has_key("totalDigits") and value <= facets["totalDigits"][0]:
   291                 if "totalDigits" in facets and value <= facets["totalDigits"][0]:
   292                     raise ValueError("\"fractionDigits\" must be lesser than or equal to \"totalDigits\"!")
   292                     raise ValueError("\"fractionDigits\" must be lesser than or equal to \"totalDigits\"!")
   293                 elif basevalue is not None and value > basevalue:
   293                 elif basevalue is not None and value > basevalue:
   294                     raise ValueError("\"totalDigits\" can't be greater than \"totalDigits\" defined in base type!")
   294                     raise ValueError("\"totalDigits\" can't be greater than \"totalDigits\" defined in base type!")
   295             facets[facettype] = (value, facet.get("fixed", False))
   295             facets[facettype] = (value, facet.get("fixed", False))
   296 
   296 
   297         # Report not redefined facet from base type to new created type
   297         # Report not redefined facet from base type to new created type
   298         for facettype, facetvalue in basetypeinfos["facets"].items():
   298         for facettype, facetvalue in basetypeinfos["facets"].items():
   299             if not facets.has_key(facettype):
   299             if not facettype in facets:
   300                 facets[facettype] = facetvalue
   300                 facets[facettype] = facetvalue
   301 
   301 
   302         # Generate extract value for new created type
   302         # Generate extract value for new created type
   303         def ExtractSimpleTypeValue(attr, extract=True):
   303         def ExtractSimpleTypeValue(attr, extract=True):
   304             value = basetypeinfos["extract"](attr, extract)
   304             value = basetypeinfos["extract"](attr, extract)
   399             raise ValueError, "Item type given isn't a simpleType!"
   399             raise ValueError, "Item type given isn't a simpleType!"
   400 
   400 
   401         simpleType["basename"] = "list"
   401         simpleType["basename"] = "list"
   402 
   402 
   403         # Check that derivation is allowed
   403         # Check that derivation is allowed
   404         if itemtypeinfos.has_key("final"):
   404         if "final" in itemtypeinfos:
   405             if itemtypeinfos["final"].has_key("#all"):
   405             if "#all" in itemtypeinfos["final"]:
   406                 raise ValueError("Item type can't be derivated!")
   406                 raise ValueError("Item type can't be derivated!")
   407             if itemtypeinfos["final"].has_key("list"):
   407             if "list" in itemtypeinfos["final"]:
   408                 raise ValueError("Item type can't be derivated by list!")
   408                 raise ValueError("Item type can't be derivated by list!")
   409 
   409 
   410         # Generate extract value for new created type
   410         # Generate extract value for new created type
   411         def ExtractSimpleTypeValue(attr, extract=True):
   411         def ExtractSimpleTypeValue(attr, extract=True):
   412             values = []
   412             values = []
   443             # Check that member type is a simple type
   443             # Check that member type is a simple type
   444             if infos["type"] != SIMPLETYPE:
   444             if infos["type"] != SIMPLETYPE:
   445                 raise ValueError("Member type given isn't a simpleType!")
   445                 raise ValueError("Member type given isn't a simpleType!")
   446 
   446 
   447             # Check that derivation is allowed
   447             # Check that derivation is allowed
   448             if infos.has_key("final"):
   448             if "final" in infos:
   449                 if infos["final"].has_key("#all"):
   449                 if "#all" in infos["final"]:
   450                     raise ValueError("Item type can't be derivated!")
   450                     raise ValueError("Item type can't be derivated!")
   451                 if infos["final"].has_key("union"):
   451                 if "union" in infos["final"]:
   452                     raise ValueError("Member type can't be derivated by union!")
   452                     raise ValueError("Member type can't be derivated by union!")
   453 
   453 
   454             membertypesinfos.append(infos)
   454             membertypesinfos.append(infos)
   455 
   455 
   456         simpleType["basename"] = "union"
   456         simpleType["basename"] = "union"
   551     return restriction
   551     return restriction
   552 
   552 
   553 
   553 
   554 def ReduceExtension(factory, attributes, elements):
   554 def ReduceExtension(factory, attributes, elements):
   555     annotations, children = factory.ReduceElements(elements)
   555     annotations, children = factory.ReduceElements(elements)
   556     if not attributes.has_key("base"):
   556     if not "base" in attributes:
   557         raise ValueError("No base type has been defined for extension!")
   557         raise ValueError("No base type has been defined for extension!")
   558     extension = {"type": "extension", "attributes": [], "elements": [], "base": attributes["base"], "doc": annotations}
   558     extension = {"type": "extension", "attributes": [], "elements": [], "base": attributes["base"], "doc": annotations}
   559     if len(children) > 0:
   559     if len(children) > 0:
   560         if children[0]["type"] in ["group", "all", CHOICE, "sequence"]:
   560         if children[0]["type"] in ["group", "all", CHOICE, "sequence"]:
   561             group = children.pop(0)
   561             group = children.pop(0)
   566                 content = group.copy()
   566                 content = group.copy()
   567                 content["name"] = "content"
   567                 content["name"] = "content"
   568                 extension["elements"].append(content)
   568                 extension["elements"].append(content)
   569             elif group["type"] == "group":
   569             elif group["type"] == "group":
   570                 elmtgroup = factory.FindSchemaElement(child["ref"], ELEMENTSGROUP)
   570                 elmtgroup = factory.FindSchemaElement(child["ref"], ELEMENTSGROUP)
   571                 if elmtgroup.has_key("elements"):
   571                 if "elements" in elmtgroup:
   572                     extension["elements"] = elmtgroup["elements"]
   572                     extension["elements"] = elmtgroup["elements"]
   573                     extension["order"] = elmtgroup["order"]
   573                     extension["order"] = elmtgroup["order"]
   574                 else:
   574                 else:
   575                     content = elmtgroup.copy()
   575                     content = elmtgroup.copy()
   576                     content["name"] = "content"
   576                     content["name"] = "content"
   589         contenttypeinfos = simpleContent.copy()
   589         contenttypeinfos = simpleContent.copy()
   590         simpleContent.pop("base")
   590         simpleContent.pop("base")
   591     elif basetypeinfos["type"] == COMPLEXTYPE and \
   591     elif basetypeinfos["type"] == COMPLEXTYPE and \
   592          len(basetypeinfos["elements"]) == 1 and \
   592          len(basetypeinfos["elements"]) == 1 and \
   593          basetypeinfos["elements"][0]["name"] == "content" and \
   593          basetypeinfos["elements"][0]["name"] == "content" and \
   594          basetypeinfos["elements"][0].has_key("elmt_type") and \
   594          "elmt_type" in basetypeinfos["elements"][0] and \
   595          basetypeinfos["elements"][0]["elmt_type"]["type"] == SIMPLETYPE:
   595          basetypeinfos["elements"][0]["elmt_type"]["type"] == SIMPLETYPE:
   596         contenttypeinfos = simpleContent.copy()
   596         contenttypeinfos = simpleContent.copy()
   597         contenttypeinfos["base"] = basetypeinfos["elements"][0]["elmt_type"]
   597         contenttypeinfos["base"] = basetypeinfos["elements"][0]["elmt_type"]
   598     else:
   598     else:
   599         raise ValueError("No compatible base type defined for simpleContent!")
   599         raise ValueError("No compatible base type defined for simpleContent!")
   649                 content = group.copy()
   649                 content = group.copy()
   650                 content["name"] = "content"
   650                 content["name"] = "content"
   651                 complexType["elements"].append(content)
   651                 complexType["elements"].append(content)
   652             elif group["type"] == "group":
   652             elif group["type"] == "group":
   653                 elmtgroup = factory.FindSchemaElement(child["ref"], ELEMENTSGROUP)
   653                 elmtgroup = factory.FindSchemaElement(child["ref"], ELEMENTSGROUP)
   654                 if elmtgroup.has_key("elements"):
   654                 if "elements" in elmtgroup:
   655                     complexType["elements"] = elmtgroup["elements"]
   655                     complexType["elements"] = elmtgroup["elements"]
   656                     complexType["order"] = elmtgroup["order"]
   656                     complexType["order"] = elmtgroup["order"]
   657                 else:
   657                 else:
   658                     content = elmtgroup.copy()
   658                     content = elmtgroup.copy()
   659                     content["name"] = "content"
   659                     content["name"] = "content"
   675 
   675 
   676 
   676 
   677 def ReduceAttribute(factory, attributes, elements):
   677 def ReduceAttribute(factory, attributes, elements):
   678     annotations, children = factory.ReduceElements(elements)
   678     annotations, children = factory.ReduceElements(elements)
   679 
   679 
   680     if attributes.has_key("default"):
   680     if "default" in attributes:
   681         if attributes.has_key("fixed"):
   681         if "fixed" in attributes:
   682             raise ValueError("\"default\" and \"fixed\" can't be defined at the same time!")
   682             raise ValueError("\"default\" and \"fixed\" can't be defined at the same time!")
   683         elif attributes.get("use", "optional") != "optional":
   683         elif attributes.get("use", "optional") != "optional":
   684             raise ValueError("if \"default\" present, \"use\" can only have the value \"optional\"!")
   684             raise ValueError("if \"default\" present, \"use\" can only have the value \"optional\"!")
   685 
   685 
   686     attribute = {"type": ATTRIBUTE, "attr_type": attributes.get("type", None), "doc": annotations}
   686     attribute = {"type": ATTRIBUTE, "attr_type": attributes.get("type", None), "doc": annotations}
   688         if attribute["attr_type"] is None:
   688         if attribute["attr_type"] is None:
   689             attribute["attr_type"] = children[0]
   689             attribute["attr_type"] = children[0]
   690         else:
   690         else:
   691             raise ValueError("Only one type can be defined for attribute!")
   691             raise ValueError("Only one type can be defined for attribute!")
   692 
   692 
   693     if attributes.has_key("ref"):
   693     if "ref" in attributes:
   694         if attributes.has_key("name"):
   694         if "name" in attributes:
   695             raise ValueError("\"ref\" and \"name\" can't be defined at the same time!")
   695             raise ValueError("\"ref\" and \"name\" can't be defined at the same time!")
   696         elif attributes.has_key("form"):
   696         elif "form" in attributes:
   697             raise ValueError("\"ref\" and \"form\" can't be defined at the same time!")
   697             raise ValueError("\"ref\" and \"form\" can't be defined at the same time!")
   698         elif attribute["attr_type"] is not None:
   698         elif attribute["attr_type"] is not None:
   699             raise ValueError("if \"ref\" is present, no type can be defined!")
   699             raise ValueError("if \"ref\" is present, no type can be defined!")
   700     elif attribute["attr_type"] is None:
   700     elif attribute["attr_type"] is None:
   701         raise ValueError("No type has been defined for attribute \"%s\"!" % attributes["name"])
   701         raise ValueError("No type has been defined for attribute \"%s\"!" % attributes["name"])
   702 
   702 
   703     if attributes.has_key("type"):
   703     if "type" in attributes:
   704         tmp_attrs = attributes.copy()
   704         tmp_attrs = attributes.copy()
   705         tmp_attrs.pop("type")
   705         tmp_attrs.pop("type")
   706         attribute.update(tmp_attrs)
   706         attribute.update(tmp_attrs)
   707     else:
   707     else:
   708         attribute.update(attributes)
   708         attribute.update(attributes)
   709     return attribute
   709     return attribute
   710 
   710 
   711 
   711 
   712 def ReduceAttributeGroup(factory, attributes, elements):
   712 def ReduceAttributeGroup(factory, attributes, elements):
   713     annotations, children = factory.ReduceElements(elements)
   713     annotations, children = factory.ReduceElements(elements)
   714     if attributes.has_key("ref"):
   714     if "ref" in attributes:
   715         return {"type": "attributeGroup", "ref": attributes["ref"], "doc": annotations}
   715         return {"type": "attributeGroup", "ref": attributes["ref"], "doc": annotations}
   716     else:
   716     else:
   717         return {"type": ATTRIBUTESGROUP, "attributes": ExtractAttributes(factory, children), "doc": annotations}
   717         return {"type": ATTRIBUTESGROUP, "attributes": ExtractAttributes(factory, children), "doc": annotations}
   718 
   718 
   719 
   719 
   736         if child["type"] == CONSTRAINT:
   736         if child["type"] == CONSTRAINT:
   737             constraints.append(child)
   737             constraints.append(child)
   738         else:
   738         else:
   739             types.append(child)
   739             types.append(child)
   740 
   740 
   741     if attributes.has_key("default") and attributes.has_key("fixed"):
   741     if "default" in attributes and "fixed" in attributes:
   742         raise ValueError("\"default\" and \"fixed\" can't be defined at the same time!")
   742         raise ValueError("\"default\" and \"fixed\" can't be defined at the same time!")
   743 
   743 
   744     if attributes.has_key("ref"):
   744     if "ref" in attributes:
   745         for attr in ["name", "default", "fixed", "form", "block", "type"]:
   745         for attr in ["name", "default", "fixed", "form", "block", "type"]:
   746             if attributes.has_key(attr):
   746             if attr in attributes:
   747                 raise ValueError("\"ref\" and \"%s\" can't be defined at the same time!" % attr)
   747                 raise ValueError("\"ref\" and \"%s\" can't be defined at the same time!" % attr)
   748         if attributes.has_key("nillable"):
   748         if "nillable" in attributes:
   749             raise ValueError("\"ref\" and \"nillable\" can't be defined at the same time!")
   749             raise ValueError("\"ref\" and \"nillable\" can't be defined at the same time!")
   750         if len(types) > 0:
   750         if len(types) > 0:
   751             raise ValueError("No type and no constraints can be defined where \"ref\" is defined!")
   751             raise ValueError("No type and no constraints can be defined where \"ref\" is defined!")
   752 
   752 
   753         infos = factory.FindSchemaElement(attributes["ref"], ELEMENT)
   753         infos = factory.FindSchemaElement(attributes["ref"], ELEMENT)
   758             element["maxOccurs"] = attributes["maxOccurs"]
   758             element["maxOccurs"] = attributes["maxOccurs"]
   759             return element
   759             return element
   760         else:
   760         else:
   761             raise ValueError("\"%s\" base type isn't defined or circular referenced!" % name)
   761             raise ValueError("\"%s\" base type isn't defined or circular referenced!" % name)
   762 
   762 
   763     elif attributes.has_key("name"):
   763     elif "name" in attributes:
   764         element = {"type": ELEMENT, "elmt_type": attributes.get("type", None), "constraints": constraints, "doc": annotations}
   764         element = {"type": ELEMENT, "elmt_type": attributes.get("type", None), "constraints": constraints, "doc": annotations}
   765         if len(types) > 0:
   765         if len(types) > 0:
   766             if element["elmt_type"] is None:
   766             if element["elmt_type"] is None:
   767                 element["elmt_type"] = types[0]
   767                 element["elmt_type"] = types[0]
   768             else:
   768             else:
   769                 raise ValueError("Only one type can be defined for attribute!")
   769                 raise ValueError("Only one type can be defined for attribute!")
   770         elif element["elmt_type"] is None:
   770         elif element["elmt_type"] is None:
   771             element["elmt_type"] = "tag"
   771             element["elmt_type"] = "tag"
   772             element["type"] = TAG
   772             element["type"] = TAG
   773 
   773 
   774         if attributes.has_key("type"):
   774         if "type" in attributes:
   775             tmp_attrs = attributes.copy()
   775             tmp_attrs = attributes.copy()
   776             tmp_attrs.pop("type")
   776             tmp_attrs.pop("type")
   777             element.update(tmp_attrs)
   777             element.update(tmp_attrs)
   778         else:
   778         else:
   779             element.update(attributes)
   779             element.update(attributes)
   806             #raise ValueError("\"sequence\" in \"choice\" is not supported. Create instead a new complex type!")
   806             #raise ValueError("\"sequence\" in \"choice\" is not supported. Create instead a new complex type!")
   807         elif child["type"] == CHOICE:
   807         elif child["type"] == CHOICE:
   808             choices.extend(child["choices"])
   808             choices.extend(child["choices"])
   809         elif child["type"] == "group":
   809         elif child["type"] == "group":
   810             elmtgroup = factory.FindSchemaElement(child["ref"], ELEMENTSGROUP)
   810             elmtgroup = factory.FindSchemaElement(child["ref"], ELEMENTSGROUP)
   811             if not elmtgroup.has_key("choices"):
   811             if not "choices" in elmtgroup:
   812                 raise ValueError("Only group composed of \"choice\" can be referenced in \"choice\" element!")
   812                 raise ValueError("Only group composed of \"choice\" can be referenced in \"choice\" element!")
   813             choices_tmp = []
   813             choices_tmp = []
   814             for choice in elmtgroup["choices"]:
   814             for choice in elmtgroup["choices"]:
   815                 if not isinstance(choice["elmt_type"], (UnicodeType, StringType)) and choice["elmt_type"]["type"] == COMPLEXTYPE:
   815                 if not isinstance(choice["elmt_type"], (UnicodeType, StringType)) and choice["elmt_type"]["type"] == COMPLEXTYPE:
   816                     elmt_type = "%s_%s" % (elmtgroup["name"], choice["name"])
   816                     elmt_type = "%s_%s" % (elmtgroup["name"], choice["name"])
   840             sequence.append(child)
   840             sequence.append(child)
   841         elif child["type"] == "sequence":
   841         elif child["type"] == "sequence":
   842             sequence.extend(child["elements"])
   842             sequence.extend(child["elements"])
   843         elif child["type"] == "group":
   843         elif child["type"] == "group":
   844             elmtgroup = factory.FindSchemaElement(child["ref"], ELEMENTSGROUP)
   844             elmtgroup = factory.FindSchemaElement(child["ref"], ELEMENTSGROUP)
   845             if not elmtgroup.has_key("elements") or not elmtgroup["order"]:
   845             if not "elements" in elmtgroup or not elmtgroup["order"]:
   846                 raise ValueError("Only group composed of \"sequence\" can be referenced in \"sequence\" element!")
   846                 raise ValueError("Only group composed of \"sequence\" can be referenced in \"sequence\" element!")
   847             elements_tmp = []
   847             elements_tmp = []
   848             for element in elmtgroup["elements"]:
   848             for element in elmtgroup["elements"]:
   849                 if not isinstance(element["elmt_type"], (UnicodeType, StringType)) and element["elmt_type"]["type"] == COMPLEXTYPE:
   849                 if not isinstance(element["elmt_type"], (UnicodeType, StringType)) and element["elmt_type"]["type"] == COMPLEXTYPE:
   850                     elmt_type = "%s_%s" % (elmtgroup["name"], element["name"])
   850                     elmt_type = "%s_%s" % (elmtgroup["name"], element["name"])
   862 
   862 
   863 
   863 
   864 def ReduceGroup(factory, attributes, elements):
   864 def ReduceGroup(factory, attributes, elements):
   865     annotations, children = factory.ReduceElements(elements)
   865     annotations, children = factory.ReduceElements(elements)
   866 
   866 
   867     if attributes.has_key("ref"):
   867     if "ref" in attributes:
   868         return {"type": "group", "ref": attributes["ref"], "doc": annotations}
   868         return {"type": "group", "ref": attributes["ref"], "doc": annotations}
   869     else:
   869     else:
   870         element = children[0]
   870         element = children[0]
   871         group = {"type": ELEMENTSGROUP, "doc": annotations}
   871         group = {"type": ELEMENTSGROUP, "doc": annotations}
   872         if element["type"] == CHOICE:
   872         if element["type"] == CHOICE:
   971     factory.Namespaces[factory.TargetNamespace] = {}
   971     factory.Namespaces[factory.TargetNamespace] = {}
   972 
   972 
   973     annotations, children = factory.ReduceElements(elements, True)
   973     annotations, children = factory.ReduceElements(elements, True)
   974 
   974 
   975     for child in children:
   975     for child in children:
   976         if child.has_key("name"):
   976         if "name" in child:
   977             infos = factory.GetQualifiedNameInfos(child["name"], factory.TargetNamespace, True)
   977             infos = factory.GetQualifiedNameInfos(child["name"], factory.TargetNamespace, True)
   978             if infos is None:
   978             if infos is None:
   979                 factory.Namespaces[factory.TargetNamespace][child["name"]] = child
   979                 factory.Namespaces[factory.TargetNamespace][child["name"]] = child
   980             elif not CompareSchema(infos, child):
   980             elif not CompareSchema(infos, child):
   981                 raise ValueError("\"%s\" is defined twice in targetNamespace!" % child["name"])
   981                 raise ValueError("\"%s\" is defined twice in targetNamespace!" % child["name"])