tons of changes
[org.ibex.xt.git] / src / org / ibex / xt / Node.java
index 3f8b2c2..b4c6026 100644 (file)
@@ -17,7 +17,7 @@ public class Node {
     public int      numattrs = 0;
     public String[] attrs = null;
     public String   uri = null;
-    private int     delta = 0;
+    private  int     delta = 0;
 
     public Node() { }
     public Node(Node n) { copyFrom(n); }
@@ -109,6 +109,19 @@ public class Node {
             }
         }
 
+        public static class Join extends Node.Stream {
+            final Node.Stream s1, s2;
+            boolean s1Done = false;
+            public Join(Node.Stream s1, Node.Stream s2) { this.s1=s1; this.s2=s2; }
+            protected boolean _read(Node n) {
+                if (!s1Done) return s2._read(n);
+                boolean ret = s1._read(n);
+                if (ret) return true;
+                s1Done = true;
+                return s2._read(n);
+            }
+        }
+
         public static class FromXML extends Node.Stream {
             private final XML.Stream xml;
             private XML.Elem parent = null;
@@ -118,8 +131,8 @@ public class Node {
             protected boolean _read(Node n) { try {
                 Object ret = xml.next();
                 if (ret == null) return false;
-                if (ret instanceof String) {
-                    n.cdata = (String)ret;
+                if (ret instanceof XML.Text) {
+                    n.cdata = ((XML.Text)ret).t;
                     n.delta = xml.getDepth() - currentdelta;
                     currentdelta = xml.getDepth();
                     return true;
@@ -139,7 +152,11 @@ public class Node {
             } catch (Exception e) { throw new RuntimeException(e); } }
         }
 
-        public void toXML(Writer writer) throws IOException { Node n = new Node(); if (read(n)) toXML(writer, n); }
+        public void toXML(Writer writer) throws IOException {
+            Node n = new Node();
+            do { if (!read(n)) n = null; } while (n!=null && n.cdata != null);
+            toXML(writer, n);
+        }
         private Node toXML(Writer w, Node n) throws IOException {
             final String name = n.name;
             if (n.cdata != null) {
@@ -160,7 +177,7 @@ public class Node {
                     w.write("/>");
                 } else {
                     w.write(">");
-                    while(n != null && n.delta > 0) n = toXML(w, n);
+                    while(n != null && n.delta > 0) {  n = toXML(w, n); }
                     w.write("</");
                     w.write(name);
                     w.write(">");