initial support for XML.Document
[org.ibex.xt-crawshaw.git] / src / java / ibex / xt / JSElement.java
1 package ibex.xt;
2
3 import ibex.js.JS;
4 import ibex.js.JSScope;
5
6 import ibex.util.*;
7 import java.util.*;
8
9 import java.io.StringReader;
10 import java.io.Writer;
11 import java.io.IOException;
12
13 public class JSElement extends JSScope implements XML.Element {
14     protected XML.Element wrapped;
15
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;
21
22         // remap parent and children
23         if (wrapped.getParent() != null) {
24             List c = wrapped.getParent().getChildren();
25             c.remove(wrapped); c.add(this);
26         }
27         List c = wrapped.getChildren();
28         for (int i=c.size(); i >= 0; i--) ((XML.Block)c.get(i)).setParent(this);
29     }
30
31     public void toXML(Writer w) throws IOException {
32         // grab all related attributes
33         try {
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;
37                 declare(a.getKey(i));
38                 put(a.getKey(i), eval(a.getVal(i)));
39             }
40         } catch (Exception e) { throw new RuntimeException(e); }
41     }
42
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);
51
52             if (!(app == null ||
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());
59
60             ret.append(app == null ? "null" : app.toString());
61         }
62         ret.append(s);
63         return ret.toString();
64     }
65
66     public Object exec(String s) {
67         try {
68             return JS.eval(JS.cloneWithNewParentScope(
69                            JS.fromReader("input", 0, new StringReader(s)), this));
70         } catch (Exception e) {
71             e.printStackTrace();
72             throw new RuntimeException(e);
73         }
74     }
75
76     // Pass Through ///////////////////////////////////////////////////////////
77
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(); }
87
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();
91         return (JSScope)e;
92     }
93
94     /** A JSElement with the element attributes merged with a second
95      *  element.
96      *
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.
101      */
102     public static class Merge extends JSElement {
103         private final XML.Attributes a;
104         public Merge(XML.Element wrapped, XML.Element merge) {
105             super(wrapped);
106             a = new MergeAttr(merge.getAttributes(), wrapped.getAttributes());
107         }
108         public XML.Attributes getAttributes() { return a; }
109     }
110
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.
115      *
116      *  FIXME: toXML() produces invalid XML if qname in both a and b.
117      */
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;
122         }
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();
126             return -1;
127         }
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();
131             return -1;
132         }
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(); }
144     }
145 }