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