saxutils.py 11 KB

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