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