Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 698954
Collapse All | Expand All

(-)a/src/py/javatoolkit/xml/SaxRewriter.py (-4 / +6 lines)
Lines 1-14 Link Here
1
# -*- coding: UTF-8 -*-
1
# -*- coding: UTF-8 -*-
2
# Copyright 2004-2005 Gentoo Foundation
2
# Copyright 2004-2020 Gentoo Authors
3
# Distributed under the terms of the GNU General Public License v2
3
# Distributed under the terms of the GNU General Public License v2
4
4
5
import io
5
import os
6
import os
6
import sys
7
import sys
7
import io
8
8
9
import xml.sax.handler
9
from xml.sax.saxutils import XMLGenerator
10
from xml.sax.saxutils import XMLGenerator
10
from xml.sax.saxutils import quoteattr
11
from xml.sax.saxutils import quoteattr
11
12
13
import javatoolkit.xml.sax
14
12
class SaxRewriter(XMLGenerator):
15
class SaxRewriter(XMLGenerator):
13
    """
16
    """
14
    Using Sax gives us the support for writing back doctypes and all easily
17
    Using Sax gives us the support for writing back doctypes and all easily
Lines 124-131 class SaxRewriter(XMLGenerator): Link Here
124
127
125
    def process(self, in_stream, callback):
128
    def process(self, in_stream, callback):
126
        self.startElement = callback
129
        self.startElement = callback
127
        from xml.sax import parseString
130
        javatoolkit.xml.sax.parse_string(in_stream.encode('UTF8'), content_handler=self, features={xml.sax.handler.feature_external_ges: 1})
128
        parseString(in_stream.encode('UTF8'), self)
129
        self.p('\n')
131
        self.p('\n')
130
132
131
# vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap:
133
# vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap:
(-)a/src/py/javatoolkit/xml/sax.py (+44 lines)
Line 0 Link Here
1
# Copyright 2020 Gentoo Authors
2
# Distributed under the terms of the GNU General Public License v2
3
4
import io
5
import xml.sax
6
import xml.sax.xmlreader
7
8
def _make_parser(*, content_handler=None, dtd_handler=None, entity_resolver=None, error_handler=None, locale=None, features=None, properties=None):
9
    parser = xml.sax.make_parser()
10
11
    if content_handler is not None:
12
        parser.setContentHandler(content_handler)
13
    if dtd_handler is not None:
14
        parser.setDTDHandler(dtd_handler)
15
    if entity_resolver is not None:
16
        parser.setEntityResolver(entity_resolver)
17
    if error_handler is not None:
18
        parser.setErrorHandler(error_handler)
19
    if locale is not None:
20
        parser.setLocale(locale)
21
    if features is not None:
22
        for feature, value in features.items():
23
            parser.setFeature(feature, value)
24
    if properties is not None:
25
        for property, value in properties.items():
26
            parser.setProperty(property, value)
27
28
    return parser
29
30
def parse(source, *, content_handler=None, dtd_handler=None, entity_resolver=None, error_handler=None, locale=None, features=None, properties=None):
31
    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)
32
33
    parser.parse(source)
34
35
def parse_string(string, *, content_handler=None, dtd_handler=None, entity_resolver=None, error_handler=None, locale=None, features=None, properties=None):
36
    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)
37
38
    inputsource = xml.sax.xmlreader.InputSource()
39
    if isinstance(string, str):
40
        inputsource.setCharacterStream(io.StringIO(string))
41
    else:
42
        inputsource.setByteStream(io.BytesIO(string))
43
44
    parser.parse(inputsource)
(-)a/src/py/xml-rewrite-2.py (-8 / +10 lines)
Lines 1-13 Link Here
1
#!/usr/bin/env python3
1
#!/usr/bin/env python3
2
# Copyright 2004-2006 Gentoo Foundation
2
# Copyright 2004-2020 Gentoo Authors
3
# Distributed under the terms of the GNU General Public Licence v2
3
# Distributed under the terms of the GNU General Public License v2
4
4
5
5
6
import sys
7
import io
6
import io
8
from xml.sax.saxutils import quoteattr, escape
7
import sys
8
9
from optparse import OptionParser, make_option
9
from optparse import OptionParser, make_option
10
from xml.sax.saxutils import XMLGenerator
10
11
import xml.sax.handler
12
from xml.sax.saxutils import XMLGenerator, escape, quoteattr
13
14
import javatoolkit.xml.sax
11
15
12
16
13
def add_gentoo_classpath(document):
17
def add_gentoo_classpath(document):
Lines 190-197 class SaxRewriter(XMLGenerator, StreamRewriterBase): Link Here
190
        XMLGenerator.__init__(self, self.buffer, 'UTF-8')
194
        XMLGenerator.__init__(self, self.buffer, 'UTF-8')
191
195
192
    def process(self, in_stream):
196
    def process(self, in_stream):
193
        from xml.sax import parse
197
        javatoolkit.xml.sax.parse(in_stream, content_handler=self, features={xml.sax.handler.feature_external_ges: 1})
194
        parse(in_stream, self)
195
        self.p('\n')
198
        self.p('\n')
196
199
197
    def startElement(self, name, attrs):
200
    def startElement(self, name, attrs):
198
- 

Return to bug 698954