3 # $Id: ElementInclude.py 1862 2004-06-18 07:31:02Z Fredrik $
5 # limited xinclude support for element trees
8 # 2003-08-15 fl created
9 # 2003-11-14 fl fixed default loader
11 # Copyright (c) 2003-2004 by Fredrik Lundh. All rights reserved.
13 # fredrik@pythonware.com
14 # http://www.pythonware.com
16 # --------------------------------------------------------------------
17 # The ElementTree toolkit is
19 # Copyright (c) 1999-2004 by Fredrik Lundh
21 # By obtaining, using, and/or copying this software and/or its
22 # associated documentation, you agree that you have read, understood,
23 # and will comply with the following terms and conditions:
25 # Permission to use, copy, modify, and distribute this software and
26 # its associated documentation for any purpose and without fee is
27 # hereby granted, provided that the above copyright notice appears in
28 # all copies, and that both that copyright notice and this permission
29 # notice appear in supporting documentation, and that the name of
30 # Secret Labs AB or the author not be used in advertising or publicity
31 # pertaining to distribution of the software without specific, written
34 # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
35 # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
36 # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
37 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
38 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
39 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
40 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
42 # --------------------------------------------------------------------
45 # Limited XInclude support for the ElementTree package.
51 XINCLUDE
= "{http://www.w3.org/2001/XInclude}"
53 XINCLUDE_INCLUDE
= XINCLUDE
+ "include"
54 XINCLUDE_FALLBACK
= XINCLUDE
+ "fallback"
57 # Fatal include error.
59 class FatalIncludeError(SyntaxError):
63 # Default loader. This loader reads an included resource from disk.
65 # @param href Resource reference.
66 # @param parse Parse mode. Either "xml" or "text".
67 # @param encoding Optional text encoding.
68 # @return The expanded resource. If the parse mode is "xml", this
69 # is an ElementTree instance. If the parse mode is "text", this
70 # is a Unicode string. If the loader fails, it can return None
71 # or raise an IOError exception.
72 # @throws IOError If the loader fails to load the resource.
74 def default_loader(href
, parse
, encoding
=None):
77 data
= ElementTree
.parse(file).getroot()
81 data
= data
.decode(encoding
)
86 # Expand XInclude directives.
88 # @param elem Root element.
89 # @param loader Optional resource loader. If omitted, it defaults
90 # to {@link default_loader}. If given, it should be a callable
91 # that implements the same interface as <b>default_loader</b>.
92 # @throws FatalIncludeError If the function fails to include a given
93 # resource, or if the tree contains malformed XInclude elements.
94 # @throws IOError If the function fails to load a given resource.
96 def include(elem
, loader
=None):
98 loader
= default_loader
99 # look for xinclude elements
103 if e
.tag
== XINCLUDE_INCLUDE
:
104 # process xinclude directive
106 parse
= e
.get("parse", "xml")
108 node
= loader(href
, parse
)
110 raise FatalIncludeError(
111 "cannot load %r as %r" % (href
, parse
)
113 node
= copy
.copy(node
)
115 node
.tail
= (node
.tail
or "") + e
.tail
117 elif parse
== "text":
118 text
= loader(href
, parse
, e
.get("encoding"))
120 raise FatalIncludeError(
121 "cannot load %r as %r" % (href
, parse
)
125 node
.tail
= (node
.tail
or "") + text
127 elem
.text
= (elem
.text
or "") + text
+ (e
.tail
or "")
131 raise FatalIncludeError(
132 "unknown parse type in xi:include tag (%r)" % parse
134 elif e
.tag
== XINCLUDE_FALLBACK
:
135 raise FatalIncludeError(
136 "xi:fallback tag must be child of xi:include (%r)" % e
.tag