initial support for XML.Document
[org.ibex.xt-crawshaw.git] / src / ibex / xt / JSElement.java
1 package ibex.xt;
2
3 import ibex.util.XML;
4 import org.ibex.js.JS;
5 import org.ibex.js.JSScope;
6
7 import ibex.collection.*;
8 import java.util.*;
9
10 import java.io.StringReader;
11 import java.io.Writer;
12 import java.io.IOException;
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.remove(wrapped); c.add(this);
27         }
28         List c = wrapped.getChildren();
29         for (int i=c.size(); i >= 0; i--) ((XML.Block)c.get(i)).setParent(this);
30     }
31
32     public void toXML(Writer w) throws IOException {
33         // grab all related attributes
34         try {
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;
38                 declare(a.getKey(i));
39                 put(a.getKey(i), eval(a.getVal(i)));
40             }
41         } catch (Exception e) { throw new RuntimeException(e); }
42     }
43
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);
52
53             if (!(app == null ||
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());
60
61             ret.append(app == null ? "null" : app.toString());
62         }
63         ret.append(s);
64         return ret.toString();
65     }
66
67     public Object exec(String s) {
68         try {
69             return JS.eval(JS.cloneWithNewParentScope(
70                            JS.fromReader("input", 0, new StringReader(s)), this));
71         } catch (Exception e) {
72             e.printStackTrace();
73             throw new RuntimeException(e);
74         }
75     }
76
77     // Pass Through ///////////////////////////////////////////////////////////
78
79     public void setParent(XML.Element p)  { wrapped.setParent(p); }
80     public XML.Element 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(); }
88
89     /** Works up the Element object model until an instance of a JSScope is found. */
90     private static JSScope findScope(XML.Element e) {
91         while (e != null && !(e instanceof JSScope)) e = e.getParent();
92         return (JSScope)e;
93     }
94
95     /** A JSElement with the element attributes merged with a second
96      *  element.
97      *
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.
102      */
103     public static class Merge extends JSElement {
104         private final XML.Attributes a;
105         public Merge(XML.Element wrapped, XML.Element merge) {
106             super(wrapped);
107             a = new MergeAttr(merge.getAttributes(), wrapped.getAttributes());
108         }
109         public XML.Attributes getAttributes() { return a; }
110     }
111
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.
116      *
117      *  FIXME: toXML() produces invalid XML if qname in both a and b.
118      */
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;
123         }
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();
127             return -1;
128         }
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();
132             return -1;
133         }
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(); }
145     }
146 }