aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/py/javatoolkit')
-rw-r--r--src/py/javatoolkit/xml/SaxRewriter.py10
-rw-r--r--src/py/javatoolkit/xml/sax.py44
2 files changed, 50 insertions, 4 deletions
diff --git a/src/py/javatoolkit/xml/SaxRewriter.py b/src/py/javatoolkit/xml/SaxRewriter.py
index f9e224a..8d76de2 100644
--- a/src/py/javatoolkit/xml/SaxRewriter.py
+++ b/src/py/javatoolkit/xml/SaxRewriter.py
@@ -1,14 +1,17 @@
# -*- coding: UTF-8 -*-
-# Copyright 2004-2005 Gentoo Foundation
+# Copyright 2004-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
+import io
import os
import sys
-import io
+import xml.sax.handler
from xml.sax.saxutils import XMLGenerator
from xml.sax.saxutils import quoteattr
+import javatoolkit.xml.sax
+
class SaxRewriter(XMLGenerator):
"""
Using Sax gives us the support for writing back doctypes and all easily
@@ -124,8 +127,7 @@ class SaxRewriter(XMLGenerator):
def process(self, in_stream, callback):
self.startElement = callback
- from xml.sax import parseString
- parseString(in_stream.encode('UTF8'), self)
+ javatoolkit.xml.sax.parse_string(in_stream.encode('UTF8'), content_handler=self, features={xml.sax.handler.feature_external_ges: 1})
self.p('\n')
# vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap:
diff --git a/src/py/javatoolkit/xml/sax.py b/src/py/javatoolkit/xml/sax.py
new file mode 100644
index 0000000..d3d2a4c
--- /dev/null
+++ b/src/py/javatoolkit/xml/sax.py
@@ -0,0 +1,44 @@
+# Copyright 2020 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+import io
+import xml.sax
+import xml.sax.xmlreader
+
+def _make_parser(*, content_handler=None, dtd_handler=None, entity_resolver=None, error_handler=None, locale=None, features=None, properties=None):
+ parser = xml.sax.make_parser()
+
+ if content_handler is not None:
+ parser.setContentHandler(content_handler)
+ if dtd_handler is not None:
+ parser.setDTDHandler(dtd_handler)
+ if entity_resolver is not None:
+ parser.setEntityResolver(entity_resolver)
+ if error_handler is not None:
+ parser.setErrorHandler(error_handler)
+ if locale is not None:
+ parser.setLocale(locale)
+ if features is not None:
+ for feature, value in features.items():
+ parser.setFeature(feature, value)
+ if properties is not None:
+ for property, value in properties.items():
+ parser.setProperty(property, value)
+
+ return parser
+
+def parse(source, *, content_handler=None, dtd_handler=None, entity_resolver=None, error_handler=None, locale=None, features=None, properties=None):
+ parser = _make_parser(content_handler=content_handler, dtd_handler=dtd_handler, entity_resolver=entity_resolver, error_handler=error_handler, locale=locale, features=features, properties=properties)
+
+ parser.parse(source)
+
+def parse_string(string, *, content_handler=None, dtd_handler=None, entity_resolver=None, error_handler=None, locale=None, features=None, properties=None):
+ parser = _make_parser(content_handler=content_handler, dtd_handler=dtd_handler, entity_resolver=entity_resolver, error_handler=error_handler, locale=locale, features=features, properties=properties)
+
+ inputsource = xml.sax.xmlreader.InputSource()
+ if isinstance(string, str):
+ inputsource.setCharacterStream(io.StringIO(string))
+ else:
+ inputsource.setByteStream(io.BytesIO(string))
+
+ parser.parse(inputsource)