3 import java.io.StringReader;
5 import java.io.OutputStream;
6 import java.io.IOException;
9 import org.ibex.util.*;
10 import org.ibex.js.JS;
11 import org.ibex.js.JSScope;
13 public class JSElement extends JSScope implements XML.Element {
14 protected XML.Element wrapped;
16 /** Creates an Element around <tt>wrapped</tt>, replacing
17 * references to it in its parent and children with this object. */
18 public JSElement(XML.Element wrapped) {
19 super(findScope(wrapped));
20 this.wrapped = wrapped;
22 // remap parent and children
23 if (wrapped.getParent() != null) {
24 List c = wrapped.getParent().getChildren();
25 c.set(c.indexOf(wrapped), this);
27 List c = wrapped.getChildren();
28 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).setParent(this);
31 public void out(OutputStream o) throws IOException { wrapped.out(o); }
32 public void out(Writer w) throws IOException { wrapped.out(w); }
34 /** Load the attributes into the js scope. */
35 protected void loadAttr() {
37 XML.Attributes a = getAttributes();
38 // FIXME: questionable abuse of XML namespaces here
39 boolean xturi = "http://xt.ibex.org/".equals(getUri());
40 for(int i=0; i < a.attrSize(); i++) {
41 if (!xturi && !"http://xt.ibex.org/".equals(a.getUri(i))) continue;
43 put(a.getKey(i), eval(a.getVal(i)));
45 } catch (Exception e) { throw new RuntimeException(e); }
48 private Object eval(String s) {
49 if (s == null) return null;
50 StringBuffer ret = new StringBuffer();
51 while (s.indexOf("${") != -1) {
52 ret.append(s.substring(0, s.indexOf("${")));
53 String s2 = s.substring(s.indexOf("${")+2);
54 Object app = exec("return (" + s2.substring(0, s2.indexOf('}')) + ");\n");
55 s = s.substring(s.indexOf('}') + 1);
58 app instanceof String ||
59 app instanceof Number ||
60 app instanceof Boolean))
61 throw new RuntimeException("javascripts within ${...} can only return " +
62 "strings, numbers, and booleans; not a " +
63 app.getClass().getName());
65 ret.append(app == null ? "null" : app.toString());
68 return ret.toString();
71 public Object exec(String s) {
73 return JS.eval(JS.cloneWithNewParentScope(
74 JS.fromReader("input", 0, new StringReader(s)), this));
75 } catch (Exception e) {
77 throw new RuntimeException(e);
81 // Pass Through ///////////////////////////////////////////////////////////
83 public void setParent(Tree.Node p) { wrapped.setParent(p); }
84 public Tree.Node getParent() { return wrapped.getParent(); }
85 public XML.Attributes getAttributes() { return wrapped.getAttributes(); }
86 public XML.Prefixes getPrefixes() { return wrapped.getPrefixes(); }
87 public List getChildren() { return wrapped.getChildren(); }
88 public String getQName() { return wrapped.getQName(); }
89 public String getLocalName() { return wrapped.getLocalName(); }
90 public String getPrefix() { return wrapped.getPrefix(); }
91 public String getUri() { return wrapped.getUri(); }
93 /** Works up the Element object model until an instance of a JSScope is found. */
94 private static JSScope findScope(Tree.Node e) {
95 while (e != null && !(e instanceof JSScope)) e = e.getParent();
99 /** A JSElement with the element attributes merged with a second
102 * All functions of the XML.Element interface are mapped onto the
103 * primary element, except <tt>getAttributes()</tt>. This function
104 * returns a MergedAttr instance with the <b>secondary</b> element
105 * acting as the primary attribute source.
107 public static class Merge extends JSElement {
108 private final XML.Attributes a;
109 public Merge(XML.Element wrapped, XML.Element merge) {
111 a = new MergeAttr(merge.getAttributes(), wrapped.getAttributes());
113 public XML.Attributes getAttributes() { return a; }
116 /** Creates a single view onto two sets of Attributes, first
117 * checking the <tt>primary</tt> array for an entry, or
118 * otherwise returning any matching entry in the
119 * <tt>secondary</tt> Attributes object.
121 * FIXME: toXML() produces invalid XML if qname in both a and b.
123 public static final class MergeAttr implements XML.Attributes {
124 private final XML.Attributes a, b;
125 public MergeAttr(XML.Attributes primary, XML.Attributes secondary) {
126 a = primary; b = secondary;
128 public int getIndex(String qname) {
129 int i = a.getIndex(qname); if (i >= 0) return i;
130 i = b.getIndex(qname); if (i >= 0) return i + a.attrSize();
133 public int getIndex(String uri, String key) {
134 int i = a.getIndex(uri, key); if (i >= 0) return i;
135 i = b.getIndex(uri, key); if (i >= 0) return i + b.attrSize();
138 public String getKey(int i) {
139 return i >= a.attrSize() ? b.getKey(i-a.attrSize()) : a.getKey(i); }
140 public String getVal(int i) {
141 return i >= a.attrSize() ? b.getVal(i-a.attrSize()) : a.getVal(i); }
142 public String getUri(int i) {
143 return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
144 public String getPrefix(int i) {
145 return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
146 public String getQName(int i) {
147 return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
148 public int attrSize() { return a.attrSize() + b.attrSize(); }