Commit 3487c507 authored by Mark Canterbury's avatar Mark Canterbury
Browse files

Extending mapping

parent c6d8e230
Loading
Loading
Loading
Loading
+21 −2
Original line number Diff line number Diff line
@@ -9,6 +9,25 @@ from translate_schema import *
logging.basicConfig(level = logging.DEBUG)

if __name__ == "__main__":    
    js = translate_schema("103280/TS_103_280.xsd", "103120.json")
    print(json.dumps(js, indent=2))
    # js = translate_schema("103280/TS_103_280.xsd", "103120.json")
    # print(json.dumps(js, indent=2))
    schemas = [ "103120/schema/ts_103120_Authorisation.xsd",
                "103120/schema/ts_103120_Common.xsd",
                "103120/schema/ts_103120_Delivery.xsd",
                "103120/schema/ts_103120_Document.xsd",
                "103120/schema/ts_103120_Notification.xsd",
                "103120/schema/ts_103120_Task.xsd",
                "103280/TS_103_280.xsd",
                "testing/deps/xmldsig/xmldsig-core-schema.xsd"]

    schema_locations = []
    for schemaFile in schemas:
        try:
            xs = XMLSchema(schemaFile, validation='skip')
            schema_locations.append((xs.target_namespace, str(Path(schemaFile).resolve())))
            print (" [ {0}  ->  {1} ]".format(xs.target_namespace, schemaFile))
        except XMLSchemaParseError as ex:
            print (" [ {0} failed to parse:  {1} ]".format(schemaFile, ex))

    js = translate_schema("103120/schema/ts_103120_core.xsd", "core.json", schema_locations)
    print(json.dumps(js, indent=2))
+2 −2
Original line number Diff line number Diff line
@@ -14,14 +14,14 @@ mappings = [
    SequenceMapping(),
]

def translate_schema (schema_path, schema_id):
def translate_schema (schema_path, schema_id, schema_locations = []):
    js = {
        "$id" : schema_id,
        "$defs" : {}
    }

    logging.info(f"Translating schema {schema_path}")
    xs = XMLSchema(schema_path, validation='lax')
    xs = XMLSchema(schema_path, validation='lax', locations=schema_locations)
    logging.info(f"Schema namespace: {xs.target_namespace}" )

    for type_name, xsd_type in xs.types.items():
+17 −8
Original line number Diff line number Diff line
@@ -37,8 +37,11 @@ class XSDNativeSimpleTypeMapping(SimpleTypeMapping):
        "anyURI" : { "type" : "string" },

        "integer" : { "type" : "integer"},
        "nonNegativeInteger" : { "type" : "integer", "minimum" : 0},

        "hexBinary" : { "type" : "string", "pattern" : "^([a-fA-F0-9]{2})*$"}
        "hexBinary" : { "type" : "string", "pattern" : "^([a-fA-F0-9]{2})*$"},

        "anyType" : {}
    }

    def map(self, xst: BaseXsdType):
@@ -51,10 +54,16 @@ class XSDNativeSimpleTypeMapping(SimpleTypeMapping):

        mapped_type = XSDNativeSimpleTypeMapping.XSD_TYPE_MAP.get(xst.base_type.local_name)
        parent_type = None

        if mapped_type is None:
            ns = extract_namespace(xst.base_type.name)
            if ns == XSDNativeSimpleTypeMapping.XSD_NS:
                print (xst)
                print (xst.base_type)
                raise Exception (f"No mapping for xs:{xst.base_type.local_name}")
            if len(xst.facets) == 0:
                mapped_type = get_ref_for(xst.base_type, xst.namespaces[''])
            else:
                parent_type = get_ref_for(xst.base_type, xst.namespaces[''])
                mapped_type = XSDNativeSimpleTypeMapping.XSD_TYPE_MAP.get(xst.root_type.local_name)
                if mapped_type is None:
@@ -132,14 +141,14 @@ def process_choice(choice: XsdGroup, current_ns : str):

class ChoiceMapping(ComplexTypeMapping):
    def map(self, xst : BaseXsdType):
        log.debug(f"Attempting mapping of {xst} to sequence")
        log.debug(f"Attempting mapping of {xst} to choice")
        j = super().map(xst)
        if j is None:
            log.debug("Not a complex type, giving up")
            return None
        content = xst.content
        if (content.model != 'choice'):
            log.debug("Not a sequence, giving up")
            log.debug("Not a choice, giving up")
            return None
        return { 'oneOf' : process_choice(content, xst.namespaces[''])}

@@ -171,7 +180,7 @@ class SequenceMapping(ComplexTypeMapping):
                    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 = process_choice(c)
                inner_choice = process_choice(c, xst.namespaces[''])
            else:
                raise Exception(f"Unknown element type {c}")
        if (inner_choice):