saxutils.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. """\
  2. A library of useful helper classes to the SAX classes, for the
  3. convenience of application and driver writers.
  4. """
  5. import os, urllib.parse, urllib.request
  6. import io
  7. import codecs
  8. from . import handler
  9. from . import xmlreader
  10. def __dict_replace(s, d):
  11. """Replace substrings of a string using a dictionary."""
  12. for key, value in d.items():
  13. s = s.replace(key, value)
  14. return s
  15. def escape(data, entities={}):
  16. """Escape &, <, and > in a string of data.
  17. You can escape other strings of data by passing a dictionary as
  18. the optional entities parameter. The keys and values must all be
  19. strings; each key will be replaced with its corresponding value.
  20. """
  21. # must do ampersand first
  22. data = data.replace("&", "&amp;")
  23. data = data.replace(">", "&gt;")
  24. data = data.replace("<", "&lt;")
  25. if entities:
  26. data = __dict_replace(data, entities)
  27. return data
  28. def unescape(data, entities={}):
  29. """Unescape &amp;, &lt;, and &gt; in a string of data.
  30. You can unescape other strings of data by passing a dictionary as
  31. the optional entities parameter. The keys and values must all be
  32. strings; each key will be replaced with its corresponding value.
  33. """
  34. data = data.replace("&lt;", "<")
  35. data = data.replace("&gt;", ">")
  36. if entities:
  37. data = __dict_replace(data, entities)
  38. # must do ampersand last
  39. return data.replace("&amp;", "&")
  40. def quoteattr(data, entities={}):
  41. """Escape and quote an attribute value.
  42. Escape &, <, and > in a string of data, then quote it for use as
  43. an attribute value. The \" character will be escaped as well, if
  44. necessary.
  45. You can escape other strings of data by passing a dictionary as
  46. the optional entities parameter. The keys and values must all be
  47. strings; each key will be replaced with its corresponding value.
  48. """
  49. entities = entities.copy()
  50. entities.update({'\n': '&#10;', '\r': '&#13;', '\t':'&#9;'})
  51. data = escape(data, entities)
  52. if '"' in data:
  53. if "'" in data:
  54. data = '"%s"' % data.replace('"', "&quot;")
  55. else:
  56. data = "'%s'" % data
  57. else:
  58. data = '"%s"' % data
  59. return data
  60. def _gettextwriter(out, encoding):
  61. if out is None:
  62. import sys
  63. return sys.stdout
  64. if isinstance(out, io.TextIOBase):
  65. # use a text writer as is
  66. return out
  67. if isinstance(out, (codecs.StreamWriter, codecs.StreamReaderWriter)):
  68. # use a codecs stream writer as is
  69. return out
  70. # wrap a binary writer with TextIOWrapper
  71. if isinstance(out, io.RawIOBase):
  72. # Keep the original file open when the TextIOWrapper is
  73. # destroyed
  74. class _wrapper:
  75. __class__ = out.__class__
  76. def __getattr__(self, name):
  77. return getattr(out, name)
  78. buffer = _wrapper()
  79. buffer.close = lambda: None
  80. else:
  81. # This is to handle passed objects that aren't in the
  82. # IOBase hierarchy, but just have a write method
  83. buffer = io.BufferedIOBase()
  84. buffer.writable = lambda: True
  85. buffer.write = out.write
  86. try:
  87. # TextIOWrapper uses this methods to determine
  88. # if BOM (for UTF-16, etc) should be added
  89. buffer.seekable = out.seekable
  90. buffer.tell = out.tell
  91. except AttributeError:
  92. pass
  93. return io.TextIOWrapper(buffer, encoding=encoding,
  94. errors='xmlcharrefreplace',
  95. newline='\n',
  96. write_through=True)
  97. class XMLGenerator(handler.ContentHandler):
  98. def __init__(self, out=None, encoding="iso-8859-1", short_empty_elements=False):
  99. handler.ContentHandler.__init__(self)
  100. out = _gettextwriter(out, encoding)
  101. self._write = out.write
  102. self._flush = out.flush
  103. self._ns_contexts = [{}] # contains uri -> prefix dicts
  104. self._current_context = self._ns_contexts[-1]
  105. self._undeclared_ns_maps = []
  106. self._encoding = encoding
  107. self._short_empty_elements = short_empty_elements
  108. self._pending_start_element = False
  109. def _qname(self, name):
  110. """Builds a qualified name from a (ns_url, localname) pair"""
  111. if name[0]:
  112. # Per http://www.w3.org/XML/1998/namespace, The 'xml' prefix is
  113. # bound by definition to http://www.w3.org/XML/1998/namespace. It
  114. # does not need to be declared and will not usually be found in
  115. # self._current_context.
  116. if 'http://www.w3.org/XML/1998/namespace' == name[0]:
  117. return 'xml:' + name[1]
  118. # The name is in a non-empty namespace
  119. prefix = self._current_context[name[0]]
  120. if prefix:
  121. # If it is not the default namespace, prepend the prefix
  122. return prefix + ":" + name[1]
  123. # Return the unqualified name
  124. return name[1]
  125. def _finish_pending_start_element(self,endElement=False):
  126. if self._pending_start_element:
  127. self._write('>')
  128. self._pending_start_element = False
  129. # ContentHandler methods
  130. def startDocument(self):
  131. self._write('<?xml version="1.0" encoding="%s"?>\n' %
  132. self._encoding)
  133. def endDocument(self):
  134. self._flush()
  135. def startPrefixMapping(self, prefix, uri):
  136. self._ns_contexts.append(self._current_context.copy())
  137. self._current_context[uri] = prefix
  138. self._undeclared_ns_maps.append((prefix, uri))
  139. def endPrefixMapping(self, prefix):
  140. self._current_context = self._ns_contexts[-1]
  141. del self._ns_contexts[-1]
  142. def startElement(self, name, attrs):
  143. self._finish_pending_start_element()
  144. self._write('<' + name)
  145. for (name, value) in attrs.items():
  146. self._write(' %s=%s' % (name, quoteattr(value)))
  147. if self._short_empty_elements:
  148. self._pending_start_element = True
  149. else:
  150. self._write(">")
  151. def endElement(self, name):
  152. if self._pending_start_element:
  153. self._write('/>')
  154. self._pending_start_element = False
  155. else:
  156. self._write('</%s>' % name)
  157. def startElementNS(self, name, qname, attrs):
  158. self._finish_pending_start_element()
  159. self._write('<' + self._qname(name))
  160. for prefix, uri in self._undeclared_ns_maps:
  161. if prefix:
  162. self._write(' xmlns:%s="%s"' % (prefix, uri))
  163. else:
  164. self._write(' xmlns="%s"' % uri)
  165. self._undeclared_ns_maps = []
  166. for (name, value) in attrs.items():
  167. self._write(' %s=%s' % (self._qname(name), quoteattr(value)))
  168. if self._short_empty_elements:
  169. self._pending_start_element = True
  170. else:
  171. self._write(">")
  172. def endElementNS(self, name, qname):
  173. if self._pending_start_element:
  174. self._write('/>')
  175. self._pending_start_element = False
  176. else:
  177. self._write('</%s>' % self._qname(name))
  178. def characters(self, content):
  179. if content:
  180. self._finish_pending_start_element()
  181. if not isinstance(content, str):
  182. content = str(content, self._encoding)
  183. self._write(escape(content))
  184. def ignorableWhitespace(self, content):
  185. if content:
  186. self._finish_pending_start_element()
  187. if not isinstance(content, str):
  188. content = str(content, self._encoding)
  189. self._write(content)
  190. def processingInstruction(self, target, data):
  191. self._finish_pending_start_element()
  192. self._write('<?%s %s?>' % (target, data))
  193. class XMLFilterBase(xmlreader.XMLReader):
  194. """This class is designed to sit between an XMLReader and the
  195. client application's event handlers. By default, it does nothing
  196. but pass requests up to the reader and events on to the handlers
  197. unmodified, but subclasses can override specific methods to modify
  198. the event stream or the configuration requests as they pass
  199. through."""
  200. def __init__(self, parent = None):
  201. xmlreader.XMLReader.__init__(self)
  202. self._parent = parent
  203. # ErrorHandler methods
  204. def error(self, exception):
  205. self._err_handler.error(exception)
  206. def fatalError(self, exception):
  207. self._err_handler.fatalError(exception)
  208. def warning(self, exception):
  209. self._err_handler.warning(exception)
  210. # ContentHandler methods
  211. def setDocumentLocator(self, locator):
  212. self._cont_handler.setDocumentLocator(locator)
  213. def startDocument(self):
  214. self._cont_handler.startDocument()
  215. def endDocument(self):
  216. self._cont_handler.endDocument()
  217. def startPrefixMapping(self, prefix, uri):
  218. self._cont_handler.startPrefixMapping(prefix, uri)
  219. def endPrefixMapping(self, prefix):
  220. self._cont_handler.endPrefixMapping(prefix)
  221. def startElement(self, name, attrs):
  222. self._cont_handler.startElement(name, attrs)
  223. def endElement(self, name):
  224. self._cont_handler.endElement(name)
  225. def startElementNS(self, name, qname, attrs):
  226. self._cont_handler.startElementNS(name, qname, attrs)
  227. def endElementNS(self, name, qname):
  228. self._cont_handler.endElementNS(name, qname)
  229. def characters(self, content):
  230. self._cont_handler.characters(content)
  231. def ignorableWhitespace(self, chars):
  232. self._cont_handler.ignorableWhitespace(chars)
  233. def processingInstruction(self, target, data):
  234. self._cont_handler.processingInstruction(target, data)
  235. def skippedEntity(self, name):
  236. self._cont_handler.skippedEntity(name)
  237. # DTDHandler methods
  238. def notationDecl(self, name, publicId, systemId):
  239. self._dtd_handler.notationDecl(name, publicId, systemId)
  240. def unparsedEntityDecl(self, name, publicId, systemId, ndata):
  241. self._dtd_handler.unparsedEntityDecl(name, publicId, systemId, ndata)
  242. # EntityResolver methods
  243. def resolveEntity(self, publicId, systemId):
  244. return self._ent_handler.resolveEntity(publicId, systemId)
  245. # XMLReader methods
  246. def parse(self, source):
  247. self._parent.setContentHandler(self)
  248. self._parent.setErrorHandler(self)
  249. self._parent.setEntityResolver(self)
  250. self._parent.setDTDHandler(self)
  251. self._parent.parse(source)
  252. def setLocale(self, locale):
  253. self._parent.setLocale(locale)
  254. def getFeature(self, name):
  255. return self._parent.getFeature(name)
  256. def setFeature(self, name, state):
  257. self._parent.setFeature(name, state)
  258. def getProperty(self, name):
  259. return self._parent.getProperty(name)
  260. def setProperty(self, name, value):
  261. self._parent.setProperty(name, value)
  262. # XMLFilter methods
  263. def getParent(self):
  264. return self._parent
  265. def setParent(self, parent):
  266. self._parent = parent
  267. # --- Utility functions
  268. def prepare_input_source(source, base=""):
  269. """This function takes an InputSource and an optional base URL and
  270. returns a fully resolved InputSource object ready for reading."""
  271. if isinstance(source, str):
  272. source = xmlreader.InputSource(source)
  273. elif hasattr(source, "read"):
  274. f = source
  275. source = xmlreader.InputSource()
  276. if isinstance(f.read(0), str):
  277. source.setCharacterStream(f)
  278. else:
  279. source.setByteStream(f)
  280. if hasattr(f, "name") and isinstance(f.name, str):
  281. source.setSystemId(f.name)
  282. if source.getCharacterStream() is None and source.getByteStream() is None:
  283. sysid = source.getSystemId()
  284. basehead = os.path.dirname(os.path.normpath(base))
  285. sysidfilename = os.path.join(basehead, sysid)
  286. if os.path.isfile(sysidfilename):
  287. source.setSystemId(sysidfilename)
  288. f = open(sysidfilename, "rb")
  289. else:
  290. source.setSystemId(urllib.parse.urljoin(base, sysid))
  291. f = urllib.request.urlopen(source.getSystemId())
  292. source.setByteStream(f)
  293. return source