xmlclass/xsdschema.py
changeset 1736 7e61baa047f0
parent 1734 750eeb7230a1
child 1739 ec153828ded2
equal deleted inserted replaced
1735:c02818d7e29f 1736:7e61baa047f0
    28 from xml.dom import minidom
    28 from xml.dom import minidom
    29 from types import *
    29 from types import *
    30 
    30 
    31 from xmlclass import *
    31 from xmlclass import *
    32 
    32 
       
    33 
    33 def GenerateDictFacets(facets):
    34 def GenerateDictFacets(facets):
    34     return dict([(name, (None, False)) for name in facets])
    35     return dict([(name, (None, False)) for name in facets])
       
    36 
    35 
    37 
    36 def GenerateSimpleTypeXMLText(function):
    38 def GenerateSimpleTypeXMLText(function):
    37     def generateXMLTextMethod(value, name=None, indent=0):
    39     def generateXMLTextMethod(value, name=None, indent=0):
    38         text = ""
    40         text = ""
    39         if name is not None:
    41         if name is not None:
    42         text += function(value)
    44         text += function(value)
    43         if name is not None:
    45         if name is not None:
    44             text += "</%s>\n" % name
    46             text += "</%s>\n" % name
    45         return text
    47         return text
    46     return generateXMLTextMethod
    48     return generateXMLTextMethod
       
    49 
    47 
    50 
    48 def GenerateFloatXMLText(extra_values=[], decimal=None):
    51 def GenerateFloatXMLText(extra_values=[], decimal=None):
    49     float_format = (lambda x: "{:.{width}f}".format(x, width=decimal).rstrip('0')
    52     float_format = (lambda x: "{:.{width}f}".format(x, width=decimal).rstrip('0')
    50                     if decimal is not None else str)
    53                     if decimal is not None else str)
    51     def generateXMLTextMethod(value, name=None, indent=0):
    54     def generateXMLTextMethod(value, name=None, indent=0):
   107             else:
   110             else:
   108                 annotation["documentation"] += text
   111                 annotation["documentation"] += text
   109     return annotation
   112     return annotation
   110 
   113 
   111 # Simple type elements
   114 # Simple type elements
       
   115 
   112 
   116 
   113 def GenerateFacetReducing(facetname, canbefixed):
   117 def GenerateFacetReducing(facetname, canbefixed):
   114     def ReduceFacet(factory, attributes, elements):
   118     def ReduceFacet(factory, attributes, elements):
   115         annotations, children = factory.ReduceElements(elements)
   119         annotations, children = factory.ReduceElements(elements)
   116         if attributes.has_key("value"):
   120         if attributes.has_key("value"):
   484     simpleType["initial"] = SimpleTypeInitialValue
   488     simpleType["initial"] = SimpleTypeInitialValue
   485     simpleType["check"] = CheckSimpleTypeValue
   489     simpleType["check"] = CheckSimpleTypeValue
   486     simpleType["generate"] = GenerateSimpleType
   490     simpleType["generate"] = GenerateSimpleType
   487     return simpleType
   491     return simpleType
   488 
   492 
       
   493 
   489 def ReduceSimpleType(factory, attributes, elements):
   494 def ReduceSimpleType(factory, attributes, elements):
   490     # Reduce all the simple type children
   495     # Reduce all the simple type children
   491     annotations, children = factory.ReduceElements(elements)
   496     annotations, children = factory.ReduceElements(elements)
   492 
   497 
   493     simpleType = CreateSimpleType(factory, attributes, children[0])
   498     simpleType = CreateSimpleType(factory, attributes, children[0])
   494     simpleType["doc"] = annotations
   499     simpleType["doc"] = annotations
   495 
   500 
   496     return simpleType
   501     return simpleType
   497 
   502 
   498 # Complex type
   503 # Complex type
       
   504 
   499 
   505 
   500 def ExtractAttributes(factory, elements, base=None):
   506 def ExtractAttributes(factory, elements, base=None):
   501     attrs = []
   507     attrs = []
   502     attrnames = {}
   508     attrnames = {}
   503     if base is not None:
   509     if base is not None:
   715 
   721 
   716     any = {"type": ANY, "doc": annotations}
   722     any = {"type": ANY, "doc": annotations}
   717     any.update(attributes)
   723     any.update(attributes)
   718     return any
   724     return any
   719 
   725 
       
   726 
   720 def ReduceElement(factory, attributes, elements):
   727 def ReduceElement(factory, attributes, elements):
   721     annotations, children = factory.ReduceElements(elements)
   728     annotations, children = factory.ReduceElements(elements)
   722 
   729 
   723     types = []
   730     types = []
   724     constraints = []
   731     constraints = []
   768         else:
   775         else:
   769             element.update(attributes)
   776             element.update(attributes)
   770         return element
   777         return element
   771     else:
   778     else:
   772         raise ValueError("\"Element\" must have at least a \"ref\" or a \"name\" defined!")
   779         raise ValueError("\"Element\" must have at least a \"ref\" or a \"name\" defined!")
       
   780 
   773 
   781 
   774 def ReduceAll(factory, attributes, elements):
   782 def ReduceAll(factory, attributes, elements):
   775     annotations, children = factory.ReduceElements(elements)
   783     annotations, children = factory.ReduceElements(elements)
   776 
   784 
   777     for child in children:
   785     for child in children:
   865         group.update(attributes)
   873         group.update(attributes)
   866         return group
   874         return group
   867 
   875 
   868 # Constraint elements
   876 # Constraint elements
   869 
   877 
       
   878 
   870 def ReduceUnique(factory, attributes, elements):
   879 def ReduceUnique(factory, attributes, elements):
   871     annotations, children = factory.ReduceElements(elements)
   880     annotations, children = factory.ReduceElements(elements)
   872 
   881 
   873     unique = {"type": CONSTRAINT, "const_type": "unique", "selector": children[0], "fields": children[1:]}
   882     unique = {"type": CONSTRAINT, "const_type": "unique", "selector": children[0], "fields": children[1:]}
   874     unique.update(attributes)
   883     unique.update(attributes)
   875     return unique
   884     return unique
   876 
   885 
       
   886 
   877 def ReduceKey(factory, attributes, elements):
   887 def ReduceKey(factory, attributes, elements):
   878     annotations, children = factory.ReduceElements(elements)
   888     annotations, children = factory.ReduceElements(elements)
   879 
   889 
   880     key = {"type": CONSTRAINT, "const_type": "key", "selector": children[0], "fields": children[1:]}
   890     key = {"type": CONSTRAINT, "const_type": "key", "selector": children[0], "fields": children[1:]}
   881     key.update(attributes)
   891     key.update(attributes)
   882     return key
   892     return key
   883 
   893 
       
   894 
   884 def ReduceKeyRef(factory, attributes, elements):
   895 def ReduceKeyRef(factory, attributes, elements):
   885     annotations, children = factory.ReduceElements(elements)
   896     annotations, children = factory.ReduceElements(elements)
   886 
   897 
   887     keyref = {"type": CONSTRAINT, "const_type": "keyref", "selector": children[0], "fields": children[1:]}
   898     keyref = {"type": CONSTRAINT, "const_type": "keyref", "selector": children[0], "fields": children[1:]}
   888     keyref.update(attributes)
   899     keyref.update(attributes)
   889     return keyref
   900     return keyref
   890 
   901 
       
   902 
   891 def ReduceSelector(factory, attributes, elements):
   903 def ReduceSelector(factory, attributes, elements):
   892     annotations, children = factory.ReduceElements(elements)
   904     annotations, children = factory.ReduceElements(elements)
   893 
   905 
   894     selector = {"type": CONSTRAINT, "const_type": "selector"}
   906     selector = {"type": CONSTRAINT, "const_type": "selector"}
   895     selector.update(attributes)
   907     selector.update(attributes)
   896     return selector
   908     return selector
   897 
   909 
       
   910 
   898 def ReduceField(factory, attributes, elements):
   911 def ReduceField(factory, attributes, elements):
   899     annotations, children = factory.ReduceElements(elements)
   912     annotations, children = factory.ReduceElements(elements)
   900 
   913 
   901     field = {"type": CONSTRAINT, "const_type": "field"}
   914     field = {"type": CONSTRAINT, "const_type": "field"}
   902     field.update(attributes)
   915     field.update(attributes)
   906 # Inclusion elements
   919 # Inclusion elements
   907 
   920 
   908 def ReduceImport(factory, attributes, elements):
   921 def ReduceImport(factory, attributes, elements):
   909     annotations, children = factory.ReduceElements(elements)
   922     annotations, children = factory.ReduceElements(elements)
   910     raise ValueError("\"import\" element isn't supported yet!")
   923     raise ValueError("\"import\" element isn't supported yet!")
       
   924 
   911 
   925 
   912 def ReduceInclude(factory, attributes, elements):
   926 def ReduceInclude(factory, attributes, elements):
   913     annotations, children = factory.ReduceElements(elements)
   927     annotations, children = factory.ReduceElements(elements)
   914 
   928 
   915     if factory.FileName is None:
   929     if factory.FileName is None:
   931     factory.ComputedClasses.update(include_factory.ComputedClasses)
   945     factory.ComputedClasses.update(include_factory.ComputedClasses)
   932     factory.ComputedClassesLookUp.update(include_factory.ComputedClassesLookUp)
   946     factory.ComputedClassesLookUp.update(include_factory.ComputedClassesLookUp)
   933     factory.EquivalentClassesParent.update(include_factory.EquivalentClassesParent)
   947     factory.EquivalentClassesParent.update(include_factory.EquivalentClassesParent)
   934     return None
   948     return None
   935 
   949 
       
   950 
   936 def ReduceRedefine(factory, attributes, elements):
   951 def ReduceRedefine(factory, attributes, elements):
   937     annotations, children = factory.ReduceElements(elements)
   952     annotations, children = factory.ReduceElements(elements)
   938     raise ValueError("\"redefine\" element isn't supported yet!")
   953     raise ValueError("\"redefine\" element isn't supported yet!")
   939 
   954 
   940 
   955 
   959             infos = factory.GetQualifiedNameInfos(child["name"], factory.TargetNamespace, True)
   974             infos = factory.GetQualifiedNameInfos(child["name"], factory.TargetNamespace, True)
   960             if infos is None:
   975             if infos is None:
   961                 factory.Namespaces[factory.TargetNamespace][child["name"]] = child
   976                 factory.Namespaces[factory.TargetNamespace][child["name"]] = child
   962             elif not CompareSchema(infos, child):
   977             elif not CompareSchema(infos, child):
   963                 raise ValueError("\"%s\" is defined twice in targetNamespace!" % child["name"])
   978                 raise ValueError("\"%s\" is defined twice in targetNamespace!" % child["name"])
       
   979 
   964 
   980 
   965 def CompareSchema(schema, reference):
   981 def CompareSchema(schema, reference):
   966     if isinstance(schema, ListType):
   982     if isinstance(schema, ListType):
   967         if not isinstance(reference, ListType) or len(schema) != len(reference):
   983         if not isinstance(reference, ListType) or len(schema) != len(reference):
   968             return False
   984             return False
  1089                 if element_infos is not None:
  1105                 if element_infos is not None:
  1090                     self.Namespaces[self.TargetNamespace][element_name] = element_infos
  1106                     self.Namespaces[self.TargetNamespace][element_name] = element_infos
  1091                     return element_infos
  1107                     return element_infos
  1092         return None
  1108         return None
  1093 
  1109 
  1094 """
  1110 
  1095 This function opens the xsd file and generate a xml parser with class lookup from
       
  1096 the xml tree
       
  1097 """
       
  1098 def GenerateParserFromXSD(filepath):
  1111 def GenerateParserFromXSD(filepath):
       
  1112     """
       
  1113     This function opens the xsd file and generate a xml parser with class lookup from
       
  1114     the xml tree
       
  1115     """
  1099     xsdfile = open(filepath, 'r')
  1116     xsdfile = open(filepath, 'r')
  1100     xsdstring = xsdfile.read()
  1117     xsdstring = xsdfile.read()
  1101     xsdfile.close()
  1118     xsdfile.close()
  1102     cwd = os.getcwd()
  1119     cwd = os.getcwd()
  1103     os.chdir(os.path.dirname(filepath))
  1120     os.chdir(os.path.dirname(filepath))
  1104     parser = GenerateParser(XSDClassFactory(minidom.parseString(xsdstring), filepath), xsdstring)
  1121     parser = GenerateParser(XSDClassFactory(minidom.parseString(xsdstring), filepath), xsdstring)
  1105     os.chdir(cwd)
  1122     os.chdir(cwd)
  1106     return parser
  1123     return parser
  1107 
  1124 
  1108 """
  1125 
  1109 This function generate a xml from the xsd given as a string
       
  1110 """
       
  1111 def GenerateParserFromXSDstring(xsdstring):
  1126 def GenerateParserFromXSDstring(xsdstring):
       
  1127     """
       
  1128     This function generate a xml from the xsd given as a string
       
  1129     """
  1112     return GenerateParser(XSDClassFactory(minidom.parseString(xsdstring)), xsdstring)
  1130     return GenerateParser(XSDClassFactory(minidom.parseString(xsdstring)), xsdstring)
  1113 
  1131 
  1114 
  1132 
  1115 #-------------------------------------------------------------------------------
  1133 #-------------------------------------------------------------------------------
  1116 #                           XSD schema syntax elements
  1134 #                           XSD schema syntax elements