1
# Copyright (C) 2005, 2006 Canonical Ltd
2
# -*- coding: UTF-8 -*-
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.
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.
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
17
18
"""XML externalization support."""
19
# "XML is like violence: if it doesn't solve your problem, you aren't
20
# using enough of it." -- various
22
# importing this module is fairly slow because it has to load several
25
from bzrlib.trace import mutter, warning
21
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
22
__author__ = "Martin Pool <mbp@canonical.com>"
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
34
from cElementTree import (ElementTree, SubElement, Element,
35
XMLTreeBuilder, fromstring, tostring)
37
ParseError = SyntaxError
25
from cElementTree import Element, ElementTree, SubElement
38
26
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
42
from util.elementtree.ElementTree import (ElementTree, SubElement,
43
Element, XMLTreeBuilder,
45
import util.elementtree as elementtree
46
from xml.parsers.expat import ExpatError as ParseError
48
from bzrlib import errors
51
class Serializer(object):
52
"""Abstract object serialize/deserialize"""
53
def write_inventory(self, inv, f):
54
"""Write inventory to a file"""
55
elt = self._pack_inventory(inv)
56
self._write_element(elt, f)
58
def write_inventory_to_string(self, inv):
59
return tostring(self._pack_inventory(inv)) + '\n'
61
def read_inventory_from_string(self, xml_string):
63
return self._unpack_inventory(fromstring(xml_string))
65
raise errors.UnexpectedInventoryFormat(e)
67
def read_inventory(self, f):
69
return self._unpack_inventory(self._read_element(f))
71
raise errors.UnexpectedInventoryFormat(e)
73
def write_revision(self, rev, f):
74
self._write_element(self._pack_revision(rev), f)
76
def write_revision_to_string(self, rev):
77
return tostring(self._pack_revision(rev)) + '\n'
79
def read_revision(self, f):
80
return self._unpack_revision(self._read_element(f))
82
def read_revision_from_string(self, xml_string):
83
return self._unpack_revision(fromstring(xml_string))
85
def _write_element(self, elt, f):
86
ElementTree(elt).write(f, 'utf-8')
27
from elementtree import Element, ElementTree, SubElement
30
from trace import mutter
34
raise Exception("XMLMixin.to_element must be overridden in concrete classes")
36
def write_xml(self, f):
37
ElementTree(self.to_element()).write(f, 'utf-8')
89
def _read_element(self, f):
90
return ElementTree().parse(f)
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.
97
escape_re = re.compile("[&'\"<>]")
100
"'":"'", # FIXME: overkill
105
def _escape_replace(match, map=escape_map):
106
return map[match.group()]
108
def _escape_attrib(text, encoding=None, replace=None):
109
# escape attribute value
113
text = elementtree.ElementTree._encode(text, encoding)
115
return elementtree.ElementTree._encode_entity(text)
117
return escape_re.sub(_escape_replace, text)
119
text = replace(text, "&", "&")
120
text = replace(text, "'", "'") # FIXME: overkill
121
text = replace(text, "\"", """)
122
text = replace(text, "<", "<")
123
text = replace(text, ">", ">")
125
except (TypeError, AttributeError):
126
elementtree.ElementTree._raise_serialization_error(text)
128
elementtree.ElementTree._escape_attrib = _escape_attrib
130
escape_cdata_re = re.compile("[&<>]")
136
def _escape_cdata_replace(match, map=escape_cdata_map):
137
return map[match.group()]
139
def _escape_cdata(text, encoding=None, replace=None):
140
# escape character data
144
text = elementtree.ElementTree._encode(text, encoding)
146
return elementtree.ElementTree._encode_entity(text)
148
return escape_cdata_re.sub(_escape_cdata_replace, text)
150
text = replace(text, "&", "&")
151
text = replace(text, "<", "<")
152
text = replace(text, ">", ">")
154
except (TypeError, AttributeError):
155
elementtree.ElementTree._raise_serialization_error(text)
157
elementtree.ElementTree._escape_cdata = _escape_cdata
41
return cls.from_element(ElementTree().parse(f))
43
read_xml = classmethod(read_xml)