Пример #1
0
def generateJsonRes(schemadoc, rootElement, namespace):
    global complexTypes
    global attributeGroups
    global groups
    global elementTypes
    schema = '{http://www.w3.org/2001/XMLSchema}'

    for key, value in schemadoc.attrib.items():
        if key == 'targetNamespace':
            pass  # handle namespace? TODO
        elif key == 'version':
            pass  # handle version? TODO
        elif key == 'id':
            pass  # handle id? TODO
        elif key == 'attributeFormDefault':
            pass  # handle attributeFormDefault? TODO
        elif key == 'elementFormDefault':
            pass  # handle elementFormDefault? TODO
        elif key == 'blockDefault':
            pass  # handle blockDefault? TODO
        elif key == 'finalDefault':
            pass  # handle finalDefault? TODO
        elif key == 'xmlns':
            pass  # handle xmlns? TODO
        else:
            print('unknown schema attribute: ' + key + ', ' + value)

    for child in schemadoc.iterfind(schema + 'complexType'):
        if child.get('name'):
            complexTypes[child.get('name')] = child

    for child in schemadoc.iterfind(schema + 'simpleType'):
        if child.get('name'):
            complexTypes[child.get('name')] = child

    for child in schemadoc.iterfind(schema + 'attributeGroup'):
        if child.get('name'):
            attributeGroups[child.get('name')] = child

    for child in schemadoc.iterfind(schema + 'group'):
        if child.get('name'):
            groups[child.get('name')] = child

    for child in schemadoc.iterfind(schema + 'element'):
        if child.get('name'):
            elementTypes[child.get('name')] = child

    for child in schemadoc.iterfind(schema + 'element'):
        if child.get('name') == rootElement:
            tag = printTag(child.tag)
            if tag != 'complexType' and tag != 'attributeGroup':
                tree = xmlElement(child.get('name'), namespace=namespace)
                analyze2(child, tree)
                if tree is not None:
                    allElements, e, a = tree.children[0].listAllElements()
                    existingElements = {}
                    existingElements['root'] = copy.deepcopy(
                        allElements[rootElement])
                    return existingElements, allElements

    # root element not found
    raise ValueError({
        'rootElement':
        'Root must be in %s' %
        [c.get('name') for c in schemadoc.iterfind(schema + 'element')]
    })
