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