Revision: 1914
          http://svn.sourceforge.net/vexi/?rev=1914&view=rev
Author:   mkpg2
Date:     2007-07-03 11:42:24 -0700 (Tue, 03 Jul 2007)

Log Message:
-----------
Feature. Resolve templates during template apply, not xml parsing. 
 i.e. when required and not early.

Modified Paths:
--------------
    core/trunk/org.vexi.core/src/org/vexi/core/Blessing.java
    core/trunk/org.vexi.core/src/org/vexi/core/Template.java
    core/trunk/org.vexi.core/src/org/vexi/core/TemplateBuilder.java
    core/trunk/org.vexi.core/src_junit/test/core/template/_r.t
    core/trunk/org.vexi.core/src_junit/test/core/template/_ra.t
    core/trunk/org.vexi.core/src_junit/test/core/template/_rb.t

Modified: core/trunk/org.vexi.core/src/org/vexi/core/Blessing.java
===================================================================
--- core/trunk/org.vexi.core/src/org/vexi/core/Blessing.java    2007-07-03 
17:37:32 UTC (rev 1913)
+++ core/trunk/org.vexi.core/src/org/vexi/core/Blessing.java    2007-07-03 
18:42:24 UTC (rev 1914)
@@ -23,7 +23,7 @@
     public JS clonee;
        private Basket.Map cache = new Basket.Hash(); 
     boolean initializing = false;
