xref: /aosp_15_r20/external/libxml2/xstc/fixup-tests.py (revision 7c5688314b92172186c154356a6374bf7684c3ca)
1#!/usr/bin/env python3
2
3import sys, os
4import libxml2
5
6
7libxml2.debugMemory(1)
8baseDir = os.path.join('msxsdtest', 'Particles')
9filenames = os.listdir(baseDir)
10mainXSD = str()
11signature = str()
12dictXSD = dict()
13
14def gatherFiles():
15    for file in filenames:
16        if (file[-5] in ["a", "b", "c"]) and (file[-3:] == 'xsd'):
17            # newfilename = string.replace(filename, ' ', '_')
18            signature = file[:-5]
19            mainXSD = signature + ".xsd"
20            imports = []
21            for sub in filenames:
22                if (mainXSD != sub) and (sub[-3:] == 'xsd') and sub.startswith(signature):
23                    imports.append(sub)
24            if len(imports) != 0:
25                dictXSD[mainXSD] = imports
26
27def debugMsg(text):
28    #pass
29    print("DEBUG:", text)
30
31
32def fixup():
33    for mainXSD in dictXSD:
34        debugMsg("fixing '%s'..." % mainXSD)
35        schemaDoc = None
36        xpmainCtx = None
37        # Load the schema document.
38        schemaFile = os.path.join(baseDir, mainXSD)
39        schemaDoc = libxml2.parseFile(schemaFile)
40        if (schemaDoc is None):
41            print("ERROR: doc '%s' not found" % mainXSD)
42            sys.exit(1)
43        try:
44            xpmainCtx = schemaDoc.xpathNewContext()
45            xpmainCtx.xpathRegisterNs("xs", "http://www.w3.org/2001/XMLSchema")
46            xpres = xpmainCtx.xpathEval("/xs:schema")
47            if len(xpres) == 0:
48                print("ERROR: doc '%s' has no <schema> element" % mainXSD)
49                sys.exit(1)
50            schemaElem = xpres[0]
51            schemaNs = schemaElem.ns()
52            # Select all <import>s.
53            xpres = xpmainCtx.xpathEval("/xs:schema/xs:import")
54            if len(xpres) != 0:
55                for elem in xpres:
56                    loc = elem.noNsProp("schemaLocation")
57                    if (loc is not None):
58                        debugMsg("  imports '%s'" % loc)
59                        if loc in dictXSD[mainXSD]:
60                            dictXSD[mainXSD].remove(loc)
61            for loc in dictXSD[mainXSD]:
62                # Read out the targetNamespace.
63                impTargetNs = None
64                impFile = os.path.join(baseDir, loc)
65                impDoc = libxml2.parseFile(impFile)
66                try:
67                    xpimpCtx = impDoc.xpathNewContext()
68                    try:
69                        xpimpCtx.setContextDoc(impDoc)
70                        xpimpCtx.xpathRegisterNs("xs", "http://www.w3.org/2001/XMLSchema")
71                        xpres = xpimpCtx.xpathEval("/xs:schema")
72                        impTargetNs = xpres[0].noNsProp("targetNamespace")
73                    finally:
74                        xpimpCtx.xpathFreeContext()
75                finally:
76                    impDoc.freeDoc()
77
78                # Add the <import>.
79                debugMsg("  adding <import namespace='%s' schemaLocation='%s'/>" % (impTargetNs, loc))
80                newElem = schemaDoc.newDocNode(schemaNs, "import", None)
81                if (impTargetNs is not None):
82                    newElem.newProp("namespace", impTargetNs)
83                newElem.newProp("schemaLocation", loc)
84                if schemaElem.children is not None:
85                    schemaElem.children.addPrevSibling(newElem)
86                schemaDoc.saveFile(schemaFile)
87        finally:
88            xpmainCtx.xpathFreeContext()
89            schemaDoc.freeDoc()
90
91try:
92    gatherFiles()
93    fixup()
94finally:
95    libxml2.cleanupParser()
96    if libxml2.debugMemory(1) != 0:
97        print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
98
99