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