paulb@18 | 1 | #!/usr/bin/env python |
paulb@18 | 2 | |
paulb@18 | 3 | """ |
paulb@75 | 4 | DOM wrapper around libxml2, specifically the libxml2mod Python extension module. |
paulb@188 | 5 | |
paulb@324 | 6 | Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Paul Boddie <paul@boddie.org.uk> |
paulb@188 | 7 | |
paulb@293 | 8 | This program is free software; you can redistribute it and/or modify it under |
paulb@293 | 9 | the terms of the GNU Lesser General Public License as published by the Free |
paulb@293 | 10 | Software Foundation; either version 3 of the License, or (at your option) any |
paulb@293 | 11 | later version. |
paulb@188 | 12 | |
paulb@293 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@293 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@293 | 15 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paulb@293 | 16 | details. |
paulb@188 | 17 | |
paulb@293 | 18 | You should have received a copy of the GNU Lesser General Public License along |
paulb@293 | 19 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@18 | 20 | """ |
paulb@18 | 21 | |
paul@332 | 22 | __version__ = "0.4.7" |
paulb@27 | 23 | |
paulb@72 | 24 | from libxml2dom.macrolib import * |
paulb@72 | 25 | from libxml2dom.macrolib import \ |
paulb@72 | 26 | createDocument as Node_createDocument, \ |
paulb@72 | 27 | parseString as Node_parseString, parseURI as Node_parseURI, \ |
paulb@72 | 28 | parseFile as Node_parseFile, \ |
paulb@72 | 29 | toString as Node_toString, toStream as Node_toStream, \ |
paulb@72 | 30 | toFile as Node_toFile |
paulb@232 | 31 | import urllib # for parseURI in HTML mode |
paul@339 | 32 | import libxml2dom.errors |
paulb@18 | 33 | |
paulb@301 | 34 | # Standard namespaces. |
paulb@301 | 35 | |
paulb@301 | 36 | XML_NAMESPACE = xml.dom.XML_NAMESPACE |
paulb@301 | 37 | |
paulb@301 | 38 | # Default namespace bindings for XPath. |
paulb@301 | 39 | |
paulb@301 | 40 | default_ns = { |
paulb@301 | 41 | "xml" : XML_NAMESPACE |
paulb@301 | 42 | } |
paulb@301 | 43 | |
paulb@218 | 44 | class Implementation(object): |
paulb@218 | 45 | |
paulb@218 | 46 | "Contains an abstraction over the DOM implementation." |
paulb@218 | 47 | |
paulb@218 | 48 | def createDocumentType(self, localName, publicId, systemId): |
paulb@218 | 49 | return DocumentType(localName, publicId, systemId) |
paulb@218 | 50 | |
paulb@218 | 51 | def createDocument(self, namespaceURI, localName, doctype): |
paulb@218 | 52 | return Document(Node_createDocument(namespaceURI, localName, doctype), self) |
paulb@218 | 53 | |
paulb@236 | 54 | # Wrapping of documents. |
paulb@236 | 55 | |
paulb@236 | 56 | def adoptDocument(self, node): |
paulb@236 | 57 | return Document(node, self) |
paulb@236 | 58 | |
paulb@218 | 59 | # Factory functions. |
paulb@218 | 60 | |
paulb@218 | 61 | def get_node(self, _node, context_node): |
paul@331 | 62 | |
paul@331 | 63 | # Return the existing document. |
paul@331 | 64 | |
paulb@218 | 65 | if Node_nodeType(_node) == context_node.DOCUMENT_NODE: |
paulb@218 | 66 | return context_node.ownerDocument |
paul@331 | 67 | |
paul@331 | 68 | # Return an attribute using the parent of the attribute as the owner |
paul@331 | 69 | # element. |
paul@331 | 70 | |
paulb@218 | 71 | elif Node_nodeType(_node) == context_node.ATTRIBUTE_NODE: |
paul@331 | 72 | return Attribute(_node, self, context_node.ownerDocument, |
paul@331 | 73 | self.get_node(Node_parentNode(_node), context_node)) |
paul@331 | 74 | |
paul@331 | 75 | # Return other nodes. |
paul@331 | 76 | |
paulb@218 | 77 | else: |
paulb@218 | 78 | return Node(_node, self, context_node.ownerDocument) |
paulb@218 | 79 | |
paulb@240 | 80 | def get_node_or_none(self, _node, context_node): |
paulb@240 | 81 | if _node is None: |
paulb@240 | 82 | return None |
paulb@240 | 83 | else: |
paulb@240 | 84 | return self.get_node(_node, context_node) |
paulb@240 | 85 | |
paulb@86 | 86 | # Attribute and node list wrappers. |
paulb@18 | 87 | |
paulb@18 | 88 | class NamedNodeMap(object): |
paulb@18 | 89 | |
paulb@75 | 90 | """ |
paulb@75 | 91 | A wrapper around Node objects providing DOM and dictionary convenience |
paulb@75 | 92 | methods. |
paulb@75 | 93 | """ |
paulb@75 | 94 | |
paulb@256 | 95 | def __init__(self, node, impl): |
paulb@18 | 96 | self.node = node |
paulb@256 | 97 | self.impl = impl |
paulb@18 | 98 | |
paulb@18 | 99 | def getNamedItem(self, name): |
paulb@72 | 100 | return self.node.getAttributeNode(name) |
paulb@18 | 101 | |
paulb@18 | 102 | def getNamedItemNS(self, ns, localName): |
paulb@72 | 103 | return self.node.getAttributeNodeNS(ns, localName) |
paulb@18 | 104 | |
paulb@18 | 105 | def setNamedItem(self, node): |
paulb@91 | 106 | try: |
paulb@91 | 107 | old = self.getNamedItem(node.nodeName) |
paulb@91 | 108 | except KeyError: |
paulb@91 | 109 | old = None |
paulb@86 | 110 | self.node.setAttributeNode(node) |
paulb@91 | 111 | return old |
paulb@18 | 112 | |
paulb@18 | 113 | def setNamedItemNS(self, node): |
paulb@91 | 114 | try: |
paulb@91 | 115 | old = self.getNamedItemNS(node.namespaceURI, node.localName) |
paulb@91 | 116 | except KeyError: |
paulb@91 | 117 | old = None |
paulb@86 | 118 | self.node.setAttributeNodeNS(node) |
paulb@91 | 119 | return old |
paulb@91 | 120 | |
paulb@91 | 121 | def removeNamedItem(self, name): |
paulb@91 | 122 | try: |
paulb@91 | 123 | old = self.getNamedItem(name) |
paulb@91 | 124 | except KeyError: |
paulb@91 | 125 | old = None |
paulb@91 | 126 | self.node.removeAttribute(name) |
paulb@91 | 127 | return old |
paulb@91 | 128 | |
paulb@91 | 129 | def removeNamedItemNS(self, ns, localName): |
paulb@91 | 130 | try: |
paulb@91 | 131 | old = self.getNamedItemNS(ns, localName) |
paulb@91 | 132 | except KeyError: |
paulb@91 | 133 | old = None |
paulb@91 | 134 | self.node.removeAttributeNS(ns, localName) |
paulb@91 | 135 | return old |
paulb@86 | 136 | |
paulb@276 | 137 | # Iterator emulation. |
paulb@276 | 138 | |
paulb@276 | 139 | def __iter__(self): |
paulb@276 | 140 | return NamedNodeMapIterator(self) |
paulb@276 | 141 | |
paulb@86 | 142 | # Dictionary emulation methods. |
paulb@18 | 143 | |
paulb@18 | 144 | def __getitem__(self, name): |
paulb@18 | 145 | return self.getNamedItem(name) |
paulb@18 | 146 | |
paulb@18 | 147 | def __setitem__(self, name, node): |
paulb@18 | 148 | if name == node.nodeName: |
paulb@18 | 149 | self.setNamedItem(node) |
paulb@18 | 150 | else: |
paulb@18 | 151 | raise KeyError, name |
paulb@18 | 152 | |
paulb@18 | 153 | def __delitem__(self, name): |
paulb@18 | 154 | # NOTE: To be implemented. |
paulb@18 | 155 | pass |
paulb@18 | 156 | |
paulb@18 | 157 | def values(self): |
paulb@218 | 158 | return [Attribute(_node, self.impl, self.node.ownerDocument) for _node in Node_attributes(self.node.as_native_node()).values()] |
paulb@18 | 159 | |
paulb@18 | 160 | def keys(self): |
paulb@18 | 161 | return [(attr.namespaceURI, attr.localName) for attr in self.values()] |
paulb@18 | 162 | |
paulb@18 | 163 | def items(self): |
paulb@18 | 164 | return [((attr.namespaceURI, attr.localName), attr) for attr in self.values()] |
paulb@18 | 165 | |
paulb@18 | 166 | def __repr__(self): |
paulb@18 | 167 | return str(self) |
paulb@18 | 168 | |
paulb@18 | 169 | def __str__(self): |
paulb@18 | 170 | return "{%s}" % ",\n".join(["%s : %s" % (repr(key), repr(value)) for key, value in self.items()]) |
paulb@18 | 171 | |
paulb@198 | 172 | def _length(self): |
paulb@198 | 173 | return len(self.values()) |
paulb@198 | 174 | |
paulb@198 | 175 | length = property(_length) |
paulb@198 | 176 | |
paulb@276 | 177 | class NamedNodeMapIterator(object): |
paulb@276 | 178 | |
paulb@276 | 179 | "An iterator over a NamedNodeMap." |
paulb@276 | 180 | |
paulb@276 | 181 | def __init__(self, nodemap): |
paulb@276 | 182 | self.nodemap = nodemap |
paulb@276 | 183 | self.items = self.nodemap.items() |
paulb@276 | 184 | |
paulb@276 | 185 | def next(self): |
paulb@276 | 186 | if self.items: |
paulb@276 | 187 | current = self.items[0][1] |
paulb@276 | 188 | self.items = self.items[1:] |
paulb@276 | 189 | return current |
paulb@276 | 190 | else: |
paulb@276 | 191 | raise StopIteration |
paulb@276 | 192 | |
paulb@86 | 193 | class NodeList(list): |
paulb@86 | 194 | |
paulb@86 | 195 | "A wrapper around node lists." |
paulb@86 | 196 | |
paulb@86 | 197 | def item(self, index): |
paulb@86 | 198 | return self[index] |
paulb@86 | 199 | |
paulb@198 | 200 | def _length(self): |
paulb@86 | 201 | return len(self) |
paulb@86 | 202 | |
paulb@198 | 203 | length = property(_length) |
paulb@198 | 204 | |
paulb@86 | 205 | # Node classes. |
paulb@86 | 206 | |
paulb@18 | 207 | class Node(object): |
paulb@18 | 208 | |
paulb@75 | 209 | """ |
paulb@75 | 210 | A DOM-style wrapper around libxml2mod objects. |
paulb@75 | 211 | """ |
paulb@75 | 212 | |
paulb@81 | 213 | ATTRIBUTE_NODE = xml.dom.Node.ATTRIBUTE_NODE |
paulb@81 | 214 | COMMENT_NODE = xml.dom.Node.COMMENT_NODE |
paulb@81 | 215 | DOCUMENT_NODE = xml.dom.Node.DOCUMENT_NODE |
paulb@81 | 216 | DOCUMENT_TYPE_NODE = xml.dom.Node.DOCUMENT_TYPE_NODE |
paulb@81 | 217 | ELEMENT_NODE = xml.dom.Node.ELEMENT_NODE |
paulb@81 | 218 | ENTITY_NODE = xml.dom.Node.ENTITY_NODE |
paulb@81 | 219 | ENTITY_REFERENCE_NODE = xml.dom.Node.ENTITY_REFERENCE_NODE |
paulb@81 | 220 | NOTATION_NODE = xml.dom.Node.NOTATION_NODE |
paulb@81 | 221 | PROCESSING_INSTRUCTION_NODE = xml.dom.Node.PROCESSING_INSTRUCTION_NODE |
paulb@81 | 222 | TEXT_NODE = xml.dom.Node.TEXT_NODE |
paulb@81 | 223 | |
paulb@223 | 224 | def __init__(self, node, impl=None, ownerDocument=None): |
paulb@18 | 225 | self._node = node |
paulb@223 | 226 | self.impl = impl or default_impl |
paulb@75 | 227 | self.ownerDocument = ownerDocument |
paulb@18 | 228 | |
paulb@18 | 229 | def as_native_node(self): |
paulb@18 | 230 | return self._node |
paulb@18 | 231 | |
paulb@18 | 232 | def _nodeType(self): |
paulb@72 | 233 | return Node_nodeType(self._node) |
paulb@18 | 234 | |
paulb@18 | 235 | def _childNodes(self): |
paulb@18 | 236 | |
paulb@18 | 237 | # NOTE: Consider a generator instead. |
paulb@18 | 238 | |
paulb@218 | 239 | return NodeList([self.impl.get_node(_node, self) for _node in Node_childNodes(self._node)]) |
paulb@18 | 240 | |
paulb@276 | 241 | def _firstChild(self): |
paulb@276 | 242 | return (self.childNodes or [None])[0] |
paulb@276 | 243 | |
paulb@276 | 244 | def _lastChild(self): |
paulb@276 | 245 | return (self.childNodes or [None])[-1] |
paulb@276 | 246 | |
paulb@18 | 247 | def _attributes(self): |
paulb@256 | 248 | return NamedNodeMap(self, self.impl) |
paulb@18 | 249 | |
paulb@18 | 250 | def _namespaceURI(self): |
paulb@72 | 251 | return Node_namespaceURI(self._node) |
paulb@18 | 252 | |
paulb@236 | 253 | def _textContent(self): |
paulb@236 | 254 | return Node_textContent(self._node) |
paulb@236 | 255 | |
paulb@18 | 256 | def _nodeValue(self): |
paulb@223 | 257 | if self.nodeType in null_value_node_types: |
paulb@223 | 258 | return None |
paulb@72 | 259 | return Node_nodeValue(self._node) |
paulb@18 | 260 | |
paulb@86 | 261 | def _setNodeValue(self, value): |
paulb@86 | 262 | Node_setNodeValue(self._node, value) |
paulb@86 | 263 | |
paulb@18 | 264 | def _prefix(self): |
paulb@72 | 265 | return Node_prefix(self._node) |
paulb@18 | 266 | |
paulb@18 | 267 | def _nodeName(self): |
paulb@72 | 268 | return Node_nodeName(self._node) |
paulb@18 | 269 | |
paulb@18 | 270 | def _tagName(self): |
paulb@72 | 271 | return Node_tagName(self._node) |
paulb@18 | 272 | |
paulb@18 | 273 | def _localName(self): |
paulb@72 | 274 | return Node_localName(self._node) |
paulb@18 | 275 | |
paulb@18 | 276 | def _parentNode(self): |
paulb@240 | 277 | return self.impl.get_node_or_none(Node_parentNode(self._node), self) |
paulb@18 | 278 | |
paulb@24 | 279 | def _previousSibling(self): |
paulb@240 | 280 | return self.impl.get_node_or_none(Node_previousSibling(self._node), self) |
paulb@24 | 281 | |
paulb@18 | 282 | def _nextSibling(self): |
paulb@240 | 283 | return self.impl.get_node_or_none(Node_nextSibling(self._node), self) |
paulb@18 | 284 | |
paulb@154 | 285 | def _doctype(self): |
paulb@276 | 286 | _doctype = Node_doctype(self._node) |
paulb@276 | 287 | if _doctype is not None: |
paulb@276 | 288 | return self.impl.get_node(_doctype, self) |
paulb@276 | 289 | else: |
paulb@276 | 290 | return None |
paulb@154 | 291 | |
paulb@154 | 292 | def _publicId(self): |
paulb@154 | 293 | # NOTE: To be fixed when the libxml2mod API has been figured out. |
paulb@154 | 294 | if self.nodeType != self.DOCUMENT_TYPE_NODE: |
paulb@154 | 295 | return None |
paulb@154 | 296 | declaration = self.toString() |
paulb@154 | 297 | return self._findId(declaration, "PUBLIC") |
paulb@154 | 298 | |
paulb@154 | 299 | def _systemId(self): |
paulb@154 | 300 | # NOTE: To be fixed when the libxml2mod API has been figured out. |
paulb@154 | 301 | if self.nodeType != self.DOCUMENT_TYPE_NODE: |
paulb@154 | 302 | return None |
paulb@154 | 303 | declaration = self.toString() |
paulb@154 | 304 | if self._findId(declaration, "PUBLIC"): |
paulb@154 | 305 | return self._findIdValue(declaration, 0) |
paulb@154 | 306 | return self._findId(declaration, "SYSTEM") |
paulb@154 | 307 | |
paulb@154 | 308 | # NOTE: To be removed when the libxml2mod API has been figured out. |
paulb@154 | 309 | |
paulb@154 | 310 | def _findId(self, declaration, identifier): |
paulb@154 | 311 | i = declaration.find(identifier) |
paulb@154 | 312 | if i == -1: |
paulb@154 | 313 | return None |
paulb@154 | 314 | return self._findIdValue(declaration, i) |
paulb@154 | 315 | |
paulb@154 | 316 | def _findIdValue(self, declaration, i): |
paulb@154 | 317 | q = declaration.find('"', i) |
paulb@154 | 318 | if q == -1: |
paulb@154 | 319 | return None |
paulb@154 | 320 | q2 = declaration.find('"', q + 1) |
paulb@154 | 321 | if q2 == -1: |
paulb@154 | 322 | return None |
paulb@154 | 323 | return declaration[q+1:q2] |
paulb@154 | 324 | |
paulb@18 | 325 | def hasAttributeNS(self, ns, localName): |
paulb@72 | 326 | return Node_hasAttributeNS(self._node, ns, localName) |
paulb@18 | 327 | |
paulb@18 | 328 | def hasAttribute(self, name): |
paulb@72 | 329 | return Node_hasAttribute(self._node, name) |
paulb@18 | 330 | |
paulb@18 | 331 | def getAttributeNS(self, ns, localName): |
paulb@72 | 332 | return Node_getAttributeNS(self._node, ns, localName) |
paulb@18 | 333 | |
paulb@18 | 334 | def getAttribute(self, name): |
paulb@72 | 335 | return Node_getAttribute(self._node, name) |
paulb@18 | 336 | |
paulb@18 | 337 | def getAttributeNodeNS(self, ns, localName): |
paulb@218 | 338 | return Attribute(Node_getAttributeNodeNS(self._node, ns, localName), self.impl, self.ownerDocument, self) |
paulb@18 | 339 | |
paulb@18 | 340 | def getAttributeNode(self, localName): |
paulb@218 | 341 | return Attribute(Node_getAttributeNode(self._node, localName), self.impl, self.ownerDocument, self) |
paulb@18 | 342 | |
paulb@18 | 343 | def setAttributeNS(self, ns, name, value): |
paulb@72 | 344 | Node_setAttributeNS(self._node, ns, name, value) |
paulb@18 | 345 | |
paulb@18 | 346 | def setAttribute(self, name, value): |
paulb@72 | 347 | Node_setAttribute(self._node, name, value) |
paulb@18 | 348 | |
paulb@86 | 349 | def setAttributeNodeNS(self, node): |
paulb@88 | 350 | Node_setAttributeNodeNS(self._node, node._node) |
paulb@18 | 351 | |
paulb@86 | 352 | def setAttributeNode(self, node): |
paulb@88 | 353 | Node_setAttributeNode(self._node, node._node) |
paulb@18 | 354 | |
paulb@91 | 355 | def removeAttributeNS(self, ns, localName): |
paulb@91 | 356 | Node_removeAttributeNS(self._node, ns, localName) |
paulb@91 | 357 | |
paulb@91 | 358 | def removeAttribute(self, name): |
paulb@91 | 359 | Node_removeAttribute(self._node, name) |
paulb@91 | 360 | |
paulb@18 | 361 | def createElementNS(self, ns, name): |
paulb@218 | 362 | return self.impl.get_node(Node_createElementNS(self._node, ns, name), self) |
paulb@18 | 363 | |
paulb@18 | 364 | def createElement(self, name): |
paulb@218 | 365 | return self.impl.get_node(Node_createElement(self._node, name), self) |
paulb@18 | 366 | |
paulb@88 | 367 | def createAttributeNS(self, ns, name): |
paulb@88 | 368 | tmp = self.createElement("tmp") |
paulb@218 | 369 | return Attribute(Node_createAttributeNS(tmp._node, self.impl, ns, name)) |
paulb@18 | 370 | |
paulb@88 | 371 | def createAttribute(self, name): |
paulb@88 | 372 | tmp = self.createElement("tmp") |
paulb@218 | 373 | return Attribute(Node_createAttribute(tmp._node, name), self.impl) |
paulb@18 | 374 | |
paulb@18 | 375 | def createTextNode(self, value): |
paulb@218 | 376 | return self.impl.get_node(Node_createTextNode(self._node, value), self) |
paulb@18 | 377 | |
paulb@34 | 378 | def createComment(self, value): |
paulb@218 | 379 | return self.impl.get_node(Node_createComment(self._node, value), self) |
paulb@18 | 380 | |
paulb@251 | 381 | def createCDATASection(self, value): |
paulb@251 | 382 | return self.impl.get_node(Node_createCDATASection(self._node, value), self) |
paulb@251 | 383 | |
paulb@18 | 384 | def importNode(self, node, deep): |
paulb@72 | 385 | if hasattr(node, "as_native_node"): |
paulb@218 | 386 | return self.impl.get_node(Node_importNode(self._node, node.as_native_node(), deep), self) |
paulb@72 | 387 | else: |
paulb@218 | 388 | return self.impl.get_node(Node_importNode_DOM(self._node, node, deep), self) |
paulb@18 | 389 | |
paulb@208 | 390 | def cloneNode(self, deep): |
paulb@208 | 391 | # This takes advantage of the ubiquity of importNode (in spite of the DOM specification). |
paulb@208 | 392 | return self.importNode(self, deep) |
paulb@208 | 393 | |
paulb@18 | 394 | def insertBefore(self, tmp, oldNode): |
paulb@276 | 395 | if tmp.ownerDocument != self.ownerDocument: |
paul@339 | 396 | raise xml.dom.WrongDocumentErr() |
paulb@276 | 397 | if oldNode.parentNode != self: |
paul@339 | 398 | raise xml.dom.NotFoundErr() |
paulb@72 | 399 | if hasattr(tmp, "as_native_node"): |
paulb@218 | 400 | return self.impl.get_node(Node_insertBefore(self._node, tmp.as_native_node(), oldNode.as_native_node()), self) |
paulb@18 | 401 | else: |
paulb@218 | 402 | return self.impl.get_node(Node_insertBefore(self._node, tmp, oldNode.as_native_node()), self) |
paulb@18 | 403 | |
paulb@18 | 404 | def replaceChild(self, tmp, oldNode): |
paulb@276 | 405 | if tmp.ownerDocument != self.ownerDocument: |
paul@339 | 406 | raise xml.dom.WrongDocumentErr() |
paulb@276 | 407 | if oldNode.parentNode != self: |
paul@339 | 408 | raise xml.dom.NotFoundErr() |
paulb@72 | 409 | if hasattr(tmp, "as_native_node"): |
paulb@218 | 410 | return self.impl.get_node(Node_replaceChild(self._node, tmp.as_native_node(), oldNode.as_native_node()), self) |
paulb@18 | 411 | else: |
paulb@218 | 412 | return self.impl.get_node(Node_replaceChild(self._node, tmp, oldNode.as_native_node()), self) |
paulb@18 | 413 | |
paulb@18 | 414 | def appendChild(self, tmp): |
paulb@276 | 415 | if tmp.ownerDocument != self.ownerDocument: |
paul@339 | 416 | raise xml.dom.WrongDocumentErr() |
paulb@72 | 417 | if hasattr(tmp, "as_native_node"): |
paulb@218 | 418 | return self.impl.get_node(Node_appendChild(self._node, tmp.as_native_node()), self) |
paulb@18 | 419 | else: |
paulb@218 | 420 | return self.impl.get_node(Node_appendChild(self._node, tmp), self) |
paulb@18 | 421 | |
paulb@18 | 422 | def removeChild(self, tmp): |
paulb@72 | 423 | if hasattr(tmp, "as_native_node"): |
paulb@72 | 424 | Node_removeChild(self._node, tmp.as_native_node()) |
paulb@72 | 425 | else: |
paulb@72 | 426 | Node_removeChild(self._node, tmp) |
paulb@276 | 427 | return tmp |
paulb@18 | 428 | |
paulb@262 | 429 | def getElementById(self, identifier): |
paul@332 | 430 | _node = Node_getElementById(self.ownerDocument.as_native_node(), identifier) |
paul@332 | 431 | if _node is None: |
paul@332 | 432 | return None |
paulb@262 | 433 | else: |
paul@332 | 434 | return self.impl.get_node(_node, self) |
paulb@262 | 435 | |
paulb@99 | 436 | def getElementsByTagName(self, tagName): |
paulb@232 | 437 | return self.xpath(".//" + tagName) |
paulb@99 | 438 | |
paulb@99 | 439 | def getElementsByTagNameNS(self, namespaceURI, localName): |
paulb@232 | 440 | return self.xpath(".//ns:" + localName, namespaces={"ns" : namespaceURI}) |
paulb@99 | 441 | |
paulb@124 | 442 | def normalize(self): |
paulb@124 | 443 | text_nodes = [] |
paulb@124 | 444 | for node in self.childNodes: |
paulb@124 | 445 | if node.nodeType == node.TEXT_NODE: |
paulb@124 | 446 | text_nodes.append(node) |
paulb@124 | 447 | elif len(text_nodes) != 0: |
paulb@124 | 448 | self._normalize(text_nodes) |
paulb@124 | 449 | text_nodes = [] |
paulb@124 | 450 | if len(text_nodes) != 0: |
paulb@124 | 451 | self._normalize(text_nodes) |
paulb@124 | 452 | |
paulb@124 | 453 | def _normalize(self, text_nodes): |
paulb@124 | 454 | texts = [] |
paulb@124 | 455 | for text_node in text_nodes[:-1]: |
paulb@124 | 456 | texts.append(text_node.nodeValue) |
paulb@124 | 457 | self.removeChild(text_node) |
paulb@124 | 458 | texts.append(text_nodes[-1].nodeValue) |
paulb@124 | 459 | self.replaceChild(self.ownerDocument.createTextNode("".join(texts)), text_nodes[-1]) |
paulb@124 | 460 | |
paulb@18 | 461 | childNodes = property(_childNodes) |
paulb@276 | 462 | firstChild = property(_firstChild) |
paulb@276 | 463 | lastChild = property(_lastChild) |
paulb@86 | 464 | value = data = nodeValue = property(_nodeValue, _setNodeValue) |
paulb@236 | 465 | textContent = property(_textContent) |
paulb@18 | 466 | name = nodeName = property(_nodeName) |
paulb@18 | 467 | tagName = property(_tagName) |
paulb@18 | 468 | namespaceURI = property(_namespaceURI) |
paulb@18 | 469 | prefix = property(_prefix) |
paulb@18 | 470 | localName = property(_localName) |
paulb@88 | 471 | parentNode = property(_parentNode) |
paulb@18 | 472 | nodeType = property(_nodeType) |
paulb@18 | 473 | attributes = property(_attributes) |
paulb@24 | 474 | previousSibling = property(_previousSibling) |
paulb@18 | 475 | nextSibling = property(_nextSibling) |
paulb@154 | 476 | doctype = property(_doctype) |
paulb@154 | 477 | publicId = property(_publicId) |
paulb@154 | 478 | systemId = property(_systemId) |
paulb@154 | 479 | |
paulb@154 | 480 | # NOTE: To be fixed - these being doctype-specific values. |
paulb@154 | 481 | |
paulb@154 | 482 | entities = {} |
paulb@154 | 483 | notations = {} |
paulb@18 | 484 | |
paulb@240 | 485 | def isSameNode(self, other): |
paulb@240 | 486 | return self == other |
paulb@18 | 487 | |
paulb@262 | 488 | def __hash__(self): |
paulb@262 | 489 | return hash(self.localName) |
paulb@262 | 490 | |
paulb@240 | 491 | def __eq__(self, other): |
paulb@276 | 492 | return isinstance(other, Node) and Node_equals(self._node, other._node) |
paulb@240 | 493 | |
paulb@240 | 494 | def __ne__(self, other): |
paulb@240 | 495 | return not (self == other) |
paulb@18 | 496 | |
paulb@32 | 497 | # 4DOM extensions to the usual PyXML API. |
paulb@32 | 498 | # NOTE: To be finished. |
paulb@32 | 499 | |
paulb@32 | 500 | def xpath(self, expr, variables=None, namespaces=None): |
paulb@324 | 501 | |
paulb@324 | 502 | """ |
paulb@324 | 503 | Evaluate the given expression 'expr' using the optional 'variables' and |
paulb@324 | 504 | 'namespaces' mappings. |
paulb@324 | 505 | """ |
paulb@324 | 506 | |
paulb@301 | 507 | ns = {} |
paulb@301 | 508 | ns.update(default_ns) |
paulb@301 | 509 | ns.update(namespaces or {}) |
paulb@301 | 510 | result = Node_xpath(self._node, expr, variables, ns) |
paulb@202 | 511 | if isinstance(result, str): |
paulb@202 | 512 | return to_unicode(result) |
paulb@202 | 513 | elif hasattr(result, "__len__"): |
paulb@218 | 514 | return NodeList([self.impl.get_node(_node, self) for _node in result]) |
paulb@81 | 515 | else: |
paulb@81 | 516 | return result |
paulb@81 | 517 | |
paulb@324 | 518 | # Other extensions to the usual PyXML API. |
paulb@324 | 519 | |
paulb@324 | 520 | def xinclude(self): |
paulb@324 | 521 | |
paulb@324 | 522 | """ |
paulb@324 | 523 | Process XInclude declarations within the document, returning the number |
paulb@324 | 524 | of substitutions performed (zero or more), raising an XIncludeException |
paulb@324 | 525 | otherwise. |
paulb@324 | 526 | """ |
paulb@324 | 527 | |
paulb@324 | 528 | return Node_xinclude(self._node) |
paulb@324 | 529 | |
paulb@81 | 530 | # Convenience methods. |
paulb@81 | 531 | |
paulb@120 | 532 | def toString(self, encoding=None, prettyprint=0): |
paulb@120 | 533 | return toString(self, encoding, prettyprint) |
paulb@81 | 534 | |
paulb@120 | 535 | def toStream(self, stream, encoding=None, prettyprint=0): |
paulb@120 | 536 | toStream(self, stream, encoding, prettyprint) |
paulb@81 | 537 | |
paulb@120 | 538 | def toFile(self, f, encoding=None, prettyprint=0): |
paulb@120 | 539 | toFile(self, f, encoding, prettyprint) |
paulb@75 | 540 | |
paulb@88 | 541 | # Attribute nodes. |
paulb@88 | 542 | |
paulb@88 | 543 | class Attribute(Node): |
paulb@88 | 544 | |
paulb@88 | 545 | "A class providing attribute access." |
paulb@88 | 546 | |
paulb@218 | 547 | def __init__(self, node, impl, ownerDocument=None, ownerElement=None): |
paulb@218 | 548 | Node.__init__(self, node, impl, ownerDocument) |
paulb@88 | 549 | self.ownerElement = ownerElement |
paulb@88 | 550 | |
paulb@88 | 551 | def _parentNode(self): |
paulb@88 | 552 | return self.ownerElement |
paulb@88 | 553 | |
paulb@88 | 554 | parentNode = property(_parentNode) |
paulb@88 | 555 | |
paulb@75 | 556 | # Document housekeeping mechanisms. |
paulb@75 | 557 | |
paulb@258 | 558 | class _Document: |
paulb@75 | 559 | |
paulb@258 | 560 | """ |
paulb@258 | 561 | An abstract class providing document-level housekeeping and distinct |
paul@339 | 562 | functionality. Configuration of the document is also supported. |
paul@339 | 563 | See: http://www.w3.org/TR/DOM-Level-3-Core/core.html#DOMConfiguration |
paulb@258 | 564 | """ |
paulb@75 | 565 | |
paul@339 | 566 | # Constants from |
paul@339 | 567 | # See: http://www.w3.org/TR/DOM-Level-3-Val/validation.html#VAL-Interfaces-NodeEditVAL |
paul@339 | 568 | |
paul@339 | 569 | VAL_TRUE = 5 |
paul@339 | 570 | VAL_FALSE = 6 |
paul@339 | 571 | VAL_UNKNOWN = 7 |
paul@339 | 572 | |
paulb@218 | 573 | def __init__(self, node, impl): |
paulb@75 | 574 | self._node = node |
paulb@256 | 575 | self.implementation = self.impl = impl |
paul@339 | 576 | self.error_handler = libxml2dom.errors.DOMErrorHandler() |
paul@339 | 577 | |
paul@339 | 578 | # Standard DOM properties and their implementations. |
paulb@256 | 579 | |
paulb@256 | 580 | def _documentElement(self): |
paulb@256 | 581 | return self.xpath("*")[0] |
paulb@75 | 582 | |
paulb@75 | 583 | def _ownerDocument(self): |
paulb@188 | 584 | return self |
paulb@75 | 585 | |
paulb@75 | 586 | def __del__(self): |
paulb@84 | 587 | #print "Freeing document", self._node |
paulb@75 | 588 | libxml2mod.xmlFreeDoc(self._node) |
paulb@75 | 589 | |
paulb@256 | 590 | documentElement = property(_documentElement) |
paulb@75 | 591 | ownerDocument = property(_ownerDocument) |
paulb@154 | 592 | |
paul@339 | 593 | # DOM Level 3 Core DOMConfiguration methods. |
paul@339 | 594 | |
paul@339 | 595 | def setParameter(self, name, value): |
paul@339 | 596 | if name == "error-handler": |
paul@339 | 597 | raise xml.dom.NotSupportedErr() |
paul@339 | 598 | raise xml.dom.NotFoundErr() |
paul@339 | 599 | |
paul@339 | 600 | def getParameter(self, name): |
paul@339 | 601 | if name == "error-handler": |
paul@339 | 602 | return self.error_handler |
paul@339 | 603 | raise xml.dom.NotFoundErr() |
paul@339 | 604 | |
paul@339 | 605 | def canSetParameter(self, name, value): |
paul@339 | 606 | return 0 |
paul@339 | 607 | |
paul@339 | 608 | def _parameterNames(self): |
paul@339 | 609 | return [] |
paul@339 | 610 | |
paul@339 | 611 | # Extensions to the usual PyXML API. |
paul@339 | 612 | |
paul@339 | 613 | def validate(self, doc): |
paul@339 | 614 | |
paul@339 | 615 | """ |
paul@339 | 616 | Validate the document against the given schema document, 'doc'. |
paul@339 | 617 | """ |
paul@339 | 618 | |
paul@340 | 619 | validation_ns = doc.documentElement.namespaceURI |
paul@340 | 620 | |
paul@339 | 621 | if hasattr(doc, "as_native_node"): |
paul@340 | 622 | _schema = Document_schema(doc.as_native_node(), validation_ns) |
paul@339 | 623 | else: |
paul@340 | 624 | _schema = Document_schemaFromString(doc.toString(), validation_ns) |
paul@339 | 625 | try: |
paul@339 | 626 | self.error_handler.reset() |
paul@340 | 627 | return Document_validate(_schema, self._node, self.error_handler, validation_ns) |
paul@339 | 628 | finally: |
paul@340 | 629 | Schema_free(_schema, validation_ns) |
paul@339 | 630 | |
paul@339 | 631 | # DOM Level 3 Validation methods. |
paul@339 | 632 | |
paul@339 | 633 | def validateDocument(self, doc): |
paul@339 | 634 | |
paul@339 | 635 | """ |
paul@339 | 636 | Validate the document against the given schema document, 'doc'. |
paul@339 | 637 | See: http://www.w3.org/TR/DOM-Level-3-Val/validation.html#VAL-Interfaces-DocumentEditVAL-validateDocument |
paul@339 | 638 | """ |
paul@339 | 639 | |
paul@339 | 640 | return self.validate(doc) and self.VAL_TRUE or self.VAL_FALSE |
paul@339 | 641 | |
paulb@258 | 642 | class Document(_Document, Node): |
paulb@258 | 643 | |
paulb@258 | 644 | """ |
paulb@258 | 645 | A generic document class. Specialised document classes should inherit from |
paulb@258 | 646 | the _Document class and their own variation of Node. |
paulb@258 | 647 | """ |
paulb@258 | 648 | |
paulb@258 | 649 | pass |
paulb@258 | 650 | |
paulb@154 | 651 | class DocumentType(object): |
paulb@154 | 652 | |
paulb@154 | 653 | "A class providing a container for document type information." |
paulb@154 | 654 | |
paulb@154 | 655 | def __init__(self, localName, publicId, systemId): |
paulb@154 | 656 | self.name = self.localName = localName |
paulb@154 | 657 | self.publicId = publicId |
paulb@154 | 658 | self.systemId = systemId |
paulb@154 | 659 | |
paulb@154 | 660 | # NOTE: Nothing is currently provided to support the following |
paulb@154 | 661 | # NOTE: attributes. |
paulb@154 | 662 | |
paulb@154 | 663 | self.entities = {} |
paulb@154 | 664 | self.notations = {} |
paulb@32 | 665 | |
paulb@223 | 666 | # Constants. |
paulb@223 | 667 | |
paulb@223 | 668 | null_value_node_types = [ |
paulb@223 | 669 | Node.DOCUMENT_NODE, Node.DOCUMENT_TYPE_NODE, Node.ELEMENT_NODE, |
paulb@223 | 670 | Node.ENTITY_NODE, Node.ENTITY_REFERENCE_NODE, Node.NOTATION_NODE |
paulb@223 | 671 | ] |
paulb@223 | 672 | |
paulb@18 | 673 | # Utility functions. |
paulb@18 | 674 | |
paulb@18 | 675 | def createDocumentType(localName, publicId, systemId): |
paulb@223 | 676 | return default_impl.createDocumentType(localName, publicId, systemId) |
paulb@18 | 677 | |
paulb@18 | 678 | def createDocument(namespaceURI, localName, doctype): |
paulb@223 | 679 | return default_impl.createDocument(namespaceURI, localName, doctype) |
paulb@18 | 680 | |
paul@332 | 681 | def parse(stream_or_string, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0, impl=None): |
paulb@105 | 682 | |
paulb@105 | 683 | """ |
paulb@105 | 684 | Parse the given 'stream_or_string', where the supplied object can either be |
paulb@136 | 685 | a stream (such as a file or stream object), or a string (containing the |
paulb@268 | 686 | filename of a document). The optional parameters described below should be |
paulb@268 | 687 | provided as keyword arguments. |
paulb@268 | 688 | |
paulb@268 | 689 | If the optional 'html' parameter is set to a true value, the content to be |
paulb@268 | 690 | parsed will be treated as being HTML rather than XML. If the optional |
paulb@268 | 691 | 'htmlencoding' is specified, HTML parsing will be performed with the |
paulb@268 | 692 | document encoding assumed to that specified. |
paulb@268 | 693 | |
paulb@268 | 694 | If the optional 'unfinished' parameter is set to a true value, unfinished |
paulb@268 | 695 | documents will be parsed, even though such documents may be missing content |
paulb@268 | 696 | such as closing tags. |
paulb@105 | 697 | |
paul@332 | 698 | If the optional 'validate' parameter is set to a true value, an attempt will |
paul@332 | 699 | be made to validate the parsed document. |
paul@332 | 700 | |
paul@332 | 701 | If the optional 'remote' parameter is set to a true value, references to |
paul@332 | 702 | remote documents (such as DTDs) will be followed in order to obtain such |
paul@332 | 703 | documents. |
paul@332 | 704 | |
paulb@105 | 705 | A document object is returned by this function. |
paulb@105 | 706 | """ |
paulb@105 | 707 | |
paulb@218 | 708 | impl = impl or default_impl |
paulb@218 | 709 | |
paulb@18 | 710 | if hasattr(stream_or_string, "read"): |
paulb@18 | 711 | stream = stream_or_string |
paul@332 | 712 | return parseString(stream.read(), html=html, htmlencoding=htmlencoding, |
paul@332 | 713 | unfinished=unfinished, validate=validate, remote=remote, impl=impl) |
paulb@18 | 714 | else: |
paul@332 | 715 | return parseFile(stream_or_string, html=html, htmlencoding=htmlencoding, |
paul@332 | 716 | unfinished=unfinished, validate=validate, remote=remote, impl=impl) |
paulb@32 | 717 | |
paul@332 | 718 | def parseFile(filename, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0, impl=None): |
paulb@105 | 719 | |
paulb@105 | 720 | """ |
paulb@268 | 721 | Parse the file having the given 'filename'. The optional parameters |
paulb@268 | 722 | described below should be provided as keyword arguments. |
paulb@268 | 723 | |
paulb@268 | 724 | If the optional 'html' parameter is set to a true value, the content to be |
paulb@268 | 725 | parsed will be treated as being HTML rather than XML. If the optional |
paulb@268 | 726 | 'htmlencoding' is specified, HTML parsing will be performed with the |
paulb@268 | 727 | document encoding assumed to that specified. |
paulb@268 | 728 | |
paulb@268 | 729 | If the optional 'unfinished' parameter is set to a true value, unfinished |
paulb@268 | 730 | documents will be parsed, even though such documents may be missing content |
paulb@268 | 731 | such as closing tags. |
paulb@105 | 732 | |
paul@332 | 733 | If the optional 'validate' parameter is set to a true value, an attempt will |
paul@332 | 734 | be made to validate the parsed document. |
paul@332 | 735 | |
paul@332 | 736 | If the optional 'remote' parameter is set to a true value, references to |
paul@332 | 737 | remote documents (such as DTDs) will be followed in order to obtain such |
paul@332 | 738 | documents. |
paul@332 | 739 | |
paulb@105 | 740 | A document object is returned by this function. |
paulb@105 | 741 | """ |
paulb@105 | 742 | |
paulb@218 | 743 | impl = impl or default_impl |
paul@332 | 744 | return impl.adoptDocument(Node_parseFile(filename, html=html, htmlencoding=htmlencoding, |
paul@332 | 745 | unfinished=unfinished, validate=validate, remote=remote)) |
paulb@18 | 746 | |
paul@332 | 747 | def parseString(s, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0, impl=None): |
paulb@105 | 748 | |
paulb@105 | 749 | """ |
paulb@268 | 750 | Parse the content of the given string 's'. The optional parameters described |
paulb@268 | 751 | below should be provided as keyword arguments. |
paulb@268 | 752 | |
paulb@268 | 753 | If the optional 'html' parameter is set to a true value, the content to be |
paulb@268 | 754 | parsed will be treated as being HTML rather than XML. If the optional |
paulb@268 | 755 | 'htmlencoding' is specified, HTML parsing will be performed with the |
paulb@268 | 756 | document encoding assumed to that specified. |
paulb@268 | 757 | |
paulb@268 | 758 | If the optional 'unfinished' parameter is set to a true value, unfinished |
paulb@268 | 759 | documents will be parsed, even though such documents may be missing content |
paulb@268 | 760 | such as closing tags. |
paulb@105 | 761 | |
paul@332 | 762 | If the optional 'validate' parameter is set to a true value, an attempt will |
paul@332 | 763 | be made to validate the parsed document. |
paul@332 | 764 | |
paul@332 | 765 | If the optional 'remote' parameter is set to a true value, references to |
paul@332 | 766 | remote documents (such as DTDs) will be followed in order to obtain such |
paul@332 | 767 | documents. |
paul@332 | 768 | |
paulb@105 | 769 | A document object is returned by this function. |
paulb@105 | 770 | """ |
paulb@105 | 771 | |
paulb@218 | 772 | impl = impl or default_impl |
paul@332 | 773 | return impl.adoptDocument(Node_parseString(s, html=html, htmlencoding=htmlencoding, |
paul@332 | 774 | unfinished=unfinished, validate=validate, remote=remote)) |
paulb@18 | 775 | |
paul@332 | 776 | def parseURI(uri, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0, impl=None): |
paulb@105 | 777 | |
paulb@105 | 778 | """ |
paulb@268 | 779 | Parse the content found at the given 'uri'. The optional parameters |
paulb@268 | 780 | described below should be provided as keyword arguments. |
paulb@268 | 781 | |
paulb@268 | 782 | If the optional 'html' parameter is set to a true value, the content to be |
paulb@268 | 783 | parsed will be treated as being HTML rather than XML. If the optional |
paulb@268 | 784 | 'htmlencoding' is specified, HTML parsing will be performed with the |
paulb@268 | 785 | document encoding assumed to that specified. |
paulb@268 | 786 | |
paulb@268 | 787 | If the optional 'unfinished' parameter is set to a true value, unfinished |
paulb@268 | 788 | documents will be parsed, even though such documents may be missing content |
paulb@268 | 789 | such as closing tags. |
paulb@128 | 790 | |
paul@332 | 791 | If the optional 'validate' parameter is set to a true value, an attempt will |
paul@332 | 792 | be made to validate the parsed document. |
paul@332 | 793 | |
paul@332 | 794 | If the optional 'remote' parameter is set to a true value, references to |
paul@332 | 795 | remote documents (such as DTDs) will be followed in order to obtain such |
paul@332 | 796 | documents. |
paul@332 | 797 | |
paulb@232 | 798 | XML documents are retrieved using libxml2's own network capabilities; HTML |
paulb@232 | 799 | documents are retrieved using the urllib module provided by Python. To |
paulb@232 | 800 | retrieve either kind of document using Python's own modules for this purpose |
paulb@232 | 801 | (such as urllib), open a stream and pass it to the parse function: |
paulb@128 | 802 | |
paulb@232 | 803 | f = urllib.urlopen(uri) |
paulb@232 | 804 | try: |
paulb@232 | 805 | doc = libxml2dom.parse(f, html) |
paulb@232 | 806 | finally: |
paulb@232 | 807 | f.close() |
paulb@105 | 808 | |
paulb@105 | 809 | A document object is returned by this function. |
paulb@105 | 810 | """ |
paulb@105 | 811 | |
paulb@232 | 812 | if html: |
paulb@232 | 813 | f = urllib.urlopen(uri) |
paulb@232 | 814 | try: |
paul@332 | 815 | return parse(f, html=html, htmlencoding=htmlencoding, unfinished=unfinished, |
paul@332 | 816 | validate=validate, remote=remote, impl=impl) |
paulb@232 | 817 | finally: |
paulb@232 | 818 | f.close() |
paulb@232 | 819 | else: |
paulb@232 | 820 | impl = impl or default_impl |
paul@332 | 821 | return impl.adoptDocument(Node_parseURI(uri, html=html, htmlencoding=htmlencoding, |
paul@332 | 822 | unfinished=unfinished, validate=validate, remote=remote)) |
paulb@18 | 823 | |
paulb@120 | 824 | def toString(node, encoding=None, prettyprint=0): |
paulb@105 | 825 | |
paulb@105 | 826 | """ |
paulb@105 | 827 | Return a string containing the serialised form of the given 'node' and its |
paulb@105 | 828 | children. The optional 'encoding' can be used to override the default |
paulb@120 | 829 | character encoding used in the serialisation. The optional 'prettyprint' |
paulb@120 | 830 | indicates whether the serialised form is prettyprinted or not (the default |
paulb@120 | 831 | setting). |
paulb@105 | 832 | """ |
paulb@105 | 833 | |
paulb@120 | 834 | return Node_toString(node.as_native_node(), encoding, prettyprint) |
paulb@18 | 835 | |
paulb@120 | 836 | def toStream(node, stream, encoding=None, prettyprint=0): |
paulb@105 | 837 | |
paulb@105 | 838 | """ |
paulb@105 | 839 | Write the serialised form of the given 'node' and its children to the given |
paulb@105 | 840 | 'stream'. The optional 'encoding' can be used to override the default |
paulb@120 | 841 | character encoding used in the serialisation. The optional 'prettyprint' |
paulb@120 | 842 | indicates whether the serialised form is prettyprinted or not (the default |
paulb@120 | 843 | setting). |
paulb@105 | 844 | """ |
paulb@105 | 845 | |
paulb@120 | 846 | Node_toStream(node.as_native_node(), stream, encoding, prettyprint) |
paulb@72 | 847 | |
paulb@120 | 848 | def toFile(node, filename, encoding=None, prettyprint=0): |
paulb@105 | 849 | |
paulb@105 | 850 | """ |
paulb@105 | 851 | Write the serialised form of the given 'node' and its children to a file |
paulb@105 | 852 | having the given 'filename'. The optional 'encoding' can be used to override |
paulb@120 | 853 | the default character encoding used in the serialisation. The optional |
paulb@120 | 854 | 'prettyprint' indicates whether the serialised form is prettyprinted or not |
paulb@120 | 855 | (the default setting). |
paulb@105 | 856 | """ |
paulb@105 | 857 | |
paulb@120 | 858 | Node_toFile(node.as_native_node(), filename, encoding, prettyprint) |
paulb@18 | 859 | |
paulb@218 | 860 | def adoptNodes(nodes, impl=None): |
paulb@105 | 861 | |
paulb@105 | 862 | """ |
paulb@105 | 863 | A special utility method which adopts the given low-level 'nodes' and which |
paulb@105 | 864 | returns a list of high-level equivalents. This is currently experimental and |
paulb@105 | 865 | should not be casually used. |
paulb@105 | 866 | """ |
paulb@105 | 867 | |
paulb@218 | 868 | impl = impl or default_impl |
paulb@218 | 869 | |
paulb@81 | 870 | if len(nodes) == 0: |
paulb@81 | 871 | return [] |
paulb@236 | 872 | doc = impl.adoptDocument(libxml2mod.doc(nodes[0])) |
paulb@81 | 873 | results = [] |
paulb@81 | 874 | for node in nodes: |
paulb@218 | 875 | results.append(Node(node, impl, doc)) |
paulb@81 | 876 | return results |
paulb@81 | 877 | |
paulb@240 | 878 | def getDOMImplementation(): |
paulb@240 | 879 | |
paulb@240 | 880 | "Return the default DOM implementation." |
paulb@240 | 881 | |
paulb@240 | 882 | return default_impl |
paulb@240 | 883 | |
paulb@218 | 884 | # Single instance of the implementation. |
paulb@218 | 885 | |
paulb@218 | 886 | default_impl = Implementation() |
paulb@218 | 887 | |
paulb@18 | 888 | # vim: tabstop=4 expandtab shiftwidth=4 |