~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-02-11 04:02:41 UTC
  • mfrom: (5017.2.2 tariff)
  • Revision ID: pqm@pqm.ubuntu.com-20100211040241-w6n021dz0uus341n
(mbp) add import-tariff tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
22
22
# importing this module is fairly slow because it has to load several
23
23
# ElementTree bits
24
24
 
25
 
import re
26
 
 
27
25
from bzrlib.serializer import Serializer
28
 
from bzrlib.trace import mutter
 
26
from bzrlib.trace import mutter, warning
29
27
 
30
28
try:
31
29
    try:
85
83
 
86
84
    def read_inventory(self, f, revision_id=None):
87
85
        try:
88
 
            try:
89
 
                return self._unpack_inventory(self._read_element(f),
90
 
                    revision_id=None)
91
 
            finally:
92
 
                f.close()
 
86
            return self._unpack_inventory(self._read_element(f),
 
87
                revision_id=None)
93
88
        except ParseError, e:
94
89
            raise errors.UnexpectedInventoryFormat(e)
95
90
 
113
108
        return ElementTree().parse(f)
114
109
 
115
110
 
 
111
# performance tuning for elementree's serialiser. This should be
 
112
# sent upstream - RBC 20060523.
 
113
# the functions here are patched into elementtree at runtime.
 
114
import re
 
115
escape_re = re.compile("[&'\"<>]")
 
116
escape_map = {
 
117
    "&":'&amp;',
 
118
    "'":"&apos;", # FIXME: overkill
 
119
    "\"":"&quot;",
 
120
    "<":"&lt;",
 
121
    ">":"&gt;",
 
122
    }
 
123
def _escape_replace(match, map=escape_map):
 
124
    return map[match.group()]
 
125
 
 
126
def _escape_attrib(text, encoding=None, replace=None):
 
127
    # escape attribute value
 
128
    try:
 
129
        if encoding:
 
130
            try:
 
131
                text = elementtree.ElementTree._encode(text, encoding)
 
132
            except UnicodeError:
 
133
                return elementtree.ElementTree._encode_entity(text)
 
134
        if replace is None:
 
135
            return escape_re.sub(_escape_replace, text)
 
136
        else:
 
137
            text = replace(text, "&", "&amp;")
 
138
            text = replace(text, "'", "&apos;") # FIXME: overkill
 
139
            text = replace(text, "\"", "&quot;")
 
140
            text = replace(text, "<", "&lt;")
 
141
            text = replace(text, ">", "&gt;")
 
142
            return text
 
143
    except (TypeError, AttributeError):
 
144
        elementtree.ElementTree._raise_serialization_error(text)
 
145
 
 
146
elementtree.ElementTree._escape_attrib = _escape_attrib
 
147
 
 
148
escape_cdata_re = re.compile("[&<>]")
 
149
escape_cdata_map = {
 
150
    "&":'&amp;',
 
151
    "<":"&lt;",
 
152
    ">":"&gt;",
 
153
    }
 
154
def _escape_cdata_replace(match, map=escape_cdata_map):
 
155
    return map[match.group()]
 
156
 
 
157
def _escape_cdata(text, encoding=None, replace=None):
 
158
    # escape character data
 
159
    try:
 
160
        if encoding:
 
161
            try:
 
162
                text = elementtree.ElementTree._encode(text, encoding)
 
163
            except UnicodeError:
 
164
                return elementtree.ElementTree._encode_entity(text)
 
165
        if replace is None:
 
166
            return escape_cdata_re.sub(_escape_cdata_replace, text)
 
167
        else:
 
168
            text = replace(text, "&", "&amp;")
 
169
            text = replace(text, "<", "&lt;")
 
170
            text = replace(text, ">", "&gt;")
 
171
            return text
 
172
    except (TypeError, AttributeError):
 
173
        elementtree.ElementTree._raise_serialization_error(text)
 
174
 
 
175
elementtree.ElementTree._escape_cdata = _escape_cdata
 
176
 
 
177
 
116
178
def escape_invalid_chars(message):
117
179
    """Escape the XML-invalid characters in a commit message.
118
180