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