1.1 --- a/libxml2dom/macrolib/__init__.py Sat May 14 23:30:46 2005 +0000
1.2 +++ b/libxml2dom/macrolib/__init__.py Sat May 14 23:32:41 2005 +0000
1.3 @@ -2,14 +2,10 @@
1.4
1.5 "DOM macro library for libxml2."
1.6
1.7 -__version__ = "0.1.3"
1.8 +__version__ = "0.2"
1.9
1.10 # Expose all functions here.
1.11
1.12 from libxml2dom.macrolib.macrolib import *
1.13
1.14 -# The following module must be processed by libxml2macro before use.
1.15 -
1.16 -from libxml2dom.macrolib.importnode import Node_importNode
1.17 -
1.18 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/libxml2dom/macrolib/importnode.py Sat May 14 23:30:46 2005 +0000
2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
2.3 @@ -1,37 +0,0 @@
2.4 -#!/usr/bin/env python
2.5 -
2.6 -"""
2.7 -An import node implementation taken from libxml2dom.Node for use with libxml2macro.
2.8 -"""
2.9 -
2.10 -from libxml2dom.macrolib.macrolib import *
2.11 -import xml.dom
2.12 -
2.13 -def importNode(x2_self, x2_node, deep):
2.14 -
2.15 - if x2_node.nodeType == xml.dom.Node.ELEMENT_NODE:
2.16 - x2_imported_element = x2_self.ownerDocument.createElementNS(x2_node.namespaceURI, x2_node.tagName)
2.17 - for x2_value in x2_node.attributes.values():
2.18 - x2_imported_element.setAttributeNS(x2_value.namespaceURI, x2_value.nodeName, x2_value.nodeValue)
2.19 -
2.20 - if deep:
2.21 - for child in x2_node.childNodes:
2.22 - x2_imported_child = x2_self.importNode(child, deep)
2.23 - if x2_imported_child:
2.24 - x2_imported_element.appendChild(x2_imported_child)
2.25 -
2.26 - return x2_imported_element
2.27 -
2.28 - elif x2_node.nodeType == xml.dom.Node.TEXT_NODE:
2.29 - return x2_self.ownerDocument.createTextNode(x2_node.nodeValue)
2.30 -
2.31 - elif x2_node.nodeType == xml.dom.Node.COMMENT_NODE:
2.32 - return x2_self.ownerDocument.createComment(x2_node.data)
2.33 -
2.34 - raise ValueError, x2_node.nodeType
2.35 -
2.36 -# Special hack for the generated code.
2.37 -
2.38 -Node_importNode = importNode
2.39 -
2.40 -# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/libxml2dom/macrolib/macrolib.py Sat May 14 23:30:46 2005 +0000
3.2 +++ b/libxml2dom/macrolib/macrolib.py Sat May 14 23:32:41 2005 +0000
3.3 @@ -95,6 +95,10 @@
3.4 def Node_nodeValue(node):
3.5 return to_unicode(libxml2mod.xmlNodeGetContent(node))
3.6
3.7 +# NOTE: Verify this.
3.8 +
3.9 +Node_data = Node_nodeValue
3.10 +
3.11 def Node_prefix(node):
3.12 ns = libxml2mod.xmlNodeGetNs(node)
3.13 if ns is not None:
3.14 @@ -264,6 +268,50 @@
3.15 def Node_removeChild(node, child):
3.16 libxml2mod.unlinkNode(child)
3.17
3.18 +def Node_importNode(node, other, deep):
3.19 + if Node_nodeType(other) == xml.dom.Node.ELEMENT_NODE:
3.20 + imported_element = Node_createElementNS(node, Node_namespaceURI(other), Node_tagName(other))
3.21 + for attr in Node_attributes(other).values():
3.22 + Node_setAttributeNS(imported_element, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr))
3.23 +
3.24 + if deep:
3.25 + for child in Node_childNodes(other):
3.26 + imported_child = Node_importNode(node, child, deep)
3.27 + if imported_child:
3.28 + Node_appendChild(imported_element, imported_child)
3.29 +
3.30 + return imported_element
3.31 +
3.32 + elif Node_nodeType(other) == xml.dom.Node.TEXT_NODE:
3.33 + return Node_createTextNode(node, Node_nodeValue(other))
3.34 +
3.35 + elif Node_nodeType(other) == xml.dom.Node.COMMENT_NODE:
3.36 + return Node_createComment(node, Node_data(other))
3.37 +
3.38 + raise ValueError, Node_nodeType(other)
3.39 +
3.40 +def Node_importNode_DOM(node, other, deep):
3.41 + if other.nodeType == xml.dom.Node.ELEMENT_NODE:
3.42 + imported_element = Node_createElementNS(node, other.namespaceURI, other.tagName)
3.43 + for attr in other.attributes.values():
3.44 + Node_setAttributeNS(imported_element, attr.namespaceURI, attr.nodeName, attr.nodeValue)
3.45 +
3.46 + if deep:
3.47 + for child in other.childNodes:
3.48 + imported_child = Node_importNode_DOM(node, child, deep)
3.49 + if imported_child:
3.50 + Node_appendChild(imported_element, imported_child)
3.51 +
3.52 + return imported_element
3.53 +
3.54 + elif other.nodeType == xml.dom.Node.TEXT_NODE:
3.55 + return Node_createTextNode(node, other.nodeValue)
3.56 +
3.57 + elif other.nodeType == xml.dom.Node.COMMENT_NODE:
3.58 + return Node_createComment(node, other.data)
3.59 +
3.60 + raise ValueError, other.nodeType
3.61 +
3.62 def Node_xpath(node, expr, variables=None, namespaces=None):
3.63 context = libxml2mod.xmlXPathNewContext(Node_ownerDocument(node))
3.64 # NOTE: Discover namespaces from the node.
3.65 @@ -315,7 +363,13 @@
3.66 libxml2mod.xmlParseDocument(context)
3.67 return libxml2mod.xmlParserGetDoc(context)
3.68
3.69 -def toFile(doc, s):
3.70 - libxml2mod.xmlSaveFile(s, doc)
3.71 +def toString(node, encoding=None):
3.72 + return libxml2mod.serializeNode(node, encoding, 0)
3.73 +
3.74 +def toStream(node, stream, encoding=None):
3.75 + stream.write(toString(node, encoding))
3.76 +
3.77 +def toFile(node, f, encoding=None):
3.78 + libxml2mod.saveNodeTo(node, f, encoding, 0)
3.79
3.80 # vim: tabstop=4 expandtab shiftwidth=4