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@346 | 22 | __version__ = "0.5" |
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 | |
paul@349 | 325 | def hasChildNodes(self): |
paul@349 | 326 | return bool(self.childNodes) |
paul@349 | 327 | |
paulb@18 | 328 | def hasAttributeNS(self, ns, localName): |
paulb@72 | 329 | return Node_hasAttributeNS(self._node, ns, localName) |
paulb@18 | 330 | |
paulb@18 | 331 | def hasAttribute(self, name): |
paulb@72 | 332 | return Node_hasAttribute(self._node, name) |
paulb@18 | 333 | |
paulb@18 | 334 | def getAttributeNS(self, ns, localName): |
paulb@72 | 335 | return Node_getAttributeNS(self._node, ns, localName) |
paulb@18 | 336 | |
paulb@18 | 337 | def getAttribute(self, name): |
paulb@72 | 338 | return Node_getAttribute(self._node, name) |
paulb@18 | 339 | |
paulb@18 | 340 | def getAttributeNodeNS(self, ns, localName): |
paulb@218 | 341 | return Attribute(Node_getAttributeNodeNS(self._node, ns, localName), self.impl, self.ownerDocument, self) |
paulb@18 | 342 | |
paulb@18 | 343 | def getAttributeNode(self, localName): |
paulb@218 | 344 | return Attribute(Node_getAttributeNode(self._node, localName), self.impl, self.ownerDocument, self) |
paulb@18 | 345 | |
paulb@18 | 346 | def setAttributeNS(self, ns, name, value): |
paulb@72 | 347 | Node_setAttributeNS(self._node, ns, name, value) |
paulb@18 | 348 | |
paulb@18 | 349 | def setAttribute(self, name, value): |
paulb@72 | 350 | Node_setAttribute(self._node, name, value) |
paulb@18 | 351 | |
paulb@86 | 352 | def setAttributeNodeNS(self, node): |
paulb@88 | 353 | Node_setAttributeNodeNS(self._node, node._node) |
paulb@18 | 354 | |
paulb@86 | 355 | def setAttributeNode(self, node): |
paulb@88 | 356 | Node_setAttributeNode(self._node, node._node) |
paulb@18 | 357 | |
paulb@91 | 358 | def removeAttributeNS(self, ns, localName): |
paulb@91 | 359 | Node_removeAttributeNS(self._node, ns, localName) |
paulb@91 | 360 | |
paulb@91 | 361 | def removeAttribute(self, name): |
paulb@91 | 362 | Node_removeAttribute(self._node, name) |
paulb@91 | 363 | |
paulb@18 | 364 | def createElementNS(self, ns, name): |
paulb@218 | 365 | return self.impl.get_node(Node_createElementNS(self._node, ns, name), self) |
paulb@18 | 366 | |
paulb@18 | 367 | def createElement(self, name): |
paulb@218 | 368 | return self.impl.get_node(Node_createElement(self._node, name), self) |
paulb@18 | 369 | |
paulb@88 | 370 | def createAttributeNS(self, ns, name): |
paulb@88 | 371 | tmp = self.createElement("tmp") |
paulb@218 | 372 | return Attribute(Node_createAttributeNS(tmp._node, self.impl, ns, name)) |
paulb@18 | 373 | |
paulb@88 | 374 | def createAttribute(self, name): |
paulb@88 | 375 | tmp = self.createElement("tmp") |
paulb@218 | 376 | return Attribute(Node_createAttribute(tmp._node, name), self.impl) |
paulb@18 | 377 | |
paulb@18 | 378 | def createTextNode(self, value): |
paulb@218 | 379 | return self.impl.get_node(Node_createTextNode(self._node, value), self) |
paulb@18 | 380 | |
paulb@34 | 381 | def createComment(self, value): |
paulb@218 | 382 | return self.impl.get_node(Node_createComment(self._node, value), self) |
paulb@18 | 383 | |
paulb@251 | 384 | def createCDATASection(self, value): |
paulb@251 | 385 | return self.impl.get_node(Node_createCDATASection(self._node, value), self) |
paulb@251 | 386 | |
paulb@18 | 387 | def importNode(self, node, deep): |
paulb@72 | 388 | if hasattr(node, "as_native_node"): |
paulb@218 | 389 | return self.impl.get_node(Node_importNode(self._node, node.as_native_node(), deep), self) |
paulb@72 | 390 | else: |
paulb@218 | 391 | return self.impl.get_node(Node_importNode_DOM(self._node, node, deep), self) |
paulb@18 | 392 | |
paulb@208 | 393 | def cloneNode(self, deep): |
paulb@208 | 394 | # This takes advantage of the ubiquity of importNode (in spite of the DOM specification). |
paulb@208 | 395 | return self.importNode(self, deep) |
paulb@208 | 396 | |
paulb@18 | 397 | def insertBefore(self, tmp, oldNode): |
paulb@276 | 398 | if tmp.ownerDocument != self.ownerDocument: |
paul@339 | 399 | raise xml.dom.WrongDocumentErr() |
paulb@276 | 400 | if oldNode.parentNode != self: |
paul@339 | 401 | raise xml.dom.NotFoundErr() |
paul@346 | 402 | |
paul@346 | 403 | # Nodes must be from this implementation before insertion. |
paul@346 | 404 | |
paul@346 | 405 | if not hasattr(tmp, "as_native_node"): |
paul@346 | 406 | raise xml.dom.WrongDocumentErr() |
paul@346 | 407 | |
paul@346 | 408 | return self.impl.get_node(Node_insertBefore(self._node, tmp.as_native_node(), oldNode.as_native_node()), self) |
paulb@18 | 409 | |
paulb@18 | 410 | def replaceChild(self, tmp, oldNode): |
paulb@276 | 411 | if tmp.ownerDocument != self.ownerDocument: |
paul@339 | 412 | raise xml.dom.WrongDocumentErr() |
paulb@276 | 413 | if oldNode.parentNode != self: |
paul@339 | 414 | raise xml.dom.NotFoundErr() |
paul@346 | 415 | |
paul@346 | 416 | # Nodes must be from this implementation before insertion. |
paul@346 | 417 | |
paul@346 | 418 | if not hasattr(tmp, "as_native_node"): |
paul@346 | 419 | raise xml.dom.WrongDocumentErr() |
paul@346 | 420 | |
paul@346 | 421 | return self.impl.get_node(Node_replaceChild(self._node, tmp.as_native_node(), oldNode.as_native_node()), self) |
paulb@18 | 422 | |
paulb@18 | 423 | def appendChild(self, tmp): |
paulb@276 | 424 | if tmp.ownerDocument != self.ownerDocument: |
paul@339 | 425 | raise xml.dom.WrongDocumentErr() |
paul@346 | 426 | |
paul@346 | 427 | # Nodes must be from this implementation before insertion. |
paul@346 | 428 | |
paul@346 | 429 | if not hasattr(tmp, "as_native_node"): |
paul@346 | 430 | raise xml.dom.WrongDocumentErr() |
paul@346 | 431 | |
paul@346 | 432 | return self.impl.get_node(Node_appendChild(self._node, tmp.as_native_node()), self) |
paulb@18 | 433 | |
paulb@18 | 434 | def removeChild(self, tmp): |
paul@346 | 435 | |
paul@346 | 436 | # Nodes must be from this implementation in order to be removed. |
paul@346 | 437 | |
paul@346 | 438 | if not hasattr(tmp, "as_native_node"): |
paul@346 | 439 | raise xml.dom.WrongDocumentErr() |
paul@346 | 440 | |
paul@346 | 441 | Node_removeChild(self._node, tmp.as_native_node()) |
paulb@276 | 442 | return tmp |
paulb@18 | 443 | |
paulb@262 | 444 | def getElementById(self, identifier): |
paul@332 | 445 | _node = Node_getElementById(self.ownerDocument.as_native_node(), identifier) |
paul@332 | 446 | if _node is None: |
paul@332 | 447 | return None |
paulb@262 | 448 | else: |
paul@332 | 449 | return self.impl.get_node(_node, self) |
paulb@262 | 450 | |
paulb@99 | 451 | def getElementsByTagName(self, tagName): |
paulb@232 | 452 | return self.xpath(".//" + tagName) |
paulb@99 | 453 | |
paulb@99 | 454 | def getElementsByTagNameNS(self, namespaceURI, localName): |
paulb@232 | 455 | return self.xpath(".//ns:" + localName, namespaces={"ns" : namespaceURI}) |
paulb@99 | 456 | |
paulb@124 | 457 | def normalize(self): |
paulb@124 | 458 | text_nodes = [] |
paulb@124 | 459 | for node in self.childNodes: |
paulb@124 | 460 | if node.nodeType == node.TEXT_NODE: |
paulb@124 | 461 | text_nodes.append(node) |
paulb@124 | 462 | elif len(text_nodes) != 0: |
paulb@124 | 463 | self._normalize(text_nodes) |
paulb@124 | 464 | text_nodes = [] |
paulb@124 | 465 | if len(text_nodes) != 0: |
paulb@124 | 466 | self._normalize(text_nodes) |
paulb@124 | 467 | |
paulb@124 | 468 | def _normalize(self, text_nodes): |
paulb@124 | 469 | texts = [] |
paulb@124 | 470 | for text_node in text_nodes[:-1]: |
paulb@124 | 471 | texts.append(text_node.nodeValue) |
paulb@124 | 472 | self.removeChild(text_node) |
paulb@124 | 473 | texts.append(text_nodes[-1].nodeValue) |
paulb@124 | 474 | self.replaceChild(self.ownerDocument.createTextNode("".join(texts)), text_nodes[-1]) |
paulb@124 | 475 | |
paulb@18 | 476 | childNodes = property(_childNodes) |
paulb@276 | 477 | firstChild = property(_firstChild) |
paulb@276 | 478 | lastChild = property(_lastChild) |
paulb@86 | 479 | value = data = nodeValue = property(_nodeValue, _setNodeValue) |
paulb@236 | 480 | textContent = property(_textContent) |
paulb@18 | 481 | name = nodeName = property(_nodeName) |
paulb@18 | 482 | tagName = property(_tagName) |
paulb@18 | 483 | namespaceURI = property(_namespaceURI) |
paulb@18 | 484 | prefix = property(_prefix) |
paulb@18 | 485 | localName = property(_localName) |
paulb@88 | 486 | parentNode = property(_parentNode) |
paulb@18 | 487 | nodeType = property(_nodeType) |
paulb@18 | 488 | attributes = property(_attributes) |
paulb@24 | 489 | previousSibling = property(_previousSibling) |
paulb@18 | 490 | nextSibling = property(_nextSibling) |
paulb@154 | 491 | doctype = property(_doctype) |
paulb@154 | 492 | publicId = property(_publicId) |
paulb@154 | 493 | systemId = property(_systemId) |
paulb@154 | 494 | |
paulb@154 | 495 | # NOTE: To be fixed - these being doctype-specific values. |
paulb@154 | 496 | |
paulb@154 | 497 | entities = {} |
paulb@154 | 498 | notations = {} |
paulb@18 | 499 | |
paulb@240 | 500 | def isSameNode(self, other): |
paulb@240 | 501 | return self == other |
paulb@18 | 502 | |
paulb@262 | 503 | def __hash__(self): |
paulb@262 | 504 | return hash(self.localName) |
paulb@262 | 505 | |
paulb@240 | 506 | def __eq__(self, other): |
paulb@276 | 507 | return isinstance(other, Node) and Node_equals(self._node, other._node) |
paulb@240 | 508 | |
paulb@240 | 509 | def __ne__(self, other): |
paulb@240 | 510 | return not (self == other) |
paulb@18 | 511 | |
paulb@32 | 512 | # 4DOM extensions to the usual PyXML API. |
paulb@32 | 513 | # NOTE: To be finished. |
paulb@32 | 514 | |
paulb@32 | 515 | def xpath(self, expr, variables=None, namespaces=None): |
paulb@324 | 516 | |
paulb@324 | 517 | """ |
paulb@324 | 518 | Evaluate the given expression 'expr' using the optional 'variables' and |
paulb@324 | 519 | 'namespaces' mappings. |
paulb@324 | 520 | """ |
paulb@324 | 521 | |
paulb@301 | 522 | ns = {} |
paulb@301 | 523 | ns.update(default_ns) |
paulb@301 | 524 | ns.update(namespaces or {}) |
paulb@301 | 525 | result = Node_xpath(self._node, expr, variables, ns) |
paulb@202 | 526 | if isinstance(result, str): |
paulb@202 | 527 | return to_unicode(result) |
paulb@202 | 528 | elif hasattr(result, "__len__"): |
paulb@218 | 529 | return NodeList([self.impl.get_node(_node, self) for _node in result]) |
paulb@81 | 530 | else: |
paulb@81 | 531 | return result |
paulb@81 | 532 | |
paulb@324 | 533 | # Other extensions to the usual PyXML API. |
paulb@324 | 534 | |
paulb@324 | 535 | def xinclude(self): |
paulb@324 | 536 | |
paulb@324 | 537 | """ |
paulb@324 | 538 | Process XInclude declarations within the document, returning the number |
paulb@324 | 539 | of substitutions performed (zero or more), raising an XIncludeException |
paulb@324 | 540 | otherwise. |
paulb@324 | 541 | """ |
paulb@324 | 542 | |
paulb@324 | 543 | return Node_xinclude(self._node) |
paulb@324 | 544 | |
paulb@81 | 545 | # Convenience methods. |
paulb@81 | 546 | |
paulb@120 | 547 | def toString(self, encoding=None, prettyprint=0): |
paulb@120 | 548 | return toString(self, encoding, prettyprint) |
paulb@81 | 549 | |
paulb@120 | 550 | def toStream(self, stream, encoding=None, prettyprint=0): |
paulb@120 | 551 | toStream(self, stream, encoding, prettyprint) |
paulb@81 | 552 | |
paulb@120 | 553 | def toFile(self, f, encoding=None, prettyprint=0): |
paulb@120 | 554 | toFile(self, f, encoding, prettyprint) |
paulb@75 | 555 | |
paulb@88 | 556 | # Attribute nodes. |
paulb@88 | 557 | |
paulb@88 | 558 | class Attribute(Node): |
paulb@88 | 559 | |
paulb@88 | 560 | "A class providing attribute access." |
paulb@88 | 561 | |
paulb@218 | 562 | def __init__(self, node, impl, ownerDocument=None, ownerElement=None): |
paulb@218 | 563 | Node.__init__(self, node, impl, ownerDocument) |
paulb@88 | 564 | self.ownerElement = ownerElement |
paulb@88 | 565 | |
paulb@88 | 566 | def _parentNode(self): |
paulb@88 | 567 | return self.ownerElement |
paulb@88 | 568 | |
paulb@88 | 569 | parentNode = property(_parentNode) |
paulb@88 | 570 | |
paulb@75 | 571 | # Document housekeeping mechanisms. |
paulb@75 | 572 | |
paulb@258 | 573 | class _Document: |
paulb@75 | 574 | |
paulb@258 | 575 | """ |
paulb@258 | 576 | An abstract class providing document-level housekeeping and distinct |
paul@339 | 577 | functionality. Configuration of the document is also supported. |
paul@339 | 578 | See: http://www.w3.org/TR/DOM-Level-3-Core/core.html#DOMConfiguration |
paulb@258 | 579 | """ |
paulb@75 | 580 | |
paul@339 | 581 | # Constants from |
paul@339 | 582 | # See: http://www.w3.org/TR/DOM-Level-3-Val/validation.html#VAL-Interfaces-NodeEditVAL |
paul@339 | 583 | |
paul@339 | 584 | VAL_TRUE = 5 |
paul@339 | 585 | VAL_FALSE = 6 |
paul@339 | 586 | VAL_UNKNOWN = 7 |
paul@339 | 587 | |
paulb@218 | 588 | def __init__(self, node, impl): |
paulb@75 | 589 | self._node = node |
paulb@256 | 590 | self.implementation = self.impl = impl |
paul@339 | 591 | self.error_handler = libxml2dom.errors.DOMErrorHandler() |
paul@339 | 592 | |
paul@339 | 593 | # Standard DOM properties and their implementations. |
paulb@256 | 594 | |
paulb@256 | 595 | def _documentElement(self): |
paulb@256 | 596 | return self.xpath("*")[0] |
paulb@75 | 597 | |
paulb@75 | 598 | def _ownerDocument(self): |
paulb@188 | 599 | return self |
paulb@75 | 600 | |
paulb@75 | 601 | def __del__(self): |
paulb@84 | 602 | #print "Freeing document", self._node |
paulb@75 | 603 | libxml2mod.xmlFreeDoc(self._node) |
paulb@75 | 604 | |
paulb@256 | 605 | documentElement = property(_documentElement) |
paulb@75 | 606 | ownerDocument = property(_ownerDocument) |
paulb@154 | 607 | |
paul@339 | 608 | # DOM Level 3 Core DOMConfiguration methods. |
paul@339 | 609 | |
paul@339 | 610 | def setParameter(self, name, value): |
paul@339 | 611 | if name == "error-handler": |
paul@339 | 612 | raise xml.dom.NotSupportedErr() |
paul@339 | 613 | raise xml.dom.NotFoundErr() |
paul@339 | 614 | |
paul@339 | 615 | def getParameter(self, name): |
paul@339 | 616 | if name == "error-handler": |
paul@339 | 617 | return self.error_handler |
paul@339 | 618 | raise xml.dom.NotFoundErr() |
paul@339 | 619 | |
paul@339 | 620 | def canSetParameter(self, name, value): |
paul@339 | 621 | return 0 |
paul@339 | 622 | |
paul@339 | 623 | def _parameterNames(self): |
paul@339 | 624 | return [] |
paul@339 | 625 | |
paul@339 | 626 | # Extensions to the usual PyXML API. |
paul@339 | 627 | |
paul@339 | 628 | def validate(self, doc): |
paul@339 | 629 | |
paul@339 | 630 | """ |
paul@339 | 631 | Validate the document against the given schema document, 'doc'. |
paul@339 | 632 | """ |
paul@339 | 633 | |
paul@340 | 634 | validation_ns = doc.documentElement.namespaceURI |
paul@340 | 635 | |
paul@339 | 636 | if hasattr(doc, "as_native_node"): |
paul@340 | 637 | _schema = Document_schema(doc.as_native_node(), validation_ns) |
paul@339 | 638 | else: |
paul@340 | 639 | _schema = Document_schemaFromString(doc.toString(), validation_ns) |
paul@339 | 640 | try: |
paul@339 | 641 | self.error_handler.reset() |
paul@340 | 642 | return Document_validate(_schema, self._node, self.error_handler, validation_ns) |
paul@339 | 643 | finally: |
paul@340 | 644 | Schema_free(_schema, validation_ns) |
paul@339 | 645 | |
paul@339 | 646 | # DOM Level 3 Validation methods. |
paul@339 | 647 | |
paul@339 | 648 | def validateDocument(self, doc): |
paul@339 | 649 | |
paul@339 | 650 | """ |
paul@339 | 651 | Validate the document against the given schema document, 'doc'. |
paul@339 | 652 | See: http://www.w3.org/TR/DOM-Level-3-Val/validation.html#VAL-Interfaces-DocumentEditVAL-validateDocument |
paul@339 | 653 | """ |
paul@339 | 654 | |
paul@339 | 655 | return self.validate(doc) and self.VAL_TRUE or self.VAL_FALSE |
paul@339 | 656 | |
paulb@258 | 657 | class Document(_Document, Node): |
paulb@258 | 658 | |
paulb@258 | 659 | """ |
paulb@258 | 660 | A generic document class. Specialised document classes should inherit from |
paulb@258 | 661 | the _Document class and their own variation of Node. |
paulb@258 | 662 | """ |
paulb@258 | 663 | |
paulb@258 | 664 | pass |
paulb@258 | 665 | |
paulb@154 | 666 | class DocumentType(object): |
paulb@154 | 667 | |
paulb@154 | 668 | "A class providing a container for document type information." |
paulb@154 | 669 | |
paulb@154 | 670 | def __init__(self, localName, publicId, systemId): |
paulb@154 | 671 | self.name = self.localName = localName |
paulb@154 | 672 | self.publicId = publicId |
paulb@154 | 673 | self.systemId = systemId |
paulb@154 | 674 | |
paulb@154 | 675 | # NOTE: Nothing is currently provided to support the following |
paulb@154 | 676 | # NOTE: attributes. |
paulb@154 | 677 | |
paulb@154 | 678 | self.entities = {} |
paulb@154 | 679 | self.notations = {} |
paulb@32 | 680 | |
paulb@223 | 681 | # Constants. |
paulb@223 | 682 | |
paulb@223 | 683 | null_value_node_types = [ |
paulb@223 | 684 | Node.DOCUMENT_NODE, Node.DOCUMENT_TYPE_NODE, Node.ELEMENT_NODE, |
paulb@223 | 685 | Node.ENTITY_NODE, Node.ENTITY_REFERENCE_NODE, Node.NOTATION_NODE |
paulb@223 | 686 | ] |
paulb@223 | 687 | |
paulb@18 | 688 | # Utility functions. |
paulb@18 | 689 | |
paulb@18 | 690 | def createDocumentType(localName, publicId, systemId): |
paulb@223 | 691 | return default_impl.createDocumentType(localName, publicId, systemId) |
paulb@18 | 692 | |
paulb@18 | 693 | def createDocument(namespaceURI, localName, doctype): |
paulb@223 | 694 | return default_impl.createDocument(namespaceURI, localName, doctype) |
paulb@18 | 695 | |
paul@332 | 696 | def parse(stream_or_string, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0, impl=None): |
paulb@105 | 697 | |
paulb@105 | 698 | """ |
paulb@105 | 699 | Parse the given 'stream_or_string', where the supplied object can either be |
paulb@136 | 700 | a stream (such as a file or stream object), or a string (containing the |
paulb@268 | 701 | filename of a document). The optional parameters described below should be |
paulb@268 | 702 | provided as keyword arguments. |
paulb@268 | 703 | |
paulb@268 | 704 | If the optional 'html' parameter is set to a true value, the content to be |
paulb@268 | 705 | parsed will be treated as being HTML rather than XML. If the optional |
paulb@268 | 706 | 'htmlencoding' is specified, HTML parsing will be performed with the |
paulb@268 | 707 | document encoding assumed to that specified. |
paulb@268 | 708 | |
paulb@268 | 709 | If the optional 'unfinished' parameter is set to a true value, unfinished |
paulb@268 | 710 | documents will be parsed, even though such documents may be missing content |
paulb@268 | 711 | such as closing tags. |
paulb@105 | 712 | |
paul@332 | 713 | If the optional 'validate' parameter is set to a true value, an attempt will |
paul@332 | 714 | be made to validate the parsed document. |
paul@332 | 715 | |
paul@332 | 716 | If the optional 'remote' parameter is set to a true value, references to |
paul@332 | 717 | remote documents (such as DTDs) will be followed in order to obtain such |
paul@332 | 718 | documents. |
paul@332 | 719 | |
paulb@105 | 720 | A document object is returned by this function. |
paulb@105 | 721 | """ |
paulb@105 | 722 | |
paulb@218 | 723 | impl = impl or default_impl |
paulb@218 | 724 | |
paulb@18 | 725 | if hasattr(stream_or_string, "read"): |
paulb@18 | 726 | stream = stream_or_string |
paul@332 | 727 | return parseString(stream.read(), html=html, htmlencoding=htmlencoding, |
paul@332 | 728 | unfinished=unfinished, validate=validate, remote=remote, impl=impl) |
paulb@18 | 729 | else: |
paul@332 | 730 | return parseFile(stream_or_string, html=html, htmlencoding=htmlencoding, |
paul@332 | 731 | unfinished=unfinished, validate=validate, remote=remote, impl=impl) |
paulb@32 | 732 | |
paul@332 | 733 | def parseFile(filename, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0, impl=None): |
paulb@105 | 734 | |
paulb@105 | 735 | """ |
paulb@268 | 736 | Parse the file having the given 'filename'. The optional parameters |
paulb@268 | 737 | described below should be provided as keyword arguments. |
paulb@268 | 738 | |
paulb@268 | 739 | If the optional 'html' parameter is set to a true value, the content to be |
paulb@268 | 740 | parsed will be treated as being HTML rather than XML. If the optional |
paulb@268 | 741 | 'htmlencoding' is specified, HTML parsing will be performed with the |
paulb@268 | 742 | document encoding assumed to that specified. |
paulb@268 | 743 | |
paulb@268 | 744 | If the optional 'unfinished' parameter is set to a true value, unfinished |
paulb@268 | 745 | documents will be parsed, even though such documents may be missing content |
paulb@268 | 746 | such as closing tags. |
paulb@105 | 747 | |
paul@332 | 748 | If the optional 'validate' parameter is set to a true value, an attempt will |
paul@332 | 749 | be made to validate the parsed document. |
paul@332 | 750 | |
paul@332 | 751 | If the optional 'remote' parameter is set to a true value, references to |
paul@332 | 752 | remote documents (such as DTDs) will be followed in order to obtain such |
paul@332 | 753 | documents. |
paul@332 | 754 | |
paulb@105 | 755 | A document object is returned by this function. |
paulb@105 | 756 | """ |
paulb@105 | 757 | |
paulb@218 | 758 | impl = impl or default_impl |
paul@332 | 759 | return impl.adoptDocument(Node_parseFile(filename, html=html, htmlencoding=htmlencoding, |
paul@332 | 760 | unfinished=unfinished, validate=validate, remote=remote)) |
paulb@18 | 761 | |
paul@332 | 762 | def parseString(s, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0, impl=None): |
paulb@105 | 763 | |
paulb@105 | 764 | """ |
paulb@268 | 765 | Parse the content of the given string 's'. The optional parameters described |
paulb@268 | 766 | below should be provided as keyword arguments. |
paulb@268 | 767 | |
paulb@268 | 768 | If the optional 'html' parameter is set to a true value, the content to be |
paulb@268 | 769 | parsed will be treated as being HTML rather than XML. If the optional |
paulb@268 | 770 | 'htmlencoding' is specified, HTML parsing will be performed with the |
paulb@268 | 771 | document encoding assumed to that specified. |
paulb@268 | 772 | |
paulb@268 | 773 | If the optional 'unfinished' parameter is set to a true value, unfinished |
paulb@268 | 774 | documents will be parsed, even though such documents may be missing content |
paulb@268 | 775 | such as closing tags. |
paulb@105 | 776 | |
paul@332 | 777 | If the optional 'validate' parameter is set to a true value, an attempt will |
paul@332 | 778 | be made to validate the parsed document. |
paul@332 | 779 | |
paul@332 | 780 | If the optional 'remote' parameter is set to a true value, references to |
paul@332 | 781 | remote documents (such as DTDs) will be followed in order to obtain such |
paul@332 | 782 | documents. |
paul@332 | 783 | |
paulb@105 | 784 | A document object is returned by this function. |
paulb@105 | 785 | """ |
paulb@105 | 786 | |
paulb@218 | 787 | impl = impl or default_impl |
paul@332 | 788 | return impl.adoptDocument(Node_parseString(s, html=html, htmlencoding=htmlencoding, |
paul@332 | 789 | unfinished=unfinished, validate=validate, remote=remote)) |
paulb@18 | 790 | |
paul@332 | 791 | def parseURI(uri, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0, impl=None): |
paulb@105 | 792 | |
paulb@105 | 793 | """ |
paulb@268 | 794 | Parse the content found at the given 'uri'. The optional parameters |
paulb@268 | 795 | described below should be provided as keyword arguments. |
paulb@268 | 796 | |
paulb@268 | 797 | If the optional 'html' parameter is set to a true value, the content to be |
paulb@268 | 798 | parsed will be treated as being HTML rather than XML. If the optional |
paulb@268 | 799 | 'htmlencoding' is specified, HTML parsing will be performed with the |
paulb@268 | 800 | document encoding assumed to that specified. |
paulb@268 | 801 | |
paulb@268 | 802 | If the optional 'unfinished' parameter is set to a true value, unfinished |
paulb@268 | 803 | documents will be parsed, even though such documents may be missing content |
paulb@268 | 804 | such as closing tags. |
paulb@128 | 805 | |
paul@332 | 806 | If the optional 'validate' parameter is set to a true value, an attempt will |
paul@332 | 807 | be made to validate the parsed document. |
paul@332 | 808 | |
paul@332 | 809 | If the optional 'remote' parameter is set to a true value, references to |
paul@332 | 810 | remote documents (such as DTDs) will be followed in order to obtain such |
paul@332 | 811 | documents. |
paul@332 | 812 | |
paulb@232 | 813 | XML documents are retrieved using libxml2's own network capabilities; HTML |
paulb@232 | 814 | documents are retrieved using the urllib module provided by Python. To |
paulb@232 | 815 | retrieve either kind of document using Python's own modules for this purpose |
paulb@232 | 816 | (such as urllib), open a stream and pass it to the parse function: |
paulb@128 | 817 | |
paulb@232 | 818 | f = urllib.urlopen(uri) |
paulb@232 | 819 | try: |
paulb@232 | 820 | doc = libxml2dom.parse(f, html) |
paulb@232 | 821 | finally: |
paulb@232 | 822 | f.close() |
paulb@105 | 823 | |
paulb@105 | 824 | A document object is returned by this function. |
paulb@105 | 825 | """ |
paulb@105 | 826 | |
paulb@232 | 827 | if html: |
paulb@232 | 828 | f = urllib.urlopen(uri) |
paulb@232 | 829 | try: |
paul@332 | 830 | return parse(f, html=html, htmlencoding=htmlencoding, unfinished=unfinished, |
paul@332 | 831 | validate=validate, remote=remote, impl=impl) |
paulb@232 | 832 | finally: |
paulb@232 | 833 | f.close() |
paulb@232 | 834 | else: |
paulb@232 | 835 | impl = impl or default_impl |
paul@332 | 836 | return impl.adoptDocument(Node_parseURI(uri, html=html, htmlencoding=htmlencoding, |
paul@332 | 837 | unfinished=unfinished, validate=validate, remote=remote)) |
paulb@18 | 838 | |
paulb@120 | 839 | def toString(node, encoding=None, prettyprint=0): |
paulb@105 | 840 | |
paulb@105 | 841 | """ |
paulb@105 | 842 | Return a string containing the serialised form of the given 'node' and its |
paulb@105 | 843 | children. The optional 'encoding' can be used to override the default |
paulb@120 | 844 | character encoding used in the serialisation. The optional 'prettyprint' |
paulb@120 | 845 | indicates whether the serialised form is prettyprinted or not (the default |
paulb@120 | 846 | setting). |
paulb@105 | 847 | """ |
paulb@105 | 848 | |
paulb@120 | 849 | return Node_toString(node.as_native_node(), encoding, prettyprint) |
paulb@18 | 850 | |
paulb@120 | 851 | def toStream(node, stream, encoding=None, prettyprint=0): |
paulb@105 | 852 | |
paulb@105 | 853 | """ |
paulb@105 | 854 | Write the serialised form of the given 'node' and its children to the given |
paulb@105 | 855 | 'stream'. The optional 'encoding' can be used to override the default |
paulb@120 | 856 | character encoding used in the serialisation. The optional 'prettyprint' |
paulb@120 | 857 | indicates whether the serialised form is prettyprinted or not (the default |
paulb@120 | 858 | setting). |
paulb@105 | 859 | """ |
paulb@105 | 860 | |
paulb@120 | 861 | Node_toStream(node.as_native_node(), stream, encoding, prettyprint) |
paulb@72 | 862 | |
paulb@120 | 863 | def toFile(node, filename, encoding=None, prettyprint=0): |
paulb@105 | 864 | |
paulb@105 | 865 | """ |
paulb@105 | 866 | Write the serialised form of the given 'node' and its children to a file |
paulb@105 | 867 | having the given 'filename'. The optional 'encoding' can be used to override |
paulb@120 | 868 | the default character encoding used in the serialisation. The optional |
paulb@120 | 869 | 'prettyprint' indicates whether the serialised form is prettyprinted or not |
paulb@120 | 870 | (the default setting). |
paulb@105 | 871 | """ |
paulb@105 | 872 | |
paulb@120 | 873 | Node_toFile(node.as_native_node(), filename, encoding, prettyprint) |
paulb@18 | 874 | |
paulb@218 | 875 | def adoptNodes(nodes, impl=None): |
paulb@105 | 876 | |
paulb@105 | 877 | """ |
paulb@105 | 878 | A special utility method which adopts the given low-level 'nodes' and which |
paulb@105 | 879 | returns a list of high-level equivalents. This is currently experimental and |
paulb@105 | 880 | should not be casually used. |
paulb@105 | 881 | """ |
paulb@105 | 882 | |
paulb@218 | 883 | impl = impl or default_impl |
paulb@218 | 884 | |
paulb@81 | 885 | if len(nodes) == 0: |
paulb@81 | 886 | return [] |
paulb@236 | 887 | doc = impl.adoptDocument(libxml2mod.doc(nodes[0])) |
paulb@81 | 888 | results = [] |
paulb@81 | 889 | for node in nodes: |
paulb@218 | 890 | results.append(Node(node, impl, doc)) |
paulb@81 | 891 | return results |
paulb@81 | 892 | |
paulb@240 | 893 | def getDOMImplementation(): |
paulb@240 | 894 | |
paulb@240 | 895 | "Return the default DOM implementation." |
paulb@240 | 896 | |
paulb@240 | 897 | return default_impl |
paulb@240 | 898 | |
paulb@218 | 899 | # Single instance of the implementation. |
paulb@218 | 900 | |
paulb@218 | 901 | default_impl = Implementation() |
paulb@218 | 902 | |
paulb@18 | 903 | # vim: tabstop=4 expandtab shiftwidth=4 |