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