saxutils.py 12 KB

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