aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2007-07-30 23:07:12 +0000
committerRob Austein <sra@hactrn.net>2007-07-30 23:07:12 +0000
commitd8daa419c8bea480ec986baafacb0eec2138b6fb (patch)
tree60bc059ce67c7433d45bed80a4739162f561ef4b /scripts
parentbaebccbe3b346c0629affb0cdb43ec9fe76699c9 (diff)
Convert to lxml
svn path=/scripts/rpki/relaxng.py; revision=794
Diffstat (limited to 'scripts')
-rw-r--r--scripts/rpki/relaxng.py44
-rw-r--r--scripts/rpki/sax_utils.py6
-rw-r--r--scripts/up-down-protocol-samples/list_response.xml1
-rwxr-xr-xscripts/xml-parse-test.py40
4 files changed, 21 insertions, 70 deletions
diff --git a/scripts/rpki/relaxng.py b/scripts/rpki/relaxng.py
deleted file mode 100644
index feb511af..00000000
--- a/scripts/rpki/relaxng.py
+++ /dev/null
@@ -1,44 +0,0 @@
-# $Id$
-
-import libxml2
-
-def relaxng(xml, rng):
- """
- Validate a chunk of XML against a RelaxNG schema.
- """
-
- # Most of this is lifted from a libxml2 example. Using py-lxml
- # might be a better approach, but this works for now.
- #
- # This is probably very inefficient, as we make no attempt to
- # retain validation contexts between calls. It's still much
- # faster than calling xmllint or jing as an external program.
- #
- # Beware of cleaning up the following code. libxml2 is not well
- # documented but there are hints that much of the following voodoo
- # is required manual memory management (see py-lxml, above)
-
- fh = open(rng, "r")
- schema = fh.read()
- fh.close()
- rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
- rngs = rngp.relaxNGParse()
- ctxt = rngs.relaxNGNewValidCtxt()
-
- doc = libxml2.parseDoc(xml)
- ret = doc.relaxNGValidateDoc(ctxt)
- if ret != 0:
- raise RuntimeError, "RelaxNG validation error"
-
- doc.freeDoc()
- del rngp
- del rngs
- del ctxt
- libxml2.relaxNGCleanupTypes()
-
- # Memory debug specific
- libxml2.cleanupParser()
- if libxml2.debugMemory(1) != 0:
- print "Memory leak %d bytes" % (libxml2.debugMemory(1))
- libxml2.dumpMemory()
- raise RuntimeError, "RelaxNG memory leak"
diff --git a/scripts/rpki/sax_utils.py b/scripts/rpki/sax_utils.py
index d645cc4f..1333df1a 100644
--- a/scripts/rpki/sax_utils.py
+++ b/scripts/rpki/sax_utils.py
@@ -23,6 +23,12 @@ class handler(xml.sax.handler.ContentHandler):
def startElement(self, name, attrs):
a = dict()
for k,v in attrs.items():
+ if isinstance(k, tuple):
+ if k == ('http://www.w3.org/XML/1998/namespace', 'lang'):
+ k = "xml:lang"
+ else:
+ assert k[0] is None
+ k = k[1]
a[k.encode("ascii")] = v.encode("ascii")
if len(self.stack) == 0:
assert not hasattr(self, "result")
diff --git a/scripts/up-down-protocol-samples/list_response.xml b/scripts/up-down-protocol-samples/list_response.xml
index a82466ec..41f77fcc 100644
--- a/scripts/up-down-protocol-samples/list_response.xml
+++ b/scripts/up-down-protocol-samples/list_response.xml
@@ -16,7 +16,6 @@
req_resource_set_ipv6="">
deadbeef
</certificate>
- <!-- Repeated for each current certificate naming the client as subject -->
<issuer>deadbeef</issuer>
</class>
</message>
diff --git a/scripts/xml-parse-test.py b/scripts/xml-parse-test.py
index f1604489..d8a954d5 100755
--- a/scripts/xml-parse-test.py
+++ b/scripts/xml-parse-test.py
@@ -1,31 +1,21 @@
# $Id$
-import glob, rpki.up_down, rpki.left_right, rpki.relaxng, xml.sax
+import glob, rpki.up_down, rpki.left_right, xml.sax, lxml.etree, lxml.sax
-if True:
- files = glob.glob("up-down-protocol-samples/*.xml")
+def test(fileglob, schema, proto):
+ rng = lxml.etree.RelaxNG(lxml.etree.parse(schema))
+ files = glob.glob(fileglob)
files.sort()
for f in files:
- print "<!--", f, "-->"
- handler = rpki.up_down.sax_handler()
- fh = open(f, "r")
- x = fh.read()
- fh.close()
- xml.sax.parseString(x, handler)
- x = str(handler.result)
- print x
- rpki.relaxng.relaxng(x, "up-down-medium-schema.rng")
+ print "\n<!--", f, "-->"
+ handler = proto.sax_handler()
+ et = lxml.etree.parse(f)
+ rng.assertValid(et)
+ lxml.sax.saxify(et, handler)
+ et = lxml.etree.fromstring(str(handler.result))
+ print lxml.etree.tostring(et)
+ rng.assertValid(et)
-if True:
- files = glob.glob("left-right-protocol-samples/*.xml")
- files.sort()
- for f in files:
- print "<!--", f, "-->"
- handler = rpki.left_right.sax_handler()
- fh = open(f, "r")
- x = fh.read()
- fh.close()
- xml.sax.parseString(x, handler)
- x = str(handler.result)
- print x
- rpki.relaxng.relaxng(x, "left-right-schema.rng")
+test("up-down-protocol-samples/*.xml", "up-down-medium-schema.rng", rpki.up_down)
+
+test("left-right-protocol-samples/*.xml", "left-right-schema.rng", rpki.left_right)