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