You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sax.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from __future__ import absolute_import, division, unicode_literals
  2. from xml.sax.xmlreader import AttributesNSImpl
  3. from ..constants import adjustForeignAttributes, unadjustForeignAttributes
  4. prefix_mapping = {}
  5. for prefix, localName, namespace in adjustForeignAttributes.values():
  6. if prefix is not None:
  7. prefix_mapping[prefix] = namespace
  8. def to_sax(walker, handler):
  9. """Call SAX-like content handler based on treewalker walker
  10. :arg walker: the treewalker to use to walk the tree to convert it
  11. :arg handler: SAX handler to use
  12. """
  13. handler.startDocument()
  14. for prefix, namespace in prefix_mapping.items():
  15. handler.startPrefixMapping(prefix, namespace)
  16. for token in walker:
  17. type = token["type"]
  18. if type == "Doctype":
  19. continue
  20. elif type in ("StartTag", "EmptyTag"):
  21. attrs = AttributesNSImpl(token["data"],
  22. unadjustForeignAttributes)
  23. handler.startElementNS((token["namespace"], token["name"]),
  24. token["name"],
  25. attrs)
  26. if type == "EmptyTag":
  27. handler.endElementNS((token["namespace"], token["name"]),
  28. token["name"])
  29. elif type == "EndTag":
  30. handler.endElementNS((token["namespace"], token["name"]),
  31. token["name"])
  32. elif type in ("Characters", "SpaceCharacters"):
  33. handler.characters(token["data"])
  34. elif type == "Comment":
  35. pass
  36. else:
  37. assert False, "Unknown token type"
  38. for prefix, namespace in prefix_mapping.items():
  39. handler.endPrefixMapping(prefix)
  40. handler.endDocument()