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@218 | 6 | Copyright (C) 2003, 2004, 2005, 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@188 | 7 | |
paulb@188 | 8 | This library is free software; you can redistribute it and/or |
paulb@188 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@188 | 10 | License as published by the Free Software Foundation; either |
paulb@188 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@188 | 12 | |
paulb@188 | 13 | This library is distributed in the hope that it will be useful, |
paulb@188 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@188 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@188 | 16 | Lesser General Public License for more details. |
paulb@188 | 17 | |
paulb@188 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@188 | 19 | License along with this library; if not, write to the Free Software |
paulb@188 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@18 | 21 | """ |
paulb@18 | 22 | |
paulb@251 | 23 | __version__ = "0.4.1" |
paulb@27 | 24 | |
paulb@72 | 25 | from libxml2dom.macrolib import * |
paulb@72 | 26 | from libxml2dom.macrolib import \ |
paulb@72 | 27 | createDocument as Node_createDocument, \ |
paulb@72 | 28 | parseString as Node_parseString, parseURI as Node_parseURI, \ |
paulb@72 | 29 | parseFile as Node_parseFile, \ |
paulb@72 | 30 | toString as Node_toString, toStream as Node_toStream, \ |
paulb@72 | 31 | toFile as Node_toFile |
paulb@232 | 32 | import urllib # for parseURI in HTML mode |
paulb@18 | 33 | |
paulb@218 | 34 | class Implementation(object): |
paulb@218 | 35 | |
paulb@218 | 36 | "Contains an abstraction over the DOM implementation." |
paulb@218 | 37 | |
paulb@218 | 38 | def createDocumentType(self, localName, publicId, systemId): |
paulb@218 | 39 | return DocumentType(localName, publicId, systemId) |
paulb@218 | 40 | |
paulb@218 | 41 | def createDocument(self, namespaceURI, localName, doctype): |
paulb@218 | 42 | return Document(Node_createDocument(namespaceURI, localName, doctype), self) |
paulb@218 | 43 | |
paulb@236 | 44 | # Wrapping of documents. |
paulb@236 | 45 | |
paulb@236 | 46 | def adoptDocument(self, node): |
paulb@236 | 47 | return Document(node, self) |
paulb@236 | 48 | |
paulb@218 | 49 | # Factory functions. |
paulb@218 | 50 | |
paulb@218 | 51 | def get_node(self, _node, context_node): |
paulb@218 | 52 | if Node_nodeType(_node) == context_node.DOCUMENT_NODE: |
paulb@218 | 53 | return context_node.ownerDocument |
paulb@218 | 54 | elif Node_nodeType(_node) == context_node.ATTRIBUTE_NODE: |
paulb@218 | 55 | return Attribute(_node, self, context_node.ownerDocument, context_node) |
paulb@218 | 56 | else: |
paulb@218 | 57 | return Node(_node, self, context_node.ownerDocument) |
paulb@218 | 58 | |
paulb@240 | 59 | def get_node_or_none(self, _node, context_node): |
paulb@240 | 60 | if _node is None: |
paulb@240 | 61 | return None |
paulb@240 | 62 | else: |
paulb@240 | 63 | return self.get_node(_node, context_node) |
paulb@240 | 64 | |
paulb@86 | 65 | # Attribute and node list wrappers. |
paulb@18 | 66 | |
paulb@18 | 67 | class NamedNodeMap(object): |
paulb@18 | 68 | |
paulb@75 | 69 | """ |
paulb@75 | 70 | A wrapper around Node objects providing DOM and dictionary convenience |
paulb@75 | 71 | methods. |
paulb@75 | 72 | """ |
paulb@75 | 73 | |
paulb@18 | 74 | def __init__(self, node): |
paulb@18 | 75 | self.node = node |
paulb@18 | 76 | |
paulb@18 | 77 | def getNamedItem(self, name): |
paulb@72 | 78 | return self.node.getAttributeNode(name) |
paulb@18 | 79 | |
paulb@18 | 80 | def getNamedItemNS(self, ns, localName): |
paulb@72 | 81 | return self.node.getAttributeNodeNS(ns, localName) |
paulb@18 | 82 | |
paulb@18 | 83 | def setNamedItem(self, node): |
paulb@91 | 84 | try: |
paulb@91 | 85 | old = self.getNamedItem(node.nodeName) |
paulb@91 | 86 | except KeyError: |
paulb@91 | 87 | old = None |
paulb@86 | 88 | self.node.setAttributeNode(node) |
paulb@91 | 89 | return old |
paulb@18 | 90 | |
paulb@18 | 91 | def setNamedItemNS(self, node): |
paulb@91 | 92 | try: |
paulb@91 | 93 | old = self.getNamedItemNS(node.namespaceURI, node.localName) |
paulb@91 | 94 | except KeyError: |
paulb@91 | 95 | old = None |
paulb@86 | 96 | self.node.setAttributeNodeNS(node) |
paulb@91 | 97 | return old |
paulb@91 | 98 | |
paulb@91 | 99 | def removeNamedItem(self, name): |
paulb@91 | 100 | try: |
paulb@91 | 101 | old = self.getNamedItem(name) |
paulb@91 | 102 | except KeyError: |
paulb@91 | 103 | old = None |
paulb@91 | 104 | self.node.removeAttribute(name) |
paulb@91 | 105 | return old |
paulb@91 | 106 | |
paulb@91 | 107 | def removeNamedItemNS(self, ns, localName): |
paulb@91 | 108 | try: |
paulb@91 | 109 | old = self.getNamedItemNS(ns, localName) |
paulb@91 | 110 | except KeyError: |
paulb@91 | 111 | old = None |
paulb@91 | 112 | self.node.removeAttributeNS(ns, localName) |
paulb@91 | 113 | return old |
paulb@86 | 114 | |
paulb@86 | 115 | # Dictionary emulation methods. |
paulb@18 | 116 | |
paulb@18 | 117 | def __getitem__(self, name): |
paulb@18 | 118 | return self.getNamedItem(name) |
paulb@18 | 119 | |
paulb@18 | 120 | def __setitem__(self, name, node): |
paulb@18 | 121 | if name == node.nodeName: |
paulb@18 | 122 | self.setNamedItem(node) |
paulb@18 | 123 | else: |
paulb@18 | 124 | raise KeyError, name |
paulb@18 | 125 | |
paulb@18 | 126 | def __delitem__(self, name): |
paulb@18 | 127 | # NOTE: To be implemented. |
paulb@18 | 128 | pass |
paulb@18 | 129 | |
paulb@18 | 130 | def values(self): |
paulb@218 | 131 | return [Attribute(_node, self.impl, self.node.ownerDocument) for _node in Node_attributes(self.node.as_native_node()).values()] |
paulb@18 | 132 | |
paulb@18 | 133 | def keys(self): |
paulb@18 | 134 | return [(attr.namespaceURI, attr.localName) for attr in self.values()] |
paulb@18 | 135 | |
paulb@18 | 136 | def items(self): |
paulb@18 | 137 | return [((attr.namespaceURI, attr.localName), attr) for attr in self.values()] |
paulb@18 | 138 | |
paulb@18 | 139 | def __repr__(self): |
paulb@18 | 140 | return str(self) |
paulb@18 | 141 | |
paulb@18 | 142 | def __str__(self): |
paulb@18 | 143 | return "{%s}" % ",\n".join(["%s : %s" % (repr(key), repr(value)) for key, value in self.items()]) |
paulb@18 | 144 | |
paulb@198 | 145 | def _length(self): |
paulb@198 | 146 | return len(self.values()) |
paulb@198 | 147 | |
paulb@198 | 148 | length = property(_length) |
paulb@198 | 149 | |
paulb@86 | 150 | class NodeList(list): |
paulb@86 | 151 | |
paulb@86 | 152 | "A wrapper around node lists." |
paulb@86 | 153 | |
paulb@86 | 154 | def item(self, index): |
paulb@86 | 155 | return self[index] |
paulb@86 | 156 | |
paulb@198 | 157 | def _length(self): |
paulb@86 | 158 | return len(self) |
paulb@86 | 159 | |
paulb@198 | 160 | length = property(_length) |
paulb@198 | 161 | |
paulb@86 | 162 | # Node classes. |
paulb@86 | 163 | |
paulb@18 | 164 | class Node(object): |
paulb@18 | 165 | |
paulb@75 | 166 | """ |
paulb@75 | 167 | A DOM-style wrapper around libxml2mod objects. |
paulb@75 | 168 | """ |
paulb@75 | 169 | |
paulb@81 | 170 | ATTRIBUTE_NODE = xml.dom.Node.ATTRIBUTE_NODE |
paulb@81 | 171 | COMMENT_NODE = xml.dom.Node.COMMENT_NODE |
paulb@81 | 172 | DOCUMENT_NODE = xml.dom.Node.DOCUMENT_NODE |
paulb@81 | 173 | DOCUMENT_TYPE_NODE = xml.dom.Node.DOCUMENT_TYPE_NODE |
paulb@81 | 174 | ELEMENT_NODE = xml.dom.Node.ELEMENT_NODE |
paulb@81 | 175 | ENTITY_NODE = xml.dom.Node.ENTITY_NODE |
paulb@81 | 176 | ENTITY_REFERENCE_NODE = xml.dom.Node.ENTITY_REFERENCE_NODE |
paulb@81 | 177 | NOTATION_NODE = xml.dom.Node.NOTATION_NODE |
paulb@81 | 178 | PROCESSING_INSTRUCTION_NODE = xml.dom.Node.PROCESSING_INSTRUCTION_NODE |
paulb@81 | 179 | TEXT_NODE = xml.dom.Node.TEXT_NODE |
paulb@81 | 180 | |
paulb@223 | 181 | def __init__(self, node, impl=None, ownerDocument=None): |
paulb@18 | 182 | self._node = node |
paulb@223 | 183 | self.impl = impl or default_impl |
paulb@75 | 184 | self.ownerDocument = ownerDocument |
paulb@18 | 185 | |
paulb@18 | 186 | def as_native_node(self): |
paulb@18 | 187 | return self._node |
paulb@18 | 188 | |
paulb@18 | 189 | def _nodeType(self): |
paulb@72 | 190 | return Node_nodeType(self._node) |
paulb@18 | 191 | |
paulb@18 | 192 | def _childNodes(self): |
paulb@18 | 193 | |
paulb@18 | 194 | # NOTE: Consider a generator instead. |
paulb@18 | 195 | |
paulb@218 | 196 | return NodeList([self.impl.get_node(_node, self) for _node in Node_childNodes(self._node)]) |
paulb@18 | 197 | |
paulb@18 | 198 | def _attributes(self): |
paulb@18 | 199 | return NamedNodeMap(self) |
paulb@18 | 200 | |
paulb@18 | 201 | def _namespaceURI(self): |
paulb@72 | 202 | return Node_namespaceURI(self._node) |
paulb@18 | 203 | |
paulb@236 | 204 | def _textContent(self): |
paulb@236 | 205 | return Node_textContent(self._node) |
paulb@236 | 206 | |
paulb@18 | 207 | def _nodeValue(self): |
paulb@223 | 208 | if self.nodeType in null_value_node_types: |
paulb@223 | 209 | return None |
paulb@72 | 210 | return Node_nodeValue(self._node) |
paulb@18 | 211 | |
paulb@86 | 212 | def _setNodeValue(self, value): |
paulb@86 | 213 | Node_setNodeValue(self._node, value) |
paulb@86 | 214 | |
paulb@18 | 215 | def _prefix(self): |
paulb@72 | 216 | return Node_prefix(self._node) |
paulb@18 | 217 | |
paulb@18 | 218 | def _nodeName(self): |
paulb@72 | 219 | return Node_nodeName(self._node) |
paulb@18 | 220 | |
paulb@18 | 221 | def _tagName(self): |
paulb@72 | 222 | return Node_tagName(self._node) |
paulb@18 | 223 | |
paulb@18 | 224 | def _localName(self): |
paulb@72 | 225 | return Node_localName(self._node) |
paulb@18 | 226 | |
paulb@18 | 227 | def _parentNode(self): |
paulb@240 | 228 | return self.impl.get_node_or_none(Node_parentNode(self._node), self) |
paulb@18 | 229 | |
paulb@24 | 230 | def _previousSibling(self): |
paulb@240 | 231 | return self.impl.get_node_or_none(Node_previousSibling(self._node), self) |
paulb@24 | 232 | |
paulb@18 | 233 | def _nextSibling(self): |
paulb@240 | 234 | return self.impl.get_node_or_none(Node_nextSibling(self._node), self) |
paulb@18 | 235 | |
paulb@154 | 236 | def _doctype(self): |
paulb@218 | 237 | return self.impl.get_node(Node_doctype(self._node), self) |
paulb@154 | 238 | |
paulb@154 | 239 | def _publicId(self): |
paulb@154 | 240 | # NOTE: To be fixed when the libxml2mod API has been figured out. |
paulb@154 | 241 | if self.nodeType != self.DOCUMENT_TYPE_NODE: |
paulb@154 | 242 | return None |
paulb@154 | 243 | declaration = self.toString() |
paulb@154 | 244 | return self._findId(declaration, "PUBLIC") |
paulb@154 | 245 | |
paulb@154 | 246 | def _systemId(self): |
paulb@154 | 247 | # NOTE: To be fixed when the libxml2mod API has been figured out. |
paulb@154 | 248 | if self.nodeType != self.DOCUMENT_TYPE_NODE: |
paulb@154 | 249 | return None |
paulb@154 | 250 | declaration = self.toString() |
paulb@154 | 251 | if self._findId(declaration, "PUBLIC"): |
paulb@154 | 252 | return self._findIdValue(declaration, 0) |
paulb@154 | 253 | return self._findId(declaration, "SYSTEM") |
paulb@154 | 254 | |
paulb@154 | 255 | # NOTE: To be removed when the libxml2mod API has been figured out. |
paulb@154 | 256 | |
paulb@154 | 257 | def _findId(self, declaration, identifier): |
paulb@154 | 258 | i = declaration.find(identifier) |
paulb@154 | 259 | if i == -1: |
paulb@154 | 260 | return None |
paulb@154 | 261 | return self._findIdValue(declaration, i) |
paulb@154 | 262 | |
paulb@154 | 263 | def _findIdValue(self, declaration, i): |
paulb@154 | 264 | q = declaration.find('"', i) |
paulb@154 | 265 | if q == -1: |
paulb@154 | 266 | return None |
paulb@154 | 267 | q2 = declaration.find('"', q + 1) |
paulb@154 | 268 | if q2 == -1: |
paulb@154 | 269 | return None |
paulb@154 | 270 | return declaration[q+1:q2] |
paulb@154 | 271 | |
paulb@18 | 272 | def hasAttributeNS(self, ns, localName): |
paulb@72 | 273 | return Node_hasAttributeNS(self._node, ns, localName) |
paulb@18 | 274 | |
paulb@18 | 275 | def hasAttribute(self, name): |
paulb@72 | 276 | return Node_hasAttribute(self._node, name) |
paulb@18 | 277 | |
paulb@18 | 278 | def getAttributeNS(self, ns, localName): |
paulb@72 | 279 | return Node_getAttributeNS(self._node, ns, localName) |
paulb@18 | 280 | |
paulb@18 | 281 | def getAttribute(self, name): |
paulb@72 | 282 | return Node_getAttribute(self._node, name) |
paulb@18 | 283 | |
paulb@18 | 284 | def getAttributeNodeNS(self, ns, localName): |
paulb@218 | 285 | return Attribute(Node_getAttributeNodeNS(self._node, ns, localName), self.impl, self.ownerDocument, self) |
paulb@18 | 286 | |
paulb@18 | 287 | def getAttributeNode(self, localName): |
paulb@218 | 288 | return Attribute(Node_getAttributeNode(self._node, localName), self.impl, self.ownerDocument, self) |
paulb@18 | 289 | |
paulb@18 | 290 | def setAttributeNS(self, ns, name, value): |
paulb@72 | 291 | Node_setAttributeNS(self._node, ns, name, value) |
paulb@18 | 292 | |
paulb@18 | 293 | def setAttribute(self, name, value): |
paulb@72 | 294 | Node_setAttribute(self._node, name, value) |
paulb@18 | 295 | |
paulb@86 | 296 | def setAttributeNodeNS(self, node): |
paulb@88 | 297 | Node_setAttributeNodeNS(self._node, node._node) |
paulb@18 | 298 | |
paulb@86 | 299 | def setAttributeNode(self, node): |
paulb@88 | 300 | Node_setAttributeNode(self._node, node._node) |
paulb@18 | 301 | |
paulb@91 | 302 | def removeAttributeNS(self, ns, localName): |
paulb@91 | 303 | Node_removeAttributeNS(self._node, ns, localName) |
paulb@91 | 304 | |
paulb@91 | 305 | def removeAttribute(self, name): |
paulb@91 | 306 | Node_removeAttribute(self._node, name) |
paulb@91 | 307 | |
paulb@18 | 308 | def createElementNS(self, ns, name): |
paulb@218 | 309 | return self.impl.get_node(Node_createElementNS(self._node, ns, name), self) |
paulb@18 | 310 | |
paulb@18 | 311 | def createElement(self, name): |
paulb@218 | 312 | return self.impl.get_node(Node_createElement(self._node, name), self) |
paulb@18 | 313 | |
paulb@88 | 314 | def createAttributeNS(self, ns, name): |
paulb@88 | 315 | tmp = self.createElement("tmp") |
paulb@218 | 316 | return Attribute(Node_createAttributeNS(tmp._node, self.impl, ns, name)) |
paulb@18 | 317 | |
paulb@88 | 318 | def createAttribute(self, name): |
paulb@88 | 319 | tmp = self.createElement("tmp") |
paulb@218 | 320 | return Attribute(Node_createAttribute(tmp._node, name), self.impl) |
paulb@18 | 321 | |
paulb@18 | 322 | def createTextNode(self, value): |
paulb@218 | 323 | return self.impl.get_node(Node_createTextNode(self._node, value), self) |
paulb@18 | 324 | |
paulb@34 | 325 | def createComment(self, value): |
paulb@218 | 326 | return self.impl.get_node(Node_createComment(self._node, value), self) |
paulb@18 | 327 | |
paulb@251 | 328 | def createCDATASection(self, value): |
paulb@251 | 329 | return self.impl.get_node(Node_createCDATASection(self._node, value), self) |
paulb@251 | 330 | |
paulb@18 | 331 | def importNode(self, node, deep): |
paulb@72 | 332 | if hasattr(node, "as_native_node"): |
paulb@218 | 333 | return self.impl.get_node(Node_importNode(self._node, node.as_native_node(), deep), self) |
paulb@72 | 334 | else: |
paulb@218 | 335 | return self.impl.get_node(Node_importNode_DOM(self._node, node, deep), self) |
paulb@18 | 336 | |
paulb@208 | 337 | def cloneNode(self, deep): |
paulb@208 | 338 | # This takes advantage of the ubiquity of importNode (in spite of the DOM specification). |
paulb@208 | 339 | return self.importNode(self, deep) |
paulb@208 | 340 | |
paulb@18 | 341 | def insertBefore(self, tmp, oldNode): |
paulb@72 | 342 | if hasattr(tmp, "as_native_node"): |
paulb@218 | 343 | return self.impl.get_node(Node_insertBefore(self._node, tmp.as_native_node(), oldNode.as_native_node()), self) |
paulb@18 | 344 | else: |
paulb@218 | 345 | return self.impl.get_node(Node_insertBefore(self._node, tmp, oldNode.as_native_node()), self) |
paulb@18 | 346 | |
paulb@18 | 347 | def replaceChild(self, tmp, oldNode): |
paulb@72 | 348 | if hasattr(tmp, "as_native_node"): |
paulb@218 | 349 | return self.impl.get_node(Node_replaceChild(self._node, tmp.as_native_node(), oldNode.as_native_node()), self) |
paulb@18 | 350 | else: |
paulb@218 | 351 | return self.impl.get_node(Node_replaceChild(self._node, tmp, oldNode.as_native_node()), self) |
paulb@18 | 352 | |
paulb@18 | 353 | def appendChild(self, tmp): |
paulb@72 | 354 | if hasattr(tmp, "as_native_node"): |
paulb@218 | 355 | return self.impl.get_node(Node_appendChild(self._node, tmp.as_native_node()), self) |
paulb@18 | 356 | else: |
paulb@218 | 357 | return self.impl.get_node(Node_appendChild(self._node, tmp), self) |
paulb@18 | 358 | |
paulb@18 | 359 | def removeChild(self, tmp): |
paulb@72 | 360 | if hasattr(tmp, "as_native_node"): |
paulb@72 | 361 | Node_removeChild(self._node, tmp.as_native_node()) |
paulb@72 | 362 | else: |
paulb@72 | 363 | Node_removeChild(self._node, tmp) |
paulb@18 | 364 | |
paulb@99 | 365 | def getElementsByTagName(self, tagName): |
paulb@232 | 366 | return self.xpath(".//" + tagName) |
paulb@99 | 367 | |
paulb@99 | 368 | def getElementsByTagNameNS(self, namespaceURI, localName): |
paulb@232 | 369 | return self.xpath(".//ns:" + localName, namespaces={"ns" : namespaceURI}) |
paulb@99 | 370 | |
paulb@124 | 371 | def normalize(self): |
paulb@124 | 372 | text_nodes = [] |
paulb@124 | 373 | for node in self.childNodes: |
paulb@124 | 374 | if node.nodeType == node.TEXT_NODE: |
paulb@124 | 375 | text_nodes.append(node) |
paulb@124 | 376 | elif len(text_nodes) != 0: |
paulb@124 | 377 | self._normalize(text_nodes) |
paulb@124 | 378 | text_nodes = [] |
paulb@124 | 379 | if len(text_nodes) != 0: |
paulb@124 | 380 | self._normalize(text_nodes) |
paulb@124 | 381 | |
paulb@124 | 382 | def _normalize(self, text_nodes): |
paulb@124 | 383 | texts = [] |
paulb@124 | 384 | for text_node in text_nodes[:-1]: |
paulb@124 | 385 | texts.append(text_node.nodeValue) |
paulb@124 | 386 | self.removeChild(text_node) |
paulb@124 | 387 | texts.append(text_nodes[-1].nodeValue) |
paulb@124 | 388 | self.replaceChild(self.ownerDocument.createTextNode("".join(texts)), text_nodes[-1]) |
paulb@124 | 389 | |
paulb@18 | 390 | childNodes = property(_childNodes) |
paulb@86 | 391 | value = data = nodeValue = property(_nodeValue, _setNodeValue) |
paulb@236 | 392 | textContent = property(_textContent) |
paulb@18 | 393 | name = nodeName = property(_nodeName) |
paulb@18 | 394 | tagName = property(_tagName) |
paulb@18 | 395 | namespaceURI = property(_namespaceURI) |
paulb@18 | 396 | prefix = property(_prefix) |
paulb@18 | 397 | localName = property(_localName) |
paulb@88 | 398 | parentNode = property(_parentNode) |
paulb@18 | 399 | nodeType = property(_nodeType) |
paulb@18 | 400 | attributes = property(_attributes) |
paulb@24 | 401 | previousSibling = property(_previousSibling) |
paulb@18 | 402 | nextSibling = property(_nextSibling) |
paulb@154 | 403 | doctype = property(_doctype) |
paulb@154 | 404 | publicId = property(_publicId) |
paulb@154 | 405 | systemId = property(_systemId) |
paulb@154 | 406 | |
paulb@154 | 407 | # NOTE: To be fixed - these being doctype-specific values. |
paulb@154 | 408 | |
paulb@154 | 409 | entities = {} |
paulb@154 | 410 | notations = {} |
paulb@18 | 411 | |
paulb@240 | 412 | def isSameNode(self, other): |
paulb@240 | 413 | return self == other |
paulb@18 | 414 | |
paulb@240 | 415 | def __eq__(self, other): |
paulb@240 | 416 | return isinstance(other, Node) and libxml2mod.xmlXPathCmpNodes(self._node, other._node) == 0 |
paulb@240 | 417 | |
paulb@240 | 418 | def __ne__(self, other): |
paulb@240 | 419 | return not (self == other) |
paulb@18 | 420 | |
paulb@32 | 421 | # 4DOM extensions to the usual PyXML API. |
paulb@32 | 422 | # NOTE: To be finished. |
paulb@32 | 423 | |
paulb@32 | 424 | def xpath(self, expr, variables=None, namespaces=None): |
paulb@81 | 425 | result = Node_xpath(self._node, expr, variables, namespaces) |
paulb@202 | 426 | if isinstance(result, str): |
paulb@202 | 427 | return to_unicode(result) |
paulb@202 | 428 | elif hasattr(result, "__len__"): |
paulb@218 | 429 | return NodeList([self.impl.get_node(_node, self) for _node in result]) |
paulb@81 | 430 | else: |
paulb@81 | 431 | return result |
paulb@81 | 432 | |
paulb@81 | 433 | # Convenience methods. |
paulb@81 | 434 | |
paulb@120 | 435 | def toString(self, encoding=None, prettyprint=0): |
paulb@120 | 436 | return toString(self, encoding, prettyprint) |
paulb@81 | 437 | |
paulb@120 | 438 | def toStream(self, stream, encoding=None, prettyprint=0): |
paulb@120 | 439 | toStream(self, stream, encoding, prettyprint) |
paulb@81 | 440 | |
paulb@120 | 441 | def toFile(self, f, encoding=None, prettyprint=0): |
paulb@120 | 442 | toFile(self, f, encoding, prettyprint) |
paulb@75 | 443 | |
paulb@88 | 444 | # Attribute nodes. |
paulb@88 | 445 | |
paulb@88 | 446 | class Attribute(Node): |
paulb@88 | 447 | |
paulb@88 | 448 | "A class providing attribute access." |
paulb@88 | 449 | |
paulb@218 | 450 | def __init__(self, node, impl, ownerDocument=None, ownerElement=None): |
paulb@218 | 451 | Node.__init__(self, node, impl, ownerDocument) |
paulb@88 | 452 | self.ownerElement = ownerElement |
paulb@88 | 453 | |
paulb@88 | 454 | def _parentNode(self): |
paulb@88 | 455 | return self.ownerElement |
paulb@88 | 456 | |
paulb@88 | 457 | parentNode = property(_parentNode) |
paulb@88 | 458 | |
paulb@75 | 459 | # Document housekeeping mechanisms. |
paulb@75 | 460 | |
paulb@75 | 461 | class Document(Node): |
paulb@75 | 462 | |
paulb@75 | 463 | "A class providing document-level housekeeping." |
paulb@75 | 464 | |
paulb@218 | 465 | def __init__(self, node, impl): |
paulb@75 | 466 | self._node = node |
paulb@218 | 467 | self.impl = impl |
paulb@75 | 468 | |
paulb@75 | 469 | def _ownerDocument(self): |
paulb@188 | 470 | return self |
paulb@75 | 471 | |
paulb@75 | 472 | def __del__(self): |
paulb@84 | 473 | #print "Freeing document", self._node |
paulb@75 | 474 | libxml2mod.xmlFreeDoc(self._node) |
paulb@75 | 475 | |
paulb@75 | 476 | ownerDocument = property(_ownerDocument) |
paulb@154 | 477 | |
paulb@154 | 478 | class DocumentType(object): |
paulb@154 | 479 | |
paulb@154 | 480 | "A class providing a container for document type information." |
paulb@154 | 481 | |
paulb@154 | 482 | def __init__(self, localName, publicId, systemId): |
paulb@154 | 483 | self.name = self.localName = localName |
paulb@154 | 484 | self.publicId = publicId |
paulb@154 | 485 | self.systemId = systemId |
paulb@154 | 486 | |
paulb@154 | 487 | # NOTE: Nothing is currently provided to support the following |
paulb@154 | 488 | # NOTE: attributes. |
paulb@154 | 489 | |
paulb@154 | 490 | self.entities = {} |
paulb@154 | 491 | self.notations = {} |
paulb@32 | 492 | |
paulb@223 | 493 | # Constants. |
paulb@223 | 494 | |
paulb@223 | 495 | null_value_node_types = [ |
paulb@223 | 496 | Node.DOCUMENT_NODE, Node.DOCUMENT_TYPE_NODE, Node.ELEMENT_NODE, |
paulb@223 | 497 | Node.ENTITY_NODE, Node.ENTITY_REFERENCE_NODE, Node.NOTATION_NODE |
paulb@223 | 498 | ] |
paulb@223 | 499 | |
paulb@18 | 500 | # Utility functions. |
paulb@18 | 501 | |
paulb@18 | 502 | def createDocumentType(localName, publicId, systemId): |
paulb@223 | 503 | return default_impl.createDocumentType(localName, publicId, systemId) |
paulb@18 | 504 | |
paulb@18 | 505 | def createDocument(namespaceURI, localName, doctype): |
paulb@223 | 506 | return default_impl.createDocument(namespaceURI, localName, doctype) |
paulb@18 | 507 | |
paulb@246 | 508 | def parse(stream_or_string, html=0, htmlencoding=None, impl=None): |
paulb@105 | 509 | |
paulb@105 | 510 | """ |
paulb@105 | 511 | Parse the given 'stream_or_string', where the supplied object can either be |
paulb@136 | 512 | a stream (such as a file or stream object), or a string (containing the |
paulb@136 | 513 | filename of a document). If the optional 'html' parameter is set to a true |
paulb@136 | 514 | value, the content to be parsed will be treated as being HTML rather than |
paulb@246 | 515 | XML. If the optional 'htmlencoding' is specified, HTML parsing will be |
paulb@246 | 516 | performed with the document encoding assumed to that specified. |
paulb@105 | 517 | |
paulb@105 | 518 | A document object is returned by this function. |
paulb@105 | 519 | """ |
paulb@105 | 520 | |
paulb@218 | 521 | impl = impl or default_impl |
paulb@218 | 522 | |
paulb@18 | 523 | if hasattr(stream_or_string, "read"): |
paulb@18 | 524 | stream = stream_or_string |
paulb@246 | 525 | return parseString(stream.read(), html, htmlencoding, impl) |
paulb@18 | 526 | else: |
paulb@246 | 527 | return parseFile(stream_or_string, html, htmlencoding, impl) |
paulb@32 | 528 | |
paulb@246 | 529 | def parseFile(filename, html=0, htmlencoding=None, impl=None): |
paulb@105 | 530 | |
paulb@105 | 531 | """ |
paulb@105 | 532 | Parse the file having the given 'filename'. If the optional 'html' parameter |
paulb@105 | 533 | is set to a true value, the content to be parsed will be treated as being |
paulb@246 | 534 | HTML rather than XML. If the optional 'htmlencoding' is specified, HTML |
paulb@246 | 535 | parsing will be performed with the document encoding assumed to be that |
paulb@246 | 536 | specified. |
paulb@105 | 537 | |
paulb@105 | 538 | A document object is returned by this function. |
paulb@105 | 539 | """ |
paulb@105 | 540 | |
paulb@218 | 541 | impl = impl or default_impl |
paulb@246 | 542 | return impl.adoptDocument(Node_parseFile(filename, html, htmlencoding)) |
paulb@18 | 543 | |
paulb@246 | 544 | def parseString(s, html=0, htmlencoding=None, impl=None): |
paulb@105 | 545 | |
paulb@105 | 546 | """ |
paulb@105 | 547 | Parse the content of the given string 's'. If the optional 'html' parameter |
paulb@105 | 548 | is set to a true value, the content to be parsed will be treated as being |
paulb@246 | 549 | HTML rather than XML. If the optional 'htmlencoding' is specified, HTML |
paulb@246 | 550 | parsing will be performed with the document encoding assumed to be that |
paulb@246 | 551 | specified. |
paulb@105 | 552 | |
paulb@105 | 553 | A document object is returned by this function. |
paulb@105 | 554 | """ |
paulb@105 | 555 | |
paulb@218 | 556 | impl = impl or default_impl |
paulb@246 | 557 | return impl.adoptDocument(Node_parseString(s, html, htmlencoding)) |
paulb@18 | 558 | |
paulb@246 | 559 | def parseURI(uri, html=0, htmlencoding=None, impl=None): |
paulb@105 | 560 | |
paulb@105 | 561 | """ |
paulb@105 | 562 | Parse the content found at the given 'uri'. If the optional 'html' parameter |
paulb@105 | 563 | is set to a true value, the content to be parsed will be treated as being |
paulb@246 | 564 | HTML rather than XML. If the optional 'htmlencoding' is specified, HTML |
paulb@246 | 565 | parsing will be performed with the document encoding assumed to be that |
paulb@246 | 566 | specified. |
paulb@128 | 567 | |
paulb@232 | 568 | XML documents are retrieved using libxml2's own network capabilities; HTML |
paulb@232 | 569 | documents are retrieved using the urllib module provided by Python. To |
paulb@232 | 570 | retrieve either kind of document using Python's own modules for this purpose |
paulb@232 | 571 | (such as urllib), open a stream and pass it to the parse function: |
paulb@128 | 572 | |
paulb@232 | 573 | f = urllib.urlopen(uri) |
paulb@232 | 574 | try: |
paulb@232 | 575 | doc = libxml2dom.parse(f, html) |
paulb@232 | 576 | finally: |
paulb@232 | 577 | f.close() |
paulb@105 | 578 | |
paulb@105 | 579 | A document object is returned by this function. |
paulb@105 | 580 | """ |
paulb@105 | 581 | |
paulb@232 | 582 | if html: |
paulb@232 | 583 | f = urllib.urlopen(uri) |
paulb@232 | 584 | try: |
paulb@246 | 585 | return parse(f, html, htmlencoding, impl) |
paulb@232 | 586 | finally: |
paulb@232 | 587 | f.close() |
paulb@232 | 588 | else: |
paulb@232 | 589 | impl = impl or default_impl |
paulb@246 | 590 | return impl.adoptDocument(Node_parseURI(uri, html, htmlencoding)) |
paulb@18 | 591 | |
paulb@120 | 592 | def toString(node, encoding=None, prettyprint=0): |
paulb@105 | 593 | |
paulb@105 | 594 | """ |
paulb@105 | 595 | Return a string containing the serialised form of the given 'node' and its |
paulb@105 | 596 | children. The optional 'encoding' can be used to override the default |
paulb@120 | 597 | character encoding used in the serialisation. The optional 'prettyprint' |
paulb@120 | 598 | indicates whether the serialised form is prettyprinted or not (the default |
paulb@120 | 599 | setting). |
paulb@105 | 600 | """ |
paulb@105 | 601 | |
paulb@120 | 602 | return Node_toString(node.as_native_node(), encoding, prettyprint) |
paulb@18 | 603 | |
paulb@120 | 604 | def toStream(node, stream, encoding=None, prettyprint=0): |
paulb@105 | 605 | |
paulb@105 | 606 | """ |
paulb@105 | 607 | Write the serialised form of the given 'node' and its children to the given |
paulb@105 | 608 | 'stream'. The optional 'encoding' can be used to override the default |
paulb@120 | 609 | character encoding used in the serialisation. The optional 'prettyprint' |
paulb@120 | 610 | indicates whether the serialised form is prettyprinted or not (the default |
paulb@120 | 611 | setting). |
paulb@105 | 612 | """ |
paulb@105 | 613 | |
paulb@120 | 614 | Node_toStream(node.as_native_node(), stream, encoding, prettyprint) |
paulb@72 | 615 | |
paulb@120 | 616 | def toFile(node, filename, encoding=None, prettyprint=0): |
paulb@105 | 617 | |
paulb@105 | 618 | """ |
paulb@105 | 619 | Write the serialised form of the given 'node' and its children to a file |
paulb@105 | 620 | having the given 'filename'. The optional 'encoding' can be used to override |
paulb@120 | 621 | the default character encoding used in the serialisation. The optional |
paulb@120 | 622 | 'prettyprint' indicates whether the serialised form is prettyprinted or not |
paulb@120 | 623 | (the default setting). |
paulb@105 | 624 | """ |
paulb@105 | 625 | |
paulb@120 | 626 | Node_toFile(node.as_native_node(), filename, encoding, prettyprint) |
paulb@18 | 627 | |
paulb@218 | 628 | def adoptNodes(nodes, impl=None): |
paulb@105 | 629 | |
paulb@105 | 630 | """ |
paulb@105 | 631 | A special utility method which adopts the given low-level 'nodes' and which |
paulb@105 | 632 | returns a list of high-level equivalents. This is currently experimental and |
paulb@105 | 633 | should not be casually used. |
paulb@105 | 634 | """ |
paulb@105 | 635 | |
paulb@218 | 636 | impl = impl or default_impl |
paulb@218 | 637 | |
paulb@81 | 638 | if len(nodes) == 0: |
paulb@81 | 639 | return [] |
paulb@236 | 640 | doc = impl.adoptDocument(libxml2mod.doc(nodes[0])) |
paulb@81 | 641 | results = [] |
paulb@81 | 642 | for node in nodes: |
paulb@218 | 643 | results.append(Node(node, impl, doc)) |
paulb@81 | 644 | return results |
paulb@81 | 645 | |
paulb@240 | 646 | def getDOMImplementation(): |
paulb@240 | 647 | |
paulb@240 | 648 | "Return the default DOM implementation." |
paulb@240 | 649 | |
paulb@240 | 650 | return default_impl |
paulb@240 | 651 | |
paulb@218 | 652 | # Single instance of the implementation. |
paulb@218 | 653 | |
paulb@218 | 654 | default_impl = Implementation() |
paulb@218 | 655 | |
paulb@18 | 656 | # vim: tabstop=4 expandtab shiftwidth=4 |