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