use //#jswitch more
[org.ibex.core.git] / src / org / ibex / core / Template.java
index 1e742c4..efa3bbb 100644 (file)
@@ -25,8 +25,8 @@ 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
@@ -85,35 +85,35 @@ public class Template {
         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));
+            // FEATURE: Cache urikeys and resolved resources
+            pis.declare(JS.S(urikeys[i]));
+            pis.put(JS.S(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);
+            b.putAndTriggerTraps(b.get(JS.S("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];
+            JS key = keys[i];
+            JS val = vals[i];
 
             if ("null".equals(val)) val = null;
 
-            if (val != null && val instanceof String && ((String)val).length() > 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 +126,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,13 +154,13 @@ 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 = "";
@@ -202,12 +202,15 @@ 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();
                 } catch (Exception e) {
                     Log.error(Template.class, e);
                 }
@@ -252,19 +255,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<t.keys.length; i++) {
-                t.keys[i] = t.keys[i].intern();
-
-                String valString = t.vals[i].toString();
+                // FEATURE: Intern
+                t.keys[i] = JS.S((String)keys.elementAt(i));
+                String valString = (String) vals.elementAt(i);
                 
-                if (valString.equals("true")) t.vals[i] = Boolean.TRUE;
-                else if (valString.equals("false")) t.vals[i] = Boolean.FALSE;
+                if (valString.equals("true")) t.vals[i] = JS.T;
+                else if (valString.equals("false")) t.vals[i] = JS.F;
                 else if (valString.equals("null")) t.vals[i] = null;
                 else {
                     boolean hasNonNumeral = false;
@@ -277,8 +278,8 @@ public class Template {
                             hasNonNumeral = true;
                             break;
                         }
-                    if (valString.length() > 0 && !hasNonNumeral) t.vals[i] = new Double(valString);
-                    else t.vals[i] = valString.intern();
+                    if (valString.length() > 0 && !hasNonNumeral) t.vals[i] = JS.N(Double.parseDouble((valString)));
+                    else t.vals[i] = JS.S(valString.intern()); // FEATURE: JS.intern() ?
                 }
             }
         }
@@ -331,8 +332,9 @@ public class Template {
         JS myStatic = null;
         void putDollar(String key, Box target) throws JSExn {
             if (parentBoxPis != null) parentBoxPis.putDollar(key, target);
-            declare("$" + key);
-            put("$" + key, target);
+            JS jskey = JS.S("$" + key);
+            declare(jskey);
+            put(jskey, target);
         }
         public PerInstantiationScope(JSScope parentScope, Ibex ibex, PerInstantiationScope parentBoxPis, JS myStatic) {
             super(parentScope);
@@ -340,11 +342,14 @@ public class Template {
             this.ibex = ibex;
             this.myStatic = myStatic;
         }
-        public Object get(Object key) throws JSExn {
+        public JS get(JS 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;
+            if(JS.isString(key)) {
+                String s = JS.toString(key);
+                if (s.equals("ibex")) return ibex;
+                if (s.equals("")) return ibex.get(key);
+                if (s.equals("static")) return myStatic;
+            }
             return super.get(key);
         }
     }