Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 1 | """\ |
| 2 | A library of useful helper classes to the SAX classes, for the |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 3 | convenience of application and driver writers. |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 4 | """ |
| 5 | |
Martin v. Löwis | 58af43f | 2000-09-24 21:31:06 +0000 | [diff] [blame] | 6 | import os, urlparse, urllib, types |
Guido van Rossum | 3b27105 | 2006-08-17 09:10:09 +0000 | [diff] [blame^] | 7 | from . import handler |
| 8 | from . import xmlreader |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 9 | |
Fred Drake | 95b4ec5 | 2000-12-16 01:45:11 +0000 | [diff] [blame] | 10 | try: |
| 11 | _StringTypes = [types.StringType, types.UnicodeType] |
| 12 | except AttributeError: |
| 13 | _StringTypes = [types.StringType] |
| 14 | |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 15 | # See whether the xmlcharrefreplace error handler is |
| 16 | # supported |
| 17 | try: |
| 18 | from codecs import xmlcharrefreplace_errors |
| 19 | _error_handling = "xmlcharrefreplace" |
| 20 | del xmlcharrefreplace_errors |
| 21 | except ImportError: |
| 22 | _error_handling = "strict" |
| 23 | |
Martin v. Löwis | 74b51ac | 2002-10-26 14:50:45 +0000 | [diff] [blame] | 24 | def __dict_replace(s, d): |
| 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 |
Martin v. Löwis | 58af43f | 2000-09-24 21:31:06 +0000 | [diff] [blame] | 29 | |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 30 | def escape(data, entities={}): |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 31 | """Escape &, <, and > in a string of data. |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 32 | |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 33 | You can escape other strings of data by passing a dictionary as |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 34 | the optional entities parameter. The keys and values must all be |
| 35 | strings; each key will be replaced with its corresponding value. |
| 36 | """ |
Martin v. Löwis | 74b51ac | 2002-10-26 14:50:45 +0000 | [diff] [blame] | 37 | |
| 38 | # must do ampersand first |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 39 | data = data.replace("&", "&") |
Fred Drake | f55222d | 2002-10-28 17:29:01 +0000 | [diff] [blame] | 40 | data = data.replace(">", ">") |
| 41 | data = data.replace("<", "<") |
| 42 | if entities: |
| 43 | data = __dict_replace(data, entities) |
| 44 | return data |
Martin v. Löwis | 74b51ac | 2002-10-26 14:50:45 +0000 | [diff] [blame] | 45 | |
| 46 | def unescape(data, entities={}): |
| 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 | """ |
Fred Drake | f55222d | 2002-10-28 17:29:01 +0000 | [diff] [blame] | 53 | data = data.replace("<", "<") |
| 54 | data = data.replace(">", ">") |
Fred Drake | f55222d | 2002-10-28 17:29:01 +0000 | [diff] [blame] | 55 | if entities: |
| 56 | data = __dict_replace(data, entities) |
Fred Drake | 407fea5 | 2002-10-28 17:46:59 +0000 | [diff] [blame] | 57 | # must do ampersand last |
Fred Drake | 6d89050 | 2002-10-28 18:09:41 +0000 | [diff] [blame] | 58 | return data.replace("&", "&") |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 59 | |
Fred Drake | acd32d3 | 2001-07-19 16:10:15 +0000 | [diff] [blame] | 60 | def quoteattr(data, entities={}): |
| 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 | """ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 71 | entities = entities.copy() |
| 72 | entities.update({'\n': ' ', '\r': ' ', '\t':'	'}) |
Fred Drake | acd32d3 | 2001-07-19 16:10:15 +0000 | [diff] [blame] | 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 | |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 83 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 84 | class XMLGenerator(handler.ContentHandler): |
| 85 | |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 86 | def __init__(self, out=None, encoding="iso-8859-1"): |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 87 | if out is None: |
| 88 | import sys |
| 89 | out = sys.stdout |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 90 | handler.ContentHandler.__init__(self) |
| 91 | self._out = out |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 92 | self._ns_contexts = [{}] # contains uri -> prefix dicts |
| 93 | self._current_context = self._ns_contexts[-1] |
Lars Gustäbel | fc643c3 | 2000-09-24 10:53:31 +0000 | [diff] [blame] | 94 | self._undeclared_ns_maps = [] |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 95 | self._encoding = encoding |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 96 | |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 97 | def _write(self, text): |
| 98 | if isinstance(text, str): |
| 99 | self._out.write(text) |
| 100 | else: |
| 101 | self._out.write(text.encode(self._encoding, _error_handling)) |
| 102 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 103 | # ContentHandler methods |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 104 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 105 | def startDocument(self): |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 106 | self._write('<?xml version="1.0" encoding="%s"?>\n' % |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 107 | self._encoding) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 108 | |
| 109 | def startPrefixMapping(self, prefix, uri): |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 110 | self._ns_contexts.append(self._current_context.copy()) |
| 111 | self._current_context[uri] = prefix |
Lars Gustäbel | fc643c3 | 2000-09-24 10:53:31 +0000 | [diff] [blame] | 112 | self._undeclared_ns_maps.append((prefix, uri)) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 113 | |
| 114 | def endPrefixMapping(self, prefix): |
Lars Gustäbel | fc643c3 | 2000-09-24 10:53:31 +0000 | [diff] [blame] | 115 | self._current_context = self._ns_contexts[-1] |
| 116 | del self._ns_contexts[-1] |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 117 | |
| 118 | def startElement(self, name, attrs): |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 119 | self._write('<' + name) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 120 | for (name, value) in attrs.items(): |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 121 | self._write(' %s=%s' % (name, quoteattr(value))) |
| 122 | self._write('>') |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 123 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 124 | def endElement(self, name): |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 125 | self._write('</%s>' % name) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 126 | |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 127 | def startElementNS(self, name, qname, attrs): |
Martin v. Löwis | cf0a1cc | 2000-10-03 22:35:29 +0000 | [diff] [blame] | 128 | if name[0] is None: |
| 129 | # if the name was not namespace-scoped, use the unqualified part |
| 130 | name = name[1] |
| 131 | else: |
| 132 | # else try to restore the original prefix from the namespace |
| 133 | name = self._current_context[name[0]] + ":" + name[1] |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 134 | self._write('<' + name) |
Lars Gustäbel | fc643c3 | 2000-09-24 10:53:31 +0000 | [diff] [blame] | 135 | |
| 136 | for pair in self._undeclared_ns_maps: |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 137 | self._write(' xmlns:%s="%s"' % pair) |
Lars Gustäbel | fc643c3 | 2000-09-24 10:53:31 +0000 | [diff] [blame] | 138 | self._undeclared_ns_maps = [] |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 139 | |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 140 | for (name, value) in attrs.items(): |
| 141 | name = self._current_context[name[0]] + ":" + name[1] |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 142 | self._write(' %s=%s' % (name, quoteattr(value))) |
| 143 | self._write('>') |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 144 | |
| 145 | def endElementNS(self, name, qname): |
Martin v. Löwis | cf0a1cc | 2000-10-03 22:35:29 +0000 | [diff] [blame] | 146 | if name[0] is None: |
| 147 | name = name[1] |
| 148 | else: |
| 149 | name = self._current_context[name[0]] + ":" + name[1] |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 150 | self._write('</%s>' % name) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 151 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 152 | def characters(self, content): |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 153 | self._write(escape(content)) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 154 | |
| 155 | def ignorableWhitespace(self, content): |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 156 | self._write(content) |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 157 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 158 | def processingInstruction(self, target, data): |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 159 | self._write('<?%s %s?>' % (target, data)) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 160 | |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 161 | |
Lars Gustäbel | fc643c3 | 2000-09-24 10:53:31 +0000 | [diff] [blame] | 162 | class XMLFilterBase(xmlreader.XMLReader): |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 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 | |
Lars Gustäbel | bc1b5c8 | 2000-10-11 22:35:00 +0000 | [diff] [blame] | 170 | def __init__(self, parent = None): |
| 171 | xmlreader.XMLReader.__init__(self) |
| 172 | self._parent = parent |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 173 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 174 | # ErrorHandler methods |
| 175 | |
| 176 | def error(self, exception): |
| 177 | self._err_handler.error(exception) |
| 178 | |
| 179 | def fatalError(self, exception): |
| 180 | self._err_handler.fatalError(exception) |
| 181 | |
| 182 | def warning(self, exception): |
| 183 | self._err_handler.warning(exception) |
| 184 | |
| 185 | # ContentHandler methods |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 186 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 187 | def setDocumentLocator(self, locator): |
| 188 | self._cont_handler.setDocumentLocator(locator) |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 189 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 190 | def startDocument(self): |
| 191 | self._cont_handler.startDocument() |
| 192 | |
| 193 | def endDocument(self): |
| 194 | self._cont_handler.endDocument() |
| 195 | |
| 196 | def startPrefixMapping(self, prefix, uri): |
| 197 | self._cont_handler.startPrefixMapping(prefix, uri) |
| 198 | |
| 199 | def endPrefixMapping(self, prefix): |
| 200 | self._cont_handler.endPrefixMapping(prefix) |
| 201 | |
| 202 | def startElement(self, name, attrs): |
| 203 | self._cont_handler.startElement(name, attrs) |
| 204 | |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 205 | def endElement(self, name): |
| 206 | self._cont_handler.endElement(name) |
| 207 | |
| 208 | def startElementNS(self, name, qname, attrs): |
Martin v. Löwis | 0ea558f | 2004-05-06 02:04:21 +0000 | [diff] [blame] | 209 | self._cont_handler.startElementNS(name, qname, attrs) |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 210 | |
| 211 | def endElementNS(self, name, qname): |
| 212 | self._cont_handler.endElementNS(name, qname) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 213 | |
| 214 | def characters(self, content): |
| 215 | self._cont_handler.characters(content) |
| 216 | |
Lars Gustäbel | fc643c3 | 2000-09-24 10:53:31 +0000 | [diff] [blame] | 217 | def ignorableWhitespace(self, chars): |
| 218 | self._cont_handler.ignorableWhitespace(chars) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 219 | |
| 220 | def processingInstruction(self, target, data): |
| 221 | self._cont_handler.processingInstruction(target, data) |
| 222 | |
| 223 | def skippedEntity(self, name): |
| 224 | self._cont_handler.skippedEntity(name) |
| 225 | |
| 226 | # DTDHandler methods |
| 227 | |
| 228 | def notationDecl(self, name, publicId, systemId): |
| 229 | self._dtd_handler.notationDecl(name, publicId, systemId) |
| 230 | |
| 231 | def unparsedEntityDecl(self, name, publicId, systemId, ndata): |
| 232 | self._dtd_handler.unparsedEntityDecl(name, publicId, systemId, ndata) |
| 233 | |
| 234 | # EntityResolver methods |
| 235 | |
| 236 | def resolveEntity(self, publicId, systemId): |
Fred Drake | e4772f3 | 2005-02-03 17:31:39 +0000 | [diff] [blame] | 237 | return self._ent_handler.resolveEntity(publicId, systemId) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 238 | |
| 239 | # XMLReader methods |
| 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 | |
| 248 | def setLocale(self, locale): |
| 249 | self._parent.setLocale(locale) |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 250 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 251 | def getFeature(self, name): |
| 252 | return self._parent.getFeature(name) |
| 253 | |
| 254 | def setFeature(self, name, state): |
| 255 | self._parent.setFeature(name, state) |
| 256 | |
| 257 | def getProperty(self, name): |
| 258 | return self._parent.getProperty(name) |
| 259 | |
| 260 | def setProperty(self, name, value): |
| 261 | self._parent.setProperty(name, value) |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 262 | |
Lars Gustäbel | bc1b5c8 | 2000-10-11 22:35:00 +0000 | [diff] [blame] | 263 | # XMLFilter methods |
| 264 | |
| 265 | def getParent(self): |
| 266 | return self._parent |
| 267 | |
| 268 | def setParent(self, parent): |
| 269 | self._parent = parent |
| 270 | |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 271 | # --- Utility functions |
| 272 | |
| 273 | def prepare_input_source(source, base = ""): |
| 274 | """This function takes an InputSource and an optional base URL and |
| 275 | returns a fully resolved InputSource object ready for reading.""" |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 276 | |
Martin v. Löwis | 58af43f | 2000-09-24 21:31:06 +0000 | [diff] [blame] | 277 | if type(source) in _StringTypes: |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 278 | source = xmlreader.InputSource(source) |
Martin v. Löwis | 58af43f | 2000-09-24 21:31:06 +0000 | [diff] [blame] | 279 | elif hasattr(source, "read"): |
| 280 | f = source |
Martin v. Löwis | 5fece7f | 2000-10-06 21:11:20 +0000 | [diff] [blame] | 281 | source = xmlreader.InputSource() |
Martin v. Löwis | 58af43f | 2000-09-24 21:31:06 +0000 | [diff] [blame] | 282 | source.setByteStream(f) |
Martin v. Löwis | 5fece7f | 2000-10-06 21:11:20 +0000 | [diff] [blame] | 283 | if hasattr(f, "name"): |
Lars Gustäbel | 4ced5e7 | 2000-10-24 15:53:12 +0000 | [diff] [blame] | 284 | source.setSystemId(f.name) |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 285 | |
Fred Drake | 0872e05 | 2000-09-26 17:23:09 +0000 | [diff] [blame] | 286 | if source.getByteStream() is None: |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 287 | sysid = source.getSystemId() |
Fred Drake | 910b282 | 2004-10-20 11:08:35 +0000 | [diff] [blame] | 288 | basehead = os.path.dirname(os.path.normpath(base)) |
Raymond Hettinger | 06d9b1f | 2004-10-20 08:21:19 +0000 | [diff] [blame] | 289 | sysidfilename = os.path.join(basehead, sysid) |
| 290 | if os.path.isfile(sysidfilename): |
| 291 | source.setSystemId(sysidfilename) |
| 292 | f = open(sysidfilename, "rb") |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 293 | else: |
| 294 | source.setSystemId(urlparse.urljoin(base, sysid)) |
Fred Drake | 0872e05 | 2000-09-26 17:23:09 +0000 | [diff] [blame] | 295 | f = urllib.urlopen(source.getSystemId()) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 296 | |
Fred Drake | 0872e05 | 2000-09-26 17:23:09 +0000 | [diff] [blame] | 297 | source.setByteStream(f) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 298 | |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 299 | return source |