Package xml ::
Package sax ::
Module saxutils
|
|
1 """\
2 A library of useful helper classes to the SAX classes, for the
3 convenience of application and driver writers.
4 """
5
6 import os, urlparse, urllib, types
7 import handler
8 import xmlreader
9
10 try:
11 _StringTypes = [types.StringType, types.UnicodeType]
12 except AttributeError:
13 _StringTypes = [types.StringType]
14
15
16
17 try:
18 from codecs import xmlcharrefreplace_errors
19 _error_handling = "xmlcharrefreplace"
20 del xmlcharrefreplace_errors
21 except ImportError:
22 _error_handling = "strict"
23
25 """Replace substrings of a string using a dictionary."""
26 for key, value in d.items():
27 s = s.replace(key, value)
28 return s
29
31 """Escape &, <, and > in a string of data.
32
33 You can escape other strings of data by passing a dictionary as
34 the optional entities parameter. The keys and values must all be
35 strings; each key will be replaced with its corresponding value.
36 """
37
38
39 data = data.replace("&", "&")
40 data = data.replace(">", ">")
41 data = data.replace("<", "<")
42 if entities:
43 data = __dict_replace(data, entities)
44 return data
45
47 """Unescape &, <, and > in a string of data.
48
49 You can unescape other strings of data by passing a dictionary as
50 the optional entities parameter. The keys and values must all be
51 strings; each key will be replaced with its corresponding value.
52 """
53 data = data.replace("<", "<")
54 data = data.replace(">", ">")
55 if entities:
56 data = __dict_replace(data, entities)
57
58 return data.replace("&", "&")
59
61 """Escape and quote an attribute value.
62
63 Escape &, <, and > in a string of data, then quote it for use as
64 an attribute value. The \" character will be escaped as well, if
65 necessary.
66
67 You can escape other strings of data by passing a dictionary as
68 the optional entities parameter. The keys and values must all be
69 strings; each key will be replaced with its corresponding value.
70 """
71 entities = entities.copy()
72 entities.update({'\n': ' ', '\r': ' ', '\t':'	'})
73 data = escape(data, entities)
74 if '"' in data:
75 if "'" in data:
76 data = '"%s"' % data.replace('"', """)
77 else:
78 data = "'%s'" % data
79 else:
80 data = '"%s"' % data
81 return data
82
83
85
87 if out is None:
88 import sys
89 out = sys.stdout
90 handler.ContentHandler.__init__(self)
91 self._out = out
92 self._ns_contexts = [{}]
93 self._current_context = self._ns_contexts[-1]
94 self._undeclared_ns_maps = []
95 self._encoding = encoding
96
102
103
104
106 self._write('<?xml version="1.0" encoding="%s"?>\n' %
107 self._encoding)
108
110 self._ns_contexts.append(self._current_context.copy())
111 self._current_context[uri] = prefix
112 self._undeclared_ns_maps.append((prefix, uri))
113
115 self._current_context = self._ns_contexts[-1]
116 del self._ns_contexts[-1]
117
119 self._write('<' + name)
120 for (name, value) in attrs.items():
121 self._write(' %s=%s' % (name, quoteattr(value)))
122 self._write('>')
123
125 self._write('</%s>' % name)
126
128 if name[0] is None:
129
130 name = name[1]
131 else:
132
133 name = self._current_context[name[0]] + ":" + name[1]
134 self._write('<' + name)
135
136 for pair in self._undeclared_ns_maps:
137 self._write(' xmlns:%s="%s"' % pair)
138 self._undeclared_ns_maps = []
139
140 for (name, value) in attrs.items():
141 name = self._current_context[name[0]] + ":" + name[1]
142 self._write(' %s=%s' % (name, quoteattr(value)))
143 self._write('>')
144
151
153 self._write(escape(content))
154
157
160
161
163 """This class is designed to sit between an XMLReader and the
164 client application's event handlers. By default, it does nothing
165 but pass requests up to the reader and events on to the handlers
166 unmodified, but subclasses can override specific methods to modify
167 the event stream or the configuration requests as they pass
168 through."""
169
171 xmlreader.XMLReader.__init__(self)
172 self._parent = parent
173
174
175
176 - def error(self, exception):
177 self._err_handler.error(exception)
178
180 self._err_handler.fatalError(exception)
181
183 self._err_handler.warning(exception)
184
185
186
188 self._cont_handler.setDocumentLocator(locator)
189
191 self._cont_handler.startDocument()
192
194 self._cont_handler.endDocument()
195
197 self._cont_handler.startPrefixMapping(prefix, uri)
198
200 self._cont_handler.endPrefixMapping(prefix)
201
204
207
209 self._cont_handler.startElementNS(name, qname, attrs)
210
212 self._cont_handler.endElementNS(name, qname)
213
216
218 self._cont_handler.ignorableWhitespace(chars)
219
221 self._cont_handler.processingInstruction(target, data)
222
224 self._cont_handler.skippedEntity(name)
225
226
227
229 self._dtd_handler.notationDecl(name, publicId, systemId)
230
232 self._dtd_handler.unparsedEntityDecl(name, publicId, systemId, ndata)
233
234
235
237 return self._ent_handler.resolveEntity(publicId, systemId)
238
239
240
241 - def parse(self, source):
242 self._parent.setContentHandler(self)
243 self._parent.setErrorHandler(self)
244 self._parent.setEntityResolver(self)
245 self._parent.setDTDHandler(self)
246 self._parent.parse(source)
247
249 self._parent.setLocale(locale)
250
252 return self._parent.getFeature(name)
253
256
258 return self._parent.getProperty(name)
259
262
263
264
267
269 self._parent = parent
270
271
272
300