3 import java.io.StringReader;
5 import java.io.IOException;
11 import 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.remove(wrapped); c.add(this);
27 List c = wrapped.getChildren();
28 for (int i=c.size(); i >= 0; i--) ((XML.Block)c.get(i)).setParent(this);
31 public void toXML(Writer w) throws IOException {
32 // grab all related attributes
34 XML.Attributes a = getAttributes();
35 for(int i=0; i < a.attrSize(); i++) {
36 if (!"http://xt.ibex.org/".equals(a.getUri(i))) continue;
38 put(a.getKey(i), eval(a.getVal(i)));
40 } catch (Exception e) { throw new RuntimeException(e); }
43 private Object eval(String s) {
44 if (s == null) return null;
45 StringBuffer ret = new StringBuffer();
46 while (s.indexOf("${") != -1) {
47 ret.append(s.substring(0, s.indexOf("${")));
48 String s2 = s.substring(s.indexOf("${")+2);
49 Object app = exec("return (" + s2.substring(0, s2.indexOf('}')) + ");\n");
50 s = s.substring(s.indexOf('}') + 1);
53 app instanceof String ||
54 app instanceof Number ||
55 app instanceof Boolean))
56 throw new RuntimeException("javascripts within ${...} can only return " +
57 "strings, numbers, and booleans; not a " +
58 app.getClass().getName());
60 ret.append(app == null ? "null" : app.toString());
63 return ret.toString();
66 public Object exec(String s) {
68 return JS.eval(JS.cloneWithNewParentScope(
69 JS.fromReader("input", 0, new StringReader(s)), this));
70 } catch (Exception e) {
72 throw new RuntimeException(e);
76 // Pass Through ///////////////////////////////////////////////////////////
78 public void setParent(XML.Element p) { wrapped.setParent(p); }
79 public XML.Element getParent() { return wrapped.getParent(); }
80 public XML.Attributes getAttributes() { return wrapped.getAttributes(); }
81 public XML.Prefixes getPrefixes() { return wrapped.getPrefixes(); }
82 public List getChildren() { return wrapped.getChildren(); }
83 public String getQName() { return wrapped.getQName(); }
84 public String getLocalName() { return wrapped.getLocalName(); }
85 public String getPrefix() { return wrapped.getPrefix(); }
86 public String getUri() { return wrapped.getUri(); }
88 /** Works up the Element object model until an instance of a JSScope is found. */
89 private static JSScope findScope(XML.Element e) {
90 while (e != null && !(e instanceof JSScope)) e = e.getParent();
94 /** A JSElement with the element attributes merged with a second
97 * All functions of the XML.Element interface are mapped onto the
98 * primary element, except <tt>getAttributes()</tt>. This function
99 * returns a MergedAttr instance with the <b>secondary</b> element
100 * acting as the primary attribute source.
102 public static class Merge extends JSElement {
103 private final XML.Attributes a;
104 public Merge(XML.Element wrapped, XML.Element merge) {
106 a = new MergeAttr(merge.getAttributes(), wrapped.getAttributes());
108 public XML.Attributes getAttributes() { return a; }
111 /** Creates a single view onto two sets of Attributes, first
112 * checking the <tt>primary</tt> array for an entry, or
113 * otherwise returning any matching entry in the
114 * <tt>secondary</tt> Attributes object.
116 * FIXME: toXML() produces invalid XML if qname in both a and b.
118 public static final class MergeAttr implements XML.Attributes {
119 private final XML.Attributes a, b;
120 public MergeAttr(XML.Attributes primary, XML.Attributes secondary) {
121 a = primary; b = secondary;
123 public int getIndex(String qname) {
124 int i = a.getIndex(qname); if (i >= 0) return i;
125 i = b.getIndex(qname); if (i >= 0) return i + a.attrSize();
128 public int getIndex(String uri, String key) {
129 int i = a.getIndex(uri, key); if (i >= 0) return i;
130 i = b.getIndex(uri, key); if (i >= 0) return i + b.attrSize();
133 public String getKey(int i) {
134 return i >= a.attrSize() ? b.getKey(i-a.attrSize()) : a.getKey(i); }
135 public String getVal(int i) {
136 return i >= a.attrSize() ? b.getVal(i-a.attrSize()) : a.getVal(i); }
137 public String getUri(int i) {
138 return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
139 public String getPrefix(int i) {
140 return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
141 public String getQName(int i) {
142 return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
143 public int attrSize() { return a.attrSize() + b.attrSize(); }