tons of changes
[org.ibex.xt.git] / src / org / ibex / xt / Node.java
index f4fe0a4..b4c6026 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
 package org.ibex.xt;
 import org.ibex.js.*;
 import org.ibex.util.*;
@@ -13,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); }
@@ -105,34 +109,54 @@ 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.Pull xml;
-            private XML.Element parent = null;
-            private XML.Element e;
+            private final XML.Stream xml;
+            private XML.Elem parent = null;
+            private XML.Elem e;
             private int currentdelta = 0;
-            public FromXML(Reader r) { this.xml = new XML.Pull(r); }
+            public FromXML(Reader r) { this.xml = new XML.Stream(r); }
             protected boolean _read(Node n) { try {
-                Object ret = xml.read();
+                Object ret = xml.next();
                 if (ret == null) return false;
-                if (ret instanceof String) {
-                    n.cdata = (String)ret;
-                    n.delta = xml.level - currentdelta;
-                    currentdelta = xml.level;
+                if (ret instanceof XML.Text) {
+                    n.cdata = ((XML.Text)ret).t;
+                    n.delta = xml.getDepth() - currentdelta;
+                    currentdelta = xml.getDepth();
                     return true;
                 }
-                XML.Element e = (XML.Element)ret;
+                XML.Elem e = (XML.Elem)ret;
                 n.name = e.getLocalName();
                 n.uri = e.getUri();
-                n.numattrs = e.getAttrLen();
-                n.delta = e.level - currentdelta;
-                currentdelta = e.level;
+                n.numattrs = e.getAttributes().attrSize();
+                n.delta = xml.getDepth() - currentdelta;
+                currentdelta = xml.getDepth();
                 if (n.attrs == null || n.attrs.length < n.numattrs*2) n.attrs = new String[n.numattrs*4];
-                for(int i=0; i<n.numattrs; i++) { n.attrs[i*2]   = e.getAttrKey(i); n.attrs[i*2+1] = e.getAttrVal(i); }
+                for(int i=0; i<n.numattrs; i++) {
+                    n.attrs[i*2] = e.getAttributes().getKey(i);
+                    n.attrs[i*2+1] = e.getAttributes().getVal(i);
+                }
                 return true;
             } 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) {
@@ -153,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(">");