Loading translation/translate/ChoiceMapping.py +15 −14 Original line number Diff line number Diff line Loading @@ -10,23 +10,24 @@ from .ComplexTypeMapping import ComplexTypeMapping log = logging.getLogger() class ChoiceMapping(ComplexTypeMapping): @classmethod def process_choice(cls, choice: XsdGroup, current_ns: str): if choice.model != 'choice': if choice.model != "choice": raise Exception(f"Wrong group type: {c.model}") oneOf = [] for c in choice.iter_model(): if not (type(c) is XsdElement): raise Exception(f"Non-element {c} encountered in choice {choice}") t = TypeMapping.get_type_from_elem(c, current_ns) oneOf.append({ oneOf.append( { "type": "object", "properties" : { c.local_name : t }, "required" : [c.local_name] }) "properties": {c.local_name: t}, "required": [c.local_name], } ) return oneOf def map(self, xst: BaseXsdType): Loading @@ -36,7 +37,7 @@ class ChoiceMapping(ComplexTypeMapping): log.debug("Not a complex type, giving up") return None content = xst.content if (content.model != 'choice'): if content.model != "choice": log.debug("Not a choice, giving up") return None return { 'oneOf' : ChoiceMapping.process_choice(content, xst.namespaces[''])} return {"oneOf": ChoiceMapping.process_choice(content, xst.namespaces[""])} translation/translate/ComplexTypeMapping.py +2 −3 Original line number Diff line number Diff line Loading @@ -2,10 +2,9 @@ from xmlschema.validators.complex_types import * from .TypeMapping import TypeMapping class ComplexTypeMapping(TypeMapping): def map(self, xst: BaseXsdType): if not (type(xst) is XsdComplexType): return None return { "type" : "object" } return {"type": "object"} translation/translate/SequenceMapping.py +27 −29 Original line number Diff line number Diff line Loading @@ -20,24 +20,17 @@ class SequenceMapping(ComplexTypeMapping): log.debug("Not a complex type, giving up") return None content = xst.content if (content.model != 'sequence'): if content.model != "sequence": log.debug("Not a sequence, giving up") return None mapped_type = { 'type' : 'object', 'properties' : {}, 'required' : [] } mapped_type = {"type": "object", "properties": {}, "required": []} # Not going to try and do all of this automatically for now # Only make insert the xsiType parameter if (xst.base_type): if xst.base_type: # mapped_type['__DESCENDENT_OF__'] = TypeMapping.get_ref_for(xst.base_type, xst.namespaces['']) mapped_type['properties']['xsiType'] = { "type" : "string", "enum" : xst.name } mapped_type['required'].append('xsiType') mapped_type["properties"]["xsiType"] = {"type": "string", "enum": xst.name} mapped_type["required"].append("xsiType") # if xst.abstract: # mapped_type['__ABSTRACT__'] = True # pass Loading @@ -47,34 +40,39 @@ class SequenceMapping(ComplexTypeMapping): log.debug(f"Processing model item {c}") if type(c) is XsdElement: if c.effective_max_occurs != 1: mapped_type['properties'][c.local_name] = { mapped_type["properties"][c.local_name] = { "type": "array", "items" : TypeMapping.get_type_from_elem(c, xst.namespaces['']) "items": TypeMapping.get_type_from_elem(c, xst.namespaces[""]), } if c.effective_max_occurs: mapped_type['properties'][c.local_name]['maxItems'] = c.effective_max_occurs mapped_type["properties"][c.local_name][ "maxItems" ] = c.effective_max_occurs if c.effective_min_occurs > 0: mapped_type['properties'][c.local_name]['minItems'] = c.effective_min_occurs mapped_type["properties"][c.local_name][ "minItems" ] = c.effective_min_occurs else: mapped_type['properties'][c.local_name] = TypeMapping.get_type_from_elem(c, xst.namespaces['']) mapped_type["properties"][ c.local_name ] = TypeMapping.get_type_from_elem(c, xst.namespaces[""]) if c.effective_min_occurs == 1: mapped_type['required'].append(c.local_name) mapped_type["required"].append(c.local_name) elif type(c) is XsdGroup: if inner_choice: raise Exception (f"Second group '{c.local_name}' encountered in {xst}") raise Exception( f"Second group '{c.local_name}' encountered in {xst}" ) if c.model != "choice": raise Exception (f"Don't know what to do with inner group {c} in {xst} - not a choice") inner_choice = ChoiceMapping.process_choice(c, xst.namespaces['']) raise Exception( f"Don't know what to do with inner group {c} in {xst} - not a choice" ) inner_choice = ChoiceMapping.process_choice(c, xst.namespaces[""]) elif type(c) is XsdAnyElement: mapped_type = {} else: raise Exception(f"Unknown element type {c}") if (inner_choice): return { 'allOf' : [ mapped_type, {'oneOf' : inner_choice} ] } if inner_choice: return {"allOf": [mapped_type, {"oneOf": inner_choice}]} else: return mapped_type translation/translate/SimpleTypeMapping.py +2 −3 Original line number Diff line number Diff line Loading @@ -7,12 +7,11 @@ from .TypeMapping import TypeMapping log = logging.getLogger() class SimpleTypeMapping(TypeMapping): def map(self, xst: BaseXsdType): log.debug(f"Attempting mapping of {xst} to simple type") if not (type(xst) is XsdAtomicRestriction): log.debug("Type is not an XsdAtomicRestriction, giving up") return None return { "$ref" : xst.base_type.name } No newline at end of file return {"$ref": xst.base_type.name} translation/translate/TypeMapping.py +20 −31 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ from xmlschema.validators.facets import * log = logging.getLogger() class TypeMapping(ABC): ns_to_id_map = {} Loading @@ -19,16 +20,12 @@ class TypeMapping(ABC): "dateTime": {"type": "string"}, "token": {"type": "string", "pattern": "^[^\r\n\t]*$"}, "anyURI": {"type": "string"}, "integer": {"type": "integer"}, "nonNegativeInteger": {"type": "integer", "minimum": 0}, "boolean": {"type": "boolean"}, "hexBinary": {"type": "string", "pattern": "^([a-fA-F0-9]{2})*$"}, "base64Binary": {"type": "string", "pattern": "^[-A-Za-z0-9+/]*={0,3}$"}, "anyType" : {} "anyType": {}, } @abstractmethod Loading @@ -37,7 +34,7 @@ class TypeMapping(ABC): @classmethod def extract_namespace(cls, qname: str): match = re.search(r'^\{([^\{\}]+)\}(([^\{\}]+))$', qname) match = re.search(r"^\{([^\{\}]+)\}(([^\{\}]+))$", qname) if match is None: return None return match.group(1) Loading @@ -54,16 +51,8 @@ class TypeMapping(ABC): @classmethod def get_type_from_elem(cls, elem: XsdElement, current_ns: str): ns = cls.extract_namespace(elem.type.name) if (ns == TypeMapping.XSD_NS): if ns == TypeMapping.XSD_NS: # this should be an XSD primitive type return dict(TypeMapping.XSD_TYPE_MAP[elem.type.local_name]) else: return cls.get_ref_for(elem.type, current_ns) Loading
translation/translate/ChoiceMapping.py +15 −14 Original line number Diff line number Diff line Loading @@ -10,23 +10,24 @@ from .ComplexTypeMapping import ComplexTypeMapping log = logging.getLogger() class ChoiceMapping(ComplexTypeMapping): @classmethod def process_choice(cls, choice: XsdGroup, current_ns: str): if choice.model != 'choice': if choice.model != "choice": raise Exception(f"Wrong group type: {c.model}") oneOf = [] for c in choice.iter_model(): if not (type(c) is XsdElement): raise Exception(f"Non-element {c} encountered in choice {choice}") t = TypeMapping.get_type_from_elem(c, current_ns) oneOf.append({ oneOf.append( { "type": "object", "properties" : { c.local_name : t }, "required" : [c.local_name] }) "properties": {c.local_name: t}, "required": [c.local_name], } ) return oneOf def map(self, xst: BaseXsdType): Loading @@ -36,7 +37,7 @@ class ChoiceMapping(ComplexTypeMapping): log.debug("Not a complex type, giving up") return None content = xst.content if (content.model != 'choice'): if content.model != "choice": log.debug("Not a choice, giving up") return None return { 'oneOf' : ChoiceMapping.process_choice(content, xst.namespaces[''])} return {"oneOf": ChoiceMapping.process_choice(content, xst.namespaces[""])}
translation/translate/ComplexTypeMapping.py +2 −3 Original line number Diff line number Diff line Loading @@ -2,10 +2,9 @@ from xmlschema.validators.complex_types import * from .TypeMapping import TypeMapping class ComplexTypeMapping(TypeMapping): def map(self, xst: BaseXsdType): if not (type(xst) is XsdComplexType): return None return { "type" : "object" } return {"type": "object"}
translation/translate/SequenceMapping.py +27 −29 Original line number Diff line number Diff line Loading @@ -20,24 +20,17 @@ class SequenceMapping(ComplexTypeMapping): log.debug("Not a complex type, giving up") return None content = xst.content if (content.model != 'sequence'): if content.model != "sequence": log.debug("Not a sequence, giving up") return None mapped_type = { 'type' : 'object', 'properties' : {}, 'required' : [] } mapped_type = {"type": "object", "properties": {}, "required": []} # Not going to try and do all of this automatically for now # Only make insert the xsiType parameter if (xst.base_type): if xst.base_type: # mapped_type['__DESCENDENT_OF__'] = TypeMapping.get_ref_for(xst.base_type, xst.namespaces['']) mapped_type['properties']['xsiType'] = { "type" : "string", "enum" : xst.name } mapped_type['required'].append('xsiType') mapped_type["properties"]["xsiType"] = {"type": "string", "enum": xst.name} mapped_type["required"].append("xsiType") # if xst.abstract: # mapped_type['__ABSTRACT__'] = True # pass Loading @@ -47,34 +40,39 @@ class SequenceMapping(ComplexTypeMapping): log.debug(f"Processing model item {c}") if type(c) is XsdElement: if c.effective_max_occurs != 1: mapped_type['properties'][c.local_name] = { mapped_type["properties"][c.local_name] = { "type": "array", "items" : TypeMapping.get_type_from_elem(c, xst.namespaces['']) "items": TypeMapping.get_type_from_elem(c, xst.namespaces[""]), } if c.effective_max_occurs: mapped_type['properties'][c.local_name]['maxItems'] = c.effective_max_occurs mapped_type["properties"][c.local_name][ "maxItems" ] = c.effective_max_occurs if c.effective_min_occurs > 0: mapped_type['properties'][c.local_name]['minItems'] = c.effective_min_occurs mapped_type["properties"][c.local_name][ "minItems" ] = c.effective_min_occurs else: mapped_type['properties'][c.local_name] = TypeMapping.get_type_from_elem(c, xst.namespaces['']) mapped_type["properties"][ c.local_name ] = TypeMapping.get_type_from_elem(c, xst.namespaces[""]) if c.effective_min_occurs == 1: mapped_type['required'].append(c.local_name) mapped_type["required"].append(c.local_name) elif type(c) is XsdGroup: if inner_choice: raise Exception (f"Second group '{c.local_name}' encountered in {xst}") raise Exception( f"Second group '{c.local_name}' encountered in {xst}" ) if c.model != "choice": raise Exception (f"Don't know what to do with inner group {c} in {xst} - not a choice") inner_choice = ChoiceMapping.process_choice(c, xst.namespaces['']) raise Exception( f"Don't know what to do with inner group {c} in {xst} - not a choice" ) inner_choice = ChoiceMapping.process_choice(c, xst.namespaces[""]) elif type(c) is XsdAnyElement: mapped_type = {} else: raise Exception(f"Unknown element type {c}") if (inner_choice): return { 'allOf' : [ mapped_type, {'oneOf' : inner_choice} ] } if inner_choice: return {"allOf": [mapped_type, {"oneOf": inner_choice}]} else: return mapped_type
translation/translate/SimpleTypeMapping.py +2 −3 Original line number Diff line number Diff line Loading @@ -7,12 +7,11 @@ from .TypeMapping import TypeMapping log = logging.getLogger() class SimpleTypeMapping(TypeMapping): def map(self, xst: BaseXsdType): log.debug(f"Attempting mapping of {xst} to simple type") if not (type(xst) is XsdAtomicRestriction): log.debug("Type is not an XsdAtomicRestriction, giving up") return None return { "$ref" : xst.base_type.name } No newline at end of file return {"$ref": xst.base_type.name}
translation/translate/TypeMapping.py +20 −31 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ from xmlschema.validators.facets import * log = logging.getLogger() class TypeMapping(ABC): ns_to_id_map = {} Loading @@ -19,16 +20,12 @@ class TypeMapping(ABC): "dateTime": {"type": "string"}, "token": {"type": "string", "pattern": "^[^\r\n\t]*$"}, "anyURI": {"type": "string"}, "integer": {"type": "integer"}, "nonNegativeInteger": {"type": "integer", "minimum": 0}, "boolean": {"type": "boolean"}, "hexBinary": {"type": "string", "pattern": "^([a-fA-F0-9]{2})*$"}, "base64Binary": {"type": "string", "pattern": "^[-A-Za-z0-9+/]*={0,3}$"}, "anyType" : {} "anyType": {}, } @abstractmethod Loading @@ -37,7 +34,7 @@ class TypeMapping(ABC): @classmethod def extract_namespace(cls, qname: str): match = re.search(r'^\{([^\{\}]+)\}(([^\{\}]+))$', qname) match = re.search(r"^\{([^\{\}]+)\}(([^\{\}]+))$", qname) if match is None: return None return match.group(1) Loading @@ -54,16 +51,8 @@ class TypeMapping(ABC): @classmethod def get_type_from_elem(cls, elem: XsdElement, current_ns: str): ns = cls.extract_namespace(elem.type.name) if (ns == TypeMapping.XSD_NS): if ns == TypeMapping.XSD_NS: # this should be an XSD primitive type return dict(TypeMapping.XSD_TYPE_MAP[elem.type.local_name]) else: return cls.get_ref_for(elem.type, current_ns)