~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml_serializer.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-23 09:00:18 UTC
  • mfrom: (5439.1.1 merge-2.2-into-devel)
  • Revision ID: pqm@pqm.ubuntu.com-20100923090018-01aj4ifyzaps50bo
(spiv) Merge lp:bzr/2.2. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
# importing this module is fairly slow because it has to load several
23
23
# ElementTree bits
24
24
 
 
25
import re
 
26
 
25
27
from bzrlib.serializer import Serializer
26
28
from bzrlib.trace import mutter
27
29
 
31
33
        from xml.etree.cElementTree import (ElementTree, SubElement, Element,
32
34
            XMLTreeBuilder, fromstring, tostring)
33
35
        import xml.etree as elementtree
 
36
        # Also import ElementTree module so monkey-patching below always works
 
37
        import xml.etree.ElementTree
34
38
    except ImportError:
35
39
        from cElementTree import (ElementTree, SubElement, Element,
36
40
                                  XMLTreeBuilder, fromstring, tostring)
111
115
        return ElementTree().parse(f)
112
116
 
113
117
 
114
 
# performance tuning for elementree's serialiser. This should be
115
 
# sent upstream - RBC 20060523.
116
 
# the functions here are patched into elementtree at runtime.
117
 
import re
118
 
escape_re = re.compile("[&'\"<>]")
119
 
escape_map = {
120
 
    "&":'&amp;',
121
 
    "'":"&apos;", # FIXME: overkill
122
 
    "\"":"&quot;",
123
 
    "<":"&lt;",
124
 
    ">":"&gt;",
125
 
    }
126
 
def _escape_replace(match, map=escape_map):
127
 
    return map[match.group()]
128
 
 
129
 
def _escape_attrib(text, encoding=None, replace=None):
130
 
    # escape attribute value
131
 
    try:
132
 
        if encoding:
133
 
            try:
134
 
                text = elementtree.ElementTree._encode(text, encoding)
135
 
            except UnicodeError:
136
 
                return elementtree.ElementTree._encode_entity(text)
137
 
        if replace is None:
138
 
            return escape_re.sub(_escape_replace, text)
139
 
        else:
140
 
            text = replace(text, "&", "&amp;")
141
 
            text = replace(text, "'", "&apos;") # FIXME: overkill
142
 
            text = replace(text, "\"", "&quot;")
143
 
            text = replace(text, "<", "&lt;")
144
 
            text = replace(text, ">", "&gt;")
145
 
            return text
146
 
    except (TypeError, AttributeError):
147
 
        elementtree.ElementTree._raise_serialization_error(text)
148
 
 
149
 
elementtree.ElementTree._escape_attrib = _escape_attrib
150
 
 
151
 
escape_cdata_re = re.compile("[&<>]")
152
 
escape_cdata_map = {
153
 
    "&":'&amp;',
154
 
    "<":"&lt;",
155
 
    ">":"&gt;",
156
 
    }
157
 
def _escape_cdata_replace(match, map=escape_cdata_map):
158
 
    return map[match.group()]
159
 
 
160
 
def _escape_cdata(text, encoding=None, replace=None):
161
 
    # escape character data
162
 
    try:
163
 
        if encoding:
164
 
            try:
165
 
                text = elementtree.ElementTree._encode(text, encoding)
166
 
            except UnicodeError:
167
 
                return elementtree.ElementTree._encode_entity(text)
168
 
        if replace is None:
169
 
            return escape_cdata_re.sub(_escape_cdata_replace, text)
170
 
        else:
171
 
            text = replace(text, "&", "&amp;")
172
 
            text = replace(text, "<", "&lt;")
173
 
            text = replace(text, ">", "&gt;")
174
 
            return text
175
 
    except (TypeError, AttributeError):
176
 
        elementtree.ElementTree._raise_serialization_error(text)
177
 
 
178
 
elementtree.ElementTree._escape_cdata = _escape_cdata
179
 
 
180
 
 
181
118
def escape_invalid_chars(message):
182
119
    """Escape the XML-invalid characters in a commit message.
183
120