--- /dev/null
+// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
+package org.ibex;
+
+import java.io.*;
+import java.util.*;
+import org.ibex.js.*;
+import org.ibex.util.*;
+
+/**
+ * Encapsulates a template node (the <template/> element of a
+ * .ibex file, or any child element thereof).
+ *
+ * Note that the Template instance corresponding to the
+ * <template/> node carries all the header information -- hence
+ * some of the instance members are not meaningful on non-root
+ * Template instances. We refer to these non-root instances as
+ * <i>anonymous templates</i>.
+ *
+ * See the Ibex reference for information on the order in which
+ * templates are applied, attributes are put, and scripts are run.
+ */
+public class Template {
+
+ // Instance Members ///////////////////////////////////////////////////////
+
+ String id = null; ///< the id of this box
+ String redirect = null; ///< the id of the redirect target; only meaningful on a root node
+ private String[] keys; ///< keys to be "put" to instances of this template; elements correspond to those of vals
+ private Object[] vals; ///< values to be "put" to instances of this template; elements correspond to those of keys
+ private String[] urikeys;
+ private String[] urivals;
+ private Vec children = new Vec(); ///< during XML parsing, this holds the list of currently-parsed children; null otherwise
+ private JS script = null; ///< the script on this node
+ Template prev;
+ Template prev2;
+ JSScope staticScope = null; ///< the scope in which the static block is executed
+ JS staticObject = null;
+
+
+ // Only used during parsing /////////////////////////////////////////////////////////////////
+
+ private StringBuffer content = null; ///< during XML parsing, this holds partially-read character data; null otherwise
+ private int content_start = 0; ///< line number of the first line of <tt>content</tt>
+ private int startLine = -1; ///< the line number that this element starts on
+ private Ibex ibex;
+
+
+ // Static data/methods ///////////////////////////////////////////////////////////////////
+
+ // for non-root nodes
+ private Template(Template t, int startLine) { prev = t; this.ibex = t.ibex; this.startLine = startLine; }
+ private Template(Ibex ibex) { this.ibex = ibex; }
+
+
+ // Methods to apply templates ////////////////////////////////////////////////////////
+
+
+ /** Applies the template to Box b
+ * @param pboxes a vector of all box parents on which to put $-references
+ * @param ptemplates a vector of the fileNames to recieve private references on the pboxes
+ */
+ void apply(Box b) throws JSExn {
+ try {
+ apply(b, null);
+ } catch (IOException e) {
+ b.clear(Box.VISIBLE);
+ b.mark_for_repack();
+ Log.warn(this, e);
+ throw new JSExn(e.toString());
+ } catch (JSExn e) {
+ b.clear(Box.VISIBLE);
+ b.mark_for_repack();
+ Log.warn(this, e);
+ throw e;
+ }
+ }
+
+ private void apply(Box b, PerInstantiationScope parentPis) throws JSExn, IOException {
+ if (prev != null) prev.apply(b, null);
+ if (prev2 != null) prev2.apply(b, null);
+
+ // FIXME this dollar stuff is all wrong
+ if (id != null) parentPis.putDollar(id, b);
+
+ PerInstantiationScope pis = new PerInstantiationScope(b, ibex, parentPis, staticObject);
+ for(int i=0; i<urikeys.length; i++) {
+ if (urikeys[i] == null) continue;
+ pis.declare(urikeys[i]);
+ pis.put(urikeys[i], ibex.resolveString(urivals[i], true));
+ }
+
+ // FIXME needs to obey the new application-ordering rules
+ for (int i=0; children != null && i<children.size(); i++) {
+ Box kid = new Box();
+ ((Template)children.elementAt(i)).apply(kid, pis);
+ b.putAndTriggerTraps(b.get("numchildren"), kid);
+ }
+
+ if (script != null) JS.cloneWithNewParentScope(script, pis).call(null, null, null, null, 0);
+
+ Object key, val;
+ for(int i=0; keys != null && i < keys.length; i++) {
+ if (keys[i] == null) continue;
+ key = keys[i];
+ val = vals[i];
+
+ if ("null".equals(val)) val = null;
+
+ if (val != null && val instanceof String && ((String)val).length() > 0) {
+ switch (((String)val).charAt(0)) {
+ case '$':
+ val = pis.get(val);
+ if (val == null) throw new JSExn("unknown box id '"+vals[i]+"' referenced in XML attribute");
+ break;
+ case '.':
+ val = ibex.resolveString(((String)val).substring(1), false);
+ // FIXME: url case
+ // FIXME: should we be resolving all of these in the XML-parsing code?
+ }
+ }
+ b.putAndTriggerTraps(key, val);
+ }
+ }
+
+
+
+ // XML Parsing /////////////////////////////////////////////////////////////////
+
+ public static Template buildTemplate(String sourceName, Object s, Ibex ibex) {
+ try {
+ return new TemplateHelper(sourceName, s, ibex).t;
+ } catch (Exception e) {
+ Log.error(Template.class, e);
+ return null;
+ }
+ }
+
+ /** handles XML parsing; builds a Template tree as it goes */
+ static final class TemplateHelper extends XML {
+
+ String sourceName;
+ private int state = STATE_INITIAL;
+ private static final int STATE_INITIAL = 0;
+ private static final int STATE_IN_ROOT_NODE = 1;
+ private static final int STATE_IN_TEMPLATE_NODE = 2;
+ private static final int STATE_IN_META_NODE = 3;
+
+ StringBuffer static_content = null;
+ int static_content_start = 0;
+ Vec nodeStack = new Vec();
+ Template t = null;
+ int meta = 0;
+ Ibex ibex;
+
+ String initial_uri = "";
+
+ public TemplateHelper(String sourceName, Object s, Ibex ibex) throws XML.Exn, IOException, JSExn {
+ this.sourceName = sourceName;
+ this.ibex = ibex;
+ InputStream is = Stream.getInputStream(s);
+ Ibex.Blessing b = Ibex.Blessing.getBlessing(s).parent;
+ while(b != null) {
+ if(b.parentkey != null) initial_uri = b.parentkey + (initial_uri.equals("") ? "" : "." + initial_uri);
+ b = b.parent;
+ }
+ initial_uri = "";
+ parse(new InputStreamReader(is));
+ JS staticScript = parseScript(static_content, static_content_start);
+ t.staticObject = new JS();
+ t.staticScope = new PerInstantiationScope(null, ibex, null, t.staticObject);
+ if (staticScript != null) JS.cloneWithNewParentScope(staticScript, t.staticScope).call(null, null, null, null, 0);
+ }
+
+ private JS parseScript(StringBuffer content, int content_start) throws IOException {
+ if (content == null) return null;
+ String contentString = content.toString();
+ if (contentString.trim().length() > 0) return JS.fromReader(sourceName, content_start, new StringReader(contentString));
+ return null;
+ }
+
+ public void startElement(XML.Element c) throws XML.Exn {
+ switch(state) {
+ case STATE_IN_META_NODE: { meta++; return; }
+ case STATE_INITIAL:
+ if (!"ibex".equals(c.getLocalName()))
+ throw new XML.Exn("root element was not <ibex>", XML.Exn.SCHEMA, getLine(), getCol());
+ if (c.getAttrLen() != 0)
+ throw new XML.Exn("root element must not have attributes", XML.Exn.SCHEMA, getLine(), getCol());
+ if (c.getUri("ui") == null || "".equals(c.getUri("ui"))) c.addUri("ui", "ibex://ui");
+ if (c.getUri("meta") == null || "".equals(c.getUri("meta"))) c.addUri("meta", "ibex://meta");
+ if (c.getUri("") == null || "".equals(c.getUri(""))) c.addUri("", initial_uri);
+ state = STATE_IN_ROOT_NODE;
+ return;
+ case STATE_IN_ROOT_NODE:
+ if ("ibex://meta".equals(c.getUri())) { state = STATE_IN_META_NODE; meta = 0; return; }
+ state = STATE_IN_TEMPLATE_NODE;
+ t = (t == null) ? new Template(ibex) : new Template(t, getLine());
+ break;
+ case STATE_IN_TEMPLATE_NODE:
+ nodeStack.addElement(t);
+ t = new Template(ibex);
+ break;
+ }
+
+ if (!("ibex://ui".equals(c.getUri()) && "box".equals(c.getLocalName()))) {
+ String tagname = (c.getUri().equals("") ? "" : (c.getUri() + ".")) + c.getLocalName();
+ // GROSS hack
+ try {
+ // GROSSER hack
+ t.prev2 = (Template)t.ibex.resolveString(tagname, false).call(null, null, null, null, 9999);
+ } catch (Exception e) {
+ Log.error(Template.class, e);
+ }
+ }
+
+ Hash urimap = c.getUriMap();
+ t.urikeys = new String[urimap.size()];
+ t.urivals = new String[urimap.size()];
+ Enumeration uriEnumeration = urimap.keys();
+ int ii = 0;
+ while(uriEnumeration.hasMoreElements()) {
+ String key = (String)uriEnumeration.nextElement();
+ String val = (String)urimap.get(key);
+ if (val.equals("ibex://ui")) continue;
+ if (val.equals("ibex://meta")) continue;
+ t.urikeys[ii] = key;
+ if (val.length() > 0 && val.charAt(0) == '.') val = val.substring(1);
+ t.urivals[ii] = val;
+ ii++;
+ }
+
+ Vec keys = new Vec(c.getAttrLen());
+ Vec vals = new Vec(c.getAttrLen());
+
+ // process attributes into Vecs, dealing with any XML Namespaces in the process
+ ATTR: for (int i=0; i < c.getAttrLen(); i++) {
+ if (c.getAttrKey(i).equals("id")) {
+ t.id = c.getAttrVal(i).toString().intern();
+ continue ATTR;
+ }
+
+ // treat value starting with '.' as resource reference
+ String uri = c.getAttrUri(i); if (!uri.equals("")) uri = '.' + uri;
+ keys.addElement(c.getAttrKey(i));
+ vals.addElement((c.getAttrVal(i).startsWith(".") ? uri : "") + c.getAttrVal(i));
+ }
+
+ if (keys.size() == 0) return;
+
+ // sort the attributes lexicographically
+ Vec.sort(keys, vals, new Vec.CompareFunc() { public int compare(Object a, Object b) {
+ return ((String)a).compareTo((String)b);
+ } });
+
+ t.keys = new String[keys.size()];
+ t.vals = new Object[vals.size()];
+ keys.copyInto(t.keys);
+ vals.copyInto(t.vals);
+
+ // convert attributes to appropriate types and intern strings
+ for(int i=0; i<t.keys.length; i++) {
+ t.keys[i] = t.keys[i].intern();
+
+ String valString = t.vals[i].toString();
+
+ if (valString.equals("true")) t.vals[i] = Boolean.TRUE;
+ else if (valString.equals("false")) t.vals[i] = Boolean.FALSE;
+ else if (valString.equals("null")) t.vals[i] = null;
+ else {
+ boolean hasNonNumeral = false;
+ boolean periodUsed = false;
+ for(int j=0; j<valString.length(); j++)
+ if (j == 0 && valString.charAt(j) == '-') {
+ } else if (valString.charAt(j) == '.' && !periodUsed && j != valString.length() - 1) {
+ periodUsed = true;
+ } else if (!Character.isDigit(valString.charAt(j))) {
+ hasNonNumeral = true;
+ break;
+ }
+ if (valString.length() > 0 && !hasNonNumeral) t.vals[i] = new Double(valString);
+ else t.vals[i] = valString.intern();
+ }
+ }
+ }
+
+ public void endElement(XML.Element c) throws XML.Exn, IOException {
+ switch(state) {
+ case STATE_IN_META_NODE: if (--meta < 0) state = STATE_IN_ROOT_NODE; return;
+ case STATE_IN_ROOT_NODE: return;
+ case STATE_IN_TEMPLATE_NODE: {
+ if (t.content != null) { t.script = parseScript(t.content, t.content_start); t.content = null; }
+ if (nodeStack.size() == 0) { state = STATE_IN_ROOT_NODE; return; }
+ Template oldt = t;
+ t = (Template)nodeStack.lastElement();
+ nodeStack.setSize(nodeStack.size() - 1);
+ t.children.addElement(oldt);
+ int oldt_lines = getLine() - oldt.startLine;
+ if (t.content == null) t.content = new StringBuffer();
+ for (int i=0; oldt_lines > i; i++) t.content.append('\n');
+ }
+ }
+ }
+
+ public void characters(char[] ch, int start, int length) throws XML.Exn {
+ for (int i=0; length >i; i++) if (ch[start+i] == '\t')
+ Log.error(Template.class, "tabs are not allowed in Ibex files ("+getLine()+":"+getCol()+")");
+ switch(state) {
+ case STATE_IN_TEMPLATE_NODE:
+ if (t.content == null) {
+ t.content_start = getLine();
+ t.content = new StringBuffer();
+ }
+ t.content.append(ch, start, length);
+ return;
+ case STATE_IN_ROOT_NODE:
+ if (static_content == null) {
+ static_content_start = getLine();
+ static_content = new StringBuffer();
+ }
+ static_content.append(ch, start, length);
+ return;
+ }
+ }
+
+ public void whitespace(char[] ch, int start, int length) throws XML.Exn { }
+ }
+
+ private static class PerInstantiationScope extends JSScope {
+ Ibex ibex = null;
+ PerInstantiationScope parentBoxPis = null;
+ JS myStatic = null;
+ void putDollar(String key, Box target) throws JSExn {
+ if (parentBoxPis != null) parentBoxPis.putDollar(key, target);
+ declare("$" + key);
+ put("$" + key, target);
+ }
+ public PerInstantiationScope(JSScope parentScope, Ibex ibex, PerInstantiationScope parentBoxPis, JS myStatic) {
+ super(parentScope);
+ this.parentBoxPis = parentBoxPis;
+ this.ibex = ibex;
+ this.myStatic = myStatic;
+ }
+ public Object get(Object key) throws JSExn {
+ if (super.has(key)) return super.get(key);
+ if (key.equals("ibex")) return ibex;
+ if (key.equals("")) return ibex.get("");
+ if (key.equals("static")) return myStatic;
+ return super.get(key);
+ }
+ }
+
+}
+
+