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