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