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