# HG changeset patch # User paulb # Date 1106532927 0 # Node ID 4777b599ab1d042ba811e44477fa5562b0be0dea # Parent 90864d2a125645144f1a0d101cf2abc48745cf63 [project @ 2005-01-24 02:15:27 by paulb] Changed the macro library to use libxml2mod instead of libxml2. Since the library already used a function-oriented approach, there was no need to retain the object-oriented layer provided by libxml2. diff -r 90864d2a1256 -r 4777b599ab1d libxml2dom/macrolib/macrolib.py --- a/libxml2dom/macrolib/macrolib.py Mon Jan 24 00:48:28 2005 +0000 +++ b/libxml2dom/macrolib/macrolib.py Mon Jan 24 02:15:27 2005 +0000 @@ -1,17 +1,18 @@ #!/usr/bin/env python """ -DOM macros for virtual libxml2 node methods and properties. +DOM macros for virtual libxml2mod node methods and properties. """ import xml.dom -import libxml2 +import libxml2mod class TemporaryNode: def __init__(self, ns, name): self._ns = ns self.name = name self.type = "attribute" + self.nodeValue = None def ns(self): return self._ns @@ -55,58 +56,49 @@ } def Node_ownerDocument(node): - return node.doc + return libxml2mod.doc(node) def Node_nodeType(node): global _nodesTypes - return _nodeTypes[node.type] + return _nodeTypes[libxml2mod.type(node)] def Node_childNodes(node): # NOTE: Consider a generator instead. child_nodes = [] - node = node.children + node = libxml2mod.children(node) while node is not None: child_nodes.append(node) - node = node.next + node = libxml2mod.next(node) return child_nodes def Node_attributes(node): attributes = {} - node = node.properties + node = libxml2mod.properties(node) while node is not None: - ns = _getNs(node) + ns = libxml2mod.xmlNodeGetNs(node) if ns is not None: - attributes[(ns.content, node.name)] = node + attributes[(libxml2mod.xmlNodeGetContent(ns), libxml2mod.name(node))] = node else: - attributes[(None, node.name)] = node - node = node.next + attributes[(None, libxml2mod.name(node))] = node + node = libxml2mod.next(node) return attributes -def _getNs(node): - - "Internal namespace information retrieval." - - try: - return node.ns() - except libxml2.treeError: - return None - def Node_namespaceURI(node): - ns = _getNs(node) + ns = libxml2mod.xmlNodeGetNs(node) if ns is not None: - return to_unicode(ns.content) + return to_unicode(libxml2mod.xmlNodeGetContent(ns)) else: return None def Node_nodeValue(node): - return to_unicode(node.content) + return to_unicode(libxml2mod.xmlNodeGetContent(node)) def Node_prefix(node): - ns = _getNs(node) + ns = libxml2mod.xmlNodeGetNs(node) if ns is not None: - return to_unicode(ns.name) + return to_unicode(libxml2mod.name(ns)) else: return None @@ -118,29 +110,29 @@ return Node_localName(node) def Node_tagName(node): - if node.type == "element": + if libxml2mod.type(node) == "element": return Node_nodeName(node) else: return None def Node_localName(node): - return to_unicode(node.name) + return to_unicode(libxml2mod.name(node)) def Node_parentNode(node): - if node.type == "document_xml": + if libxml2mod.type(node) == "document_xml": return None else: - return node.parent + return libxml2mod.parent(node) def Node_previousSibling(node): - if node.prev is not None: - return node.prev + if libxml2mod.prev(node) is not None: + return libxml2mod.prev(node) else: return None def Node_nextSibling(node): - if node.next is not None: - return node.next + if libxml2mod.next(node) is not None: + return libxml2mod.next(node) else: return None @@ -151,64 +143,77 @@ return Node_getAttribute(name) is not None def Node_getAttributeNS(node, ns, localName): - return to_unicode(node.nsProp(localName, ns)) + return to_unicode(libxml2mod.xmlGetNsProp(node, localName, ns)) def Node_getAttribute(node, name): - return to_unicode(node.prop(name)) + return to_unicode(libxml2mod.xmlGetProp(node, name)) def Node_getAttributeNodeNS(node, ns, localName): - return node.nsProp(localName, ns) + # NOTE: Needs verifying. + return libxml2mod.xmlGetNsProp(node, localName, ns) def Node_getAttributeNode(node, name): # NOTE: Needs verifying. - return node.prop(name) + return libxml2mod.xmlGetProp(node, name) def Node_setAttributeNS(node, ns, name, value): # NOTE: Need to convert from Unicode. ns, name, value = map(from_unicode, [ns, name, value]) prefix, localName = _get_prefix_and_localName(name) + + # NOTE: Might need to be xmlSetNsProp. if prefix is not None: - node.setNsProp(node.newNs(ns, prefix), localName, value) - elif ns is not None and ns == node.ns().content: - node.setNsProp(node.ns(), localName, value) + libxml2mod.xmlNewNsProp(node, libxml2mod.xmlNewNs(node, ns, prefix), localName, value) + elif ns is not None and ns == libxml2mod.xmlNodeGetContent(libxml2mod.xmlNodeGetNs(node)): + libxml2mod.xmlNewNsProp(node, libxml2mod.xmlNodeGetNs(node), localName, value) else: # NOTE: Needs verifying: what should happen to the namespace? # NOTE: This also catches the case where None is the element's # NOTE: namespace and is also used for the attribute. - node.setNsProp(None, localName, value) + libxml2mod.xmlNewNsProp(node, None, localName, value) def Node_setAttribute(node, name, value): # NOTE: Need to convert from Unicode. name, value = map(from_unicode, [name, value]) - node.setProp(name, value) + libxml2mod.xmlSetProp(node, name, value) + +def _add_node(node, tmp): + if tmp.ns is not None: + child = libxml2mod.xmlNewNsProp(node, None, Node_localName(tmp), None) + ns = libxml2mod.xmlNewNs(child, Node_namespaceURI(tmp), Node_prefix(tmp)) + libxml2mod.xmlNodeSetNs(child, ns) + else: + child = libxml2mod.xmlNewProp(node, libxml2mod.name(tmp)) + + return child def Node_setAttributeNodeNS(node, ns, name, attr): # NOTE: Not actually putting the node on the element. - Node_setAttributeNS(node, ns, name, Node_nodeValue(attr)) + Node_setAttributeNS(node, ns, name, attr.nodeValue) # Node_nodeValue(attr) def Node_setAttributeNode(node, name, attr): # NOTE: Not actually putting the node on the element. - Node_setAttribute(node, name, Node_nodeValue(attr)) + Node_setAttribute(node, name, attr.nodeValue) # Node_nodeValue(attr) def Node_createElementNS(node, ns, name): # NOTE: Need to convert from Unicode. ns, name = map(from_unicode, [ns, name]) prefix, localName = _get_prefix_and_localName(name) - new_node = libxml2.newNode(localName) + new_node = libxml2mod.xmlNewNode(localName) # NOTE: Does it make sense to set the namespace if it is empty? if ns is not None: - new_ns = new_node.newNs(ns, prefix) - new_node.setNs(new_ns) + new_ns = libxml2mod.xmlNewNs(new_node, ns, prefix) + libxml2mod.xmlSetNs(new_node, new_ns) return new_node def Node_createElement(node, name): # NOTE: Need to convert from Unicode. name = from_unicode(name) - new_node = libxml2.newNode(name) + new_node = libxml2mod.xmlNewNode(name) return new_node def Node_createAttributeNS(node, ns, name): @@ -218,7 +223,7 @@ prefix, localName = _get_prefix_and_localName(name) # NOTE: Does it make sense to set the namespace if it is empty? if ns is not None: - new_ns = new_node.newNs(ns, prefix) + new_ns = libxml2mod.xmlNewNs(new_node, ns, prefix) else: new_ns = None return TemporaryNode(new_ns, localName) @@ -233,50 +238,40 @@ # NOTE: Need to convert from Unicode. value = from_unicode(value) - return libxml2.newText(value) + return libxml2mod.xmlNewText(value) def Node_createComment(node, value): # NOTE: Need to convert from Unicode. value = from_unicode(value) - return libxml2.newComment(value) - -def _add_node(node, tmp): - if tmp.ns is not None: - child = node.newNsProp(None, Node_localName(tmp), None) - ns = child.newNs(Node_namespaceURI(tmp), Node_prefix(tmp)) - child.setNs(ns) - else: - child = node.newProp(None, tmp.name, None) - - return child + return libxml2mod.xmlNewComment(value) def Node_insertBefore(node, tmp, oldNode): if not isinstance(tmp, TemporaryNode): - return oldNode.addPrevSibling(tmp) + return libxml2mod.xmlAddPrevSibling(oldNode, tmp) else: return None def Node_replaceChild(node, tmp, oldNode): if not isinstance(tmp, TemporaryNode): - return oldNode.replaceNode(tmp) + return libxml2mod.xmlReplaceNode(oldNode, tmp) else: return None def Node_appendChild(node, tmp): - return node.addChild(tmp) + return libxml2mod.xmlAddChild(node, tmp) def Node_removeChild(node, child): - child.unlinkNode() + libxml2mod.unlinkNode(child) def Node_xpath(node, expr, variables=None, namespaces=None): - context = Node_ownerDocument(node).xpathNewContext() - context.setContextNode(node) + context = libxml2mod.xmlXPathNewContext(Node_ownerDocument(node)) + libxml2mod.xmlXPathSetContextNode(context, node) # NOTE: Discover namespaces from the node. for prefix, ns in (namespaces or {}).items(): - context.xpathRegisterNs(prefix, ns) + libxml2mod.xmlXPathRegisterNs(context, prefix, ns) # NOTE: May need to tidy up the context. - return context.xpathEval(expr) + return libxml2mod.xmlXPathEval(context, expr) # Utility functions. @@ -285,7 +280,7 @@ def createDocument(namespaceURI, localName, doctype): # NOTE: Fixed to use version 1.0 only. - d = libxml2.newDoc("1.0") + d = libxml2mod.xmlNewDoc("1.0") if localName is not None: root = Node_createElementNS(d, namespaceURI, localName) Node_appendChild(d, root) @@ -300,21 +295,28 @@ def parseFile(s): # NOTE: Switching off validation and remote DTD resolution. - context = libxml2.createFileParserCtxt(s) - context.validate(0) - context.ctxtUseOptions(0) - context.parseDocument() - return context.doc() + context = libxml2mod.xmlCreateFileParserCtxt(s) + libxml2mod.xmlParserSetValidate(context, 0) + libxml2mod.xmlCtxtUseOptions(context, 0) + libxml2mod.xmlParseDocument(context) + return libxml2mod.xmlParserGetDoc(context) def parseString(s): # NOTE: Switching off validation and remote DTD resolution. - context = libxml2.createMemoryParserCtxt(s, len(s)) - context.validate(0) - context.ctxtUseOptions(0) - context.parseDocument() - return context.doc() + context = libxml2mod.xmlCreateMemoryParserCtxt(s, len(s)) + libxml2mod.xmlParserSetValidate(context, 0) + libxml2mod.xmlCtxtUseOptions(context, 0) + libxml2mod.xmlParseDocument(context) + return libxml2mod.xmlParserGetDoc(context) def parseURI(uri): - return libxml2.parseURI(uri) + context = libxml2mod.xmlCreateURLParserCtxt(url) + libxml2mod.xmlParserSetValidate(context, 0) + libxml2mod.xmlCtxtUseOptions(context, 0) + libxml2mod.xmlParseDocument(context) + return libxml2mod.xmlParserGetDoc(context) + +def toFile(doc, s): + libxml2mod.xmlSaveFile(s, doc) # vim: tabstop=4 expandtab shiftwidth=4