generated from sethub/template
[libxml2]Add libxml2 library
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
# Additional tests for runsuite
|
||||
|
||||
URL=http://www.w3.org/XML/2004/xml-schema-test-suite
|
||||
|
||||
mkdir -p Tests
|
||||
|
||||
TARBALL_2=xsts-2004-01-14.tar.gz
|
||||
NISTTESTDEF_2=NISTXMLSchemaDatatypes.testSet
|
||||
curl -LJO $URL/xmlschema2004-01-14/$TARBALL_2
|
||||
tar -xzf $TARBALL_2 Tests/Datatypes Tests/Metadata/$NISTTESTDEF_2
|
||||
rm $TARBALL_2
|
||||
|
||||
TARBALL=xsts-2002-01-16.tar.gz
|
||||
MSTESTDEF=MSXMLSchema1-0-20020116.testSet
|
||||
SUNTESTDEF=SunXMLSchema1-0-20020116.testSet
|
||||
NISTTESTDEF=NISTXMLSchema1-0-20020116.testSet
|
||||
curl -LJO $URL/xmlschema2002-01-16/$TARBALL
|
||||
tar -C Tests -xzf $TARBALL \
|
||||
xmlschema2002-01-16/suntest \
|
||||
xmlschema2002-01-16/msxsdtest \
|
||||
xmlschema2002-01-16/$MSTESTDEF \
|
||||
xmlschema2002-01-16/$SUNTESTDEF
|
||||
if [ -d Tests/suntest ] ; then rm -r Tests/suntest ; fi
|
||||
if [ -d Tests/msxsdtest ] ; then rm -r Tests/msxsdtest ; fi
|
||||
mv Tests/xmlschema2002-01-16/* Tests
|
||||
mv Tests/*.testSet Tests/Metadata
|
||||
rm -r Tests/xmlschema2002-01-16
|
||||
rm $TARBALL
|
||||
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys, os
|
||||
import libxml2
|
||||
|
||||
# TODO: Check what this does and move away from libxml2 Python bindings
|
||||
|
||||
libxml2.debugMemory(1)
|
||||
baseDir = os.path.join('msxsdtest', 'Particles')
|
||||
filenames = os.listdir(baseDir)
|
||||
mainXSD = str()
|
||||
signature = str()
|
||||
dictXSD = dict()
|
||||
|
||||
def gatherFiles():
|
||||
for file in filenames:
|
||||
if (file[-5] in ["a", "b", "c"]) and (file[-3:] == 'xsd'):
|
||||
# newfilename = string.replace(filename, ' ', '_')
|
||||
signature = file[:-5]
|
||||
mainXSD = signature + ".xsd"
|
||||
imports = []
|
||||
for sub in filenames:
|
||||
if (mainXSD != sub) and (sub[-3:] == 'xsd') and sub.startswith(signature):
|
||||
imports.append(sub)
|
||||
if len(imports) != 0:
|
||||
dictXSD[mainXSD] = imports
|
||||
|
||||
def debugMsg(text):
|
||||
#pass
|
||||
print("DEBUG:", text)
|
||||
|
||||
|
||||
def fixup():
|
||||
for mainXSD in dictXSD:
|
||||
debugMsg("fixing '%s'..." % mainXSD)
|
||||
schemaDoc = None
|
||||
xpmainCtx = None
|
||||
# Load the schema document.
|
||||
schemaFile = os.path.join(baseDir, mainXSD)
|
||||
schemaDoc = libxml2.parseFile(schemaFile)
|
||||
if (schemaDoc is None):
|
||||
print("ERROR: doc '%s' not found" % mainXSD)
|
||||
sys.exit(1)
|
||||
try:
|
||||
xpmainCtx = schemaDoc.xpathNewContext()
|
||||
xpmainCtx.xpathRegisterNs("xs", "http://www.w3.org/2001/XMLSchema")
|
||||
xpres = xpmainCtx.xpathEval("/xs:schema")
|
||||
if len(xpres) == 0:
|
||||
print("ERROR: doc '%s' has no <schema> element" % mainXSD)
|
||||
sys.exit(1)
|
||||
schemaElem = xpres[0]
|
||||
schemaNs = schemaElem.ns()
|
||||
# Select all <import>s.
|
||||
xpres = xpmainCtx.xpathEval("/xs:schema/xs:import")
|
||||
if len(xpres) != 0:
|
||||
for elem in xpres:
|
||||
loc = elem.noNsProp("schemaLocation")
|
||||
if (loc is not None):
|
||||
debugMsg(" imports '%s'" % loc)
|
||||
if loc in dictXSD[mainXSD]:
|
||||
dictXSD[mainXSD].remove(loc)
|
||||
for loc in dictXSD[mainXSD]:
|
||||
# Read out the targetNamespace.
|
||||
impTargetNs = None
|
||||
impFile = os.path.join(baseDir, loc)
|
||||
impDoc = libxml2.parseFile(impFile)
|
||||
try:
|
||||
xpimpCtx = impDoc.xpathNewContext()
|
||||
try:
|
||||
xpimpCtx.setContextDoc(impDoc)
|
||||
xpimpCtx.xpathRegisterNs("xs", "http://www.w3.org/2001/XMLSchema")
|
||||
xpres = xpimpCtx.xpathEval("/xs:schema")
|
||||
impTargetNs = xpres[0].noNsProp("targetNamespace")
|
||||
finally:
|
||||
xpimpCtx.xpathFreeContext()
|
||||
finally:
|
||||
impDoc.freeDoc()
|
||||
|
||||
# Add the <import>.
|
||||
debugMsg(" adding <import namespace='%s' schemaLocation='%s'/>" % (impTargetNs, loc))
|
||||
newElem = schemaDoc.newDocNode(schemaNs, "import", None)
|
||||
if (impTargetNs is not None):
|
||||
newElem.newProp("namespace", impTargetNs)
|
||||
newElem.newProp("schemaLocation", loc)
|
||||
if schemaElem.children is not None:
|
||||
schemaElem.children.addPrevSibling(newElem)
|
||||
schemaDoc.saveFile(schemaFile)
|
||||
finally:
|
||||
xpmainCtx.xpathFreeContext()
|
||||
schemaDoc.freeDoc()
|
||||
|
||||
try:
|
||||
gatherFiles()
|
||||
fixup()
|
||||
finally:
|
||||
libxml2.cleanupParser()
|
||||
if libxml2.debugMemory(1) != 0:
|
||||
print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
|
||||
|
||||
Reference in New Issue
Block a user