X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fcore%2FTemplate.java;h=1b70f308608931f01e2bbbc621d2d72b077b419d;hp=2f3e2acef89baddfa7cbe2619bb22bcead772540;hb=4c916f5d87aa03b737c6cd804c2cef525c4c1bc7;hpb=4daeeb4119b901d53b44913c86f8af3ce67db925 diff --git a/src/org/ibex/core/Template.java b/src/org/ibex/core/Template.java index 2f3e2ac..1b70f30 100644 --- a/src/org/ibex/core/Template.java +++ b/src/org/ibex/core/Template.java @@ -1,5 +1,8 @@ -// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] -package org.ibex; +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the GNU General Public License version 2 ("the License"). +// You may not use this file except in compliance with the License. + +package org.ibex.core; import java.io.*; import java.util.*; @@ -25,15 +28,14 @@ public class Template { 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 JS[] keys; ///< keys to be "put" to instances of this template; elements correspond to those of vals + private JS[] 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; @@ -59,7 +61,7 @@ public class Template { * @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 { + public void apply(Box b) throws JSExn { try { apply(b, null); } catch (IOException e) { @@ -75,6 +77,7 @@ public class Template { } } + private static final JS[] callempty = new JS[0]; private void apply(Box b, PerInstantiationScope parentPis) throws JSExn, IOException { if (prev != null) prev.apply(b, null); if (prev2 != null) prev2.apply(b, null); @@ -85,35 +88,36 @@ public class Template { PerInstantiationScope pis = new PerInstantiationScope(b, ibex, parentPis, staticObject); for(int i=0; i 0) { - switch (((String)val).charAt(0)) { + if (JSU.isString(val) && (JSU.toString(val).length() > 0)) { + switch (JSU.toString(val).charAt(0)) { case '$': val = pis.get(val); - if (val == null) throw new JSExn("unknown box id '"+vals[i]+"' referenced in XML attribute"); + if (val == null) throw new JSExn("unknown box id '"+JSU.str(vals[i])+"' referenced in XML attribute"); break; case '.': - val = ibex.resolveString(((String)val).substring(1), false); + val = ibex.resolveString(JSU.toString(val).substring(1), false); // FIXME: url case // FIXME: should we be resolving all of these in the XML-parsing code? } @@ -126,7 +130,7 @@ public class Template { // XML Parsing ///////////////////////////////////////////////////////////////// - public static Template buildTemplate(String sourceName, Object s, Ibex ibex) { + public static Template buildTemplate(String sourceName, JS s, Ibex ibex) { try { return new TemplateHelper(sourceName, s, ibex).t; } catch (Exception e) { @@ -138,12 +142,13 @@ public class Template { /** handles XML parsing; builds a Template tree as it goes */ static final class TemplateHelper extends XML { - String sourceName; + 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; + private static final JS[] callempty = new JS[0]; StringBuffer static_content = null; int static_content_start = 0; @@ -154,41 +159,39 @@ public class Template { String initial_uri = ""; - public TemplateHelper(String sourceName, Object s, Ibex ibex) throws XML.Exn, IOException, JSExn { + public TemplateHelper(String sourceName, JS s, Ibex ibex) throws XML.Exn, IOException, JSExn { this.sourceName = sourceName; this.ibex = ibex; - InputStream is = Stream.getInputStream(s); + InputStream is = s.getInputStream(); Ibex.Blessing b = Ibex.Blessing.getBlessing(s).parent; while(b != null) { - if(b.parentkey != null) initial_uri = b.parentkey + (initial_uri.equals("") ? "" : "." + initial_uri); + if(b.parentkey != null) initial_uri = JSU.toString(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); + t.staticObject = new JS.Obj(); + JS staticScope = new PerInstantiationScope(null, ibex, null, t.staticObject); + if (staticScript != null) JSU.cloneWithNewGlobalScope(staticScript, staticScope).call(callempty); } 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)); + if (contentString.trim().length() > 0) return JSU.fromReader(sourceName, content_start, new StringReader(contentString)); return null; } - public void startElement(XML.Element c) throws XML.Exn { + public void startElement(Tree.Element c) throws XML.Exn { + Tree.Attributes a = c.getAttributes(); 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 ", XML.Exn.SCHEMA, getLine(), getCol()); - if (c.getAttrLen() != 0) + if (a.attrSize() != 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: @@ -202,25 +205,30 @@ public class Template { break; } + // FIXME: This is all wrong if (!("ibex://ui".equals(c.getUri()) && "box".equals(c.getLocalName()))) { - String tagname = (c.getUri().equals("") ? "" : (c.getUri() + ".")) + c.getLocalName(); + String tagname = (c.getUri() == null || "".equals(c.getUri()) ? "" : + (c.getUri() + ".")) + c.getLocalName(); // GROSS hack try { // GROSSER hack - t.prev2 = (Template)t.ibex.resolveString(tagname, false).call(null, null, null, null, 9999); + // t.prev2 = (Template)t.ibex.resolveString(tagname, false).call(null, null, null, null, 9999); + if(t.prev2 != null) throw new Error("FIXME: t.prev2 != null"); + t.prev2 = ((Ibex.Blessing)t.ibex.resolveString(tagname, false)).getTemplate(); + if(t.prev2 == null) throw new Exception("" + tagname + " not found"); } 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(); + + Tree.Prefixes prefixes = c.getPrefixes(); + t.urikeys = new String[prefixes.pfxSize()]; + t.urivals = new String[prefixes.pfxSize()]; + int ii = 0; - while(uriEnumeration.hasMoreElements()) { - String key = (String)uriEnumeration.nextElement(); - String val = (String)urimap.get(key); + for (int i=0; i < prefixes.pfxSize(); i++) { + String key = prefixes.getPrefixKey(i); + String val = prefixes.getPrefixVal(i); if (val.equals("ibex://ui")) continue; if (val.equals("ibex://meta")) continue; t.urikeys[ii] = key; @@ -228,43 +236,42 @@ public class Template { t.urivals[ii] = val; ii++; } - - Vec keys = new Vec(c.getAttrLen()); - Vec vals = new Vec(c.getAttrLen()); + + // FIXME: 2-value Array + Basket.Array keys = new Basket.Array(a.attrSize()); + Basket.Array vals = new Basket.Array(a.attrSize()); // 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(); + ATTR: for (int i=0; i < a.attrSize(); i++) { + if (a.getKey(i).equals("id")) { + t.id = a.getVal(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)); + String uri = a.getUri(i); if (uri != null && !uri.equals("")) uri = '.' + uri; + keys.add(a.getKey(i)); + vals.add((a.getVal(i).startsWith(".") ? uri : "") + a.getVal(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); + Basket.Array.sort(keys, vals, new Basket.CompareFunc() { + public int compare(Object a, Object b) { return ((String)a).compareTo((String)b); } + }, 0, keys.size()); + t.keys = new JS[keys.size()]; + t.vals = new JS[vals.size()]; + // convert attributes to appropriate types and intern strings for(int i=0; i 0 && !hasNonNumeral) t.vals[i] = new Double(valString); - else t.vals[i] = valString.intern(); + if (valString.length() > 0 && !hasNonNumeral) t.vals[i] = JSU.N(Double.parseDouble((valString))); + else t.vals[i] = JSU.S(valString.intern()); // FEATURE: JS.intern() ? } } } - public void endElement(XML.Element c) throws XML.Exn, IOException { + public void endElement(Tree.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; @@ -325,28 +332,42 @@ public class Template { public void whitespace(char[] ch, int start, int length) throws XML.Exn { } } - private static class PerInstantiationScope extends JSScope { + // FIXME: david, get to work + private static class PerInstantiationScope extends JS.Obj { Ibex ibex = null; PerInstantiationScope parentBoxPis = null; JS myStatic = null; + JS box; void putDollar(String key, Box target) throws JSExn { if (parentBoxPis != null) parentBoxPis.putDollar(key, target); - declare("$" + key); - put("$" + key, target); + JS jskey = JSU.S("$" + key); + //declare(jskey); + sput(jskey, target); } - public PerInstantiationScope(JSScope parentScope, Ibex ibex, PerInstantiationScope parentBoxPis, JS myStatic) { - super(parentScope); + // JS:FIXME: ugly + void sput(JS key, JS val) throws JSExn { super.put(key,val); } + public PerInstantiationScope(JS box, Ibex ibex, PerInstantiationScope parentBoxPis, JS myStatic) { this.parentBoxPis = parentBoxPis; this.ibex = ibex; this.myStatic = myStatic; + this.box = box; } - 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); + public JS get(JS key) throws JSExn { + if(JSU.isString(key)) { + String s = JSU.toString(key); + // JS:FIXME This is a hack + if (super.get(key) != null) return super.get(key); + if (s.equals("ibex")) return ibex; + if (s.equals("")) return ibex.get(key); + if (s.equals("static")) return myStatic; + } + // JS:FIXME: This won't work with traps that do blocking operations + return box.getAndTriggerTraps(key); } + // JS:FIXME: Everything below here should come from js.scope or something + public void put(JS key, JS val) throws JSExn { if(box != null) box.putAndTriggerTraps(key,val); else super.put(key,val); } + public void addTrap(JS key, JS f) throws JSExn { box.addTrap(key,f); } + public void delTrap(JS key, JS f) throws JSExn { box.delTrap(key,f); } } }