Armin Rigo | 9ed7306 | 2005-12-14 18:10:45 +0000 | [diff] [blame] | 1 | # |
| 2 | # ElementTree |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3 | # $Id: ElementInclude.py 3375 2008-02-13 08:05:08Z fredrik $ |
Armin Rigo | 9ed7306 | 2005-12-14 18:10:45 +0000 | [diff] [blame] | 4 | # |
| 5 | # limited xinclude support for element trees |
| 6 | # |
| 7 | # history: |
| 8 | # 2003-08-15 fl created |
| 9 | # 2003-11-14 fl fixed default loader |
| 10 | # |
| 11 | # Copyright (c) 2003-2004 by Fredrik Lundh. All rights reserved. |
| 12 | # |
| 13 | # fredrik@pythonware.com |
| 14 | # http://www.pythonware.com |
| 15 | # |
| 16 | # -------------------------------------------------------------------- |
| 17 | # The ElementTree toolkit is |
| 18 | # |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 19 | # Copyright (c) 1999-2008 by Fredrik Lundh |
Armin Rigo | 9ed7306 | 2005-12-14 18:10:45 +0000 | [diff] [blame] | 20 | # |
| 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: |
| 24 | # |
| 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 |
| 32 | # prior permission. |
| 33 | # |
| 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 |
| 41 | # OF THIS SOFTWARE. |
| 42 | # -------------------------------------------------------------------- |
| 43 | |
Fredrik Lundh | 63168a5 | 2005-12-14 22:29:34 +0000 | [diff] [blame] | 44 | # Licensed to PSF under a Contributor Agreement. |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 45 | # See http://www.python.org/psf/license for licensing details. |
Fredrik Lundh | 63168a5 | 2005-12-14 22:29:34 +0000 | [diff] [blame] | 46 | |
Armin Rigo | 9ed7306 | 2005-12-14 18:10:45 +0000 | [diff] [blame] | 47 | ## |
| 48 | # Limited XInclude support for the ElementTree package. |
| 49 | ## |
| 50 | |
| 51 | import copy |
Alex Martelli | c5c45ba | 2006-08-21 20:54:38 +0000 | [diff] [blame] | 52 | from . import ElementTree |
Armin Rigo | 9ed7306 | 2005-12-14 18:10:45 +0000 | [diff] [blame] | 53 | |
| 54 | XINCLUDE = "{http://www.w3.org/2001/XInclude}" |
| 55 | |
| 56 | XINCLUDE_INCLUDE = XINCLUDE + "include" |
| 57 | XINCLUDE_FALLBACK = XINCLUDE + "fallback" |
| 58 | |
| 59 | ## |
| 60 | # Fatal include error. |
| 61 | |
| 62 | class FatalIncludeError(SyntaxError): |
| 63 | pass |
| 64 | |
| 65 | ## |
| 66 | # Default loader. This loader reads an included resource from disk. |
| 67 | # |
| 68 | # @param href Resource reference. |
| 69 | # @param parse Parse mode. Either "xml" or "text". |
Victor Stinner | eaf399e | 2011-06-30 18:10:14 +0200 | [diff] [blame] | 70 | # @param encoding Optional text encoding (UTF-8 by default for "text"). |
Armin Rigo | 9ed7306 | 2005-12-14 18:10:45 +0000 | [diff] [blame] | 71 | # @return The expanded resource. If the parse mode is "xml", this |
| 72 | # is an ElementTree instance. If the parse mode is "text", this |
| 73 | # is a Unicode string. If the loader fails, it can return None |
| 74 | # or raise an IOError exception. |
| 75 | # @throws IOError If the loader fails to load the resource. |
| 76 | |
| 77 | def default_loader(href, parse, encoding=None): |
Armin Rigo | 9ed7306 | 2005-12-14 18:10:45 +0000 | [diff] [blame] | 78 | if parse == "xml": |
Victor Stinner | eaf399e | 2011-06-30 18:10:14 +0200 | [diff] [blame] | 79 | file = open(href, 'rb') |
Armin Rigo | 9ed7306 | 2005-12-14 18:10:45 +0000 | [diff] [blame] | 80 | data = ElementTree.parse(file).getroot() |
| 81 | else: |
Victor Stinner | eaf399e | 2011-06-30 18:10:14 +0200 | [diff] [blame] | 82 | if not encoding: |
| 83 | encoding = 'UTF-8' |
| 84 | file = open(href, 'r', encoding=encoding) |
Armin Rigo | 9ed7306 | 2005-12-14 18:10:45 +0000 | [diff] [blame] | 85 | data = file.read() |
Armin Rigo | 9ed7306 | 2005-12-14 18:10:45 +0000 | [diff] [blame] | 86 | file.close() |
| 87 | return data |
| 88 | |
| 89 | ## |
| 90 | # Expand XInclude directives. |
| 91 | # |
| 92 | # @param elem Root element. |
| 93 | # @param loader Optional resource loader. If omitted, it defaults |
| 94 | # to {@link default_loader}. If given, it should be a callable |
| 95 | # that implements the same interface as <b>default_loader</b>. |
| 96 | # @throws FatalIncludeError If the function fails to include a given |
| 97 | # resource, or if the tree contains malformed XInclude elements. |
| 98 | # @throws IOError If the function fails to load a given resource. |
| 99 | |
| 100 | def include(elem, loader=None): |
| 101 | if loader is None: |
| 102 | loader = default_loader |
| 103 | # look for xinclude elements |
| 104 | i = 0 |
| 105 | while i < len(elem): |
| 106 | e = elem[i] |
| 107 | if e.tag == XINCLUDE_INCLUDE: |
| 108 | # process xinclude directive |
| 109 | href = e.get("href") |
| 110 | parse = e.get("parse", "xml") |
| 111 | if parse == "xml": |
| 112 | node = loader(href, parse) |
| 113 | if node is None: |
| 114 | raise FatalIncludeError( |
| 115 | "cannot load %r as %r" % (href, parse) |
| 116 | ) |
| 117 | node = copy.copy(node) |
| 118 | if e.tail: |
| 119 | node.tail = (node.tail or "") + e.tail |
| 120 | elem[i] = node |
| 121 | elif parse == "text": |
| 122 | text = loader(href, parse, e.get("encoding")) |
| 123 | if text is None: |
| 124 | raise FatalIncludeError( |
| 125 | "cannot load %r as %r" % (href, parse) |
| 126 | ) |
| 127 | if i: |
| 128 | node = elem[i-1] |
Florent Xicluna | ba8a986 | 2010-08-08 23:08:41 +0000 | [diff] [blame] | 129 | node.tail = (node.tail or "") + text + (e.tail or "") |
Armin Rigo | 9ed7306 | 2005-12-14 18:10:45 +0000 | [diff] [blame] | 130 | else: |
| 131 | elem.text = (elem.text or "") + text + (e.tail or "") |
| 132 | del elem[i] |
| 133 | continue |
| 134 | else: |
| 135 | raise FatalIncludeError( |
| 136 | "unknown parse type in xi:include tag (%r)" % parse |
| 137 | ) |
| 138 | elif e.tag == XINCLUDE_FALLBACK: |
| 139 | raise FatalIncludeError( |
| 140 | "xi:fallback tag must be child of xi:include (%r)" % e.tag |
| 141 | ) |
| 142 | else: |
| 143 | include(e, loader) |
| 144 | i = i + 1 |