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