pass correct range to sort()
[org.ibex.core.git] / src / org / ibex / core / Template.java
index 249ba11..a59e99d 100644 (file)
@@ -77,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);
@@ -88,19 +89,19 @@ public class Template {
         for(int i=0; i<urikeys.length; i++) {
             if (urikeys[i] == null) continue;
             // FEATURE: Cache urikeys and resolved resources
-            //pis.declare(JS.S(urikeys[i]));
+            //pis.declare(JSU.S(urikeys[i]));
             // JS:FIXME: ugly
-            pis.sput(JS.S(urikeys[i]), ibex.resolveString(urivals[i], true));
+            pis.sput(JSU.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(JS.S("numchildren")), kid);
+            b.putAndTriggerTraps(b.get(JSU.S("numchildren")), kid);
         }
 
-        if (script != null) JS.cloneWithNewGlobalScope(script, pis).call(null, null, null, null, 0);
+        if (script != null) JSU.cloneWithNewGlobalScope(script, pis).call(callempty);
 
         for(int i=0; keys != null && i < keys.length; i++) {
             if (keys[i] == null) continue;
@@ -109,14 +110,14 @@ public class Template {
 
             if ("null".equals(val)) val = null;
 
-            if (JS.isString(val) && (JS.toString(val).length() > 0)) {
-                switch (JS.toString(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 '"+JS.toString(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(JS.toString(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?
                 }
@@ -141,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;
@@ -157,41 +159,39 @@ public class Template {
 
         String initial_uri = "";
 
-       public TemplateHelper(String sourceName, JS 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 = s.getInputStream();
             Ibex.Blessing b = Ibex.Blessing.getBlessing(s).parent;
             while(b != null) {
-                if(b.parentkey != null) initial_uri = JS.toString(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.O();
+            t.staticObject = new JS.Obj();
             JS staticScope = new PerInstantiationScope(null, ibex, null, t.staticObject);
-            if (staticScript != null) JS.cloneWithNewGlobalScope(staticScript, staticScope).call(null, null, null, null, 0);
+            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 <ibex>", 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:
@@ -207,7 +207,8 @@ public class Template {
 
             // 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
@@ -219,15 +220,15 @@ public class Template {
                     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;
@@ -235,29 +236,30 @@ 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);
-            } });
+            Basket.Array.sort(keys, vals, new Basket.CompareFunc() {
+                public int compare(Object a, Object b) { return ((String)a).compareTo((String)b); }
+            }, 0, keys.size() - 1);
 
             t.keys = new JS[keys.size()];
             t.vals = new JS[vals.size()];
@@ -265,11 +267,11 @@ public class Template {
             // convert attributes to appropriate types and intern strings
             for(int i=0; i<t.keys.length; i++) {
                 // FEATURE: Intern
-                t.keys[i] = JS.S((String)keys.elementAt(i));
-                String valString = (String) vals.elementAt(i);
+                t.keys[i] = JSU.S((String)keys.get(i));
+                String valString = (String) vals.get(i);
                 
-                if (valString.equals("true")) t.vals[i] = JS.T;
-                else if (valString.equals("false")) t.vals[i] = JS.F;
+                if (valString.equals("true")) t.vals[i] = JSU.T;
+                else if (valString.equals("false")) t.vals[i] = JSU.F;
                 else if (valString.equals("null")) t.vals[i] = null;
                 else {
                     boolean hasNonNumeral = false;
@@ -282,13 +284,13 @@ public class Template {
                             hasNonNumeral = true;
                             break;
                         }
-                    if (valString.length() > 0 && !hasNonNumeral) t.vals[i] = JS.N(Double.parseDouble((valString)));
-                    else t.vals[i] = JS.S(valString.intern()); // FEATURE: JS.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;
@@ -330,14 +332,15 @@ public class Template {
         public void whitespace(char[] ch, int start, int length) throws XML.Exn { }
     }
 
-    private static class PerInstantiationScope extends JS.O {
+    // 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);
-            JS jskey = JS.S("$" + key);
+            JS jskey = JSU.S("$" + key);
             //declare(jskey);
             sput(jskey, target);
         }
@@ -350,8 +353,8 @@ public class Template {
             this.box = box;
         }
         public JS get(JS key) throws JSExn {
-            if(JS.isString(key)) {
-                String s = JS.toString(key);
+            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;
@@ -363,8 +366,8 @@ public class Template {
         }
         // 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); }
+        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); }
     }
 
 }