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