3 import java.io.Serializable;
4 import java.io.StringReader;
6 import java.io.OutputStream;
7 import java.io.IOException;
10 import org.ibex.util.*;
11 import org.ibex.js.JS;
12 import org.ibex.js.JSScope;
13 import org.ibex.js.JSExn;
15 public class JSLeaf implements Tree.Leaf, Serializable {
16 private transient JSScope scope = null;
17 protected Tree.Leaf wrapped;
19 /** Creates a Leaf around <tt>wrapped</tt>, replacing
20 * references to it in its parent with this object. */
21 public JSLeaf(Tree.Leaf wrapped) {
22 this.wrapped = wrapped;
24 // remap parent and children
25 if (wrapped.getParent() != null) {
26 List c = wrapped.getParent().getChildren();
27 c.set(c.indexOf(wrapped), this);
31 public JSScope scope() {
33 Tree.Leaf e = wrapped;
34 while (e != null && !(e instanceof JSLeaf)) e = e.getParent();
35 scope = new JSScope(e == null ? null : ((JSLeaf)e).scope());
41 public void out(OutputStream o) throws IOException { wrapped.out(o); }
42 public void out(Writer w) throws IOException { wrapped.out(w); }
44 protected Object eval(String s) {
45 if (s == null) return null;
46 StringBuffer ret = new StringBuffer();
47 while (s.indexOf("${") != -1) {
48 ret.append(s.substring(0, s.indexOf("${")));
49 String s2 = s.substring(s.indexOf("${")+2);
50 Object app = exec("return (" + s2.substring(0, s2.indexOf('}')) + ");\n");
51 s = s.substring(s.indexOf('}') + 1);
54 app instanceof String ||
55 app instanceof Number ||
56 app instanceof Boolean))
57 throw new Exn("javascripts within ${...} can only return " +
58 "strings, numbers, and booleans; not a " +
59 app.getClass().getName());
61 ret.append(app == null ? "null" : app.toString());
64 return ret.toString();
67 protected Object exec(String s) {
69 return JS.eval(JS.cloneWithNewParentScope(
70 JS.fromReader("input", 0, new StringReader(s)), scope()));
71 } catch (IOException e) {
73 throw new Exn("error parsing script", e);
79 // Pass Through ///////////////////////////////////////////////////////////
81 public void setParent(Tree.Node p) { wrapped.setParent(p); }
82 public Tree.Node getParent() { return wrapped.getParent(); }
84 public static class Node extends JSLeaf implements Tree.Node {
85 public Node(Tree.Node wrapped) {
87 List c = wrapped.getChildren();
88 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).setParent(this);
91 public List getChildren() { return ((Tree.Node)wrapped).getChildren(); }
94 public static class Element extends Node implements Tree.Element {
95 public Element(Tree.Element wrapped) { super(wrapped); }
97 public Tree.Attributes getAttributes() { return ((Tree.Element)wrapped).getAttributes(); }
98 public Tree.Prefixes getPrefixes() { return ((Tree.Element)wrapped).getPrefixes(); }
100 public String getQName() { return ((Tree.Element)wrapped).getQName(); }
101 public String getLocalName() { return ((Tree.Element)wrapped).getLocalName(); }
102 public String getPrefix() { return ((Tree.Element)wrapped).getPrefix(); }
103 public String getUri() { return ((Tree.Element)wrapped).getUri(); }
106 /** A JSLeaf.Element with the element attributes merged with a second
109 * All functions of the Tree.Element interface are mapped onto the
110 * primary element, except <tt>getAttributes()</tt>. This function
111 * returns a MergedAttr instance with the <b>secondary</b> element
112 * acting as the primary attribute source.
114 public static class Merge extends Element {
115 private final Tree.Attributes a;
116 public Merge(Tree.Element wrapped, Tree.Element merge) {
118 a = new MergeAttr(merge.getAttributes(), wrapped.getAttributes());
120 public Tree.Attributes getAttributes() { return a; }
123 /** Creates a single view onto two sets of Attributes, first
124 * checking the <tt>primary</tt> array for an entry, or
125 * otherwise returning any matching entry in the
126 * <tt>secondary</tt> Attributes object.
128 * FIXME: out(Writer) produces invalid XML if qname in both a and b.
129 * create org.ibex.util.XMLHelper and have a proper version of
130 * this as a subclass.
132 public static final class MergeAttr implements Tree.Attributes {
133 private final Tree.Attributes a, b;
134 public MergeAttr(Tree.Attributes primary, Tree.Attributes secondary) {
135 a = primary; b = secondary;
137 public int getIndex(String qname) {
138 int i = a.getIndex(qname); if (i >= 0) return i;
139 i = b.getIndex(qname); if (i >= 0) return i + a.attrSize();
142 public int getIndex(String uri, String key) {
143 int i = a.getIndex(uri, key); if (i >= 0) return i;
144 i = b.getIndex(uri, key); if (i >= 0) return i + b.attrSize();
147 public String getKey(int i) {
148 return i >= a.attrSize() ? b.getKey(i-a.attrSize()) : a.getKey(i); }
149 public String getVal(int i) {
150 return i >= a.attrSize() ? b.getVal(i-a.attrSize()) : a.getVal(i); }
151 public String getUri(int i) {
152 return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
153 public String getPrefix(int i) {
154 return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
155 public String getQName(int i) {
156 return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
157 public int attrSize() { return a.attrSize() + b.attrSize(); }
160 public static class Exn extends RuntimeException {
161 public Exn(String cause) { super(cause); }
162 public Exn(JSExn e) { super(e); }
163 public Exn(String msg, Exception e) { super(msg + ": " + e.getMessage()); }
164 public String toString() { return "JSElement.Exn: "+getMessage(); }