~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml.py

  • Committer: Robert Collins
  • Date: 2005-10-06 22:15:52 UTC
  • mfrom: (1185.13.2)
  • mto: This revision was merged to the branch mainline in revision 1420.
  • Revision ID: robertc@robertcollins.net-20051006221552-9b15c96fa504e0ad
mergeĀ fromĀ upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
2
 
#
 
1
#! /usr/bin/env python
 
2
# -*- coding: UTF-8 -*-
 
3
 
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
5
6
# the Free Software Foundation; either version 2 of the License, or
6
7
# (at your option) any later version.
7
 
#
 
8
 
8
9
# This program is distributed in the hope that it will be useful,
9
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
12
# GNU General Public License for more details.
12
 
#
 
13
 
13
14
# You should have received a copy of the GNU General Public License
14
15
# along with this program; if not, write to the Free Software
15
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
26
from bzrlib.trace import mutter, warning
26
27
 
27
28
try:
28
 
    try:
29
 
        # it's in this package in python2.5
30
 
        from xml.etree.cElementTree import (ElementTree, SubElement, Element,
31
 
            XMLTreeBuilder, fromstring, tostring)
32
 
        import xml.etree as elementtree
33
 
    except ImportError:
34
 
        from cElementTree import (ElementTree, SubElement, Element,
35
 
                                  XMLTreeBuilder, fromstring, tostring)
36
 
        import elementtree
37
 
    ParseError = SyntaxError
 
29
    from cElementTree import (ElementTree, SubElement, Element,
 
30
                              XMLTreeBuilder, fromstring, tostring)
38
31
except ImportError:
39
 
    mutter('WARNING: using slower ElementTree; consider installing cElementTree'
40
 
           " and make sure it's on your PYTHONPATH")
41
 
    # this copy is shipped with bzr
 
32
    ## from warnings import warn
 
33
    ## warn('using slower ElementTree; consider installing cElementTree')
42
34
    from util.elementtree.ElementTree import (ElementTree, SubElement,
43
35
                                              Element, XMLTreeBuilder,
44
36
                                              fromstring, tostring)
45
 
    import util.elementtree as elementtree
46
 
    from xml.parsers.expat import ExpatError as ParseError
47
37
 
48
 
from bzrlib import errors
 
38
from bzrlib.errors import BzrError
49
39
 
50
40
 
51
41
class Serializer(object):
56
46
        self._write_element(elt, f)
57
47
 
58
48
    def write_inventory_to_string(self, inv):
59
 
        return tostring(self._pack_inventory(inv)) + '\n'
 
49
        return tostring(self._pack_inventory(inv))
60
50
 
61
51
    def read_inventory_from_string(self, xml_string):
62
 
        try:
63
 
            return self._unpack_inventory(fromstring(xml_string))
64
 
        except ParseError, e:
65
 
            raise errors.UnexpectedInventoryFormat(e)
 
52
        return self._unpack_inventory(fromstring(xml_string))
66
53
 
67
54
    def read_inventory(self, f):
68
 
        try:
69
 
            return self._unpack_inventory(self._read_element(f))
70
 
        except ParseError, e:
71
 
            raise errors.UnexpectedInventoryFormat(e)
 
55
        return self._unpack_inventory(self._read_element(f))
72
56
 
73
57
    def write_revision(self, rev, f):
74
58
        self._write_element(self._pack_revision(rev), f)
75
59
 
76
60
    def write_revision_to_string(self, rev):
77
 
        return tostring(self._pack_revision(rev)) + '\n'
 
61
        return tostring(self._pack_revision(rev), f)
78
62
 
79
63
    def read_revision(self, f):
80
64
        return self._unpack_revision(self._read_element(f))
88
72
 
89
73
    def _read_element(self, f):
90
74
        return ElementTree().parse(f)
91
 
 
92
 
 
93
 
# performance tuning for elementree's serialiser. This should be
94
 
# sent upstream - RBC 20060523.
95
 
# the functions here are patched into elementtree at runtime.
96
 
import re
97
 
escape_re = re.compile("[&'\"<>]")
98
 
escape_map = {
99
 
    "&":'&amp;',
100
 
    "'":"&apos;", # FIXME: overkill
101
 
    "\"":"&quot;",
102
 
    "<":"&lt;",
103
 
    ">":"&gt;",
104
 
    }
105
 
def _escape_replace(match, map=escape_map):
106
 
    return map[match.group()]
107
 
 
108
 
def _escape_attrib(text, encoding=None, replace=None):
109
 
    # escape attribute value
110
 
    try:
111
 
        if encoding:
112
 
            try:
113
 
                text = elementtree.ElementTree._encode(text, encoding)
114
 
            except UnicodeError:
115
 
                return elementtree.ElementTree._encode_entity(text)
116
 
        if replace is None:
117
 
            return escape_re.sub(_escape_replace, text)
118
 
        else:
119
 
            text = replace(text, "&", "&amp;")
120
 
            text = replace(text, "'", "&apos;") # FIXME: overkill
121
 
            text = replace(text, "\"", "&quot;")
122
 
            text = replace(text, "<", "&lt;")
123
 
            text = replace(text, ">", "&gt;")
124
 
            return text
125
 
    except (TypeError, AttributeError):
126
 
        elementtree.ElementTree._raise_serialization_error(text)
127
 
 
128
 
elementtree.ElementTree._escape_attrib = _escape_attrib
129
 
 
130
 
escape_cdata_re = re.compile("[&<>]")
131
 
escape_cdata_map = {
132
 
    "&":'&amp;',
133
 
    "<":"&lt;",
134
 
    ">":"&gt;",
135
 
    }
136
 
def _escape_cdata_replace(match, map=escape_cdata_map):
137
 
    return map[match.group()]
138
 
 
139
 
def _escape_cdata(text, encoding=None, replace=None):
140
 
    # escape character data
141
 
    try:
142
 
        if encoding:
143
 
            try:
144
 
                text = elementtree.ElementTree._encode(text, encoding)
145
 
            except UnicodeError:
146
 
                return elementtree.ElementTree._encode_entity(text)
147
 
        if replace is None:
148
 
            return escape_cdata_re.sub(_escape_cdata_replace, text)
149
 
        else:
150
 
            text = replace(text, "&", "&amp;")
151
 
            text = replace(text, "<", "&lt;")
152
 
            text = replace(text, ">", "&gt;")
153
 
            return text
154
 
    except (TypeError, AttributeError):
155
 
        elementtree.ElementTree._raise_serialization_error(text)
156
 
 
157
 
elementtree.ElementTree._escape_cdata = _escape_cdata