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