X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Forg%2Fibex%2Fcore%2FTemplate.java;h=c2a7c2cbd21134bbdde276130b81648d126b9b4c;hb=586485c446bc6870e08cfca8675a41442cca2c54;hp=6e29c4a1452cef387af86f1e75f4a3bffe4b85b6;hpb=8e190fb0ff508ccf4962bbfbf8295a431805c12b;p=org.ibex.core.git diff --git a/src/org/ibex/core/Template.java b/src/org/ibex/core/Template.java index 6e29c4a..c2a7c2c 100644 --- a/src/org/ibex/core/Template.java +++ b/src/org/ibex/core/Template.java @@ -1,4 +1,7 @@ -// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] +// 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.*; @@ -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; @@ -85,35 +87,36 @@ public class Template { PerInstantiationScope pis = new PerInstantiationScope(b, ibex, parentPis, staticObject); for(int i=0; i 0) { - switch (((String)val).charAt(0)) { + if (JS.isString(val) && (JS.toString(val).length() > 0)) { + switch (JS.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 '"+JS.toString(vals[i])+"' referenced in XML attribute"); break; case '.': - val = ibex.resolveString(((String)val).substring(1), false); + val = ibex.resolveString(JS.toString(val).substring(1), false); // FIXME: url case // FIXME: should we be resolving all of these in the XML-parsing code? } @@ -126,7 +129,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) { @@ -154,21 +157,21 @@ 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 = JS.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.O(); + JS staticScope = new PerInstantiationScope(null, ibex, null, t.staticObject); + if (staticScript != null) JS.cloneWithNewGlobalScope(staticScript, staticScope).call(null, null, null, null, 0); } private JS parseScript(StringBuffer content, int content_start) throws IOException { @@ -202,12 +205,16 @@ 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(); // 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); } @@ -252,19 +259,17 @@ public class Template { 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); - + 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() ? } } } @@ -325,28 +330,41 @@ public class Template { public void whitespace(char[] ch, int start, int length) throws XML.Exn { } } - private static class PerInstantiationScope extends JSScope { + private static class PerInstantiationScope extends JS.O { 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(JS.isString(key)) { + String s = JS.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, JSFunction f) throws JSExn { box.addTrap(key,f); } + public void delTrap(JS key, JSFunction f) throws JSExn { box.delTrap(key,f); } } }