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 |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 7 | import io |
Georg Brandl | c502df4 | 2013-05-12 11:41:12 +0200 | [diff] [blame^] | 8 | import codecs |
Guido van Rossum | 3b27105 | 2006-08-17 09:10:09 +0000 | [diff] [blame] | 9 | from . import handler |
| 10 | from . import xmlreader |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 11 | |
Martin v. Löwis | 74b51ac | 2002-10-26 14:50:45 +0000 | [diff] [blame] | 12 | def __dict_replace(s, d): |
| 13 | """Replace substrings of a string using a dictionary.""" |
| 14 | for key, value in d.items(): |
| 15 | s = s.replace(key, value) |
| 16 | return s |
Martin v. Löwis | 58af43f | 2000-09-24 21:31:06 +0000 | [diff] [blame] | 17 | |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 18 | def escape(data, entities={}): |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 19 | """Escape &, <, and > in a string of data. |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 20 | |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 21 | You can escape other strings of data by passing a dictionary as |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 22 | the optional entities parameter. The keys and values must all be |
| 23 | strings; each key will be replaced with its corresponding value. |
| 24 | """ |
Martin v. Löwis | 74b51ac | 2002-10-26 14:50:45 +0000 | [diff] [blame] | 25 | |
| 26 | # must do ampersand first |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 27 | data = data.replace("&", "&") |
Fred Drake | f55222d | 2002-10-28 17:29:01 +0000 | [diff] [blame] | 28 | data = data.replace(">", ">") |
| 29 | data = data.replace("<", "<") |
| 30 | if entities: |
| 31 | data = __dict_replace(data, entities) |
| 32 | return data |
Martin v. Löwis | 74b51ac | 2002-10-26 14:50:45 +0000 | [diff] [blame] | 33 | |
| 34 | def unescape(data, entities={}): |
| 35 | """Unescape &, <, and > in a string of data. |
| 36 | |
| 37 | You can unescape 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 | """ |
Fred Drake | f55222d | 2002-10-28 17:29:01 +0000 | [diff] [blame] | 41 | data = data.replace("<", "<") |
| 42 | data = data.replace(">", ">") |
Fred Drake | f55222d | 2002-10-28 17:29:01 +0000 | [diff] [blame] | 43 | if entities: |
| 44 | data = __dict_replace(data, entities) |
Fred Drake | 407fea5 | 2002-10-28 17:46:59 +0000 | [diff] [blame] | 45 | # must do ampersand last |
Fred Drake | 6d89050 | 2002-10-28 18:09:41 +0000 | [diff] [blame] | 46 | return data.replace("&", "&") |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 47 | |
Fred Drake | acd32d3 | 2001-07-19 16:10:15 +0000 | [diff] [blame] | 48 | def quoteattr(data, entities={}): |
| 49 | """Escape and quote an attribute value. |
| 50 | |
| 51 | Escape &, <, and > in a string of data, then quote it for use as |
| 52 | an attribute value. The \" character will be escaped as well, if |
| 53 | necessary. |
| 54 | |
| 55 | You can escape other strings of data by passing a dictionary as |
| 56 | the optional entities parameter. The keys and values must all be |
| 57 | strings; each key will be replaced with its corresponding value. |
| 58 | """ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 59 | entities = entities.copy() |
| 60 | entities.update({'\n': ' ', '\r': ' ', '\t':'	'}) |
Fred Drake | acd32d3 | 2001-07-19 16:10:15 +0000 | [diff] [blame] | 61 | data = escape(data, entities) |
| 62 | if '"' in data: |
| 63 | if "'" in data: |
| 64 | data = '"%s"' % data.replace('"', """) |
| 65 | else: |
| 66 | data = "'%s'" % data |
| 67 | else: |
| 68 | data = '"%s"' % data |
| 69 | return data |
| 70 | |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 71 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 72 | def _gettextwriter(out, encoding): |
| 73 | if out is None: |
| 74 | import sys |
| 75 | return sys.stdout |
| 76 | |
| 77 | if isinstance(out, io.TextIOBase): |
| 78 | # use a text writer as is |
| 79 | return out |
| 80 | |
Georg Brandl | c502df4 | 2013-05-12 11:41:12 +0200 | [diff] [blame^] | 81 | if isinstance(out, (codecs.StreamWriter, codecs.StreamReaderWriter)): |
| 82 | # use a codecs stream writer as is |
| 83 | return out |
| 84 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 85 | # wrap a binary writer with TextIOWrapper |
| 86 | if isinstance(out, io.RawIOBase): |
| 87 | # Keep the original file open when the TextIOWrapper is |
| 88 | # destroyed |
| 89 | class _wrapper: |
| 90 | __class__ = out.__class__ |
| 91 | def __getattr__(self, name): |
| 92 | return getattr(out, name) |
| 93 | buffer = _wrapper() |
| 94 | buffer.close = lambda: None |
| 95 | else: |
| 96 | # This is to handle passed objects that aren't in the |
| 97 | # IOBase hierarchy, but just have a write method |
| 98 | buffer = io.BufferedIOBase() |
| 99 | buffer.writable = lambda: True |
| 100 | buffer.write = out.write |
| 101 | try: |
| 102 | # TextIOWrapper uses this methods to determine |
| 103 | # if BOM (for UTF-16, etc) should be added |
| 104 | buffer.seekable = out.seekable |
| 105 | buffer.tell = out.tell |
| 106 | except AttributeError: |
| 107 | pass |
| 108 | return io.TextIOWrapper(buffer, encoding=encoding, |
| 109 | errors='xmlcharrefreplace', |
| 110 | newline='\n', |
| 111 | write_through=True) |
| 112 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 113 | class XMLGenerator(handler.ContentHandler): |
| 114 | |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 115 | def __init__(self, out=None, encoding="iso-8859-1", short_empty_elements=False): |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 116 | handler.ContentHandler.__init__(self) |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 117 | out = _gettextwriter(out, encoding) |
| 118 | self._write = out.write |
| 119 | self._flush = out.flush |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 120 | self._ns_contexts = [{}] # contains uri -> prefix dicts |
| 121 | self._current_context = self._ns_contexts[-1] |
Lars Gustäbel | fc643c3 | 2000-09-24 10:53:31 +0000 | [diff] [blame] | 122 | self._undeclared_ns_maps = [] |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 123 | self._encoding = encoding |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 124 | self._short_empty_elements = short_empty_elements |
| 125 | self._pending_start_element = False |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 126 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 127 | def _qname(self, name): |
| 128 | """Builds a qualified name from a (ns_url, localname) pair""" |
| 129 | if name[0]: |
Antoine Pitrou | 6b03ee6 | 2010-10-27 18:33:30 +0000 | [diff] [blame] | 130 | # Per http://www.w3.org/XML/1998/namespace, The 'xml' prefix is |
| 131 | # bound by definition to http://www.w3.org/XML/1998/namespace. It |
| 132 | # does not need to be declared and will not usually be found in |
| 133 | # self._current_context. |
| 134 | if 'http://www.w3.org/XML/1998/namespace' == name[0]: |
| 135 | return 'xml:' + name[1] |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 136 | # The name is in a non-empty namespace |
| 137 | prefix = self._current_context[name[0]] |
| 138 | if prefix: |
| 139 | # If it is not the default namespace, prepend the prefix |
| 140 | return prefix + ":" + name[1] |
| 141 | # Return the unqualified name |
| 142 | return name[1] |
| 143 | |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 144 | def _finish_pending_start_element(self,endElement=False): |
| 145 | if self._pending_start_element: |
| 146 | self._write('>') |
| 147 | self._pending_start_element = False |
| 148 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 149 | # ContentHandler methods |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 150 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 151 | def startDocument(self): |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 152 | self._write('<?xml version="1.0" encoding="%s"?>\n' % |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 153 | self._encoding) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 154 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 155 | def endDocument(self): |
| 156 | self._flush() |
| 157 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 158 | def startPrefixMapping(self, prefix, uri): |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 159 | self._ns_contexts.append(self._current_context.copy()) |
| 160 | self._current_context[uri] = prefix |
Lars Gustäbel | fc643c3 | 2000-09-24 10:53:31 +0000 | [diff] [blame] | 161 | self._undeclared_ns_maps.append((prefix, uri)) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 162 | |
| 163 | def endPrefixMapping(self, prefix): |
Lars Gustäbel | fc643c3 | 2000-09-24 10:53:31 +0000 | [diff] [blame] | 164 | self._current_context = self._ns_contexts[-1] |
| 165 | del self._ns_contexts[-1] |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 166 | |
| 167 | def startElement(self, name, attrs): |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 168 | self._finish_pending_start_element() |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 169 | self._write('<' + name) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 170 | for (name, value) in attrs.items(): |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 171 | self._write(' %s=%s' % (name, quoteattr(value))) |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 172 | if self._short_empty_elements: |
| 173 | self._pending_start_element = True |
| 174 | else: |
| 175 | self._write(">") |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 176 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 177 | def endElement(self, name): |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 178 | if self._pending_start_element: |
| 179 | self._write('/>') |
| 180 | self._pending_start_element = False |
| 181 | else: |
| 182 | self._write('</%s>' % name) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 183 | |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 184 | def startElementNS(self, name, qname, attrs): |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 185 | self._finish_pending_start_element() |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 186 | self._write('<' + self._qname(name)) |
Lars Gustäbel | fc643c3 | 2000-09-24 10:53:31 +0000 | [diff] [blame] | 187 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 188 | for prefix, uri in self._undeclared_ns_maps: |
| 189 | if prefix: |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 190 | self._write(' xmlns:%s="%s"' % (prefix, uri)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 191 | else: |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 192 | self._write(' xmlns="%s"' % uri) |
Lars Gustäbel | fc643c3 | 2000-09-24 10:53:31 +0000 | [diff] [blame] | 193 | self._undeclared_ns_maps = [] |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 194 | |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 195 | for (name, value) in attrs.items(): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 196 | self._write(' %s=%s' % (self._qname(name), quoteattr(value))) |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 197 | if self._short_empty_elements: |
| 198 | self._pending_start_element = True |
| 199 | else: |
| 200 | self._write(">") |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 201 | |
| 202 | def endElementNS(self, name, qname): |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 203 | if self._pending_start_element: |
| 204 | self._write('/>') |
| 205 | self._pending_start_element = False |
| 206 | else: |
| 207 | self._write('</%s>' % self._qname(name)) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 208 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 209 | def characters(self, content): |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 210 | if content: |
| 211 | self._finish_pending_start_element() |
| 212 | self._write(escape(content)) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 213 | |
| 214 | def ignorableWhitespace(self, content): |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 215 | if content: |
| 216 | self._finish_pending_start_element() |
| 217 | self._write(content) |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 218 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 219 | def processingInstruction(self, target, data): |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 220 | self._finish_pending_start_element() |
Martin v. Löwis | ae20722 | 2004-05-06 02:22:43 +0000 | [diff] [blame] | 221 | self._write('<?%s %s?>' % (target, data)) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 222 | |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 223 | |
Lars Gustäbel | fc643c3 | 2000-09-24 10:53:31 +0000 | [diff] [blame] | 224 | class XMLFilterBase(xmlreader.XMLReader): |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 225 | """This class is designed to sit between an XMLReader and the |
| 226 | client application's event handlers. By default, it does nothing |
| 227 | but pass requests up to the reader and events on to the handlers |
| 228 | unmodified, but subclasses can override specific methods to modify |
| 229 | the event stream or the configuration requests as they pass |
| 230 | through.""" |
| 231 | |
Lars Gustäbel | bc1b5c8 | 2000-10-11 22:35:00 +0000 | [diff] [blame] | 232 | def __init__(self, parent = None): |
| 233 | xmlreader.XMLReader.__init__(self) |
| 234 | self._parent = parent |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 235 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 236 | # ErrorHandler methods |
| 237 | |
| 238 | def error(self, exception): |
| 239 | self._err_handler.error(exception) |
| 240 | |
| 241 | def fatalError(self, exception): |
| 242 | self._err_handler.fatalError(exception) |
| 243 | |
| 244 | def warning(self, exception): |
| 245 | self._err_handler.warning(exception) |
| 246 | |
| 247 | # ContentHandler methods |
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 setDocumentLocator(self, locator): |
| 250 | self._cont_handler.setDocumentLocator(locator) |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 251 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 252 | def startDocument(self): |
| 253 | self._cont_handler.startDocument() |
| 254 | |
| 255 | def endDocument(self): |
| 256 | self._cont_handler.endDocument() |
| 257 | |
| 258 | def startPrefixMapping(self, prefix, uri): |
| 259 | self._cont_handler.startPrefixMapping(prefix, uri) |
| 260 | |
| 261 | def endPrefixMapping(self, prefix): |
| 262 | self._cont_handler.endPrefixMapping(prefix) |
| 263 | |
| 264 | def startElement(self, name, attrs): |
| 265 | self._cont_handler.startElement(name, attrs) |
| 266 | |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 267 | def endElement(self, name): |
| 268 | self._cont_handler.endElement(name) |
| 269 | |
| 270 | def startElementNS(self, name, qname, attrs): |
Martin v. Löwis | 0ea558f | 2004-05-06 02:04:21 +0000 | [diff] [blame] | 271 | self._cont_handler.startElementNS(name, qname, attrs) |
Lars Gustäbel | c5cec51 | 2000-09-21 08:25:28 +0000 | [diff] [blame] | 272 | |
| 273 | def endElementNS(self, name, qname): |
| 274 | self._cont_handler.endElementNS(name, qname) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 275 | |
| 276 | def characters(self, content): |
| 277 | self._cont_handler.characters(content) |
| 278 | |
Lars Gustäbel | fc643c3 | 2000-09-24 10:53:31 +0000 | [diff] [blame] | 279 | def ignorableWhitespace(self, chars): |
| 280 | self._cont_handler.ignorableWhitespace(chars) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 281 | |
| 282 | def processingInstruction(self, target, data): |
| 283 | self._cont_handler.processingInstruction(target, data) |
| 284 | |
| 285 | def skippedEntity(self, name): |
| 286 | self._cont_handler.skippedEntity(name) |
| 287 | |
| 288 | # DTDHandler methods |
| 289 | |
| 290 | def notationDecl(self, name, publicId, systemId): |
| 291 | self._dtd_handler.notationDecl(name, publicId, systemId) |
| 292 | |
| 293 | def unparsedEntityDecl(self, name, publicId, systemId, ndata): |
| 294 | self._dtd_handler.unparsedEntityDecl(name, publicId, systemId, ndata) |
| 295 | |
| 296 | # EntityResolver methods |
| 297 | |
| 298 | def resolveEntity(self, publicId, systemId): |
Fred Drake | e4772f3 | 2005-02-03 17:31:39 +0000 | [diff] [blame] | 299 | return self._ent_handler.resolveEntity(publicId, systemId) |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 300 | |
| 301 | # XMLReader methods |
| 302 | |
| 303 | def parse(self, source): |
| 304 | self._parent.setContentHandler(self) |
| 305 | self._parent.setErrorHandler(self) |
| 306 | self._parent.setEntityResolver(self) |
| 307 | self._parent.setDTDHandler(self) |
| 308 | self._parent.parse(source) |
| 309 | |
| 310 | def setLocale(self, locale): |
| 311 | self._parent.setLocale(locale) |
Fred Drake | a12adfe | 2000-09-18 17:40:22 +0000 | [diff] [blame] | 312 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 313 | def getFeature(self, name): |
| 314 | return self._parent.getFeature(name) |
| 315 | |
| 316 | def setFeature(self, name, state): |
| 317 | self._parent.setFeature(name, state) |
| 318 | |
| 319 | def getProperty(self, name): |
| 320 | return self._parent.getProperty(name) |
| 321 | |
| 322 | def setProperty(self, name, value): |
| 323 | self._parent.setProperty(name, value) |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 324 | |
Lars Gustäbel | bc1b5c8 | 2000-10-11 22:35:00 +0000 | [diff] [blame] | 325 | # XMLFilter methods |
| 326 | |
| 327 | def getParent(self): |
| 328 | return self._parent |
| 329 | |
| 330 | def setParent(self, parent): |
| 331 | self._parent = parent |
| 332 | |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 333 | # --- Utility functions |
| 334 | |
Georg Brandl | fe99105 | 2009-09-16 15:54:04 +0000 | [diff] [blame] | 335 | def prepare_input_source(source, base=""): |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 336 | """This function takes an InputSource and an optional base URL and |
| 337 | returns a fully resolved InputSource object ready for reading.""" |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 338 | |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 339 | if isinstance(source, str): |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 340 | source = xmlreader.InputSource(source) |
Martin v. Löwis | 58af43f | 2000-09-24 21:31:06 +0000 | [diff] [blame] | 341 | elif hasattr(source, "read"): |
| 342 | f = source |
Martin v. Löwis | 5fece7f | 2000-10-06 21:11:20 +0000 | [diff] [blame] | 343 | source = xmlreader.InputSource() |
Martin v. Löwis | 58af43f | 2000-09-24 21:31:06 +0000 | [diff] [blame] | 344 | source.setByteStream(f) |
Martin v. Löwis | 5fece7f | 2000-10-06 21:11:20 +0000 | [diff] [blame] | 345 | if hasattr(f, "name"): |
Lars Gustäbel | 4ced5e7 | 2000-10-24 15:53:12 +0000 | [diff] [blame] | 346 | source.setSystemId(f.name) |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 347 | |
Fred Drake | 0872e05 | 2000-09-26 17:23:09 +0000 | [diff] [blame] | 348 | if source.getByteStream() is None: |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 349 | sysid = source.getSystemId() |
Fred Drake | 910b282 | 2004-10-20 11:08:35 +0000 | [diff] [blame] | 350 | basehead = os.path.dirname(os.path.normpath(base)) |
Raymond Hettinger | 06d9b1f | 2004-10-20 08:21:19 +0000 | [diff] [blame] | 351 | sysidfilename = os.path.join(basehead, sysid) |
| 352 | if os.path.isfile(sysidfilename): |
| 353 | source.setSystemId(sysidfilename) |
| 354 | f = open(sysidfilename, "rb") |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 355 | else: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 356 | source.setSystemId(urllib.parse.urljoin(base, sysid)) |
| 357 | f = urllib.request.urlopen(source.getSystemId()) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 358 | |
Fred Drake | 0872e05 | 2000-09-26 17:23:09 +0000 | [diff] [blame] | 359 | source.setByteStream(f) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 360 | |
Lars Gustäbel | 523b0a6 | 2000-09-24 18:54:49 +0000 | [diff] [blame] | 361 | return source |