/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/xml_serializer.py

  • Committer: John Arbash Meinel
  • Date: 2011-04-20 09:46:28 UTC
  • mfrom: (5609.33.4 2.3)
  • mto: (5609.33.5 2.3)
  • mto: This revision was merged to the branch mainline in revision 5811.
  • Revision ID: john@arbash-meinel.com-20110420094628-l0bafq1lwb6ib1v2
Merge lp:bzr/2.3 @ 5640 so we can update the release notes (aka NEWS)

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