# HG changeset patch # User paulb # Date 1133402346 0 # Node ID 587985051e3d2101881bfff20e9b25be1202505a # Parent 2c443945c409b585c32b6b3f7edefc4ae3e60260 [project @ 2005-12-01 01:59:06 by paulb] Updated version number. Fixed Document.parentNode. Added doctype support (dubious in places due to a lack of libxml2mod support/understanding). diff -r 2c443945c409 -r 587985051e3d libxml2dom/__init__.py --- a/libxml2dom/__init__.py Thu Dec 01 01:58:25 2005 +0000 +++ b/libxml2dom/__init__.py Thu Dec 01 01:59:06 2005 +0000 @@ -4,9 +4,8 @@ DOM wrapper around libxml2, specifically the libxml2mod Python extension module. """ -__version__ = "0.2.4" +__version__ = "0.2.5" -import libxml2 from libxml2dom.macrolib import * from libxml2dom.macrolib import \ createDocument as Node_createDocument, \ @@ -174,6 +173,42 @@ def _nextSibling(self): return Node(Node_nextSibling(self._node), self.ownerDocument) + def _doctype(self): + return Node(Node_doctype(self._node), self.ownerDocument) + + def _publicId(self): + # NOTE: To be fixed when the libxml2mod API has been figured out. + if self.nodeType != self.DOCUMENT_TYPE_NODE: + return None + declaration = self.toString() + return self._findId(declaration, "PUBLIC") + + def _systemId(self): + # NOTE: To be fixed when the libxml2mod API has been figured out. + if self.nodeType != self.DOCUMENT_TYPE_NODE: + return None + declaration = self.toString() + if self._findId(declaration, "PUBLIC"): + return self._findIdValue(declaration, 0) + return self._findId(declaration, "SYSTEM") + + # NOTE: To be removed when the libxml2mod API has been figured out. + + def _findId(self, declaration, identifier): + i = declaration.find(identifier) + if i == -1: + return None + return self._findIdValue(declaration, i) + + def _findIdValue(self, declaration, i): + q = declaration.find('"', i) + if q == -1: + return None + q2 = declaration.find('"', q + 1) + if q2 == -1: + return None + return declaration[q+1:q2] + def hasAttributeNS(self, ns, localName): return Node_hasAttributeNS(self._node, ns, localName) @@ -285,8 +320,6 @@ texts.append(text_nodes[-1].nodeValue) self.replaceChild(self.ownerDocument.createTextNode("".join(texts)), text_nodes[-1]) - # NOTE: normalize must be implemented specially for libxml2dom. - childNodes = property(_childNodes) value = data = nodeValue = property(_nodeValue, _setNodeValue) name = nodeName = property(_nodeName) @@ -299,6 +332,14 @@ attributes = property(_attributes) previousSibling = property(_previousSibling) nextSibling = property(_nextSibling) + doctype = property(_doctype) + publicId = property(_publicId) + systemId = property(_systemId) + + # NOTE: To be fixed - these being doctype-specific values. + + entities = {} + notations = {} #def isSameNode(self, other): # return self._node.nodePath() == other._node.nodePath() @@ -355,11 +396,30 @@ def _ownerDocument(self): return self.weakref_ownerDocument() + def _parentNode(self): + return None + def __del__(self): #print "Freeing document", self._node libxml2mod.xmlFreeDoc(self._node) ownerDocument = property(_ownerDocument) + parentNode = property(_parentNode) + +class DocumentType(object): + + "A class providing a container for document type information." + + def __init__(self, localName, publicId, systemId): + self.name = self.localName = localName + self.publicId = publicId + self.systemId = systemId + + # NOTE: Nothing is currently provided to support the following + # NOTE: attributes. + + self.entities = {} + self.notations = {} # Factory functions. @@ -374,7 +434,7 @@ # Utility functions. def createDocumentType(localName, publicId, systemId): - return None + return DocumentType(localName, publicId, systemId) def createDocument(namespaceURI, localName, doctype): return Document(Node_createDocument(namespaceURI, localName, doctype))