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