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