/Youtube/elementtree/SgmlopXMLTreeBuilder.py

http://xbmc-scripting.googlecode.com/ · Python · 103 lines · 25 code · 12 blank · 66 comment · 3 complexity · f497da02b84e0c2726df370c2e623178 MD5 · raw file

  1. #
  2. # ElementTree
  3. # $Id$
  4. #
  5. # A simple XML tree builder, based on the sgmlop library.
  6. #
  7. # Note that this version does not support namespaces. This may be
  8. # changed in future versions.
  9. #
  10. # history:
  11. # 2004-03-28 fl created
  12. #
  13. # Copyright (c) 1999-2004 by Fredrik Lundh. All rights reserved.
  14. #
  15. # fredrik@pythonware.com
  16. # http://www.pythonware.com
  17. #
  18. # --------------------------------------------------------------------
  19. # The ElementTree toolkit is
  20. #
  21. # Copyright (c) 1999-2004 by Fredrik Lundh
  22. #
  23. # By obtaining, using, and/or copying this software and/or its
  24. # associated documentation, you agree that you have read, understood,
  25. # and will comply with the following terms and conditions:
  26. #
  27. # Permission to use, copy, modify, and distribute this software and
  28. # its associated documentation for any purpose and without fee is
  29. # hereby granted, provided that the above copyright notice appears in
  30. # all copies, and that both that copyright notice and this permission
  31. # notice appear in supporting documentation, and that the name of
  32. # Secret Labs AB or the author not be used in advertising or publicity
  33. # pertaining to distribution of the software without specific, written
  34. # prior permission.
  35. #
  36. # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  37. # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
  38. # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
  39. # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  40. # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  41. # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  42. # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  43. # OF THIS SOFTWARE.
  44. # --------------------------------------------------------------------
  45. ##
  46. # Tools to build element trees from XML, based on the SGMLOP parser.
  47. # <p>
  48. # The current version does not support XML namespaces.
  49. # <p>
  50. # This tree builder requires the <b>sgmlop</b> extension module
  51. # (available from
  52. # <a href='http://effbot.org/downloads'>http://effbot.org/downloads</a>).
  53. ##
  54. import ElementTree
  55. ##
  56. # ElementTree builder for XML source data, based on the SGMLOP parser.
  57. #
  58. # @see elementtree.ElementTree
  59. class TreeBuilder:
  60. def __init__(self, html=0):
  61. try:
  62. import sgmlop
  63. except ImportError:
  64. raise RuntimeError("sgmlop parser not available")
  65. self.__builder = ElementTree.TreeBuilder()
  66. if html:
  67. import htmlentitydefs
  68. self.entitydefs.update(htmlentitydefs.entitydefs)
  69. self.__parser = sgmlop.XMLParser()
  70. self.__parser.register(self)
  71. ##
  72. # Feeds data to the parser.
  73. #
  74. # @param data Encoded data.
  75. def feed(self, data):
  76. self.__parser.feed(data)
  77. ##
  78. # Finishes feeding data to the parser.
  79. #
  80. # @return An element structure.
  81. # @defreturn Element
  82. def close(self):
  83. self.__parser.close()
  84. self.__parser = None
  85. return self.__builder.close()
  86. def finish_starttag(self, tag, attrib):
  87. self.__builder.start(tag, attrib)
  88. def finish_endtag(self, tag):
  89. self.__builder.end(tag)
  90. def handle_data(self, data):
  91. self.__builder.data(data)