Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 1 | """\ |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 2 | minidom.py -- a lightweight DOM implementation. |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 4 | parse("foo.xml") |
Paul Prescod | 623511b | 2000-07-21 22:05:49 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 6 | parseString("<foo><bar/></foo>") |
Paul Prescod | 623511b | 2000-07-21 22:05:49 +0000 | [diff] [blame] | 7 | |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 8 | Todo: |
| 9 | ===== |
| 10 | * convenience methods for getting elements and text. |
| 11 | * more testing |
| 12 | * bring some of the writer and linearizer code into conformance with this |
| 13 | interface |
| 14 | * SAX 2 namespaces |
| 15 | """ |
| 16 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 17 | import string |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 18 | _string = string |
| 19 | del string |
| 20 | |
Fred Drake | 49a5d03 | 2001-11-30 22:21:58 +0000 | [diff] [blame] | 21 | from xml.dom import HierarchyRequestErr, EMPTY_NAMESPACE |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 22 | |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 23 | # localize the types, and allow support for Unicode values if available: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 24 | import types |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 25 | _TupleType = types.TupleType |
| 26 | try: |
| 27 | _StringTypes = (types.StringType, types.UnicodeType) |
| 28 | except AttributeError: |
| 29 | _StringTypes = (types.StringType,) |
| 30 | del types |
| 31 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 32 | import xml.dom |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 33 | |
Fred Drake | 3ac6a09 | 2001-09-28 04:33:06 +0000 | [diff] [blame] | 34 | |
| 35 | if list is type([]): |
| 36 | class NodeList(list): |
| 37 | def item(self, index): |
| 38 | if 0 <= index < len(self): |
| 39 | return self[index] |
| 40 | |
Fred Drake | 575712e | 2001-09-28 20:25:45 +0000 | [diff] [blame] | 41 | length = property(lambda self: len(self), |
| 42 | doc="The number of nodes in the NodeList.") |
Fred Drake | 3ac6a09 | 2001-09-28 04:33:06 +0000 | [diff] [blame] | 43 | |
| 44 | else: |
| 45 | def NodeList(): |
| 46 | return [] |
Martin v. Löwis | 95700f7 | 2002-03-15 13:51:59 +0000 | [diff] [blame] | 47 | |
Fred Drake | 3ac6a09 | 2001-09-28 04:33:06 +0000 | [diff] [blame] | 48 | |
Fred Drake | 575712e | 2001-09-28 20:25:45 +0000 | [diff] [blame] | 49 | class Node(xml.dom.Node): |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 50 | allnodes = {} |
| 51 | _debug = 0 |
| 52 | _makeParentNodes = 1 |
| 53 | debug = None |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 54 | childNodeTypes = () |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 55 | namespaceURI = None # this is non-null only for elements and attributes |
Fred Drake | 575712e | 2001-09-28 20:25:45 +0000 | [diff] [blame] | 56 | parentNode = None |
| 57 | ownerDocument = None |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 58 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 59 | def __init__(self): |
Fred Drake | 3ac6a09 | 2001-09-28 04:33:06 +0000 | [diff] [blame] | 60 | self.childNodes = NodeList() |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 61 | if Node._debug: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 62 | index = repr(id(self)) + repr(self.__class__) |
| 63 | Node.allnodes[index] = repr(self.__dict__) |
| 64 | if Node.debug is None: |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 65 | Node.debug = _get_StringIO() |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 66 | #open("debug4.out", "w") |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 67 | Node.debug.write("create %s\n" % index) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 68 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 69 | def __nonzero__(self): |
| 70 | return 1 |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 71 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 72 | def toxml(self): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 73 | writer = _get_StringIO() |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 74 | self.writexml(writer) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 75 | return writer.getvalue() |
| 76 | |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 77 | def toprettyxml(self, indent="\t", newl="\n"): |
Martin v. Löwis | cb67ea1 | 2001-03-31 16:30:40 +0000 | [diff] [blame] | 78 | # indent = the indentation string to prepend, per level |
| 79 | # newl = the newline string to append |
| 80 | writer = _get_StringIO() |
| 81 | self.writexml(writer, "", indent, newl) |
| 82 | return writer.getvalue() |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 83 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 84 | def hasChildNodes(self): |
| 85 | if self.childNodes: |
| 86 | return 1 |
| 87 | else: |
| 88 | return 0 |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 89 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 90 | def _get_firstChild(self): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 91 | if self.childNodes: |
| 92 | return self.childNodes[0] |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 93 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 94 | def _get_lastChild(self): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 95 | if self.childNodes: |
| 96 | return self.childNodes[-1] |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 97 | |
Fred Drake | d157237 | 2001-09-29 04:58:32 +0000 | [diff] [blame] | 98 | try: |
| 99 | property |
| 100 | except NameError: |
| 101 | def __getattr__(self, key): |
| 102 | if key[0:2] == "__": |
| 103 | raise AttributeError, key |
| 104 | # getattr should never call getattr! |
| 105 | if self.__dict__.has_key("inGetAttr"): |
| 106 | del self.inGetAttr |
| 107 | raise AttributeError, key |
| 108 | |
| 109 | prefix, attrname = key[:5], key[5:] |
| 110 | if prefix == "_get_": |
| 111 | self.inGetAttr = 1 |
| 112 | if hasattr(self, attrname): |
| 113 | del self.inGetAttr |
| 114 | return (lambda self=self, attrname=attrname: |
| 115 | getattr(self, attrname)) |
| 116 | else: |
| 117 | del self.inGetAttr |
| 118 | raise AttributeError, key |
| 119 | else: |
| 120 | self.inGetAttr = 1 |
| 121 | try: |
| 122 | func = getattr(self, "_get_" + key) |
| 123 | except AttributeError: |
| 124 | raise AttributeError, key |
| 125 | del self.inGetAttr |
| 126 | return func() |
| 127 | else: |
| 128 | firstChild = property(_get_firstChild, |
| 129 | doc="First child node, or None.") |
| 130 | lastChild = property(_get_lastChild, |
| 131 | doc="Last child node, or None.") |
| 132 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 133 | def insertBefore(self, newChild, refChild): |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 134 | if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE: |
Fred Drake | e50959a | 2001-12-06 04:32:18 +0000 | [diff] [blame] | 135 | for c in tuple(newChild.childNodes): |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 136 | self.insertBefore(c, refChild) |
| 137 | ### The DOM does not clearly specify what to return in this case |
| 138 | return newChild |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 139 | if newChild.nodeType not in self.childNodeTypes: |
| 140 | raise HierarchyRequestErr, \ |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 141 | "%s cannot be child of %s" % (repr(newChild), repr(self)) |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 142 | if newChild.parentNode is not None: |
| 143 | newChild.parentNode.removeChild(newChild) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 144 | if refChild is None: |
| 145 | self.appendChild(newChild) |
| 146 | else: |
| 147 | index = self.childNodes.index(refChild) |
| 148 | self.childNodes.insert(index, newChild) |
| 149 | newChild.nextSibling = refChild |
| 150 | refChild.previousSibling = newChild |
| 151 | if index: |
| 152 | node = self.childNodes[index-1] |
| 153 | node.nextSibling = newChild |
| 154 | newChild.previousSibling = node |
| 155 | else: |
| 156 | newChild.previousSibling = None |
| 157 | if self._makeParentNodes: |
| 158 | newChild.parentNode = self |
| 159 | return newChild |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 160 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 161 | def appendChild(self, node): |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 162 | if node.nodeType == self.DOCUMENT_FRAGMENT_NODE: |
Fred Drake | e50959a | 2001-12-06 04:32:18 +0000 | [diff] [blame] | 163 | for c in tuple(node.childNodes): |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 164 | self.appendChild(c) |
| 165 | ### The DOM does not clearly specify what to return in this case |
| 166 | return node |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 167 | if node.nodeType not in self.childNodeTypes: |
| 168 | raise HierarchyRequestErr, \ |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 169 | "%s cannot be child of %s" % (repr(node), repr(self)) |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 170 | if node.parentNode is not None: |
| 171 | node.parentNode.removeChild(node) |
Fred Drake | 13a3069 | 2000-10-09 20:04:16 +0000 | [diff] [blame] | 172 | if self.childNodes: |
| 173 | last = self.lastChild |
| 174 | node.previousSibling = last |
| 175 | last.nextSibling = node |
| 176 | else: |
| 177 | node.previousSibling = None |
| 178 | node.nextSibling = None |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 179 | self.childNodes.append(node) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 180 | if self._makeParentNodes: |
| 181 | node.parentNode = self |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 182 | return node |
| 183 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 184 | def replaceChild(self, newChild, oldChild): |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 185 | if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE: |
| 186 | refChild = oldChild.nextSibling |
| 187 | self.removeChild(oldChild) |
| 188 | return self.insertBefore(newChild, refChild) |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 189 | if newChild.nodeType not in self.childNodeTypes: |
| 190 | raise HierarchyRequestErr, \ |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 191 | "%s cannot be child of %s" % (repr(newChild), repr(self)) |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 192 | if newChild.parentNode is not None: |
| 193 | newChild.parentNode.removeChild(newChild) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 194 | if newChild is oldChild: |
| 195 | return |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 196 | index = self.childNodes.index(oldChild) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 197 | self.childNodes[index] = newChild |
| 198 | if self._makeParentNodes: |
| 199 | newChild.parentNode = self |
| 200 | oldChild.parentNode = None |
| 201 | newChild.nextSibling = oldChild.nextSibling |
| 202 | newChild.previousSibling = oldChild.previousSibling |
Martin v. Löwis | 156c337 | 2000-12-28 18:40:56 +0000 | [diff] [blame] | 203 | oldChild.nextSibling = None |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 204 | oldChild.previousSibling = None |
Martin v. Löwis | 156c337 | 2000-12-28 18:40:56 +0000 | [diff] [blame] | 205 | if newChild.previousSibling: |
| 206 | newChild.previousSibling.nextSibling = newChild |
| 207 | if newChild.nextSibling: |
| 208 | newChild.nextSibling.previousSibling = newChild |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 209 | return oldChild |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 210 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 211 | def removeChild(self, oldChild): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 212 | self.childNodes.remove(oldChild) |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 213 | if oldChild.nextSibling is not None: |
| 214 | oldChild.nextSibling.previousSibling = oldChild.previousSibling |
| 215 | if oldChild.previousSibling is not None: |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 216 | oldChild.previousSibling.nextSibling = oldChild.nextSibling |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 217 | oldChild.nextSibling = oldChild.previousSibling = None |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 218 | |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 219 | if self._makeParentNodes: |
| 220 | oldChild.parentNode = None |
| 221 | return oldChild |
| 222 | |
| 223 | def normalize(self): |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 224 | L = [] |
| 225 | for child in self.childNodes: |
| 226 | if child.nodeType == Node.TEXT_NODE: |
| 227 | data = child.data |
| 228 | if data and L and L[-1].nodeType == child.nodeType: |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 229 | # collapse text node |
| 230 | node = L[-1] |
| 231 | node.data = node.nodeValue = node.data + child.data |
| 232 | node.nextSibling = child.nextSibling |
| 233 | child.unlink() |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 234 | elif data: |
| 235 | if L: |
| 236 | L[-1].nextSibling = child |
| 237 | child.previousSibling = L[-1] |
| 238 | else: |
| 239 | child.previousSibling = None |
| 240 | L.append(child) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 241 | else: |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 242 | # empty text node; discard |
| 243 | child.unlink() |
| 244 | else: |
| 245 | if L: |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 246 | L[-1].nextSibling = child |
| 247 | child.previousSibling = L[-1] |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 248 | else: |
| 249 | child.previousSibling = None |
| 250 | L.append(child) |
| 251 | if child.nodeType == Node.ELEMENT_NODE: |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 252 | child.normalize() |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 253 | self.childNodes[:] = L |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 254 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 255 | def cloneNode(self, deep): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 256 | import new |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 257 | clone = new.instance(self.__class__, self.__dict__.copy()) |
| 258 | if self._makeParentNodes: |
| 259 | clone.parentNode = None |
Fred Drake | 3ac6a09 | 2001-09-28 04:33:06 +0000 | [diff] [blame] | 260 | clone.childNodes = NodeList() |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 261 | if deep: |
| 262 | for child in self.childNodes: |
| 263 | clone.appendChild(child.cloneNode(1)) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 264 | return clone |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 265 | |
Fred Drake | 2523977 | 2001-02-02 19:40:19 +0000 | [diff] [blame] | 266 | # DOM Level 3 (Working Draft 2001-Jan-26) |
| 267 | |
| 268 | def isSameNode(self, other): |
| 269 | return self is other |
| 270 | |
| 271 | # minidom-specific API: |
| 272 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 273 | def unlink(self): |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 274 | self.parentNode = self.ownerDocument = None |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 275 | for child in self.childNodes: |
| 276 | child.unlink() |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 277 | self.childNodes = None |
Paul Prescod | 4221ff0 | 2000-10-13 20:11:42 +0000 | [diff] [blame] | 278 | self.previousSibling = None |
| 279 | self.nextSibling = None |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 280 | if Node._debug: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 281 | index = repr(id(self)) + repr(self.__class__) |
| 282 | self.debug.write("Deleting: %s\n" % index) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 283 | del Node.allnodes[index] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 284 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 285 | def _write_data(writer, data): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 286 | "Writes datachars to writer." |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 287 | replace = _string.replace |
| 288 | data = replace(data, "&", "&") |
| 289 | data = replace(data, "<", "<") |
| 290 | data = replace(data, "\"", """) |
| 291 | data = replace(data, ">", ">") |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 292 | writer.write(data) |
| 293 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 294 | def _getElementsByTagNameHelper(parent, name, rc): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 295 | for node in parent.childNodes: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 296 | if node.nodeType == Node.ELEMENT_NODE and \ |
| 297 | (name == "*" or node.tagName == name): |
| 298 | rc.append(node) |
| 299 | _getElementsByTagNameHelper(node, name, rc) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 300 | return rc |
| 301 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 302 | def _getElementsByTagNameNSHelper(parent, nsURI, localName, rc): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 303 | for node in parent.childNodes: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 304 | if node.nodeType == Node.ELEMENT_NODE: |
Martin v. Löwis | ed525fb | 2001-06-03 14:06:42 +0000 | [diff] [blame] | 305 | if ((localName == "*" or node.localName == localName) and |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 306 | (nsURI == "*" or node.namespaceURI == nsURI)): |
| 307 | rc.append(node) |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 308 | _getElementsByTagNameNSHelper(node, nsURI, localName, rc) |
| 309 | return rc |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 310 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 311 | class DocumentFragment(Node): |
| 312 | nodeType = Node.DOCUMENT_FRAGMENT_NODE |
| 313 | nodeName = "#document-fragment" |
| 314 | nodeValue = None |
| 315 | attributes = None |
| 316 | parentNode = None |
| 317 | childNodeTypes = (Node.ELEMENT_NODE, |
| 318 | Node.TEXT_NODE, |
| 319 | Node.CDATA_SECTION_NODE, |
| 320 | Node.ENTITY_REFERENCE_NODE, |
| 321 | Node.PROCESSING_INSTRUCTION_NODE, |
| 322 | Node.COMMENT_NODE, |
| 323 | Node.NOTATION_NODE) |
| 324 | |
| 325 | |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 326 | class Attr(Node): |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 327 | nodeType = Node.ATTRIBUTE_NODE |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 328 | attributes = None |
| 329 | ownerElement = None |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 330 | childNodeTypes = (Node.TEXT_NODE, Node.ENTITY_REFERENCE_NODE) |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 331 | |
Fred Drake | 49a5d03 | 2001-11-30 22:21:58 +0000 | [diff] [blame] | 332 | def __init__(self, qName, namespaceURI=EMPTY_NAMESPACE, localName=None, prefix=None): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 333 | # skip setattr for performance |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 334 | d = self.__dict__ |
| 335 | d["localName"] = localName or qName |
| 336 | d["nodeName"] = d["name"] = qName |
| 337 | d["namespaceURI"] = namespaceURI |
| 338 | d["prefix"] = prefix |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 339 | Node.__init__(self) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 340 | # nodeValue and value are set elsewhere |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 341 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 342 | def __setattr__(self, name, value): |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 343 | d = self.__dict__ |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 344 | if name in ("value", "nodeValue"): |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 345 | d["value"] = d["nodeValue"] = value |
| 346 | elif name in ("name", "nodeName"): |
| 347 | d["name"] = d["nodeName"] = value |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 348 | else: |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 349 | d[name] = value |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 350 | |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 351 | def cloneNode(self, deep): |
| 352 | clone = Node.cloneNode(self, deep) |
| 353 | if clone.__dict__.has_key("ownerElement"): |
| 354 | del clone.ownerElement |
| 355 | return clone |
| 356 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 357 | |
| 358 | class NamedNodeMap: |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 359 | """The attribute list is a transient interface to the underlying |
| 360 | dictionaries. Mutations here will change the underlying element's |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 361 | dictionary. |
| 362 | |
| 363 | Ordering is imposed artificially and does not reflect the order of |
| 364 | attributes as found in an input document. |
| 365 | """ |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 366 | |
Fred Drake | 2998a55 | 2001-12-06 18:27:48 +0000 | [diff] [blame] | 367 | def __init__(self, attrs, attrsNS, ownerElement): |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 368 | self._attrs = attrs |
| 369 | self._attrsNS = attrsNS |
Fred Drake | 2998a55 | 2001-12-06 18:27:48 +0000 | [diff] [blame] | 370 | self._ownerElement = ownerElement |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 371 | |
Fred Drake | d157237 | 2001-09-29 04:58:32 +0000 | [diff] [blame] | 372 | try: |
| 373 | property |
| 374 | except NameError: |
| 375 | def __getattr__(self, name): |
| 376 | if name == "length": |
| 377 | return len(self._attrs) |
| 378 | raise AttributeError, name |
| 379 | else: |
| 380 | length = property(lambda self: len(self._attrs), |
| 381 | doc="Number of nodes in the NamedNodeMap.") |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 382 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 383 | def item(self, index): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 384 | try: |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 385 | return self[self._attrs.keys()[index]] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 386 | except IndexError: |
| 387 | return None |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 388 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 389 | def items(self): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 390 | L = [] |
| 391 | for node in self._attrs.values(): |
Martin v. Löwis | d5fb58f | 2001-01-27 08:38:34 +0000 | [diff] [blame] | 392 | L.append((node.nodeName, node.value)) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 393 | return L |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 394 | |
| 395 | def itemsNS(self): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 396 | L = [] |
| 397 | for node in self._attrs.values(): |
Fred Drake | 49a5d03 | 2001-11-30 22:21:58 +0000 | [diff] [blame] | 398 | L.append(((node.namespaceURI, node.localName), node.value)) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 399 | return L |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 400 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 401 | def keys(self): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 402 | return self._attrs.keys() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 403 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 404 | def keysNS(self): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 405 | return self._attrsNS.keys() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 406 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 407 | def values(self): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 408 | return self._attrs.values() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 409 | |
Martin v. Löwis | d5fb58f | 2001-01-27 08:38:34 +0000 | [diff] [blame] | 410 | def get(self, name, value = None): |
| 411 | return self._attrs.get(name, value) |
| 412 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 413 | def __len__(self): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 414 | return self.length |
| 415 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 416 | def __cmp__(self, other): |
| 417 | if self._attrs is getattr(other, "_attrs", None): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 418 | return 0 |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 419 | else: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 420 | return cmp(id(self), id(other)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 421 | |
| 422 | #FIXME: is it appropriate to return .value? |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 423 | def __getitem__(self, attname_or_tuple): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 424 | if type(attname_or_tuple) is _TupleType: |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 425 | return self._attrsNS[attname_or_tuple] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 426 | else: |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 427 | return self._attrs[attname_or_tuple] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 428 | |
Paul Prescod | 1e68827 | 2000-07-01 19:21:47 +0000 | [diff] [blame] | 429 | # same as set |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 430 | def __setitem__(self, attname, value): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 431 | if type(value) in _StringTypes: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 432 | node = Attr(attname) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 433 | node.value = value |
Fred Drake | 2998a55 | 2001-12-06 18:27:48 +0000 | [diff] [blame] | 434 | node.ownerDocument = self._ownerElement.ownerDocument |
Paul Prescod | 1e68827 | 2000-07-01 19:21:47 +0000 | [diff] [blame] | 435 | else: |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 436 | if not isinstance(value, Attr): |
| 437 | raise TypeError, "value must be a string or Attr object" |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 438 | node = value |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 439 | self.setNamedItem(node) |
| 440 | |
| 441 | def setNamedItem(self, node): |
Andrew M. Kuchling | bc8f72c | 2001-02-21 01:30:26 +0000 | [diff] [blame] | 442 | if not isinstance(node, Attr): |
| 443 | raise HierarchyRequestErr, \ |
| 444 | "%s cannot be child of %s" % (repr(node), repr(self)) |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 445 | old = self._attrs.get(node.name) |
Paul Prescod | 1e68827 | 2000-07-01 19:21:47 +0000 | [diff] [blame] | 446 | if old: |
| 447 | old.unlink() |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 448 | self._attrs[node.name] = node |
| 449 | self._attrsNS[(node.namespaceURI, node.localName)] = node |
Fred Drake | 2998a55 | 2001-12-06 18:27:48 +0000 | [diff] [blame] | 450 | node.ownerElement = self._ownerElement |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 451 | return old |
| 452 | |
| 453 | def setNamedItemNS(self, node): |
| 454 | return self.setNamedItem(node) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 455 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 456 | def __delitem__(self, attname_or_tuple): |
| 457 | node = self[attname_or_tuple] |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 458 | node.unlink() |
| 459 | del self._attrs[node.name] |
| 460 | del self._attrsNS[(node.namespaceURI, node.localName)] |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 461 | self.length = len(self._attrs) |
| 462 | |
| 463 | AttributeList = NamedNodeMap |
| 464 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 465 | |
Martin v. Löwis | a2fda0d | 2000-10-07 12:10:28 +0000 | [diff] [blame] | 466 | class Element(Node): |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 467 | nodeType = Node.ELEMENT_NODE |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 468 | nextSibling = None |
| 469 | previousSibling = None |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 470 | childNodeTypes = (Node.ELEMENT_NODE, Node.PROCESSING_INSTRUCTION_NODE, |
| 471 | Node.COMMENT_NODE, Node.TEXT_NODE, |
| 472 | Node.CDATA_SECTION_NODE, Node.ENTITY_REFERENCE_NODE) |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 473 | |
Fred Drake | 49a5d03 | 2001-11-30 22:21:58 +0000 | [diff] [blame] | 474 | def __init__(self, tagName, namespaceURI=EMPTY_NAMESPACE, prefix=None, |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 475 | localName=None): |
| 476 | Node.__init__(self) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 477 | self.tagName = self.nodeName = tagName |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 478 | self.localName = localName or tagName |
| 479 | self.prefix = prefix |
| 480 | self.namespaceURI = namespaceURI |
| 481 | self.nodeValue = None |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 482 | |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 483 | self._attrs = {} # attributes are double-indexed: |
| 484 | self._attrsNS = {} # tagName -> Attribute |
| 485 | # URI,localName -> Attribute |
| 486 | # in the future: consider lazy generation |
| 487 | # of attribute objects this is too tricky |
| 488 | # for now because of headaches with |
| 489 | # namespaces. |
| 490 | |
| 491 | def cloneNode(self, deep): |
| 492 | clone = Node.cloneNode(self, deep) |
| 493 | clone._attrs = {} |
| 494 | clone._attrsNS = {} |
| 495 | for attr in self._attrs.values(): |
| 496 | node = attr.cloneNode(1) |
| 497 | clone._attrs[node.name] = node |
| 498 | clone._attrsNS[(node.namespaceURI, node.localName)] = node |
| 499 | node.ownerElement = clone |
| 500 | return clone |
| 501 | |
| 502 | def unlink(self): |
| 503 | for attr in self._attrs.values(): |
| 504 | attr.unlink() |
| 505 | self._attrs = None |
| 506 | self._attrsNS = None |
| 507 | Node.unlink(self) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 508 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 509 | def getAttribute(self, attname): |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 510 | try: |
| 511 | return self._attrs[attname].value |
| 512 | except KeyError: |
| 513 | return "" |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 514 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 515 | def getAttributeNS(self, namespaceURI, localName): |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 516 | try: |
| 517 | return self._attrsNS[(namespaceURI, localName)].value |
| 518 | except KeyError: |
| 519 | return "" |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 520 | |
| 521 | def setAttribute(self, attname, value): |
| 522 | attr = Attr(attname) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 523 | # for performance |
Fred Drake | 2998a55 | 2001-12-06 18:27:48 +0000 | [diff] [blame] | 524 | d = attr.__dict__ |
| 525 | d["value"] = d["nodeValue"] = value |
| 526 | d["ownerDocument"] = self.ownerDocument |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 527 | self.setAttributeNode(attr) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 528 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 529 | def setAttributeNS(self, namespaceURI, qualifiedName, value): |
| 530 | prefix, localname = _nssplit(qualifiedName) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 531 | # for performance |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 532 | attr = Attr(qualifiedName, namespaceURI, localname, prefix) |
Fred Drake | 2998a55 | 2001-12-06 18:27:48 +0000 | [diff] [blame] | 533 | d = attr.__dict__ |
| 534 | d["value"] = d["nodeValue"] = value |
| 535 | d["ownerDocument"] = self.ownerDocument |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 536 | self.setAttributeNode(attr) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 537 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 538 | def getAttributeNode(self, attrname): |
| 539 | return self._attrs.get(attrname) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 540 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 541 | def getAttributeNodeNS(self, namespaceURI, localName): |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 542 | return self._attrsNS.get((namespaceURI, localName)) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 543 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 544 | def setAttributeNode(self, attr): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 545 | if attr.ownerElement not in (None, self): |
Martin v. Löwis | d5fb58f | 2001-01-27 08:38:34 +0000 | [diff] [blame] | 546 | raise xml.dom.InuseAttributeErr("attribute node already owned") |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 547 | old = self._attrs.get(attr.name, None) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 548 | if old: |
| 549 | old.unlink() |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 550 | self._attrs[attr.name] = attr |
| 551 | self._attrsNS[(attr.namespaceURI, attr.localName)] = attr |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 552 | |
| 553 | # This creates a circular reference, but Element.unlink() |
| 554 | # breaks the cycle since the references to the attribute |
| 555 | # dictionaries are tossed. |
| 556 | attr.ownerElement = self |
| 557 | |
| 558 | if old is not attr: |
| 559 | # It might have already been part of this node, in which case |
| 560 | # it doesn't represent a change, and should not be returned. |
| 561 | return old |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 562 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 563 | setAttributeNodeNS = setAttributeNode |
| 564 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 565 | def removeAttribute(self, name): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 566 | attr = self._attrs[name] |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 567 | self.removeAttributeNode(attr) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 568 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 569 | def removeAttributeNS(self, namespaceURI, localName): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 570 | attr = self._attrsNS[(namespaceURI, localName)] |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 571 | self.removeAttributeNode(attr) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 572 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 573 | def removeAttributeNode(self, node): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 574 | node.unlink() |
| 575 | del self._attrs[node.name] |
| 576 | del self._attrsNS[(node.namespaceURI, node.localName)] |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 577 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 578 | removeAttributeNodeNS = removeAttributeNode |
| 579 | |
Martin v. Löwis | 156c337 | 2000-12-28 18:40:56 +0000 | [diff] [blame] | 580 | def hasAttribute(self, name): |
| 581 | return self._attrs.has_key(name) |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 582 | |
Martin v. Löwis | 156c337 | 2000-12-28 18:40:56 +0000 | [diff] [blame] | 583 | def hasAttributeNS(self, namespaceURI, localName): |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 584 | return self._attrsNS.has_key((namespaceURI, localName)) |
| 585 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 586 | def getElementsByTagName(self, name): |
Martin v. Löwis | 95700f7 | 2002-03-15 13:51:59 +0000 | [diff] [blame] | 587 | return _getElementsByTagNameHelper(self, name, NodeList()) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 588 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 589 | def getElementsByTagNameNS(self, namespaceURI, localName): |
Fred Drake | 15b6893 | 2002-03-15 14:37:23 +0000 | [diff] [blame] | 590 | return _getElementsByTagNameNSHelper(self, namespaceURI, localName, |
| 591 | NodeList()) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 592 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 593 | def __repr__(self): |
| 594 | return "<DOM Element: %s at %s>" % (self.tagName, id(self)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 595 | |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 596 | def writexml(self, writer, indent="", addindent="", newl=""): |
| 597 | # indent = current indentation |
| 598 | # addindent = indentation to add to higher levels |
| 599 | # newl = newline string |
| 600 | writer.write(indent+"<" + self.tagName) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 601 | |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 602 | attrs = self._get_attributes() |
| 603 | a_names = attrs.keys() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 604 | a_names.sort() |
| 605 | |
| 606 | for a_name in a_names: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 607 | writer.write(" %s=\"" % a_name) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 608 | _write_data(writer, attrs[a_name].value) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 609 | writer.write("\"") |
| 610 | if self.childNodes: |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 611 | writer.write(">%s"%(newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 612 | for node in self.childNodes: |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 613 | node.writexml(writer,indent+addindent,addindent,newl) |
| 614 | writer.write("%s</%s>%s" % (indent,self.tagName,newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 615 | else: |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 616 | writer.write("/>%s"%(newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 617 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 618 | def _get_attributes(self): |
Fred Drake | 2998a55 | 2001-12-06 18:27:48 +0000 | [diff] [blame] | 619 | return NamedNodeMap(self._attrs, self._attrsNS, self) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 620 | |
Fred Drake | d157237 | 2001-09-29 04:58:32 +0000 | [diff] [blame] | 621 | try: |
| 622 | property |
| 623 | except NameError: |
| 624 | pass |
| 625 | else: |
| 626 | attributes = property(_get_attributes, |
| 627 | doc="NamedNodeMap of attributes on the element.") |
| 628 | |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 629 | def hasAttributes(self): |
| 630 | if self._attrs or self._attrsNS: |
| 631 | return 1 |
| 632 | else: |
| 633 | return 0 |
| 634 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 635 | class Comment(Node): |
| 636 | nodeType = Node.COMMENT_NODE |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 637 | nodeName = "#comment" |
| 638 | attributes = None |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 639 | childNodeTypes = () |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 640 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 641 | def __init__(self, data): |
| 642 | Node.__init__(self) |
| 643 | self.data = self.nodeValue = data |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 644 | |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 645 | def writexml(self, writer, indent="", addindent="", newl=""): |
| 646 | writer.write("%s<!--%s-->%s" % (indent,self.data,newl)) |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 647 | |
| 648 | class ProcessingInstruction(Node): |
| 649 | nodeType = Node.PROCESSING_INSTRUCTION_NODE |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 650 | attributes = None |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 651 | childNodeTypes = () |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 652 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 653 | def __init__(self, target, data): |
| 654 | Node.__init__(self) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 655 | self.target = self.nodeName = target |
| 656 | self.data = self.nodeValue = data |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 657 | |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 658 | def writexml(self, writer, indent="", addindent="", newl=""): |
| 659 | writer.write("%s<?%s %s?>%s" % (indent,self.target, self.data, newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 660 | |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 661 | class CharacterData(Node): |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 662 | def __init__(self, data): |
Fred Drake | daa823a | 2001-01-08 04:04:34 +0000 | [diff] [blame] | 663 | if type(data) not in _StringTypes: |
| 664 | raise TypeError, "node contents must be a string" |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 665 | Node.__init__(self) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 666 | self.data = self.nodeValue = data |
Fred Drake | 33d2b84 | 2001-04-04 15:15:18 +0000 | [diff] [blame] | 667 | self.length = len(data) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 668 | |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 669 | def __repr__(self): |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 670 | if len(self.data) > 10: |
| 671 | dotdotdot = "..." |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 672 | else: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 673 | dotdotdot = "" |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 674 | return "<DOM %s node \"%s%s\">" % ( |
| 675 | self.__class__.__name__, self.data[0:10], dotdotdot) |
| 676 | |
| 677 | def substringData(self, offset, count): |
| 678 | if offset < 0: |
| 679 | raise xml.dom.IndexSizeErr("offset cannot be negative") |
| 680 | if offset >= len(self.data): |
| 681 | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
| 682 | if count < 0: |
| 683 | raise xml.dom.IndexSizeErr("count cannot be negative") |
| 684 | return self.data[offset:offset+count] |
| 685 | |
| 686 | def appendData(self, arg): |
| 687 | self.data = self.data + arg |
| 688 | self.nodeValue = self.data |
Fred Drake | 33d2b84 | 2001-04-04 15:15:18 +0000 | [diff] [blame] | 689 | self.length = len(self.data) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 690 | |
| 691 | def insertData(self, offset, arg): |
| 692 | if offset < 0: |
| 693 | raise xml.dom.IndexSizeErr("offset cannot be negative") |
| 694 | if offset >= len(self.data): |
| 695 | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
| 696 | if arg: |
| 697 | self.data = "%s%s%s" % ( |
| 698 | self.data[:offset], arg, self.data[offset:]) |
| 699 | self.nodeValue = self.data |
Fred Drake | 33d2b84 | 2001-04-04 15:15:18 +0000 | [diff] [blame] | 700 | self.length = len(self.data) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 701 | |
| 702 | def deleteData(self, offset, count): |
| 703 | if offset < 0: |
| 704 | raise xml.dom.IndexSizeErr("offset cannot be negative") |
| 705 | if offset >= len(self.data): |
| 706 | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
| 707 | if count < 0: |
| 708 | raise xml.dom.IndexSizeErr("count cannot be negative") |
| 709 | if count: |
| 710 | self.data = self.data[:offset] + self.data[offset+count:] |
| 711 | self.nodeValue = self.data |
Fred Drake | 33d2b84 | 2001-04-04 15:15:18 +0000 | [diff] [blame] | 712 | self.length = len(self.data) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 713 | |
| 714 | def replaceData(self, offset, count, arg): |
| 715 | if offset < 0: |
| 716 | raise xml.dom.IndexSizeErr("offset cannot be negative") |
| 717 | if offset >= len(self.data): |
| 718 | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
| 719 | if count < 0: |
| 720 | raise xml.dom.IndexSizeErr("count cannot be negative") |
| 721 | if count: |
| 722 | self.data = "%s%s%s" % ( |
| 723 | self.data[:offset], arg, self.data[offset+count:]) |
| 724 | self.nodeValue = self.data |
Fred Drake | 33d2b84 | 2001-04-04 15:15:18 +0000 | [diff] [blame] | 725 | self.length = len(self.data) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 726 | |
| 727 | class Text(CharacterData): |
| 728 | nodeType = Node.TEXT_NODE |
| 729 | nodeName = "#text" |
| 730 | attributes = None |
| 731 | childNodeTypes = () |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 732 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 733 | def splitText(self, offset): |
| 734 | if offset < 0 or offset > len(self.data): |
Martin v. Löwis | d5fb58f | 2001-01-27 08:38:34 +0000 | [diff] [blame] | 735 | raise xml.dom.IndexSizeErr("illegal offset value") |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 736 | newText = Text(self.data[offset:]) |
| 737 | next = self.nextSibling |
| 738 | if self.parentNode and self in self.parentNode.childNodes: |
| 739 | if next is None: |
| 740 | self.parentNode.appendChild(newText) |
| 741 | else: |
| 742 | self.parentNode.insertBefore(newText, next) |
| 743 | self.data = self.data[:offset] |
Fred Drake | 33d2b84 | 2001-04-04 15:15:18 +0000 | [diff] [blame] | 744 | self.nodeValue = self.data |
| 745 | self.length = len(self.data) |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 746 | return newText |
| 747 | |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 748 | def writexml(self, writer, indent="", addindent="", newl=""): |
| 749 | _write_data(writer, "%s%s%s"%(indent, self.data, newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 750 | |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 751 | |
| 752 | class CDATASection(Text): |
| 753 | nodeType = Node.CDATA_SECTION_NODE |
| 754 | nodeName = "#cdata-section" |
| 755 | |
| 756 | def writexml(self, writer, indent="", addindent="", newl=""): |
Guido van Rossum | 5b5e0b9 | 2001-09-19 13:28:25 +0000 | [diff] [blame] | 757 | writer.write("<![CDATA[%s]]>" % self.data) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 758 | |
| 759 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 760 | def _nssplit(qualifiedName): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 761 | fields = _string.split(qualifiedName, ':', 1) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 762 | if len(fields) == 2: |
| 763 | return fields |
| 764 | elif len(fields) == 1: |
Fred Drake | 49a5d03 | 2001-11-30 22:21:58 +0000 | [diff] [blame] | 765 | return (None, fields[0]) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 766 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 767 | |
| 768 | class DocumentType(Node): |
| 769 | nodeType = Node.DOCUMENT_TYPE_NODE |
| 770 | nodeValue = None |
| 771 | attributes = None |
| 772 | name = None |
| 773 | publicId = None |
| 774 | systemId = None |
Fred Drake | dc80670 | 2001-04-05 14:41:30 +0000 | [diff] [blame] | 775 | internalSubset = None |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 776 | entities = None |
| 777 | notations = None |
| 778 | |
| 779 | def __init__(self, qualifiedName): |
| 780 | Node.__init__(self) |
| 781 | if qualifiedName: |
| 782 | prefix, localname = _nssplit(qualifiedName) |
| 783 | self.name = localname |
| 784 | |
| 785 | |
| 786 | class DOMImplementation: |
| 787 | def hasFeature(self, feature, version): |
| 788 | if version not in ("1.0", "2.0"): |
| 789 | return 0 |
| 790 | feature = _string.lower(feature) |
| 791 | return feature == "core" |
| 792 | |
| 793 | def createDocument(self, namespaceURI, qualifiedName, doctype): |
| 794 | if doctype and doctype.parentNode is not None: |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 795 | raise xml.dom.WrongDocumentErr( |
| 796 | "doctype object owned by another DOM tree") |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 797 | doc = self._createDocument() |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 798 | if doctype is None: |
| 799 | doctype = self.createDocumentType(qualifiedName, None, None) |
Martin v. Löwis | b417be2 | 2001-02-06 01:16:06 +0000 | [diff] [blame] | 800 | if not qualifiedName: |
| 801 | # The spec is unclear what to raise here; SyntaxErr |
| 802 | # would be the other obvious candidate. Since Xerces raises |
| 803 | # InvalidCharacterErr, and since SyntaxErr is not listed |
| 804 | # for createDocument, that seems to be the better choice. |
| 805 | # XXX: need to check for illegal characters here and in |
| 806 | # createElement. |
| 807 | raise xml.dom.InvalidCharacterErr("Element with no name") |
| 808 | prefix, localname = _nssplit(qualifiedName) |
| 809 | if prefix == "xml" \ |
| 810 | and namespaceURI != "http://www.w3.org/XML/1998/namespace": |
| 811 | raise xml.dom.NamespaceErr("illegal use of 'xml' prefix") |
| 812 | if prefix and not namespaceURI: |
| 813 | raise xml.dom.NamespaceErr( |
| 814 | "illegal use of prefix without namespaces") |
| 815 | element = doc.createElementNS(namespaceURI, qualifiedName) |
| 816 | doc.appendChild(element) |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 817 | doctype.parentNode = doctype.ownerDocument = doc |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 818 | doc.doctype = doctype |
| 819 | doc.implementation = self |
| 820 | return doc |
| 821 | |
| 822 | def createDocumentType(self, qualifiedName, publicId, systemId): |
| 823 | doctype = DocumentType(qualifiedName) |
| 824 | doctype.publicId = publicId |
| 825 | doctype.systemId = systemId |
| 826 | return doctype |
| 827 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 828 | # internal |
| 829 | def _createDocument(self): |
| 830 | return Document() |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 831 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 832 | class Document(Node): |
| 833 | nodeType = Node.DOCUMENT_NODE |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 834 | nodeName = "#document" |
| 835 | nodeValue = None |
| 836 | attributes = None |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 837 | doctype = None |
| 838 | parentNode = None |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 839 | previousSibling = nextSibling = None |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 840 | |
| 841 | implementation = DOMImplementation() |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 842 | childNodeTypes = (Node.ELEMENT_NODE, Node.PROCESSING_INSTRUCTION_NODE, |
| 843 | Node.COMMENT_NODE, Node.DOCUMENT_TYPE_NODE) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 844 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 845 | def appendChild(self, node): |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 846 | if node.nodeType not in self.childNodeTypes: |
| 847 | raise HierarchyRequestErr, \ |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 848 | "%s cannot be child of %s" % (repr(node), repr(self)) |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 849 | if node.parentNode is not None: |
| 850 | node.parentNode.removeChild(node) |
| 851 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 852 | if node.nodeType == Node.ELEMENT_NODE \ |
| 853 | and self._get_documentElement(): |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 854 | raise xml.dom.HierarchyRequestErr( |
| 855 | "two document elements disallowed") |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 856 | return Node.appendChild(self, node) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 857 | |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 858 | def removeChild(self, oldChild): |
| 859 | self.childNodes.remove(oldChild) |
| 860 | oldChild.nextSibling = oldChild.previousSibling = None |
| 861 | oldChild.parentNode = None |
| 862 | if self.documentElement is oldChild: |
| 863 | self.documentElement = None |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 864 | |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 865 | return oldChild |
| 866 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 867 | def _get_documentElement(self): |
| 868 | for node in self.childNodes: |
| 869 | if node.nodeType == Node.ELEMENT_NODE: |
| 870 | return node |
| 871 | |
Fred Drake | d157237 | 2001-09-29 04:58:32 +0000 | [diff] [blame] | 872 | try: |
| 873 | property |
| 874 | except NameError: |
| 875 | pass |
| 876 | else: |
| 877 | documentElement = property(_get_documentElement, |
| 878 | doc="Top-level element of this document.") |
| 879 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 880 | def unlink(self): |
| 881 | if self.doctype is not None: |
| 882 | self.doctype.unlink() |
| 883 | self.doctype = None |
| 884 | Node.unlink(self) |
| 885 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 886 | def createDocumentFragment(self): |
| 887 | d = DocumentFragment() |
| 888 | d.ownerDoc = self |
| 889 | return d |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 890 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 891 | def createElement(self, tagName): |
| 892 | e = Element(tagName) |
| 893 | e.ownerDocument = self |
| 894 | return e |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 895 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 896 | def createTextNode(self, data): |
| 897 | t = Text(data) |
| 898 | t.ownerDocument = self |
| 899 | return t |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 900 | |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 901 | def createCDATASection(self, data): |
| 902 | c = CDATASection(data) |
| 903 | c.ownerDocument = self |
| 904 | return c |
| 905 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 906 | def createComment(self, data): |
| 907 | c = Comment(data) |
| 908 | c.ownerDocument = self |
| 909 | return c |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 910 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 911 | def createProcessingInstruction(self, target, data): |
| 912 | p = ProcessingInstruction(target, data) |
| 913 | p.ownerDocument = self |
| 914 | return p |
| 915 | |
| 916 | def createAttribute(self, qName): |
| 917 | a = Attr(qName) |
| 918 | a.ownerDocument = self |
Martin v. Löwis | cb67ea1 | 2001-03-31 16:30:40 +0000 | [diff] [blame] | 919 | a.value = "" |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 920 | return a |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 921 | |
| 922 | def createElementNS(self, namespaceURI, qualifiedName): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 923 | prefix, localName = _nssplit(qualifiedName) |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 924 | e = Element(qualifiedName, namespaceURI, prefix, localName) |
| 925 | e.ownerDocument = self |
| 926 | return e |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 927 | |
| 928 | def createAttributeNS(self, namespaceURI, qualifiedName): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 929 | prefix, localName = _nssplit(qualifiedName) |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 930 | a = Attr(qualifiedName, namespaceURI, localName, prefix) |
| 931 | a.ownerDocument = self |
Martin v. Löwis | cb67ea1 | 2001-03-31 16:30:40 +0000 | [diff] [blame] | 932 | a.value = "" |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 933 | return a |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 934 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 935 | def getElementsByTagName(self, name): |
Martin v. Löwis | 95700f7 | 2002-03-15 13:51:59 +0000 | [diff] [blame] | 936 | return _getElementsByTagNameHelper(self, name, NodeList()) |
Fred Drake | fbe7b4f | 2001-07-04 06:25:53 +0000 | [diff] [blame] | 937 | |
| 938 | def getElementsByTagNameNS(self, namespaceURI, localName): |
Fred Drake | 15b6893 | 2002-03-15 14:37:23 +0000 | [diff] [blame] | 939 | return _getElementsByTagNameNSHelper(self, namespaceURI, localName, |
| 940 | NodeList()) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 941 | |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 942 | def writexml(self, writer, indent="", addindent="", newl=""): |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 943 | writer.write('<?xml version="1.0" ?>\n') |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 944 | for node in self.childNodes: |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 945 | node.writexml(writer, indent, addindent, newl) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 946 | |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 947 | def _get_StringIO(): |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 948 | # we can't use cStringIO since it doesn't support Unicode strings |
| 949 | from StringIO import StringIO |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 950 | return StringIO() |
| 951 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 952 | def _doparse(func, args, kwargs): |
| 953 | events = apply(func, args, kwargs) |
| 954 | toktype, rootNode = events.getEvent() |
| 955 | events.expandNode(rootNode) |
Martin v. Löwis | b417be2 | 2001-02-06 01:16:06 +0000 | [diff] [blame] | 956 | events.clear() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 957 | return rootNode |
| 958 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 959 | def parse(*args, **kwargs): |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 960 | """Parse a file into a DOM by filename or file object.""" |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 961 | from xml.dom import pulldom |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 962 | return _doparse(pulldom.parse, args, kwargs) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 963 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 964 | def parseString(*args, **kwargs): |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 965 | """Parse a file into a DOM from a string.""" |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 966 | from xml.dom import pulldom |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 967 | return _doparse(pulldom.parseString, args, kwargs) |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 968 | |
| 969 | def getDOMImplementation(): |
| 970 | return Document.implementation |