eval expressions in normal html attributes
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / JSLeaf.java
1 package org.ibex.xt;
2
3 import java.io.Reader;
4 import java.io.Serializable;
5 import java.io.StringReader;
6 import java.io.Writer;
7 import java.io.OutputStream;
8 import java.io.IOException;
9 import java.util.*;
10
11 import org.ibex.util.*;
12 import org.ibex.js.JS;
13 import org.ibex.js.JSScope;
14 import org.ibex.js.JSExn;
15
16 public class JSLeaf implements Tree.Leaf, Serializable {
17     private transient JSScope scope = null;
18     protected Tree.Leaf wrapped;
19
20     /** Creates a Leaf around <tt>wrapped</tt>, replacing
21      *  references to it in its parent with this object. */
22     public JSLeaf(Tree.Leaf wrapped) {
23         this.wrapped = wrapped;
24
25         // remap parent and children
26         if (wrapped.getParent() != null) {
27             List c = wrapped.getParent().getChildren();
28             c.set(c.indexOf(wrapped), this);
29         }
30     }
31
32     public JSScope scope() {
33         if (scope == null) {
34             Tree.Leaf e = wrapped;
35             while (e != null && !(e instanceof JSLeaf)) e = e.getParent();
36             scope = new JSScope(e == null ? null : ((JSLeaf)e).scope());
37         }
38         return scope;
39     }
40
41     public void out(Writer w) throws IOException { wrapped.out(w); }
42     public void out(OutputStream o) throws IOException { wrapped.out(o); }
43
44     protected Object eval(String s) {
45         if (s == null) return null;
46
47         StringBuffer ret = new StringBuffer();
48         int exp, pos = 0;
49         while ((exp = s.indexOf("${", pos)) >= 0) {
50             ret.append(s.substring(pos, exp));
51             pos = s.indexOf("}", exp);
52             Object app = exec("return (" + s.substring(exp + 2, pos) + ");");
53             pos++;
54
55             if (!(app == null || app instanceof String ||
56                                  app instanceof Number ||
57                                  app instanceof Boolean))
58                 throw new Exn("javascripts within ${...} can only return " +
59                               "strings, numbers, and booleans; not a " +
60                               app.getClass().getName());
61             ret.append(app == null ? "null" : app.toString());
62         }
63
64         if (pos < s.length()) ret.append(s.substring(pos));
65         return ret.toString();
66     }
67
68     protected Object exec(String s) {
69         StringBuffer ret = new StringBuffer();
70         int exp, pos = 0;
71         while ((exp = s.indexOf("${", pos)) >= 0) {
72             ret.append(s.substring(pos, exp));
73             pos = s.indexOf("}", exp);
74             ret.append("return (");
75             ret.append(s.substring(exp + 2, pos));
76             ret.append(");");
77             pos++;
78         }
79
80         Reader r = new StringReader(ret.toString());
81         try {
82             return JS.eval(JS.cloneWithNewParentScope(
83                            JS.fromReader("input", 0, r), scope()));
84         } catch (IOException e) {
85             throw new Exn("error parsing script", e);
86         } catch (JSExn e) {
87             throw new Exn(e);
88         }
89     }
90
91     // Pass Through ///////////////////////////////////////////////////////////
92
93     public void setParent(Tree.Node p) { wrapped.setParent(p); }
94     public Tree.Node getParent()       { return wrapped.getParent(); }
95
96     public static class Node extends JSLeaf implements Tree.Node {
97         public Node(Tree.Node wrapped) {
98             super(wrapped);
99             List c = wrapped.getChildren();
100             for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).setParent(this);
101         }
102
103         public List getChildren() { return ((Tree.Node)wrapped).getChildren(); }
104     }
105
106     public static class Element extends Node implements Tree.Element {
107         public Element(Tree.Element wrapped) { super(wrapped); }
108
109         public Tree.Attributes getAttributes() { return ((Tree.Element)wrapped).getAttributes(); }
110         public Tree.Prefixes getPrefixes()     { return ((Tree.Element)wrapped).getPrefixes(); }
111
112         public void setAttributes(Tree.Attributes a) { ((Tree.Element)wrapped).setAttributes(a); }
113         public void setPrefixes(Tree.Prefixes p) { ((Tree.Element)wrapped).setPrefixes(p); }
114
115         public String getQName()     { return ((Tree.Element)wrapped).getQName(); }
116         public String getLocalName() { return ((Tree.Element)wrapped).getLocalName(); }
117         public String getPrefix()    { return ((Tree.Element)wrapped).getPrefix(); }
118         public String getUri()       { return ((Tree.Element)wrapped).getUri(); }
119     }
120
121     /** Creates a single view onto two sets of Attributes, first
122      *  checking the <tt>primary</tt> array for an entry, or
123      *  otherwise returning any matching entry in the
124      *  <tt>secondary</tt> Attributes object.
125      *
126      *  FIXME: out(Writer) produces invalid XML if qname in both a and b.
127      *         create org.ibex.util.XMLHelper and have a proper version of
128      *         this as a subclass.
129      */
130     public static final class MergeAttr implements Tree.Attributes {
131         private final Tree.Attributes a, b;
132         public MergeAttr(Tree.Attributes primary, Tree.Attributes secondary) {
133             a = primary; b = secondary;
134         }
135         public int getIndex(String qname) {
136             int i = a.getIndex(qname); if (i >= 0) return i;
137                 i = b.getIndex(qname); if (i >= 0) return i + a.attrSize();
138             return -1;
139         }
140         public int getIndex(String uri, String key) {
141             int i = a.getIndex(uri, key); if (i >= 0) return i;
142                 i = b.getIndex(uri, key); if (i >= 0) return i + b.attrSize();
143             return -1;
144         }
145         public String getKey(int i) {
146             return i >= a.attrSize() ? b.getKey(i-a.attrSize()) : a.getKey(i); }
147         public String getVal(int i) {
148             return i >= a.attrSize() ? b.getVal(i-a.attrSize()) : a.getVal(i); }
149         public String getUri(int i) {
150             return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
151         public String getPrefix(int i) {
152             return i >= a.attrSize() ? b.getPrefix(i-a.attrSize()) : a.getPrefix(i); }
153         public String getQName(int i) {
154             return i >= a.attrSize() ? b.getQName(i-a.attrSize()) : a.getQName(i); }
155         public int attrSize() { return a.attrSize() + b.attrSize(); }
156     }
157
158     public static class Exn extends RuntimeException {
159         public Exn(String cause) { super(cause); }
160         public Exn(JSExn e) { super(e); }
161         public Exn(String msg, Exception e) { super(msg + ": " + e.getMessage()); }
162         public String toString() { return "JSElement.Exn: "+getMessage(); }
163     }
164 }