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 [] |
| 47 | |
| 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: |
| 135 | for c in newChild.childNodes: |
| 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: |
| 163 | for c in node.childNodes: |
| 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 | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 367 | def __init__(self, attrs, attrsNS): |
| 368 | self._attrs = attrs |
| 369 | self._attrsNS = attrsNS |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 370 | |
Fred Drake | d157237 | 2001-09-29 04:58:32 +0000 | [diff] [blame] | 371 | try: |
| 372 | property |
| 373 | except NameError: |
| 374 | def __getattr__(self, name): |
| 375 | if name == "length": |
| 376 | return len(self._attrs) |
| 377 | raise AttributeError, name |
| 378 | else: |
| 379 | length = property(lambda self: len(self._attrs), |
| 380 | doc="Number of nodes in the NamedNodeMap.") |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 381 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 382 | def item(self, index): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 383 | try: |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 384 | return self[self._attrs.keys()[index]] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 385 | except IndexError: |
| 386 | return None |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 387 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 388 | def items(self): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 389 | L = [] |
| 390 | for node in self._attrs.values(): |
Martin v. Löwis | d5fb58f | 2001-01-27 08:38:34 +0000 | [diff] [blame] | 391 | L.append((node.nodeName, node.value)) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 392 | return L |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 393 | |
| 394 | def itemsNS(self): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 395 | L = [] |
| 396 | for node in self._attrs.values(): |
Fred Drake | 49a5d03 | 2001-11-30 22:21:58 +0000 | [diff] [blame^] | 397 | L.append(((node.namespaceURI, node.localName), node.value)) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 398 | return L |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 399 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 400 | def keys(self): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 401 | return self._attrs.keys() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 402 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 403 | def keysNS(self): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 404 | return self._attrsNS.keys() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 405 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 406 | def values(self): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 407 | return self._attrs.values() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 408 | |
Martin v. Löwis | d5fb58f | 2001-01-27 08:38:34 +0000 | [diff] [blame] | 409 | def get(self, name, value = None): |
| 410 | return self._attrs.get(name, value) |
| 411 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 412 | def __len__(self): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 413 | return self.length |
| 414 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 415 | def __cmp__(self, other): |
| 416 | if self._attrs is getattr(other, "_attrs", None): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 417 | return 0 |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 418 | else: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 419 | return cmp(id(self), id(other)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 420 | |
| 421 | #FIXME: is it appropriate to return .value? |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 422 | def __getitem__(self, attname_or_tuple): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 423 | if type(attname_or_tuple) is _TupleType: |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 424 | return self._attrsNS[attname_or_tuple] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 425 | else: |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 426 | return self._attrs[attname_or_tuple] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 427 | |
Paul Prescod | 1e68827 | 2000-07-01 19:21:47 +0000 | [diff] [blame] | 428 | # same as set |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 429 | def __setitem__(self, attname, value): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 430 | if type(value) in _StringTypes: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 431 | node = Attr(attname) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 432 | node.value = value |
Paul Prescod | 1e68827 | 2000-07-01 19:21:47 +0000 | [diff] [blame] | 433 | else: |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 434 | if not isinstance(value, Attr): |
| 435 | raise TypeError, "value must be a string or Attr object" |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 436 | node = value |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 437 | self.setNamedItem(node) |
| 438 | |
| 439 | def setNamedItem(self, node): |
Andrew M. Kuchling | bc8f72c | 2001-02-21 01:30:26 +0000 | [diff] [blame] | 440 | if not isinstance(node, Attr): |
| 441 | raise HierarchyRequestErr, \ |
| 442 | "%s cannot be child of %s" % (repr(node), repr(self)) |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 443 | old = self._attrs.get(node.name) |
Paul Prescod | 1e68827 | 2000-07-01 19:21:47 +0000 | [diff] [blame] | 444 | if old: |
| 445 | old.unlink() |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 446 | self._attrs[node.name] = node |
| 447 | self._attrsNS[(node.namespaceURI, node.localName)] = node |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 448 | return old |
| 449 | |
| 450 | def setNamedItemNS(self, node): |
| 451 | return self.setNamedItem(node) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 452 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 453 | def __delitem__(self, attname_or_tuple): |
| 454 | node = self[attname_or_tuple] |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 455 | node.unlink() |
| 456 | del self._attrs[node.name] |
| 457 | del self._attrsNS[(node.namespaceURI, node.localName)] |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 458 | self.length = len(self._attrs) |
| 459 | |
| 460 | AttributeList = NamedNodeMap |
| 461 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 462 | |
Martin v. Löwis | a2fda0d | 2000-10-07 12:10:28 +0000 | [diff] [blame] | 463 | class Element(Node): |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 464 | nodeType = Node.ELEMENT_NODE |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 465 | nextSibling = None |
| 466 | previousSibling = None |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 467 | childNodeTypes = (Node.ELEMENT_NODE, Node.PROCESSING_INSTRUCTION_NODE, |
| 468 | Node.COMMENT_NODE, Node.TEXT_NODE, |
| 469 | Node.CDATA_SECTION_NODE, Node.ENTITY_REFERENCE_NODE) |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 470 | |
Fred Drake | 49a5d03 | 2001-11-30 22:21:58 +0000 | [diff] [blame^] | 471 | def __init__(self, tagName, namespaceURI=EMPTY_NAMESPACE, prefix=None, |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 472 | localName=None): |
| 473 | Node.__init__(self) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 474 | self.tagName = self.nodeName = tagName |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 475 | self.localName = localName or tagName |
| 476 | self.prefix = prefix |
| 477 | self.namespaceURI = namespaceURI |
| 478 | self.nodeValue = None |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 479 | |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 480 | self._attrs = {} # attributes are double-indexed: |
| 481 | self._attrsNS = {} # tagName -> Attribute |
| 482 | # URI,localName -> Attribute |
| 483 | # in the future: consider lazy generation |
| 484 | # of attribute objects this is too tricky |
| 485 | # for now because of headaches with |
| 486 | # namespaces. |
| 487 | |
| 488 | def cloneNode(self, deep): |
| 489 | clone = Node.cloneNode(self, deep) |
| 490 | clone._attrs = {} |
| 491 | clone._attrsNS = {} |
| 492 | for attr in self._attrs.values(): |
| 493 | node = attr.cloneNode(1) |
| 494 | clone._attrs[node.name] = node |
| 495 | clone._attrsNS[(node.namespaceURI, node.localName)] = node |
| 496 | node.ownerElement = clone |
| 497 | return clone |
| 498 | |
| 499 | def unlink(self): |
| 500 | for attr in self._attrs.values(): |
| 501 | attr.unlink() |
| 502 | self._attrs = None |
| 503 | self._attrsNS = None |
| 504 | Node.unlink(self) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 505 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 506 | def getAttribute(self, attname): |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 507 | try: |
| 508 | return self._attrs[attname].value |
| 509 | except KeyError: |
| 510 | return "" |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 511 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 512 | def getAttributeNS(self, namespaceURI, localName): |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 513 | try: |
| 514 | return self._attrsNS[(namespaceURI, localName)].value |
| 515 | except KeyError: |
| 516 | return "" |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 517 | |
| 518 | def setAttribute(self, attname, value): |
| 519 | attr = Attr(attname) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 520 | # for performance |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 521 | attr.__dict__["value"] = attr.__dict__["nodeValue"] = value |
| 522 | self.setAttributeNode(attr) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 523 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 524 | def setAttributeNS(self, namespaceURI, qualifiedName, value): |
| 525 | prefix, localname = _nssplit(qualifiedName) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 526 | # for performance |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 527 | attr = Attr(qualifiedName, namespaceURI, localname, prefix) |
| 528 | attr.__dict__["value"] = attr.__dict__["nodeValue"] = value |
| 529 | self.setAttributeNode(attr) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 530 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 531 | def getAttributeNode(self, attrname): |
| 532 | return self._attrs.get(attrname) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 533 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 534 | def getAttributeNodeNS(self, namespaceURI, localName): |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 535 | return self._attrsNS.get((namespaceURI, localName)) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 536 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 537 | def setAttributeNode(self, attr): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 538 | if attr.ownerElement not in (None, self): |
Martin v. Löwis | d5fb58f | 2001-01-27 08:38:34 +0000 | [diff] [blame] | 539 | raise xml.dom.InuseAttributeErr("attribute node already owned") |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 540 | old = self._attrs.get(attr.name, None) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 541 | if old: |
| 542 | old.unlink() |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 543 | self._attrs[attr.name] = attr |
| 544 | self._attrsNS[(attr.namespaceURI, attr.localName)] = attr |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 545 | |
| 546 | # This creates a circular reference, but Element.unlink() |
| 547 | # breaks the cycle since the references to the attribute |
| 548 | # dictionaries are tossed. |
| 549 | attr.ownerElement = self |
| 550 | |
| 551 | if old is not attr: |
| 552 | # It might have already been part of this node, in which case |
| 553 | # it doesn't represent a change, and should not be returned. |
| 554 | return old |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 555 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 556 | setAttributeNodeNS = setAttributeNode |
| 557 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 558 | def removeAttribute(self, name): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 559 | attr = self._attrs[name] |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 560 | self.removeAttributeNode(attr) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 561 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 562 | def removeAttributeNS(self, namespaceURI, localName): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 563 | attr = self._attrsNS[(namespaceURI, localName)] |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 564 | self.removeAttributeNode(attr) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 565 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 566 | def removeAttributeNode(self, node): |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 567 | node.unlink() |
| 568 | del self._attrs[node.name] |
| 569 | del self._attrsNS[(node.namespaceURI, node.localName)] |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 570 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 571 | removeAttributeNodeNS = removeAttributeNode |
| 572 | |
Martin v. Löwis | 156c337 | 2000-12-28 18:40:56 +0000 | [diff] [blame] | 573 | def hasAttribute(self, name): |
| 574 | return self._attrs.has_key(name) |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 575 | |
Martin v. Löwis | 156c337 | 2000-12-28 18:40:56 +0000 | [diff] [blame] | 576 | def hasAttributeNS(self, namespaceURI, localName): |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 577 | return self._attrsNS.has_key((namespaceURI, localName)) |
| 578 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 579 | def getElementsByTagName(self, name): |
| 580 | return _getElementsByTagNameHelper(self, name, []) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 581 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 582 | def getElementsByTagNameNS(self, namespaceURI, localName): |
Fred Drake | fbe7b4f | 2001-07-04 06:25:53 +0000 | [diff] [blame] | 583 | return _getElementsByTagNameNSHelper(self, namespaceURI, localName, []) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 584 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 585 | def __repr__(self): |
| 586 | return "<DOM Element: %s at %s>" % (self.tagName, id(self)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 587 | |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 588 | def writexml(self, writer, indent="", addindent="", newl=""): |
| 589 | # indent = current indentation |
| 590 | # addindent = indentation to add to higher levels |
| 591 | # newl = newline string |
| 592 | writer.write(indent+"<" + self.tagName) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 593 | |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 594 | attrs = self._get_attributes() |
| 595 | a_names = attrs.keys() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 596 | a_names.sort() |
| 597 | |
| 598 | for a_name in a_names: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 599 | writer.write(" %s=\"" % a_name) |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 600 | _write_data(writer, attrs[a_name].value) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 601 | writer.write("\"") |
| 602 | if self.childNodes: |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 603 | writer.write(">%s"%(newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 604 | for node in self.childNodes: |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 605 | node.writexml(writer,indent+addindent,addindent,newl) |
| 606 | writer.write("%s</%s>%s" % (indent,self.tagName,newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 607 | else: |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 608 | writer.write("/>%s"%(newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 609 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 610 | def _get_attributes(self): |
| 611 | return AttributeList(self._attrs, self._attrsNS) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 612 | |
Fred Drake | d157237 | 2001-09-29 04:58:32 +0000 | [diff] [blame] | 613 | try: |
| 614 | property |
| 615 | except NameError: |
| 616 | pass |
| 617 | else: |
| 618 | attributes = property(_get_attributes, |
| 619 | doc="NamedNodeMap of attributes on the element.") |
| 620 | |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 621 | def hasAttributes(self): |
| 622 | if self._attrs or self._attrsNS: |
| 623 | return 1 |
| 624 | else: |
| 625 | return 0 |
| 626 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 627 | class Comment(Node): |
| 628 | nodeType = Node.COMMENT_NODE |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 629 | nodeName = "#comment" |
| 630 | attributes = None |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 631 | childNodeTypes = () |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 632 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 633 | def __init__(self, data): |
| 634 | Node.__init__(self) |
| 635 | self.data = self.nodeValue = data |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 636 | |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 637 | def writexml(self, writer, indent="", addindent="", newl=""): |
| 638 | writer.write("%s<!--%s-->%s" % (indent,self.data,newl)) |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 639 | |
| 640 | class ProcessingInstruction(Node): |
| 641 | nodeType = Node.PROCESSING_INSTRUCTION_NODE |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 642 | attributes = None |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 643 | childNodeTypes = () |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 644 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 645 | def __init__(self, target, data): |
| 646 | Node.__init__(self) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 647 | self.target = self.nodeName = target |
| 648 | self.data = self.nodeValue = data |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 649 | |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 650 | def writexml(self, writer, indent="", addindent="", newl=""): |
| 651 | writer.write("%s<?%s %s?>%s" % (indent,self.target, self.data, newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 652 | |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 653 | class CharacterData(Node): |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 654 | def __init__(self, data): |
Fred Drake | daa823a | 2001-01-08 04:04:34 +0000 | [diff] [blame] | 655 | if type(data) not in _StringTypes: |
| 656 | raise TypeError, "node contents must be a string" |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 657 | Node.__init__(self) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 658 | self.data = self.nodeValue = data |
Fred Drake | 33d2b84 | 2001-04-04 15:15:18 +0000 | [diff] [blame] | 659 | self.length = len(data) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 660 | |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 661 | def __repr__(self): |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 662 | if len(self.data) > 10: |
| 663 | dotdotdot = "..." |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 664 | else: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 665 | dotdotdot = "" |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 666 | return "<DOM %s node \"%s%s\">" % ( |
| 667 | self.__class__.__name__, self.data[0:10], dotdotdot) |
| 668 | |
| 669 | def substringData(self, offset, count): |
| 670 | if offset < 0: |
| 671 | raise xml.dom.IndexSizeErr("offset cannot be negative") |
| 672 | if offset >= len(self.data): |
| 673 | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
| 674 | if count < 0: |
| 675 | raise xml.dom.IndexSizeErr("count cannot be negative") |
| 676 | return self.data[offset:offset+count] |
| 677 | |
| 678 | def appendData(self, arg): |
| 679 | self.data = self.data + arg |
| 680 | self.nodeValue = self.data |
Fred Drake | 33d2b84 | 2001-04-04 15:15:18 +0000 | [diff] [blame] | 681 | self.length = len(self.data) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 682 | |
| 683 | def insertData(self, offset, arg): |
| 684 | if offset < 0: |
| 685 | raise xml.dom.IndexSizeErr("offset cannot be negative") |
| 686 | if offset >= len(self.data): |
| 687 | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
| 688 | if arg: |
| 689 | self.data = "%s%s%s" % ( |
| 690 | self.data[:offset], arg, self.data[offset:]) |
| 691 | self.nodeValue = self.data |
Fred Drake | 33d2b84 | 2001-04-04 15:15:18 +0000 | [diff] [blame] | 692 | self.length = len(self.data) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 693 | |
| 694 | def deleteData(self, offset, count): |
| 695 | if offset < 0: |
| 696 | raise xml.dom.IndexSizeErr("offset cannot be negative") |
| 697 | if offset >= len(self.data): |
| 698 | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
| 699 | if count < 0: |
| 700 | raise xml.dom.IndexSizeErr("count cannot be negative") |
| 701 | if count: |
| 702 | self.data = self.data[:offset] + self.data[offset+count:] |
| 703 | self.nodeValue = self.data |
Fred Drake | 33d2b84 | 2001-04-04 15:15:18 +0000 | [diff] [blame] | 704 | self.length = len(self.data) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 705 | |
| 706 | def replaceData(self, offset, count, arg): |
| 707 | if offset < 0: |
| 708 | raise xml.dom.IndexSizeErr("offset cannot be negative") |
| 709 | if offset >= len(self.data): |
| 710 | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
| 711 | if count < 0: |
| 712 | raise xml.dom.IndexSizeErr("count cannot be negative") |
| 713 | if count: |
| 714 | self.data = "%s%s%s" % ( |
| 715 | self.data[:offset], arg, self.data[offset+count:]) |
| 716 | self.nodeValue = self.data |
Fred Drake | 33d2b84 | 2001-04-04 15:15:18 +0000 | [diff] [blame] | 717 | self.length = len(self.data) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 718 | |
| 719 | class Text(CharacterData): |
| 720 | nodeType = Node.TEXT_NODE |
| 721 | nodeName = "#text" |
| 722 | attributes = None |
| 723 | childNodeTypes = () |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 724 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 725 | def splitText(self, offset): |
| 726 | if offset < 0 or offset > len(self.data): |
Martin v. Löwis | d5fb58f | 2001-01-27 08:38:34 +0000 | [diff] [blame] | 727 | raise xml.dom.IndexSizeErr("illegal offset value") |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 728 | newText = Text(self.data[offset:]) |
| 729 | next = self.nextSibling |
| 730 | if self.parentNode and self in self.parentNode.childNodes: |
| 731 | if next is None: |
| 732 | self.parentNode.appendChild(newText) |
| 733 | else: |
| 734 | self.parentNode.insertBefore(newText, next) |
| 735 | self.data = self.data[:offset] |
Fred Drake | 33d2b84 | 2001-04-04 15:15:18 +0000 | [diff] [blame] | 736 | self.nodeValue = self.data |
| 737 | self.length = len(self.data) |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 738 | return newText |
| 739 | |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 740 | def writexml(self, writer, indent="", addindent="", newl=""): |
| 741 | _write_data(writer, "%s%s%s"%(indent, self.data, newl)) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 742 | |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 743 | |
| 744 | class CDATASection(Text): |
| 745 | nodeType = Node.CDATA_SECTION_NODE |
| 746 | nodeName = "#cdata-section" |
| 747 | |
| 748 | def writexml(self, writer, indent="", addindent="", newl=""): |
Guido van Rossum | 5b5e0b9 | 2001-09-19 13:28:25 +0000 | [diff] [blame] | 749 | writer.write("<![CDATA[%s]]>" % self.data) |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 750 | |
| 751 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 752 | def _nssplit(qualifiedName): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 753 | fields = _string.split(qualifiedName, ':', 1) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 754 | if len(fields) == 2: |
| 755 | return fields |
| 756 | elif len(fields) == 1: |
Fred Drake | 49a5d03 | 2001-11-30 22:21:58 +0000 | [diff] [blame^] | 757 | return (None, fields[0]) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 758 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 759 | |
| 760 | class DocumentType(Node): |
| 761 | nodeType = Node.DOCUMENT_TYPE_NODE |
| 762 | nodeValue = None |
| 763 | attributes = None |
| 764 | name = None |
| 765 | publicId = None |
| 766 | systemId = None |
Fred Drake | dc80670 | 2001-04-05 14:41:30 +0000 | [diff] [blame] | 767 | internalSubset = None |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 768 | entities = None |
| 769 | notations = None |
| 770 | |
| 771 | def __init__(self, qualifiedName): |
| 772 | Node.__init__(self) |
| 773 | if qualifiedName: |
| 774 | prefix, localname = _nssplit(qualifiedName) |
| 775 | self.name = localname |
| 776 | |
| 777 | |
| 778 | class DOMImplementation: |
| 779 | def hasFeature(self, feature, version): |
| 780 | if version not in ("1.0", "2.0"): |
| 781 | return 0 |
| 782 | feature = _string.lower(feature) |
| 783 | return feature == "core" |
| 784 | |
| 785 | def createDocument(self, namespaceURI, qualifiedName, doctype): |
| 786 | if doctype and doctype.parentNode is not None: |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 787 | raise xml.dom.WrongDocumentErr( |
| 788 | "doctype object owned by another DOM tree") |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 789 | doc = self._createDocument() |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 790 | if doctype is None: |
| 791 | doctype = self.createDocumentType(qualifiedName, None, None) |
Martin v. Löwis | b417be2 | 2001-02-06 01:16:06 +0000 | [diff] [blame] | 792 | if not qualifiedName: |
| 793 | # The spec is unclear what to raise here; SyntaxErr |
| 794 | # would be the other obvious candidate. Since Xerces raises |
| 795 | # InvalidCharacterErr, and since SyntaxErr is not listed |
| 796 | # for createDocument, that seems to be the better choice. |
| 797 | # XXX: need to check for illegal characters here and in |
| 798 | # createElement. |
| 799 | raise xml.dom.InvalidCharacterErr("Element with no name") |
| 800 | prefix, localname = _nssplit(qualifiedName) |
| 801 | if prefix == "xml" \ |
| 802 | and namespaceURI != "http://www.w3.org/XML/1998/namespace": |
| 803 | raise xml.dom.NamespaceErr("illegal use of 'xml' prefix") |
| 804 | if prefix and not namespaceURI: |
| 805 | raise xml.dom.NamespaceErr( |
| 806 | "illegal use of prefix without namespaces") |
| 807 | element = doc.createElementNS(namespaceURI, qualifiedName) |
| 808 | doc.appendChild(element) |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 809 | doctype.parentNode = doctype.ownerDocument = doc |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 810 | doc.doctype = doctype |
| 811 | doc.implementation = self |
| 812 | return doc |
| 813 | |
| 814 | def createDocumentType(self, qualifiedName, publicId, systemId): |
| 815 | doctype = DocumentType(qualifiedName) |
| 816 | doctype.publicId = publicId |
| 817 | doctype.systemId = systemId |
| 818 | return doctype |
| 819 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 820 | # internal |
| 821 | def _createDocument(self): |
| 822 | return Document() |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 823 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 824 | class Document(Node): |
| 825 | nodeType = Node.DOCUMENT_NODE |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 826 | nodeName = "#document" |
| 827 | nodeValue = None |
| 828 | attributes = None |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 829 | doctype = None |
| 830 | parentNode = None |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 831 | previousSibling = nextSibling = None |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 832 | |
| 833 | implementation = DOMImplementation() |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 834 | childNodeTypes = (Node.ELEMENT_NODE, Node.PROCESSING_INSTRUCTION_NODE, |
| 835 | Node.COMMENT_NODE, Node.DOCUMENT_TYPE_NODE) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 836 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 837 | def appendChild(self, node): |
Andrew M. Kuchling | 291ed4f | 2000-12-31 03:50:23 +0000 | [diff] [blame] | 838 | if node.nodeType not in self.childNodeTypes: |
| 839 | raise HierarchyRequestErr, \ |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 840 | "%s cannot be child of %s" % (repr(node), repr(self)) |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 841 | if node.parentNode is not None: |
| 842 | node.parentNode.removeChild(node) |
| 843 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 844 | if node.nodeType == Node.ELEMENT_NODE \ |
| 845 | and self._get_documentElement(): |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 846 | raise xml.dom.HierarchyRequestErr( |
| 847 | "two document elements disallowed") |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 848 | return Node.appendChild(self, node) |
Paul Prescod | 73678da | 2000-07-01 04:58:47 +0000 | [diff] [blame] | 849 | |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 850 | def removeChild(self, oldChild): |
| 851 | self.childNodes.remove(oldChild) |
| 852 | oldChild.nextSibling = oldChild.previousSibling = None |
| 853 | oldChild.parentNode = None |
| 854 | if self.documentElement is oldChild: |
| 855 | self.documentElement = None |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 856 | |
Andrew M. Kuchling | 04a45e9 | 2000-12-20 14:47:24 +0000 | [diff] [blame] | 857 | return oldChild |
| 858 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 859 | def _get_documentElement(self): |
| 860 | for node in self.childNodes: |
| 861 | if node.nodeType == Node.ELEMENT_NODE: |
| 862 | return node |
| 863 | |
Fred Drake | d157237 | 2001-09-29 04:58:32 +0000 | [diff] [blame] | 864 | try: |
| 865 | property |
| 866 | except NameError: |
| 867 | pass |
| 868 | else: |
| 869 | documentElement = property(_get_documentElement, |
| 870 | doc="Top-level element of this document.") |
| 871 | |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 872 | def unlink(self): |
| 873 | if self.doctype is not None: |
| 874 | self.doctype.unlink() |
| 875 | self.doctype = None |
| 876 | Node.unlink(self) |
| 877 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 878 | def createDocumentFragment(self): |
| 879 | d = DocumentFragment() |
| 880 | d.ownerDoc = self |
| 881 | return d |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 882 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 883 | def createElement(self, tagName): |
| 884 | e = Element(tagName) |
| 885 | e.ownerDocument = self |
| 886 | return e |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 887 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 888 | def createTextNode(self, data): |
| 889 | t = Text(data) |
| 890 | t.ownerDocument = self |
| 891 | return t |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 892 | |
Fred Drake | 87432f4 | 2001-04-04 14:09:46 +0000 | [diff] [blame] | 893 | def createCDATASection(self, data): |
| 894 | c = CDATASection(data) |
| 895 | c.ownerDocument = self |
| 896 | return c |
| 897 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 898 | def createComment(self, data): |
| 899 | c = Comment(data) |
| 900 | c.ownerDocument = self |
| 901 | return c |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 902 | |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 903 | def createProcessingInstruction(self, target, data): |
| 904 | p = ProcessingInstruction(target, data) |
| 905 | p.ownerDocument = self |
| 906 | return p |
| 907 | |
| 908 | def createAttribute(self, qName): |
| 909 | a = Attr(qName) |
| 910 | a.ownerDocument = self |
Martin v. Löwis | cb67ea1 | 2001-03-31 16:30:40 +0000 | [diff] [blame] | 911 | a.value = "" |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 912 | return a |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 913 | |
| 914 | def createElementNS(self, namespaceURI, qualifiedName): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 915 | prefix, localName = _nssplit(qualifiedName) |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 916 | e = Element(qualifiedName, namespaceURI, prefix, localName) |
| 917 | e.ownerDocument = self |
| 918 | return e |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 919 | |
| 920 | def createAttributeNS(self, namespaceURI, qualifiedName): |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 921 | prefix, localName = _nssplit(qualifiedName) |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 922 | a = Attr(qualifiedName, namespaceURI, localName, prefix) |
| 923 | a.ownerDocument = self |
Martin v. Löwis | cb67ea1 | 2001-03-31 16:30:40 +0000 | [diff] [blame] | 924 | a.value = "" |
Martin v. Löwis | 126f2f6 | 2001-03-13 10:50:13 +0000 | [diff] [blame] | 925 | return a |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 926 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 927 | def getElementsByTagName(self, name): |
Fred Drake | fbe7b4f | 2001-07-04 06:25:53 +0000 | [diff] [blame] | 928 | return _getElementsByTagNameHelper(self, name, []) |
| 929 | |
| 930 | def getElementsByTagNameNS(self, namespaceURI, localName): |
| 931 | return _getElementsByTagNameNSHelper(self, namespaceURI, localName, []) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 932 | |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 933 | def writexml(self, writer, indent="", addindent="", newl=""): |
Guido van Rossum | 9e1fe1e | 2001-02-05 19:17:50 +0000 | [diff] [blame] | 934 | writer.write('<?xml version="1.0" ?>\n') |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 935 | for node in self.childNodes: |
Martin v. Löwis | 46fa39a | 2001-02-06 00:14:08 +0000 | [diff] [blame] | 936 | node.writexml(writer, indent, addindent, newl) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 937 | |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 938 | def _get_StringIO(): |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 939 | # we can't use cStringIO since it doesn't support Unicode strings |
| 940 | from StringIO import StringIO |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 941 | return StringIO() |
| 942 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 943 | def _doparse(func, args, kwargs): |
| 944 | events = apply(func, args, kwargs) |
| 945 | toktype, rootNode = events.getEvent() |
| 946 | events.expandNode(rootNode) |
Martin v. Löwis | b417be2 | 2001-02-06 01:16:06 +0000 | [diff] [blame] | 947 | events.clear() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 948 | return rootNode |
| 949 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 950 | def parse(*args, **kwargs): |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 951 | """Parse a file into a DOM by filename or file object.""" |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 952 | from xml.dom import pulldom |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 953 | return _doparse(pulldom.parse, args, kwargs) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 954 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 955 | def parseString(*args, **kwargs): |
Fred Drake | f7cf40d | 2000-12-14 18:16:11 +0000 | [diff] [blame] | 956 | """Parse a file into a DOM from a string.""" |
Fred Drake | 4ccf4a1 | 2000-11-21 22:02:22 +0000 | [diff] [blame] | 957 | from xml.dom import pulldom |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 958 | return _doparse(pulldom.parseString, args, kwargs) |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 959 | |
| 960 | def getDOMImplementation(): |
| 961 | return Document.implementation |