2003/08/10 06:03:02
[org.ibex.core.git] / src / org / xwt / Template.java
index a9ea9b9..1bf8b2b 100644 (file)
@@ -73,10 +73,10 @@ public class Template {
     private boolean changed = false;
 
     /** the script on the static node of this template, null if it has already been executed */
-    private JS.Script staticscript = null;
+    private JS.CompiledFunction staticscript = null;
 
     /** the script on this node */
-    private JS.Script script = null;
+    private JS.CompiledFunction script = null;
 
     /** during XML parsing, this holds the list of currently-parsed children; null otherwise */
     private Vec childvect = new Vec();
@@ -165,7 +165,7 @@ public class Template {
      *  @param pboxes a vector of all box parents on which to put $-references
      *  @param ptemplates a vector of the nodeNames to recieve private references on the pboxes
      */
-    void apply(Box b, Vec pboxes, Vec ptemplates, JS.Function callback, int numerator, int denominator) {
+    void apply(Box b, Vec pboxes, Vec ptemplates, JS.Callable callback, int numerator, int denominator) {
 
         int original_numerator = numerator;
 
@@ -197,7 +197,9 @@ public class Template {
             }
 
         for (int i=0; children != null && i<children.length; i++) {
-            b.put(Integer.MAX_VALUE, new Box(children[i], pboxes, ptemplates, callback, numerator, denominator));
+            Box newkid = new Box();
+            children[i].apply(newkid, pboxes, ptemplates, callback, numerator, denominator);
+            b.put(Integer.MAX_VALUE, newkid);
             numerator += children[i].numUnits();
         }
 
@@ -206,23 +208,25 @@ public class Template {
         if (redirect != null && !"self".equals(redirect)) redir = (Box)b.get("$" + redirect);
 
         if (script != null) try {
-           JS.Array jsa = new JS.Array();
-           jsa.addElement(b);
-            script.call(jsa);
+            script.call(new JS.Array(), b);
         } catch (JS.Exn e) {
             if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e.getMessage());
         }
 
         for(int i=0; keys != null && i<keys.length; i++) {
-           // FIXME: make sure that if exceptions are thrown in here, the line number of the offending XML is logged
-            if (keys[i] == null) { }
-            else if (keys[i].equals("border") || keys[i].equals("image") &&
-                     !vals[i].toString().startsWith("http://") && !vals[i].toString().startsWith("https://")) {
-                String s = Resources.resolve(vals[i].toString() + ".png", importlist);
-                if (s != null) b.put(keys[i], s.substring(0, s.length() - 4));
-                else if (Log.on) Log.log(this, "unable to resolve image " + vals[i].toString() + " referenced in attributes of " + nodeName); 
+            try {
+                if (keys[i] == null) { }
+                else if (keys[i].equals("border") || keys[i].equals("image") &&
+                        !vals[i].toString().startsWith("http://") && !vals[i].toString().startsWith("https://")) {
+                    String s = Resources.resolve(vals[i].toString() + ".png", importlist);
+                    if (s != null) b.put(keys[i], s.substring(0, s.length() - 4));
+                    else if (Log.on) Log.log(this, "unable to resolve image " + vals[i].toString() + " referenced in attributes of " + nodeName); 
+                }
+                else b.put(keys[i], vals[i]);
+            } catch(JS.Exn e) {
+                if(Log.on) Log.log(this,"WARNING: uncaught ecmascript exception while putting attr \"" + keys[i] + 
+                    "\" of " + nodeName + " : " + e.getMessage());
             }
-            else b.put(keys[i], vals[i]);
         }
 
         if (redirect != null && !"self".equals(redirect)) b.redirect = redir;
@@ -240,18 +244,18 @@ public class Template {
 
         if (callback != null)
             try {
-               JS.Array args = new JS.Array();
-               args.addElement(new Double(numerator));
-               args.addElement(new Double(denominator));
+                JS.Array args = new JS.Array();
+                args.addElement(new Double(numerator));
+                args.addElement(new Double(denominator));
                 callback.call(args);
             } catch (JS.Exn e) {
                 if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e);
             }
 
         if (Thread.currentThread() instanceof ThreadMessage) try {
-           XWT.sleep(0);
+            XWT.sleep(0);
         } catch (JS.Exn e) {
-           if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e);
+            if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e);
         }
     }
 
@@ -266,7 +270,8 @@ public class Template {
     }
 
     /** adds a theme mapping, retemplatizing as needed */
-    public static void retheme(JS.Function callback) {
+    public static void retheme(JS.Callable callback) {
+        /*
         XWF.flushXWFs();
 
         // clear changed marker and relink
@@ -286,20 +291,21 @@ public class Template {
 
         if (callback != null)
             try {
-               JS.Array args = new JS.Array();
-               args.addElement(new Double(1.0));
-               args.addElement(new Double(1.0));
+                JS.Array args = new JS.Array();
+                args.addElement(new Double(1.0));
+                args.addElement(new Double(1.0));
                 callback.call(args);
             } catch (JS.Exn ex) {
                 if (Log.on) Log.log(Template.class, "WARNING: uncaught ecmascript exception: " + ex.getMessage());
             }
+        */
     }
 
     /** template reapplication procedure */
     private static void reapply(Box b) {
 
-       throw new Error("not implemented");
-       /*
+        Log.log(Template.class, "Template.reapply() not implemented");
+        /*
         // Ref 7.5.1: check if we need to retemplatize
         boolean retemplatize = false;
         if (b.templatename != null) {
@@ -353,7 +359,7 @@ public class Template {
 
         // Recurse
         for(Box j = b.getChild(0); j != null; j = j.nextSibling()) reapply(j);
-       */
+        */
     }
 
     /** runs statics, resolves string references to other templates into actual Template instance references, and sets <tt>change</tt> as needed */
@@ -365,20 +371,18 @@ public class Template {
         if (staticscript != null) try { 
             JS.Scope s = Static.createStatic(nodeName, false);
             if (staticscript != null) {
-                JS.Script temp = staticscript;
+                JS.CompiledFunction temp = staticscript;
                 staticscript = null;
 
-               // we layer a transparent scope over the Static so that we can catch requests for the xwt object
-               // yet not screw up paths that include a package called xwt (ie xwt.static.org.xwt.foo)
-               JS.Scope varScope = new JS.Scope(s) {
-                       public boolean isTransparent() { return true; }
-                       public Object get(Object key) {
-                           if ("xwt".equals(key)) return XWT.singleton; else return super.get(key);
-                       } };
-
-               JS.Array args = new JS.Array();
-               args.addElement(varScope);
-                temp.call(args);
+                // we layer a transparent scope over the Static so that we can catch requests for the xwt object
+                // yet not screw up paths that include a package called xwt (ie xwt.static.org.xwt.foo)
+                JS.Scope varScope = new JS.Scope(s) {
+                        public boolean isTransparent() { return true; }
+                        public Object get(Object key) {
+                            if ("xwt".equals(key)) return XWT.singleton; else return super.get(key);
+                        } };
+
+                temp.call(new JS.Array(), varScope);
             }
         } catch (JS.Exn e) {
             if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e.getMessage());
@@ -492,6 +496,7 @@ public class Template {
                     if (t.redirect != null)
                         throw new XML.SchemaException("the <redirect> header element may not appear more than once");
                     t.redirect = c.vals[0].toString();
+                    if(t.redirect.equals("null")) t.redirect = null;
                     return;
 
                 } else if (c.localName.equals("preapply")) {
@@ -654,10 +659,10 @@ public class Template {
             }
         }
 
-        private JS.Script genscript(boolean isstatic) {
-            JS.Script thisscript = null;
+        private JS.CompiledFunction genscript(boolean isstatic) {
+            JS.CompiledFunction thisscript = null;
             try {
-                thisscript = JS.Script.parse(new StringReader(t.content.toString()), t.nodeName + (isstatic ? "._" : ""), t.content_start);
+                thisscript = JS.parse(t.nodeName + (isstatic ? "._" : ""), t.content_start, new StringReader(t.content.toString()));
             } catch (JS.Exn ee) {
                 if (Log.on) Log.log(this, "  ERROR: " + ee.getMessage());
                 thisscript = null;