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