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 { throw new UnsupportedOperationException(); }
32 public void out(Writer w) throws IOException {
33 // grab all related attributes
35 XML.Attributes a = getAttributes();
36 for(int i=0; i < a.attrSize(); i++) {
37 if (!"http://xt.ibex.org/".equals(a.getUri(i))) continue;
39 put(a.getKey(i), eval(a.getVal(i)));
41 } catch (Exception e) { throw new RuntimeException(e); }
44 private 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 RuntimeException("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 public Object exec(String s) {
69 return JS.eval(JS.cloneWithNewParentScope(
70 JS.fromReader("input", 0, new StringReader(s)), this));
71 } catch (Exception e) {
73 throw new RuntimeException(e);
77 // Pass Through ///////////////////////////////////////////////////////////
79 public void setParent(Tree.Node p) { wrapped.setParent(p); }
80 public Tree.Node getParent() { return wrapped.getParent(); }
81 public XML.Attributes getAttributes() { return wrapped.getAttributes(); }
82 public XML.Prefixes getPrefixes() { return wrapped.getPrefixes(); }
83 public List getChildren() { return wrapped.getChildren(); }
84 public String getQName() { return wrapped.getQName(); }
85 public String getLocalName() { return wrapped.getLocalName(); }
86 public String getPrefix() { return wrapped.getPrefix(); }
87 public String getUri() { return wrapped.getUri(); }
89 /** Works up the Element object model until an instance of a JSScope is found. */
90 private static JSScope findScope(Tree.Node e) {
91 while (e != null && !(e instanceof JSScope)) e = e.getParent();
95 /** A JSElement with the element attributes merged with a second
98 * All functions of the XML.Element interface are mapped onto the
99 * primary element, except <tt>getAttributes()</tt>. This function
100 * returns a MergedAttr instance with the <b>secondary</b> element
101 * acting as the primary attribute source.
103 public static class Merge extends JSElement {
104 private final XML.Attributes a;
105 public Merge(XML.Element wrapped, XML.Element merge) {
107 a = new MergeAttr(merge.getAttributes(), wrapped.getAttributes());
109 public XML.Attributes getAttributes() { return a; }
112 /** Creates a single view onto two sets of Attributes, first
113 * checking the <tt>primary</tt> array for an entry, or
114 * otherwise returning any matching entry in the
115 * <tt>secondary</tt> Attributes object.
117 * FIXME: toXML() produces invalid XML if qname in both a and b.
119 public static final class MergeAttr implements XML.Attributes {
120 private final XML.Attributes a, b;
121 public MergeAttr(XML.Attributes primary, XML.Attributes secondary) {
122 a = primary; b = secondary;
124 public int getIndex(String qname) {
125 int i = a.getIndex(qname); if (i >= 0) return i;
126 i = b.getIndex(qname); if (i >= 0) return i + a.attrSize();
129 public int getIndex(String uri, String key) {
130 int i = a.getIndex(uri, key); if (i >= 0) return i;
131 i = b.getIndex(uri, key); if (i >= 0) return i + b.attrSize();
134 public String getKey(int i) {
135 return i >= a.attrSize() ? b.getKey(i-a.attrSize()) : a.getKey(i); }
136 public String getVal(int i) {
137 return i >= a.attrSize() ? b.getVal(i-a.attrSize()) : a.getVal(i); }
138 public String getUri(int i) {
139 return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
140 public String getPrefix(int i) {
141 return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
142 public String getQName(int i) {
143 return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
144 public int attrSize() { return a.attrSize() + b.attrSize(); }