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