source: exist/trunk/python/elementtree-1.3a6-20070220-badc/elementtree/SimpleXMLTreeBuilder.py @ 3578

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/elementtree-1.3a6-20070220-badc/elementtree/SimpleXMLTreeBuilder.py@3578
Revision 3578, 4.9 KB checked in by pjkersha, 11 years ago (diff)

Latest releases from Fredrik Lundh. 10 March release has exclusive C14N support with namespace prefixes.

Line 
1#
2# ElementTree
3# $Id: SimpleXMLTreeBuilder.py 3375 2008-02-13 08:05:08Z fredrik $
4#
5# A simple XML tree builder, based on Python's xmllib
6#
7# Note that due to bugs in xmllib, this builder does not fully support
8# namespaces (unqualified attributes are put in the default namespace,
9# instead of being left as is).  Run this module as a script to find
10# out if this affects your Python version.
11#
12# history:
13# 2001-10-20 fl   created
14# 2002-05-01 fl   added namespace support for xmllib
15# 2002-08-17 fl   added xmllib sanity test
16#
17# Copyright (c) 1999-2008 by Fredrik Lundh.  All rights reserved.
18#
19# fredrik@pythonware.com
20# http://www.pythonware.com
21#
22# --------------------------------------------------------------------
23# The ElementTree toolkit is
24#
25# Copyright (c) 1999-2008 by Fredrik Lundh
26#
27# By obtaining, using, and/or copying this software and/or its
28# associated documentation, you agree that you have read, understood,
29# and will comply with the following terms and conditions:
30#
31# Permission to use, copy, modify, and distribute this software and
32# its associated documentation for any purpose and without fee is
33# hereby granted, provided that the above copyright notice appears in
34# all copies, and that both that copyright notice and this permission
35# notice appear in supporting documentation, and that the name of
36# Secret Labs AB or the author not be used in advertising or publicity
37# pertaining to distribution of the software without specific, written
38# prior permission.
39#
40# SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
41# TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
42# ABILITY AND FITNESS.  IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
43# BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
44# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
45# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
47# OF THIS SOFTWARE.
48# --------------------------------------------------------------------
49
50import sys
51
52if sys.platform[:4] != "java":
53    import warnings
54    warnings.warn(
55        "The SimpleXMLTreeBuilder will be removed in future versions. "
56        "Use the standard parser instead (XMLParser).",
57        DeprecationWarning)
58
59##
60# Tools to build element trees from XML files, using <b>xmllib</b>.
61# This module can be used instead of the standard tree builder, for
62# Python versions where "expat" is not available (such as 1.5.2).
63# <p>
64# Note that due to bugs in <b>xmllib</b>, the namespace support is
65# not reliable (you can run the module as a script to find out exactly
66# how unreliable it is on your Python version).
67##
68
69import xmllib, string
70
71import ElementTree
72
73##
74# ElementTree builder for XML source data.
75#
76# @see elementtree.ElementTree
77
78class TreeBuilder(xmllib.XMLParser):
79
80    def __init__(self, html=0):
81        self.__builder = ElementTree.TreeBuilder()
82        if html:
83            import htmlentitydefs
84            self.entitydefs.update(htmlentitydefs.entitydefs)
85        xmllib.XMLParser.__init__(self)
86
87    ##
88    # Feeds data to the parser.
89    #
90    # @param data Encoded data.
91
92    def feed(self, data):
93        xmllib.XMLParser.feed(self, data)
94
95    ##
96    # Finishes feeding data to the parser.
97    #
98    # @return An element structure.
99    # @defreturn Element
100
101    def close(self):
102        xmllib.XMLParser.close(self)
103        return self.__builder.close()
104
105    def handle_data(self, data):
106        self.__builder.data(data)
107
108    handle_cdata = handle_data
109
110    def unknown_starttag(self, tag, attrs):
111        attrib = {}
112        for key, value in attrs.items():
113            attrib[fixname(key)] = value
114        self.__builder.start(fixname(tag), attrib)
115
116    def unknown_endtag(self, tag):
117        self.__builder.end(fixname(tag))
118
119
120def fixname(name, split=string.split):
121    # xmllib in 2.0 and later provides limited (and slightly broken)
122    # support for XML namespaces.
123    if " " not in name:
124        return name
125    return "{%s}%s" % tuple(split(name, " ", 1))
126
127
128if __name__ == "__main__":
129    import sys
130    # sanity check: look for known namespace bugs in xmllib
131    p = TreeBuilder()
132    text = """\
133    <root xmlns='default'>
134       <tag attribute='value' />
135    </root>
136    """
137    p.feed(text)
138    tree = p.close()
139    status = []
140    # check for bugs in the xmllib implementation
141    tag = tree.find("{default}tag")
142    if tag is None:
143        status.append("namespaces not supported")
144    if tag is not None and tag.get("{default}attribute"):
145        status.append("default namespace applied to unqualified attribute")
146    # report bugs
147    if status:
148        print "xmllib doesn't work properly in this Python version:"
149        for bug in status:
150            print "-", bug
151    else:
152        print "congratulations; no problems found in xmllib"
Note: See TracBrowser for help on using the repository browser.