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