~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-01-21 02:43:10 UTC
  • mfrom: (4976.1.3 ghost-diffs)
  • Revision ID: pqm@pqm.ubuntu.com-20100121024310-y0jq14xdry72ktxl
(Jelmer) Deal with ghosts in 'bzr diff'.

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:
33
31
        from xml.etree.cElementTree import (ElementTree, SubElement, Element,
34
32
            XMLTreeBuilder, fromstring, tostring)
35
33
        import xml.etree as elementtree
36
 
        # Also import ElementTree module so monkey-patching below always works
37
 
        import xml.etree.ElementTree
38
34
    except ImportError:
39
35
        from cElementTree import (ElementTree, SubElement, Element,
40
36
                                  XMLTreeBuilder, fromstring, tostring)
87
83
 
88
84
    def read_inventory(self, f, revision_id=None):
89
85
        try:
90
 
            try:
91
 
                return self._unpack_inventory(self._read_element(f),
92
 
                    revision_id=None)
93
 
            finally:
94
 
                f.close()
 
86
            return self._unpack_inventory(self._read_element(f),
 
87
                revision_id=None)
95
88
        except ParseError, e:
96
89
            raise errors.UnexpectedInventoryFormat(e)
97
90
 
115
108
        return ElementTree().parse(f)
116
109
 
117
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
 
118
178
def escape_invalid_chars(message):
119
179
    """Escape the XML-invalid characters in a commit message.
120
180