seperate JSScope and update for new Tree interfaces
authorcrawshaw <crawshaw@ibex.org>
Thu, 25 Nov 2004 13:44:29 +0000 (13:44 +0000)
committercrawshaw <crawshaw@ibex.org>
Thu, 25 Nov 2004 13:44:29 +0000 (13:44 +0000)
darcs-hash:20041125134429-2eb37-e1b38846685aed09549b4bdcefe2202891cd8f1e.gz

src/java/org/ibex/xt/JSLeaf.java [moved from src/java/org/ibex/xt/JSElement.java with 61% similarity]
src/java/org/ibex/xt/Servlet.java
src/java/org/ibex/xt/Template.java

similarity index 61%
rename from src/java/org/ibex/xt/JSElement.java
rename to src/java/org/ibex/xt/JSLeaf.java
index 35ee51e..2def4c1 100644 (file)
@@ -1,5 +1,6 @@
 package org.ibex.xt;
 
+import java.io.Serializable;
 import java.io.StringReader;
 import java.io.Writer;
 import java.io.OutputStream;
@@ -11,13 +12,13 @@ import org.ibex.js.JS;
 import org.ibex.js.JSScope;
 import org.ibex.js.JSExn;
 
-public class JSElement extends JSScope implements XML.Element {
-    protected XML.Element wrapped;
+public class JSLeaf implements Tree.Leaf, Serializable {
+    private transient JSScope scope = null;
+    protected Tree.Leaf wrapped;
 
-    /** Creates an Element around <tt>wrapped</tt>, replacing
-     *  references to it in its parent and children with this object. */
-    public JSElement(XML.Element wrapped) {
-        super(findScope(wrapped));
+    /** Creates a Leaf around <tt>wrapped</tt>, replacing
+     *  references to it in its parent with this object. */
+    public JSLeaf(Tree.Leaf wrapped) {
         this.wrapped = wrapped;
 
         // remap parent and children
@@ -25,8 +26,16 @@ public class JSElement extends JSScope implements XML.Element {
             List c = wrapped.getParent().getChildren();
             c.set(c.indexOf(wrapped), this);
         }
-        List c = wrapped.getChildren();
-        for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).setParent(this);
+    }
+
+    public JSScope scope() {
+        if (scope == null) {
+            Tree.Leaf e = wrapped;
+            while (e != null && !(e instanceof JSLeaf)) e = e.getParent();
+            scope = new JSScope(e == null ? null : ((JSLeaf)e).scope());
+        }
+
+        return scope;
     }
 
     public void out(OutputStream o) throws IOException { wrapped.out(o); }
@@ -55,10 +64,10 @@ public class JSElement extends JSScope implements XML.Element {
         return ret.toString();
     }
 
-    public Object exec(String s) {
+    protected Object exec(String s) {
         try {
             return JS.eval(JS.cloneWithNewParentScope(
-                           JS.fromReader("input", 0, new StringReader(s)), this));
+                           JS.fromReader("input", 0, new StringReader(s)), scope()));
         } catch (IOException e) {
             e.printStackTrace();
             throw new Exn("error parsing script", e);
@@ -69,37 +78,46 @@ public class JSElement extends JSScope implements XML.Element {
 
     // Pass Through ///////////////////////////////////////////////////////////
 
-    public void setParent(Tree.Node p)    { wrapped.setParent(p); }
-    public Tree.Node getParent()          { return wrapped.getParent(); }
-    public XML.Attributes getAttributes() { return wrapped.getAttributes(); }
-    public XML.Prefixes getPrefixes()     { return wrapped.getPrefixes(); }
-    public List getChildren()             { return wrapped.getChildren(); }
-    public String getQName()              { return wrapped.getQName(); }
-    public String getLocalName()          { return wrapped.getLocalName(); }
-    public String getPrefix()             { return wrapped.getPrefix(); }
-    public String getUri()                { return wrapped.getUri(); }
-
-    /** Works up the Element object model until an instance of a JSScope is found. */
-    private static JSScope findScope(Tree.Node e) {
-        while (e != null && !(e instanceof JSScope)) e = e.getParent();
-        return (JSScope)e;
+    public void setParent(Tree.Node p) { wrapped.setParent(p); }
+    public Tree.Node getParent()       { return wrapped.getParent(); }
+
+    public static class Node extends JSLeaf implements Tree.Node {
+        public Node(Tree.Node wrapped) {
+            super(wrapped);
+            List c = wrapped.getChildren();
+            for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).setParent(this);
+        }
+
+        public List getChildren() { return ((Tree.Node)wrapped).getChildren(); }
+    }
+
+    public static class Element extends Node implements Tree.Element {
+        public Element(Tree.Element wrapped) { super(wrapped); }
+
+        public Tree.Attributes getAttributes() { return ((Tree.Element)wrapped).getAttributes(); }
+        public Tree.Prefixes getPrefixes()     { return ((Tree.Element)wrapped).getPrefixes(); }
+
+        public String getQName()     { return ((Tree.Element)wrapped).getQName(); }
+        public String getLocalName() { return ((Tree.Element)wrapped).getLocalName(); }
+        public String getPrefix()    { return ((Tree.Element)wrapped).getPrefix(); }
+        public String getUri()       { return ((Tree.Element)wrapped).getUri(); }
     }
 
-    /** A JSElement with the element attributes merged with a second
+    /** A JSLeaf.Element with the element attributes merged with a second
      *  element.
      *
-     *  All functions of the XML.Element interface are mapped onto the
+     *  All functions of the Tree.Element interface are mapped onto the
      *  primary element, except <tt>getAttributes()</tt>. This function
      *  returns a MergedAttr instance with the <b>secondary</b> element
      *  acting as the primary attribute source.
      */
-    public static class Merge extends JSElement {
-        private final XML.Attributes a;
-        public Merge(XML.Element wrapped, XML.Element merge) {
+    public static class Merge extends Element {
+        private final Tree.Attributes a;
+        public Merge(Tree.Element wrapped, Tree.Element merge) {
             super(wrapped);
             a = new MergeAttr(merge.getAttributes(), wrapped.getAttributes());
         }
-        public XML.Attributes getAttributes() { return a; }
+        public Tree.Attributes getAttributes() { return a; }
     }
 
     /** Creates a single view onto two sets of Attributes, first
@@ -107,11 +125,13 @@ public class JSElement extends JSScope implements XML.Element {
      *  otherwise returning any matching entry in the
      *  <tt>secondary</tt> Attributes object.
      *
-     *  FIXME: toXML() produces invalid XML if qname in both a and b.
+     *  FIXME: out(Writer) produces invalid XML if qname in both a and b.
+     *         create org.ibex.util.XMLHelper and have a proper version of
+     *         this as a subclass.
      */
-    public static final class MergeAttr implements XML.Attributes {
-        private final XML.Attributes a, b;
-        public MergeAttr(XML.Attributes primary, XML.Attributes secondary) {
+    public static final class MergeAttr implements Tree.Attributes {
+        private final Tree.Attributes a, b;
+        public MergeAttr(Tree.Attributes primary, Tree.Attributes secondary) {
             a = primary; b = secondary;
         }
         public int getIndex(String qname) {
index c05101a..1a7ae61 100644 (file)
@@ -50,7 +50,7 @@ public class Servlet extends HttpServlet {
             }
         } catch (Template.Signal s) {
             // exit silently
-        } catch (JSElement.Exn e) {
+        } catch (JSLeaf.Exn e) {
             w.print("\n"+src+": ");
             w.println(e.getMessage());
             System.out.println(e);
index 7f75bd2..c30ece6 100644 (file)
@@ -14,15 +14,18 @@ import java.util.*;
 import org.ibex.util.*;
 import org.ibex.js.*;
 
-public class Template extends JSElement {
+public class Template extends JSLeaf.Element {
     public static Template parse(String path, Template.Scope s) throws FileNotFoundException, IOException {
         Reader xmlreader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
-        XML.Document doc = new XML.Document();
+        XML.Document doc = new XML.Document(); // FIXME: switch to Tree.Stream
         doc.parse(xmlreader);
         return new Template(doc.getRoot(), s);
     }
 
-    public static XML.Element wrap(XML.Element e, Template.Scope s) throws IOException {
+    public static Tree.Leaf wrap(Tree.Leaf leaf, Template.Scope s) throws IOException {
+        if (!(leaf instanceof Tree.Element)) return new Text(leaf);
+
+        Tree.Element e = (Tree.Element)leaf;
         final String uri = e.getUri();
 
         if (uri == null) {
@@ -40,7 +43,7 @@ public class Template extends JSElement {
             //#switch(uri.substring(19))
             case "io": System.out.println("ibex.xt.io not yet implemented"); // TODO
             //#end
-            //throw new JSElement.Exn("Unknown XT library: "+uri);
+            //throw new JSLeaf.Exn("Unknown XT library: "+uri);
 
         } else if (uri.startsWith("local:")) {
             // merge a new template into this tree
@@ -50,8 +53,8 @@ public class Template extends JSElement {
             List c = e.getChildren();
             if (c.size() > 0) {
                 // move all children from e to placeholder
-                XML.Element placeholder = findPlaceholder(t);
-                if (placeholder == null) throw new JSElement.Exn(
+                Tree.Element placeholder = findPlaceholder(t);
+                if (placeholder == null) throw new JSLeaf.Exn(
                     "<"+e.getQName()+"> attempted to include children into a " +
                     "template which does not contain an <xt:children /> tag.");
 
@@ -59,7 +62,7 @@ public class Template extends JSElement {
                 e.getChildren().clear();
             }
 
-            XML.Element merged = new JSElement.Merge(t, e);
+            Tree.Element merged = new JSLeaf.Merge(t, e);
 
             // remap the parent of the original element
             if (e.getParent() != null) {
@@ -74,41 +77,41 @@ public class Template extends JSElement {
 
         // wrap children
         List c = e.getChildren();
-        for (int i=0; i < c.size(); i++)
-            if (c.get(i) instanceof XML.Element) wrap((XML.Element)c.get(i), s);
+        for (int i=0; i < c.size(); i++) wrap((Tree.Leaf)c.get(i), s);
 
         return e;
     }
 
     /** Returns the first Template.Children child found. */
-    private static XML.Element findPlaceholder(XML.Element e) {
+    private static Tree.Element findPlaceholder(Tree.Element e) {
         if ("http://xt.ibex.org/".equals(e.getUri()) && "children".equals(e.getLocalName()))
             return e;
 
         List c = e.getChildren();
         for (int i=0; i < c.size(); i++) {
-            if (!(c.get(i) instanceof XML.Element)) continue;
-            XML.Element ret = findPlaceholder((XML.Element)c.get(i));
+            if (!(c.get(i) instanceof Tree.Element)) continue;
+            Tree.Element ret = findPlaceholder((Tree.Element)c.get(i));
             if (ret != null) return ret;
         }
         return null;
     }
 
-    private Template.Scope tscope;
+    private transient Template.Scope tscope;
+    private transient JSScope scope = null;
 
-    public Template(XML.Element w, Template.Scope t) { super(w); tscope = t; }
+    public Template(Tree.Element w, Template.Scope t) { super(w); tscope = t; }
 
-    public JSScope getParentScope() { return tscope; }
+    public JSScope scope() { return scope == null ? scope = new JSScope(tscope) : scope; }
 
 
     /** Processes ${...} blocks in attributes, loads applicable
      *  attributes into the JS scope and processes global attributes. */
-    public static class AttributeEval extends JSElement implements XML.Attributes {
-        protected XML.Attributes a;
+    public static class AttributeEval extends JSLeaf.Element implements Tree.Attributes {
+        protected Tree.Attributes a;
 
-        public AttributeEval(XML.Element wrapped) { super(wrapped); a = wrapped.getAttributes(); }
+        public AttributeEval(Tree.Element wrapped) { super(wrapped); a = wrapped.getAttributes(); }
 
-        public XML.Attributes getAttributes() { return this; }
+        public Tree.Attributes getAttributes() { return this; }
 
         public int getIndex(String q) { return a.getIndex(q); }
         public int getIndex(String u, String k) { return a.getIndex(u, k); }
@@ -121,7 +124,7 @@ public class Template extends JSElement {
 
         public void out(Writer w) throws IOException {
             try {
-                // FIXME: questionable abuse of XML namespaces here
+                // FIXME: questionable abuse of namespaces here
                 boolean xturi = "http://xt.ibex.org/".equals(getUri());
                 for(int i=0; i < a.attrSize(); i++) {
                     if (!xturi && !"http://xt.ibex.org/".equals(a.getUri(i))) continue;
@@ -130,16 +133,16 @@ public class Template extends JSElement {
                     case "if": if (!"true".equals(eval(a.getVal(i)))) return;
                     case "declare":
                         Object d = eval(a.getVal(i));
-                        if (!(d instanceof String)) throw new JSElement.Exn(
+                        if (!(d instanceof String)) throw new JSLeaf.Exn(
                             "attribute "+getPrefix()+":declare can only contain a "+
                             "space seperated list of variable names to declare.");
                         StringTokenizer st = new StringTokenizer((String)d, " ");
-                        while (st.hasMoreTokens()) declare(st.nextToken());
+                        while (st.hasMoreTokens()) scope().declare(st.nextToken());
                         continue;
                     //#end
 
-                    declare(a.getKey(i));
-                    put(a.getKey(i), eval(a.getVal(i)));
+                    scope().declare(a.getKey(i));
+                    scope().put(a.getKey(i), eval(a.getVal(i)));
                 }
             } catch (JSExn e) { throw new Exn(e); }
 
@@ -147,12 +150,12 @@ public class Template extends JSElement {
         }
     }
 
-    public static final class JSTag extends JSElement {
-        public JSTag(XML.Element e) {
+    public static final class JSTag extends JSLeaf.Element {
+        public JSTag(Tree.Element e) {
             super(e);
             List c = getChildren();
             for (int i=0; i < c.size(); i++)
-                if (c.get(i) instanceof XML.Element) throw new JSElement.Exn(
+                if (c.get(i) instanceof Tree.Element) throw new JSLeaf.Exn(
                     "<"+getPrefix()+":js> tags may not have child elements");
         }
 
@@ -164,61 +167,62 @@ public class Template extends JSElement {
         }
     }
 
-    public static final class ForEach extends JSElement {
-        public ForEach(XML.Element e) { super(e); }
+    public static final class ForEach extends JSLeaf.Element {
+        public ForEach(Tree.Element e) { super(e); }
 
         public void out(Writer w) throws IOException {
             try {
-                Object varIn = get("in"); if (varIn != null) undeclare("in");
-                Object varPut = get("put"); if (varPut != null) undeclare("put");
+                JSScope s = scope();
+                Object varIn = s.get("in"); if (varIn != null) s.undeclare("in");
+                Object varPut = s.get("put"); if (varPut != null) s.undeclare("put");
 
                 varIn = exec("return (" + varIn + ");");
-                if (varIn == null || (varIn instanceof JSArray)) throw new JSElement.Exn(
+                if (varIn == null || (varIn instanceof JSArray)) throw new JSLeaf.Exn(
                     "<"+getPrefix()+":foreach> requires attribute 'in' to specify " +
                     "the name of a valid js array in the current scope, not in='"+varIn+"'.");
 
                 if (varPut == null) varPut = "x";
-                else if (!(varPut instanceof String) || get(varPut) != null)
-                    throw new JSElement.Exn(
+                else if (!(varPut instanceof String) || s.get(varPut) != null)
+                    throw new JSLeaf.Exn(
                     "<"+getPrefix()+":foreach> 'put' attribute requires the name of "+
                     "an undeclared variable, not put='"+varPut+"'.");
-                if (get(varPut) != null) throw new JSElement.Exn(
+                if (scope().get(varPut) != null) throw new JSLeaf.Exn(
                     "<"+getPrefix()+":foreach> has no 'put' attribute defined and the "+
                     "default variable 'x' already exists in the current scope.");
 
                 List c = getChildren();
 
-                declare((String)varPut);
+                s.declare((String)varPut);
                 Iterator it = ((JSArray)varIn).toList().iterator(); while (it.hasNext()) {
-                    put(varPut, it.next());
+                    s.put(varPut, it.next());
                     for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(w);
                 }
-            } catch (JSExn e) { throw new JSElement.Exn(e); }
+            } catch (JSExn e) { throw new JSLeaf.Exn(e); }
         }
     }
 
-    public static final class Children extends JSElement {
-        public Children(XML.Element e) { super(e); }
+    public static final class Children extends JSLeaf.Element {
+        public Children(Tree.Element e) { super(e); }
     }
 
-    public static final class Redirect extends JSElement {
-        public Redirect(XML.Element e) { super(e); }
+    public static final class Redirect extends JSLeaf.Element {
+        public Redirect(Tree.Element e) { super(e); }
 
         public void out(Writer w) throws IOException {
             try {
-                Object p = get("page"); if (p != null) undeclare("page");
+                Object p = scope().get("page"); if (p != null) scope().undeclare("page");
                 if (p == null || !(p instanceof String) || ((String)p).trim().equals(""))
-                    throw new JSElement.Exn("<"+getPrefix()+":redirect> requires 'page' "+
+                    throw new JSLeaf.Exn("<"+getPrefix()+":redirect> requires 'page' "+
                                             "attribute to be a valid template path");
                 throw new RedirectSignal((String)p);
-            } catch (JSExn e) { throw new JSElement.Exn(e); }
+            } catch (JSExn e) { throw new JSLeaf.Exn(e); }
         }
     }
 
     // TODO: finish
-    public static final class Transaction extends JSElement {
+    public static final class Transaction extends JSLeaf.Element {
         private final Template.Scope scope; // FIXME: HACK. unstatisise all tags, or do this to all
-        public Transaction(XML.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids
+        public Transaction(Tree.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids
 
         public void out(Writer w) throws IOException {
             // TODO: <xt:use />
@@ -231,6 +235,16 @@ public class Template extends JSElement {
         }
     }
 
+    public static final class Text extends JSLeaf {
+        public Text(Tree.Leaf w) { super(w); }
+        public void out(Writer w) throws IOException {
+            // FIXME: make eval() take a writer
+            StringWriter sw = new StringWriter();
+            super.out(sw);
+            w.write((String)eval(sw.toString()));
+        }
+    }
+
     public abstract static class Scope extends JSScope {
         public Scope(JSScope j) { super(j); }