2003/09/24 07:33:32
[org.ibex.core.git] / src / org / xwt / Template.java
index 363886c..95846c1 100644 (file)
@@ -149,8 +149,10 @@ public class Template {
         }
 
         for (int i=0; children != null && i<children.length; i++) {
-            children[i].apply(new Box(), callback, numerator, denominator, resourceRoot);
+            Box kid = new Box();
+            children[i].apply(kid, callback, numerator, denominator, resourceRoot);
             numerator += children[i].numUnits();
+            b.put(b.numChildren(), kid);
         }
 
         // whom to redirect to; doesn't take effect until after script runs
@@ -309,14 +311,18 @@ public class Template {
 
             }
 
-            // TODO: Sort contents straight from one array to another
-            // FIXME: height must come after image
-            // FIXME: use Vec here
             t.keys = new String[c.len];
             t.vals = new Object[c.len];
-            System.arraycopy(c.keys, 0, t.keys, 0, c.len);
-            System.arraycopy(c.vals, 0, t.vals, 0, c.len);
-            quickSortAttributes(0, t.keys.length - 1);
+            Hash h = new Hash(c.len * 2, 3);
+            for(int i=0; i<c.len; i++) h.put(c.keys[i], c.vals[i]);
+            Vec v = new Vec(c.len, c.keys);
+            v.sort(new Vec.CompareFunc() { public int compare(Object a, Object b) { return ((String)a).compareTo((String)b); } });
+            for(int i=0; i<c.len; i++) {
+                // FIXME: height must come after image
+                // FIXME: thisbox must come first
+                t.keys[i] = (String)v.elementAt(i);
+                t.vals[i] = h.get(t.keys[i]);
+            }
 
             for(int i=0; i<t.keys.length; i++) {
                 if (t.keys[i].equals("id")) {
@@ -358,37 +364,26 @@ public class Template {
             }
         }
 
-        /** simple quicksort, from http://sourceforge.net/snippet/detail.php?type=snippet&id=100240 */
-        private int partitionAttributes(int left, int right) {
-            int i, j, middle;
-            middle = (left + right) / 2;
-            String s = t.keys[right]; t.keys[right] = t.keys[middle]; t.keys[middle] = s;
-            Object o = t.vals[right]; t.vals[right] = t.vals[middle]; t.vals[middle] = o;
-            for (i = left - 1, j = right; ; ) {
-                while (t.keys[++i].compareTo(t.keys[right]) < 0);
-                while (j > left && t.keys[--j].compareTo(t.keys[right]) > 0);
-                if (i >= j) break;
-                s = t.keys[i]; t.keys[i] = t.keys[j]; t.keys[j] = s;
-                o = t.vals[i]; t.vals[i] = t.vals[j]; t.vals[j] = o;
+        private JS.CompiledFunction genscript(boolean isstatic) {
+            JS.CompiledFunction thisscript = null;
+            try {
+                thisscript = JS.parse(t.fileName + (isstatic ? "._" : ""), t.content_start, new StringReader(t.content.toString()));
+            } catch (IOException ioe) {
+                if (Log.on) Log.log(this, "  ERROR: " + ioe.getMessage());
+                thisscript = null;
             }
-            s = t.keys[right]; t.keys[right] = t.keys[i]; t.keys[i] = s;
-            o = t.vals[right]; t.vals[right] = t.vals[i]; t.vals[i] = o;
-            return i;
-        }
 
-        /** simple quicksort, from http://sourceforge.net/snippet/detail.php?type=snippet&id=100240 */
-        private void quickSortAttributes(int left, int right) {
-            if (left >= right) return;
-            int p = partitionAttributes(left, right);
-            quickSortAttributes(left, p - 1);
-            quickSortAttributes(p + 1, right);
+            t.content = null;
+            t.content_start = 0;
+            t.content_lines = 0;
+            return thisscript;
         }
 
         public void endElement(XML.Element c) throws XML.SchemaException {
             if (rootNodeHasBeenEncountered && !templateNodeHasBeenEncountered) {
                 if ("static".equals(nameOfHeaderNodeBeingProcessed) && t.content != null) t.staticscript = genscript(true);
                 nameOfHeaderNodeBeingProcessed = null;
-
+                
             } else if (templateNodeHasBeenEncountered && !templateNodeHasBeenFinished) {
                 // turn our childvect into a Template[]
                 t.childvect.copyInto(t.children = new Template[t.childvect.size()]);
@@ -398,7 +393,7 @@ public class Template {
                 if (nodeStack.size() == 0) {
                     // </template>
                     templateNodeHasBeenFinished = true;
-
+                    
                 } else {
                     // add this template as a child of its parent
                     Template oldt = t;
@@ -406,24 +401,8 @@ public class Template {
                     nodeStack.setSize(nodeStack.size() - 1);
                     t.childvect.addElement(oldt);
                 }
-
             }
-        }
-
-        private JS.CompiledFunction genscript(boolean isstatic) {
-            JS.CompiledFunction thisscript = null;
-            try {
-                thisscript = JS.parse(t.fileName + (isstatic ? "._" : ""), t.content_start, new StringReader(t.content.toString()));
-            } catch (IOException ioe) {
-                if (Log.on) Log.log(this, "  ERROR: " + ioe.getMessage());
-                thisscript = null;
-            }
-
-            t.content = null;
-            t.content_start = 0;
-            t.content_lines = 0;
-            return thisscript;
-        }
+         }
 
         public void characters(char[] ch, int start, int length) throws XML.SchemaException {
             // invoke the no-tab crusade