XMLTreeBuilder.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #
  2. # ElementTree
  3. # $Id: XMLTreeBuilder.py 2305 2005-03-01 17:43:09Z fredrik $
  4. #
  5. # an XML tree builder
  6. #
  7. # history:
  8. # 2001-10-20 fl created
  9. # 2002-05-01 fl added namespace support for xmllib
  10. # 2002-07-27 fl require expat (1.5.2 code can use SimpleXMLTreeBuilder)
  11. # 2002-08-17 fl use tag/attribute name memo cache
  12. # 2002-12-04 fl moved XMLTreeBuilder to the ElementTree module
  13. #
  14. # Copyright (c) 1999-2004 by Fredrik Lundh. All rights reserved.
  15. #
  16. # fredrik@pythonware.com
  17. # http://www.pythonware.com
  18. #
  19. # --------------------------------------------------------------------
  20. # The ElementTree toolkit is
  21. #
  22. # Copyright (c) 1999-2004 by Fredrik Lundh
  23. #
  24. # By obtaining, using, and/or copying this software and/or its
  25. # associated documentation, you agree that you have read, understood,
  26. # and will comply with the following terms and conditions:
  27. #
  28. # Permission to use, copy, modify, and distribute this software and
  29. # its associated documentation for any purpose and without fee is
  30. # hereby granted, provided that the above copyright notice appears in
  31. # all copies, and that both that copyright notice and this permission
  32. # notice appear in supporting documentation, and that the name of
  33. # Secret Labs AB or the author not be used in advertising or publicity
  34. # pertaining to distribution of the software without specific, written
  35. # prior permission.
  36. #
  37. # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  38. # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
  39. # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
  40. # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  41. # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  42. # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  43. # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  44. # OF THIS SOFTWARE.
  45. # --------------------------------------------------------------------
  46. ##
  47. # Tools to build element trees from XML files.
  48. ##
  49. import ElementTree
  50. ##
  51. # (obsolete) ElementTree builder for XML source data, based on the
  52. # <b>expat</b> parser.
  53. # <p>
  54. # This class is an alias for ElementTree.XMLTreeBuilder. New code
  55. # should use that version instead.
  56. #
  57. # @see elementtree.ElementTree
  58. class TreeBuilder(ElementTree.XMLTreeBuilder):
  59. pass
  60. ##
  61. # (experimental) An alternate builder that supports manipulation of
  62. # new elements.
  63. class FancyTreeBuilder(TreeBuilder):
  64. def __init__(self, html=0):
  65. TreeBuilder.__init__(self, html)
  66. self._parser.StartNamespaceDeclHandler = self._start_ns
  67. self._parser.EndNamespaceDeclHandler = self._end_ns
  68. self.namespaces = []
  69. def _start(self, tag, attrib_in):
  70. elem = TreeBuilder._start(self, tag, attrib_in)
  71. self.start(elem)
  72. def _start_list(self, tag, attrib_in):
  73. elem = TreeBuilder._start_list(self, tag, attrib_in)
  74. self.start(elem)
  75. def _end(self, tag):
  76. elem = TreeBuilder._end(self, tag)
  77. self.end(elem)
  78. def _start_ns(self, prefix, value):
  79. self.namespaces.insert(0, (prefix, value))
  80. def _end_ns(self, prefix):
  81. assert self.namespaces.pop(0)[0] == prefix, "implementation confused"
  82. ##
  83. # Hook method that's called when a new element has been opened.
  84. # May access the <b>namespaces</b> attribute.
  85. #
  86. # @param element The new element. The tag name and attributes are,
  87. # set, but it has no children, and the text and tail attributes
  88. # are still empty.
  89. def start(self, element):
  90. pass
  91. ##
  92. # Hook method that's called when a new element has been closed.
  93. # May access the <b>namespaces</b> attribute.
  94. #
  95. # @param element The new element.
  96. def end(self, element):
  97. pass