[Zope3-checkins] CVS: zopeproducts/xml/dom - adapters.py:1.3

Philipp von Weitershausen philikon@philikon.de
Mon, 23 Jun 2003 14:59:16 -0400


Update of /cvs-repository/zopeproducts/xml/dom
In directory cvs.zope.org:/tmp/cvs-serv28915

Modified Files:
	adapters.py 
Log Message:
The source attribute of the NodeToXMLTextAdapter is now writable. The XML
string to which it is set is parsed and used to replace to the node that is
being adapted. Added unit tests for that.


=== zopeproducts/xml/dom/adapters.py 1.2 => 1.3 ===
--- zopeproducts/xml/dom/adapters.py:1.2	Mon Jun 23 01:02:26 2003
+++ zopeproducts/xml/dom/adapters.py	Mon Jun 23 14:58:45 2003
@@ -17,6 +17,7 @@
 $Id$
 """
 
+import xml.dom
 from StringIO import StringIO
 
 from zope.interface import implements
@@ -26,6 +27,8 @@
 from zopeproducts.xml.interfaces.dom.core import INode, IDocument
 from printer import Printer
 
+__metaclass__ = type
+
 class NodeToXMLTextAdapter:
 
     implements(IXMLText)
@@ -41,7 +44,33 @@
         out.seek(0)
         return out.read()
 
-    source = property(_get_source)
+    def _set_source(self, source):
+        node = self.context
+        parent = node.parentNode
+        if parent is None:
+            # our context is a document node. we only work on the
+            # document element, though, so do a "parameter shift"
+            parent, node = node, node.firstChild
+        sibling = node.nextSibling
+        stream = StringIO(source)
+        fragment = expatbuilder.parseFragment(stream, parent, True)
+
+        if fragment.childNodes.length != 1:
+            # we could do this, actually, if we wanted
+            raise xml.dom.HierarchyRequestErr(
+                "replacing a Node with less or more than one tree of "\
+                "Nodes is not implemented"
+                )
+
+        parent.removeChild(node)
+        newnode = parent.insertBefore(fragment.firstChild, sibling)
+
+        # update our own context unless we were dealing with a
+        # document node
+        if parent.nodeType != xml.dom.Node.DOCUMENT_NODE:
+            self.context = newnode
+
+    source = property(_get_source, _set_source)
 
 class XMLTextToDocumentAdapterFactory: