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