diff options
author | Rafael H. Schloming <rhs@apache.org> | 2009-07-21 19:02:15 +0000 |
---|---|---|
committer | Rafael H. Schloming <rhs@apache.org> | 2009-07-21 19:02:15 +0000 |
commit | fc6c7aed051482f011e1b484dc9fd45a4b36ada9 (patch) | |
tree | 0369afe56cd72040c9196989a92546a4ff73192a /python/mllib | |
parent | 6f0e2063cf8a4e6861e6c8167109f9beaab151f3 (diff) | |
download | qpid-python-fc6c7aed051482f011e1b484dc9fd45a4b36ada9.tar.gz |
workaround jython/python xml parser bug with unicode sources; added path support for mllib.xml_parse; uncommented dtd decl in spec file
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@796477 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'python/mllib')
-rw-r--r-- | python/mllib/__init__.py | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/python/mllib/__init__.py b/python/mllib/__init__.py index 39e9363614..9aa1e56e66 100644 --- a/python/mllib/__init__.py +++ b/python/mllib/__init__.py @@ -24,6 +24,8 @@ both SGML and XML. import os, dom, transforms, parsers, sys import xml.sax, types +from xml.sax.handler import ErrorHandler +from xml.sax.xmlreader import InputSource from cStringIO import StringIO def transform(node, *args): @@ -49,15 +51,33 @@ def sgml_parse(source): p.close() return p.parser.tree -def xml_parse(filename): +class Resolver: + + def __init__(self, path): + self.path = path + + def resolveEntity(self, publicId, systemId): + for p in self.path: + fname = os.path.join(p, systemId) + if os.path.exists(fname): + source = InputSource(systemId) + source.setByteStream(open(fname)) + return source + return InputSource(systemId) + +def xml_parse(filename, path=()): if sys.version_info[0:2] == (2,3): # XXX: this is for older versions of python - source = "file://%s" % os.path.abspath(filename) + source = "file://%s" % os.path.abspath(filename) else: source = filename - p = parsers.XMLParser() - xml.sax.parse(source, p) - return p.parser.tree + h = parsers.XMLParser() + p = xml.sax.make_parser() + p.setContentHandler(h) + p.setErrorHandler(ErrorHandler()) + p.setEntityResolver(Resolver(path)) + p.parse(source) + return h.parser.tree def sexp(node): s = transforms.Sexp() |