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