DocBuffer.asnode: Convert null properly.
[jsvc.git] / src / dolda / jsvc / next / DocBuffer.java
index b2d1f0a..b936c0a 100644 (file)
@@ -46,7 +46,9 @@ public class DocBuffer {
        Node c = cursor(cursor);
        if(c == null)
            throw(new RuntimeException("No such cursor: `" + cursor + "'"));
-       c.getParentNode().insertBefore(doc.importNode(n, true), c);
+       if(n.getOwnerDocument() != doc)
+           n = doc.importNode(n, true);
+       c.getParentNode().insertBefore(n, c);
     }
     
     public Element makecursor(String name) {
@@ -69,9 +71,25 @@ public class DocBuffer {
     }
     
     public Text text(String text) {
+       if(text == null)
+           return(null);
        return(doc.createTextNode(text));
     }
     
+    public Node asnode(Object o) {
+       if(o == null)
+           return(text(""));
+       if(o instanceof Node) {
+           Node n = (Node)o;
+           if(n.getOwnerDocument() != doc)
+               return(doc.importNode(n, true));
+           return(n);
+       }
+       if(o instanceof String)
+           return(text((String)o));
+       throw(new RuntimeException("Cannot convert a " + o.getClass().getName() + " to a DOM node"));
+    }
+    
     public void finalise() {
        Node n = doc;
        while(true) {