2def4c1e86d674c744421527738028e87f7b737c
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / JSLeaf.java
1 package org.ibex.xt;
2
3 import java.io.Serializable;
4 import java.io.StringReader;
5 import java.io.Writer;
6 import java.io.OutputStream;
7 import java.io.IOException;
8 import java.util.*;
9
10 import org.ibex.util.*;
11 import org.ibex.js.JS;
12 import org.ibex.js.JSScope;
13 import org.ibex.js.JSExn;
14
15 public class JSLeaf implements Tree.Leaf, Serializable {
16     private transient JSScope scope = null;
17     protected Tree.Leaf wrapped;
18
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;
23
24         // remap parent and children
25         if (wrapped.getParent() != null) {
26             List c = wrapped.getParent().getChildren();
27             c.set(c.indexOf(wrapped), this);
28         }
29     }
30
31     public JSScope scope() {
32         if (scope == null) {
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());
36         }
37
38         return scope;
39     }
40
41     public void out(OutputStream o) throws IOException { wrapped.out(o); }
42     public void out(Writer w) throws IOException { wrapped.out(w); }
43
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);
52
53             if (!(app == null ||
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());
60
61             ret.append(app == null ? "null" : app.toString());
62         }
63         ret.append(s);
64         return ret.toString();
65     }
66
67     protected Object exec(String s) {
68         try {
69             return JS.eval(JS.cloneWithNewParentScope(
70                            JS.fromReader("input", 0, new StringReader(s)), scope()));
71         } catch (IOException e) {
72             e.printStackTrace();
73             throw new Exn("error parsing script", e);
74         } catch (JSExn e) {
75             throw new Exn(e);
76         }
77     }
78
79     // Pass Through ///////////////////////////////////////////////////////////
80
81     public void setParent(Tree.Node p) { wrapped.setParent(p); }
82     public Tree.Node getParent()       { return wrapped.getParent(); }
83
84     public static class Node extends JSLeaf implements Tree.Node {
85         public Node(Tree.Node wrapped) {
86             super(wrapped);
87             List c = wrapped.getChildren();
88             for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).setParent(this);
89         }
90
91         public List getChildren() { return ((Tree.Node)wrapped).getChildren(); }
92     }
93
94     public static class Element extends Node implements Tree.Element {
95         public Element(Tree.Element wrapped) { super(wrapped); }
96
97         public Tree.Attributes getAttributes() { return ((Tree.Element)wrapped).getAttributes(); }
98         public Tree.Prefixes getPrefixes()     { return ((Tree.Element)wrapped).getPrefixes(); }
99
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(); }
104     }
105
106     /** A JSLeaf.Element with the element attributes merged with a second
107      *  element.
108      *
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.
113      */
114     public static class Merge extends Element {
115         private final Tree.Attributes a;
116         public Merge(Tree.Element wrapped, Tree.Element merge) {
117             super(wrapped);
118             a = new MergeAttr(merge.getAttributes(), wrapped.getAttributes());
119         }
120         public Tree.Attributes getAttributes() { return a; }
121     }
122
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.
127      *
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.
131      */
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;
136         }
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();
140             return -1;
141         }
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();
145             return -1;
146         }
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(); }
158     }
159
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(); }
165     }
166 }