package org.ibex.xt;
+import java.io.Serializable;
import java.io.StringReader;
import java.io.Writer;
import java.io.OutputStream;
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
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); }
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);
// 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
* 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) {
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) {
//#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
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.");
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) {
// 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); }
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;
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); }
}
}
- 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");
}
}
}
- 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 />
}
}
+ 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); }