-    public Blessing(JS clonee, JS vexi, Blessing parent, JS parentkey) throws 
JSExn {
+    Blessing(JS clonee, JS vexi, Blessing parent, JS parentkey) throws JSExn {
         super(clonee);
         this.clonee = clonee;
         this.vexi = vexi;
@@ -76,7 +76,10 @@
        return getForExtensions(EXTS_TTF);
     }
     
-    public JS getStatic() throws JSExn {
+    JS getStatic() throws JSExn {
+        return getStatic(new Template(vexi, JSU.toString(parentkey)));
+    }
+    JS getStatic(Template unresolved) throws JSExn {
         try {
             if (t == null) {
                // Otherwise we recurse infinitely.
@@ -85,7 +88,7 @@
                initializing = true;
                 // FEATURE: Might want to handle the ".t" part better
                 JS res = parent.get(JSU.S(JSU.toString(parentkey) + ".t"));
-                t = TemplateBuilder.build(description(), res, vexi);
+                t = TemplateBuilder.build(unresolved, res);
                 initializing = false;
             }
             return t != null ? t.getStatic(): null;
@@ -139,8 +142,10 @@
     }
     
     // FEATURE: This is a gross hack
-    Template getTemplate() throws JSExn {
-        getStatic();
+    //Template getTemplate() throws JSExn { ...
+    // COROLLARY: this must also be then ...
+    Template initTemplate(Template unresolved) throws JSExn {
+       getStatic(unresolved);
         if (t == null) throw new JSExn("No such template '" + 
description()+"'");
         return t;
     }

Modified: core/trunk/org.vexi.core/src/org/vexi/core/Template.java
===================================================================
--- core/trunk/org.vexi.core/src/org/vexi/core/Template.java    2007-07-03 
17:37:32 UTC (rev 1913)
+++ core/trunk/org.vexi.core/src/org/vexi/core/Template.java    2007-07-03 
18:42:24 UTC (rev 1914)
@@ -100,15 +100,21 @@
     //////
     // Static part - outer most templates have a static part 
     Static staticPart = null;
-    class Static{ 
+    class Static implements Backtraceable{ 
        String sourceName;
                JS staticObject;
        Prefixes uriPrefixes;           // Uri prefixes->Blessings
-       public Static(String sourceName, Prefixes staticPrefixes) {
+       public Static(String sourceName){
+         this.sourceName = sourceName;
+       }
+       public void init(Prefixes staticPrefixes) {
                 staticObject = new JS.Obj();
                 uriPrefixes =staticPrefixes;
-                this.sourceName = sourceName;
                }
+
+               public String traceLine() {
+                       return sourceName + "(init)";
+               }
     }
 
     public JS getStatic(){
@@ -124,18 +130,21 @@
     
 
     // Static data/methods 
///////////////////////////////////////////////////////////////////
-
+    // FOOTNOTE:1
     // For when there has been a preapply (templates are chained in a list)
     Template(Template t, int startLine) { preapply = t; this.vexi = t.vexi; 
this.startLine = startLine; }
-    // For First template 
-    Template(JS vexi, int startLine) { this.vexi = vexi; this.startLine = 
startLine; }
+       // Named template, unresolved template
+       Template(JS vexi, String sourceName) {this.vexi = vexi; this.staticPart 
= new Static(sourceName);}
+       Template init(int startLine){this.startLine = startLine; return this;}
     // For Anon template
-    Template(JS vexi, int startLine, Template parent) { this(vexi, startLine); 
this.parent = parent; }
+    Template(JS vexi, int startLine, Template parent) { this.vexi = vexi; 
this.startLine = startLine; this.parent = parent; }
     
+       
+    
     // Methods to apply templates 
////////////////////////////////////////////////////////
 
-   
-    /** Applies the template to Box b
+
+       /** Applies the template to Box b
      *  @param pboxes a vector of all box parents on which to put $-references
      *  @param ptemplates a vector of the fileNames to recieve private 
references on the pboxes
      */
@@ -149,13 +158,33 @@
         }
     }
 
+    private static Template preapply(Box b, Template t) throws JSExn, 
IOException{
+       if (t != null){
+               // make sure we have resolved
+               if(t.staticPart.staticObject==null){
+                       Thread.getCurrentInterpreter().enterNonJSCall( 
t.staticPart);
+                       try{
+                               t = 
((Blessing)Vexi.resolveString(t.vexi,t.staticPart.sourceName, 
false)).initTemplate(t);
+                }finally{
+                       Thread.getCurrentInterpreter().exitNonJSCall();
+                }
+               }
+               t.apply(b, null);
+        }
+       return t;
+    }
+    
     private static final JS[] callempty = new JS[0];
     private void apply(Box b, PerInstantiationScope parentPis) throws JSExn, 
IOException {
        Thread.getCurrentInterpreter().enterNonJSCall( this);
        try{
-               if (preapply != null) preapply.apply(b, null);
-               if (principal != null) principal.apply(b, null);
+               // REMARK - the preapplies may not have been resolved yet, 
+               // the resolved template is not necessarily the same object.
+               // FOOTNOTE:2
+               preapply = preapply(b, preapply);
+               principal = preapply(b, principal);
 
+
                // FIXME this dollar stuff is all wrong
                if (id != null && parentPis!=null) parentPis.putDollar(id, b);
 
@@ -194,10 +223,7 @@
                        }
                        b.putAndTriggerTraps(key, val);
                }
-        }/*catch(JSExn e){
-               Log.uError("", fileName() +":" +startLine);
-               throw e;
-        }*/finally{
+        }finally{
                Thread.getCurrentInterpreter().exitNonJSCall();
         }
     }
@@ -211,13 +237,7 @@
        return parent.fileName();
     }
 
-    // XML Parsing 
/////////////////////////////////////////////////////////////////
 
-    /*public static Template buildTemplate(String sourceName, JS s, Vexi vexi) 
throws IOException, JSExn, XML.Exn {
-       return new TemplateHelper(sourceName, s, vexi).t;
-
-    }*/
-
     /**
      * Contains list of xml namespace prefixes. The <vexi/> and
      * each template node has its xml namespace converted into JS objects
@@ -412,3 +432,38 @@
     int content_start = 0;         ///< line number of the first line of 
<tt>content</tt>
     int startLine = -1;            ///< the line number that this element 
starts on
 }
+
+
+/* FOOTNOTES
+ * 1.
+ * Templates form a tree in the general case. For example for a template file
+ * t.t:
+ * 
+ * <p> ... <p/>
+ * <q> ... <q/>
+ * <r> ... <r/>
+ *     ...
+ * It is actually a linked list of templates. Typically one may be considered
+ * the main (with code inside) and the others just pre/post applies. Actually
+ * there is no distinction. Its just a list of templates with an ordering.
+ * 
+ * If the element name resolves to another template then there it is inheriting
+ * from a named template (i.e. from a file), this is the principal. 
+ * 
+ * 
+ * 
+ * t1-->  t2-->  t3 
+ *  \      \      \
+ *   p - .. q - .. r --> ...
+ *    \       \      \
+ *     ...     ...    ...
+ *     
+ * Application order is depth first, right to left. 
+ * 
+ * A leaf is an element that resolves to a <ui:box/> and has no preapplies.
+ *  
+ * 2.
+ * The principal (see diagram above) is only resolved at during the first
+ * access to its files information (either the static object or because of an
+ * apply).
+ */

Modified: core/trunk/org.vexi.core/src/org/vexi/core/TemplateBuilder.java
===================================================================
--- core/trunk/org.vexi.core/src/org/vexi/core/TemplateBuilder.java     
2007-07-03 17:37:32 UTC (rev 1913)
+++ core/trunk/org.vexi.core/src/org/vexi/core/TemplateBuilder.java     
2007-07-03 18:42:24 UTC (rev 1914)
@@ -19,6 +19,7 @@
 import org.ibex.util.Vec;
 import org.ibex.util.XML;
 import org.vexi.core.Template.Prefixes;
+import org.vexi.core.Template.Static;
 
 
 public class TemplateBuilder{
@@ -29,9 +30,9 @@
                TemplateBuilder.instance = instance;
        }
        
-       public static Template build(String sourceName, JS s, JS vexi) throws 
IOException, JSExn, XML.Exn {
+       public static Template build(Template unresolved, JS s) throws 
IOException, JSExn, XML.Exn {
                if(instance==null) instance = new TemplateBuilder();
-       return instance.new TemplateHelper(sourceName, s, vexi).t;
+       return instance.new TemplateHelper(unresolved, s).t;
     }
 
     static private void insertNewLines(StringBuffer content, int nLines){
@@ -50,9 +51,8 @@
        
        
 /** handles XML parsing; builds a Template tree as it goes */
-    final class TemplateHelper extends XML implements Backtraceable {
+    final class TemplateHelper extends XML{
 
-        String sourceName;
         private int state = STATE_INITIAL;
 
         CodeBlock staticCode = new CodeBlock();
@@ -61,43 +61,48 @@
         int meta_start = -1;  // Used in calculating endLines to add to 
static_content
         
         Vec nodeStack = new Vec();
+        Template unresolved = null;
         Template t = null;
         CodeBlock cb = null;
         int meta = 0;
-        JS vexi;
 
-        public TemplateHelper(String sourceName, JS s, JS vexi) throws 
XML.Exn, IOException, JSExn {
+        public TemplateHelper(Template unresolved, JS s) throws XML.Exn, 
IOException, JSExn {
                try{
-                       this.sourceName = sourceName;
-                       this.vexi = vexi;
+                       this.unresolved = unresolved;
                        InputStream is = JSU.getInputStream(s);
                        
                        if (is != null) {
                                parse(new InputStreamReader(is));
-                               
if(Log.sLevel==Log.DEBUG)Log.uDebug(TemplateHelper.class, "Parsing template: " 
+ sourceName);
+                               
if(Log.sLevel==Log.DEBUG)Log.uDebug(TemplateHelper.class, "Parsing template: " 
+ sourceName());
                                JS staticScript = 
parseScript(staticCode.content, staticCode.content_start, 
createStaticChecker(staticPrefixes));
                                if (t != null) {
-                                       createStatic(sourceName);
+                                       initStatic();
                                        JS staticScope = t.new StaticScope();
                                        if (staticScript != null) 
JSU.cloneWithNewGlobalScope(staticScript, staticScope).call(null, callempty);
                                }else{
-                                       Log.uWarn(LOG_TYPE, "'" + sourceName + 
".t' does not declare a template");
+                                       Log.uWarn(LOG_TYPE, "'" +  
unresolved.staticPart.sourceName + ".t' does not declare a template");
                                }
                        }
                }catch(XML.Exn e){
                        // XML class is not aware of the name of the source,
                        // so we add it here
-                       e.setSourceName(sourceName);
+                       e.setSourceName(sourceName());
                        throw e;
                }
         }
 
-        private void createStatic(String sourceName){
-                       t.staticPart = t.new Static(sourceName, staticPrefixes);
-                       // Share static part with preapplies 
+        final private String sourceName(){ return 
unresolved.staticPart.sourceName; }
+        final private JS vexi(){ return unresolved.vexi; }
+        
+        
+        private void initStatic(){
+               Static staticPart = unresolved.staticPart;
+               staticPart.init(staticPrefixes);
+               // Share static part with preapplies 
                Template t2 = t;
-               while((t2=t2.preapply)!=null){
-                       t2.staticPart = t.staticPart;
+               while(t2!=null){
+                       t2.staticPart = staticPart;
+                       t2=t2.preapply;
                }
         }
         
@@ -106,7 +111,7 @@
                JS parseScript(StringBuffer content, int content_start, 
GlobalsChecker parserParam) throws IOException {
             if (content == null) return null;
             String contentString = content.toString();
-            if (contentString.trim().length() > 0) return 
Parser.fromReader(sourceName, content_start, new StringReader(contentString), 
parserParam);
+            if (contentString.trim().length() > 0) return 
Parser.fromReader(sourceName(), content_start, new StringReader(contentString), 
parserParam);
             return null;
         }
 
@@ -122,7 +127,7 @@
                     if (a.attrSize() != 0)
                         throw new XML.Exn("root element must not have 
attributes", XML.Exn.SCHEMA, getLine(), getCol());
                     state = STATE_IN_ROOT_NODE;
-                    staticPrefixes = new Prefixes(vexi,null, c);
+                    staticPrefixes = new Prefixes(vexi(),null, c);
                     cb = staticCode;
                     // Check uri declarations. Warn if no default namespace 
created
                        // TODO improve feedback from resultant mistakes so 
that this
@@ -147,44 +152,27 @@
                     }
                     state = STATE_IN_TEMPLATE_NODE;
                     parentPrefixes = staticPrefixes;
-                    cb = t = (t == null) ? new Template(vexi, getLine()) : new 
Template(t, getLine());
+                    cb = t = (t == null) ? 
+                               unresolved.init(getLine()) 
+                               : new Template(t, getLine());
                     break;
                 case STATE_IN_TEMPLATE_NODE:
                     nodeStack.addElement(t);
                     parentPrefixes = t.uriPrefixes;
-                    cb = t = new Template(vexi, getLine(),t);
+                    cb = t = new Template(vexi(), getLine(),t);
                     break;
             }
 
-            // FIXME: This is all wrong
+            // // FIXME: This is all wrong
+            // Hopefully less wrong now.
             if (!("vexi://ui".equals(c.getUri()) && 
"box".equals(c.getLocalName()))) {
                 String tagname = (c.getUri() == null || "".equals(c.getUri()) 
? "" :
                                  (c.getUri() + ".")) + c.getLocalName();
-                // GROSS hack
-                try {
-                    // GROSSER hack
-                    // t.prev2 = (Template)t.vexi.resolveString(tagname, 
false).call(null, null, null, null, 9999);
-                    if(t.principal != null) throw new Error("FIXME: 
t.principal != null");
-                               
Thread.getCurrentInterpreter().enterNonJSCall(this);
-                    try{
-                       t.principal = 
((Blessing)Vexi.resolveString(t.vexi,tagname, false)).getTemplate();
-                    }finally{
-                       Thread.getCurrentInterpreter().exitNonJSCall(); 
-                    }
-                    if(t.principal == null) throw new SourceException("" + 
tagname + " not found", sourceName, getLine());
-                }catch(SourceException e){
-                       Log.uError(e.getMessageSig(), e.getWhere() + "\n- "+ 
e.getMessage());
-                       Log.debug(this, e);
-                }catch(JSExn e){ 
-                       Log.uError(Template.class, e);
-                }
-                catch (Exception e) {
-                    Log.uError(Template.class, e + " at " + this.sourceName + 
", line " + this.getLine() + ".");
-                }
+                t.principal = new Template(t.vexi, tagname);
             }
 
             // JS Code has access to namespace objects
-            t.uriPrefixes = new Prefixes(vexi, parentPrefixes, c);
+            t.uriPrefixes = new Prefixes(vexi(), parentPrefixes, c);
 
             // FIXME: 2-value Array
             Basket.Array keys = new Basket.Array(a.attrSize());
@@ -333,9 +321,5 @@
                }
                }
 
-               public String traceLine() {
-                       int l = getLine() ;
-                       return sourceName + ":" + getLine() + "(init)";
-               }
     }
 }
\ No newline at end of file

Modified: core/trunk/org.vexi.core/src_junit/test/core/template/_r.t
===================================================================
--- core/trunk/org.vexi.core/src_junit/test/core/template/_r.t  2007-07-03 
17:37:32 UTC (rev 1913)
+++ core/trunk/org.vexi.core/src_junit/test/core/template/_r.t  2007-07-03 
18:42:24 UTC (rev 1914)
@@ -4,6 +4,7 @@
        .util..assertEquals(false,.resolution_order..inited_b);
        .util..assertEquals(false,.resolution_order..inited_a);
     <_rb>
+           vexi.log.info(.resolution_order[""]);
            .util..assertEquals(true,.resolution_order..inited_b);
            .util..assertEquals(false,.resolution_order..inited_a);
     </_rb>

Modified: core/trunk/org.vexi.core/src_junit/test/core/template/_ra.t
===================================================================
--- core/trunk/org.vexi.core/src_junit/test/core/template/_ra.t 2007-07-03 
17:37:32 UTC (rev 1913)
+++ core/trunk/org.vexi.core/src_junit/test/core/template/_ra.t 2007-07-03 
18:42:24 UTC (rev 1914)
@@ -1,6 +1,6 @@
 <vexi xmlns:ui="vexi://ui" xmlns="">
        
-       .resolution_order.inited_a = true;
+       .resolution_order..inited_a = true;
     
     <ui:box/>
 </vexi>

Modified: core/trunk/org.vexi.core/src_junit/test/core/template/_rb.t
===================================================================
--- core/trunk/org.vexi.core/src_junit/test/core/template/_rb.t 2007-07-03 
17:37:32 UTC (rev 1913)
+++ core/trunk/org.vexi.core/src_junit/test/core/template/_rb.t 2007-07-03 
18:42:24 UTC (rev 1914)
@@ -1,6 +1,8 @@
 <vexi xmlns:ui="vexi://ui" xmlns="">
-       
-       .resolution_order.inited_b = true;
+       vexi.log.info("_rb!");
+       .resolution_order..inited_b = true;
+       vexi.log.info(.resolution_order[""]);
+       vexi.log.info("_rb!!");
 
     <ui:box/>
 </vexi>


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Vexi-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to