Пример #2
0
def analyze2(element, tree, usedTypes=[], minC=1, maxC=1, choise=-1):
    global choiseCount
    global elCount
    global complexTypes
    global attributeGroups
    global finishedGroups
    global attributesComplexTypes
    global elementTypes

    tag = printTag(element.tag)
    ref = element.get('ref')
    el_type = element.get('type')

    if tag == 'element':
        minOccurs = element.get('minOccurs')
        maxOccurs = element.get('maxOccurs')

        if minOccurs and int(minOccurs) >= 0:
            minC = int(minOccurs)

        if maxOccurs:
            maxC = -1 if maxOccurs == 'unbounded' else int(maxOccurs)

        if ref:
            key = ref
            if ':' in key:
                key = key.split(':')[1]
            if key not in usedTypes:
                if key in elementTypes:
                    usedTypes.append(key)
                    analyze2(elementTypes[key], tree, usedTypes=usedTypes)
                else:
                    print("type unknown: " + key)
        else:
            t = xmlElement(element.get('name'), namespace=tree.namespace)
            t.karMin = minC
            t.karMax = maxC

            if el_type is None:
                tree.addChild(t)
                for child in element:
                    analyze2(child, t, usedTypes=usedTypes)
            elif getPrefix(el_type) in ['xs', 'xsd']:
                att = OrderedDict()
                att['key'] = '#content'
                att['type'] = 'input'
                templateOptions = OrderedDict()
                templateOptions['type'] = 'text'  # TODO
                templateOptions['label'] = 'Content'
                templateOptions['placeholder'] = 'Content'
                templateOptions['required'] = True
                att['templateOptions'] = templateOptions
                t.attributes.append(att)
                tree.addChild(t)
            else:
                key = el_type
                if ':' in key:
                    key = key.split(':')[1]
                tpyeDef = element.get('name') + key
                if tpyeDef not in usedTypes:
                    if key in complexTypes:
                        tree.addChild(t)
                        usedTypes.append(tpyeDef)
                        for child in complexTypes[key]:
                            analyze2(child, t, usedTypes=usedTypes)
                        finishedComplexTypes[key] = tree.calculateChildren()
                        attributesComplexTypes[key] = tree.attributes
                    else:
                        print("type unknown: " + el_type)
                else:
                    if key in finishedComplexTypes:
                        t.type = TYPE_TO
                        t.attributes = attributesComplexTypes[key]
                        tree.addChild(t)
    elif tag in ['complexType', 'complexContent', 'sequence']:
        for child in element:
            analyze2(child, tree, usedTypes=usedTypes)
    elif tag == 'extension':
        if element.get('base'):
            key = element.get('base')
            if key not in usedTypes:
                if key in complexTypes:
                    usedTypes.append(key)
                    for child in complexTypes[key]:
                        analyze2(child,
                                 tree,
                                 usedTypes=usedTypes,
                                 minC=minC,
                                 maxC=maxC)
                    finishedComplexTypes[key] = tree.calculateChildren()
                    attributesComplexTypes[key] = tree.attributes
            elif key in finishedComplexTypes:
                t = xmlElement('finishedGroup', namespace=tree.namespace)
                t.type = TYPE_TO_CHOISE
                t.attributes = attributesComplexTypes[key]
                t.children = finishedComplexTypes[key]
                tree.addChild(t)

            for child in element:
                analyze2(child, tree, usedTypes=usedTypes)
    elif tag == 'choice':
        t = xmlElement('choice', namespace=tree.namespace)
        t.type = TYPE_CHOISE
        tree.addChild(t)
        for child in element:
            analyze2(child, t, usedTypes=usedTypes, choise=choiseCount)
        choiseCount += 1
    elif tag == 'attribute':
        if ref:
            if ':' in ref:
                ref = ref.split(':')[1]
            if ref in attributeGroups:
                for child in attributeGroups[ref]:
                    analyze2(child, tree, usedTypes=usedTypes)
        else:
            att = parseAttribute(element)
            if att is not None:
                tree.attributes.append(att)
            else:
                print('attribute == none')
    elif tag == 'attributeGroup':
        if ref:
            if ':' in ref:
                ref = ref.split(':')[1]
            if ref in attributeGroups:
                for child in attributeGroups[ref]:
                    analyze2(child, tree, usedTypes=usedTypes)
            else:
                print('attributegroup not found: ' + ref)
    elif tag == 'anyAttribute':
        tree.anyAttribute = True
    elif tag == 'simpleContent':
        att = OrderedDict()
        att['key'] = '#content'
        att['type'] = 'input'
        templateOptions = OrderedDict()
        templateOptions['type'] = 'text'  # TODO
        templateOptions['label'] = 'Content'
        templateOptions['placeholder'] = 'Content'
        templateOptions['required'] = True
        att['templateOptions'] = templateOptions
        tree.attributes.append(att)
        for child in element:
            analyze2(child, tree, usedTypes=usedTypes)
    elif tag == 'any':
        tree.anyElement = True
    elif tag == 'all':
        if element.get('minOccurs') is not None:
            minC = int(element.get('minOccurs'))
            if minC != 0 and minC != 1:
                minC = 1
        maxC = 1
        for child in element:
            analyze2(child, tree, usedTypes=usedTypes, minC=minC, maxC=maxC)
    elif tag == 'group':
        if ref not in usedTypes:
            if ref in groups:
                usedTypes.append(ref)
                for child in groups[ref]:
                    analyze2(child, tree, usedTypes=usedTypes)
                finishedGroups[ref] = tree.calculateChildren()
        elif ref in finishedGroups:
            t = xmlElement('finishedGroup', namespace=tree.namespace)
            t.type = TYPE_TO_CHOISE
            t.children = finishedGroups[ref]
            tree.addChild(t)

    elif tag == 'annotation':
        pass  # comments
    else:
        print('other: ' + tag)
Пример #3
0
def generateExtensionRef(schemadoc, namespace):
    global complexTypes
    global attributeGroups
    global groups
    global elementTypes
    schema = '{http://www.w3.org/2001/XMLSchema}'

    for key, value in schemadoc.attrib.items():
        if key == 'targetNamespace':
            pass  # handle namespace? TODO
        elif key == 'version':
            pass  # handle version? TODO
        elif key == 'id':
            pass  # handle id? TODO
        elif key == 'attributeFormDefault':
            pass  # handle attributeFormDefault? TODO
        elif key == 'elementFormDefault':
            pass  # handle elementFormDefault? TODO
        elif key == 'blockDefault':
            pass  # handle blockDefault? TODO
        elif key == 'finalDefault':
            pass  # handle finalDefault? TODO
        elif key == 'xmlns':
            pass  # handle xmlns? TODO
        else:
            print('unknown schema attribute: ' + key + ', ' + value)

    for child in schemadoc.iterfind(schema + 'complexType'):
        if child.get('name'):
            complexTypes[child.get('name')] = child

    for child in schemadoc.iterfind(schema + 'simpleType'):
        if child.get('name'):
            complexTypes[child.get('name')] = child

    for child in schemadoc.iterfind(schema + 'attributeGroup'):
        if child.get('name'):
            attributeGroups[child.get('name')] = child

    for child in schemadoc.iterfind(schema + 'group'):
        if child.get('name'):
            groups[child.get('name')] = child

    for child in schemadoc.iterfind(schema + 'element'):
        if child.get('name'):
            elementTypes[child.get('name')] = child

    allElements = {}
    existingElements = {}
    attributes = {}
    for child in schemadoc:
        if child.tag == (schema + 'element'):
            tag = printTag(child.tag)
            if tag != 'complexType' and tag != 'attributeGroup':
                tree = xmlElement(child.get('name'), namespace=namespace)
                analyze2(child, tree)
                if tree is not None:
                    aE, e, a = tree.children[0].listAllElements()
                    allElements.update(aE)
                    existingElements[tree.tagName] = copy.deepcopy(
                        allElements[tree.tagName])
        elif child.tag == (schema + 'attribute'):
            attributes[child.get('name')] = parseAttribute(child)

    return existingElements, allElements, attributes