~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml_serializer.py

  • Committer: Jelmer Vernooij
  • Date: 2010-12-20 02:23:31 UTC
  • mto: This revision was merged to the branch mainline in revision 5577.
  • Revision ID: jelmer@samba.org-20101220022331-hbm91o1ps9gjrlns
Add testr magic for bzr selftest --list

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
 
113
115
        return ElementTree().parse(f)
114
116
 
115
117
 
116
 
# performance tuning for elementree's serialiser. This should be
117
 
# sent upstream - RBC 20060523.
118
 
# the functions here are patched into elementtree at runtime.
119
 
import re
120
 
escape_re = re.compile("[&'\"<>]")
121
 
escape_map = {
122
 
    "&":'&amp;',
123
 
    "'":"&apos;", # FIXME: overkill
124
 
    "\"":"&quot;",
125
 
    "<":"&lt;",
126
 
    ">":"&gt;",
127
 
    }
128
 
def _escape_replace(match, map=escape_map):
129
 
    return map[match.group()]
130
 
 
131
 
def _escape_attrib(text, encoding=None, replace=None):
132
 
    # escape attribute value
133
 
    try:
134
 
        if encoding:
135
 
            try:
136
 
                text = elementtree.ElementTree._encode(text, encoding)
137
 
            except UnicodeError:
138
 
                return elementtree.ElementTree._encode_entity(text)
139
 
        if replace is None:
140
 
            return escape_re.sub(_escape_replace, text)
141
 
        else:
142
 
            text = replace(text, "&", "&amp;")
143
 
            text = replace(text, "'", "&apos;") # FIXME: overkill
144
 
            text = replace(text, "\"", "&quot;")
145
 
            text = replace(text, "<", "&lt;")
146
 
            text = replace(text, ">", "&gt;")
147
 
            return text
148
 
    except (TypeError, AttributeError):
149
 
        elementtree.ElementTree._raise_serialization_error(text)
150
 
 
151
 
elementtree.ElementTree._escape_attrib = _escape_attrib
152
 
 
153
 
escape_cdata_re = re.compile("[&<>]")
154
 
escape_cdata_map = {
155
 
    "&":'&amp;',
156
 
    "<":"&lt;",
157
 
    ">":"&gt;",
158
 
    }
159
 
def _escape_cdata_replace(match, map=escape_cdata_map):
160
 
    return map[match.group()]
161
 
 
162
 
def _escape_cdata(text, encoding=None, replace=None):
163
 
    # escape character data
164
 
    try:
165
 
        if encoding:
166
 
            try:
167
 
                text = elementtree.ElementTree._encode(text, encoding)
168
 
            except UnicodeError:
169
 
                return elementtree.ElementTree._encode_entity(text)
170
 
        if replace is None:
171
 
            return escape_cdata_re.sub(_escape_cdata_replace, text)
172
 
        else:
173
 
            text = replace(text, "&", "&amp;")
174
 
            text = replace(text, "<", "&lt;")
175
 
            text = replace(text, ">", "&gt;")
176
 
            return text
177
 
    except (TypeError, AttributeError):
178
 
        elementtree.ElementTree._raise_serialization_error(text)
179
 
 
180
 
elementtree.ElementTree._escape_cdata = _escape_cdata
181
 
 
182
 
 
183
118
def escape_invalid_chars(message):
184
119
    """Escape the XML-invalid characters in a commit message.
185
120