X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FTemplate.java;h=f85f3a7ae8a4289da67f007af80fc28f389a5446;hb=9c2602143956cd39ecf5ef4c9eb31f5f56b5bd66;hp=95846c1ce446f132c2be6b627c1401701191d223;hpb=8235361e8601ae7b36ab707058de3b52225d15a2;p=org.ibex.core.git diff --git a/src/org/xwt/Template.java b/src/org/xwt/Template.java index 95846c1..f85f3a7 100644 --- a/src/org/xwt/Template.java +++ b/src/org/xwt/Template.java @@ -6,6 +6,7 @@ import java.util.zip.*; import java.util.*; import java.lang.*; import org.xwt.js.*; +import org.xwt.translators.*; import org.xwt.util.*; /** @@ -21,115 +22,86 @@ import org.xwt.util.*; * See the XWT reference for information on the order in which * templates are applied, attributes are put, and scripts are run. */ - -// FIXME imports public class Template { // Instance Members /////////////////////////////////////////////////////// - /** the id of this box */ - String id = null; - - /** the id of the redirect target; only meaningful on a root node */ - String redirect = null; - - /** templates that should be preapplied (in the order of application); only meaningful on a root node */ - private Template[] preapply; - - /** templates that should be postapplied (in the order of application); only meaningful on a root node */ - private Template[] postapply; - - /** keys to be "put" to instances of this template; elements correspond to those of vals */ - private String[] keys; + 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 Vec children = new Vec(); ///< during XML parsing, this holds the list of currently-parsed children; null otherwise + private int numunits = -1; ///< see numUnits(); -1 means that this value has not yet been computed - /** values to be "put" to instances of this template; elements correspond to those of keys */ - private Object[] vals; + private JSFunction script = null; ///< the script on this node + private String fileName = "unknown"; ///< the filename this node came from; used only for debugging + private Vec preapply = new Vec(); ///< templates that should be preapplied (in the order of application) - /** child template objects */ - private Template[] children; - /** see numUnits(); -1 means that this value has not yet been computed */ - private int numunits = -1; + // Instance Members that are only meaningful on root Template ////////////////////////////////////// - /** the scope in which the static block is executed */ - private JS.Scope staticScope = null; - - /** the script on the static node of this template, null if it has already been executed */ - private JS.CompiledFunction staticscript = null; - - /** the script on this node */ - private JS.CompiledFunction script = null; - - /** the filename this node came from; used only for debugging */ - private String fileName = "unknown"; + private JSScope staticScope = null; ///< the scope in which the static block is executed + private JSFunction staticscript = null; ///< the script on the static node of this template, null already performed // Only used during parsing ///////////////////////////////////////////////////////////////// - /** during XML parsing, this holds the list of currently-parsed children; null otherwise */ - private Vec childvect = new Vec(); - - /** during XML parsing, this holds partially-read character data; null otherwise */ - private StringBuffer content = null; - - /** line number of the first line of content */ - private int content_start = 0; - - /** number of lines in content */ - private int content_lines = 0; - - /** the line number that this element starts on */ - private int startLine = -1; + private StringBuffer content = null; ///< during XML parsing, this holds partially-read character data; null otherwise + private int content_start = 0; ///< line number of the first line of content + private int startLine = -1; ///< the line number that this element starts on + private final Stream r; ///< the resource we came from // Static data/methods /////////////////////////////////////////////////////////////////// - private Template(String fileName) { this.fileName = fileName; } - - public static Template getTemplate(Res r) { + // FIXME need to provide the XWT object too + public static Template getTemplate(Stream r) throws JSExn { try { + r = r.addExtension(".xwt"); if (r.t != null) return r.t; - r.t = new Template(r.getDescriptiveName()); + r.t = new Template(r); new TemplateHelper().parseit(r.getInputStream(), r.t); return r.t; - } catch (XML.SchemaException e) { - if (Log.on) Log.log(Template.class, "error parsing template " + r.t.fileName); - if (Log.on) Log.log(Template.class, e.getMessage()); - return null; - } catch (XML.XMLException e) { - if (Log.on) Log.log(Template.class, "error parsing template at " + r.t.fileName + ":" + e.getLine() + "," + e.getCol()); - if (Log.on) Log.log(Template.class, e.getMessage()); - return null; - } catch (IOException e) { - if (Log.on) Log.log(Template.class, "IOException while parsing template " + r.t.fileName + " -- this should never happen"); - if (Log.on) Log.log(Template.class, e); - return null; + } catch (Exception e) { + throw new JSExn("Error reading template stream: " + r + "\n" + e.toString()); + } + } + + public static Stream resolveStringToResource(String str, XWT xwt, boolean permitAbsolute) throws JSExn { + // URL + if (str.indexOf("://") != -1) { + if (permitAbsolute) return (Stream)xwt.url2res(str); + throw new JSExn("absolute URL " + str + " not permitted here"); + } + + // root-relative + Stream ret = xwt.rr; + while(str.indexOf('.') != -1) { + String path = str.substring(0, str.indexOf('.')); + str = str.substring(str.indexOf('.') + 1); + ret = (Stream)ret.get(path); } + ret = (Stream)ret.get(str); + return ret; } // Methods to apply templates //////////////////////////////////////////////////////// - /** calculates, caches, and returns an integer approximation of how long it will take to apply this template, - * including pre/post and children */ - int numUnits() { - if (numunits != -1) return numunits; - numunits = 1; - for(int i=0; preapply != null && i 0) { + switch (((String)val).charAt(0)) { + case '$': + val = pis.get(val); + if (val == null) throw new JSExn("unknown box id '"+vals[i]+"' referenced in XML attribute"); + break; + case '.': + val = resolveStringToResource(((String)val).substring(1), xwt, true); + } + } - if (callback != null) try { - 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 (val != null && "redirect".equals(key)) { + val = pis.get("$"+val); + if (val == null) throw new JSExn("redirect target '"+vals[i]+"' not found"); + } - if (Thread.currentThread() instanceof ThreadMessage) XWT.sleep(0); + try { + b.putAndTriggerTraps(key, val); + } catch(JSExn e) { + e.addBacktrace(fileName + ":attr-" + key,0); + throw e; + } + } } @@ -190,147 +182,114 @@ public class Template { TemplateHelper() { } + private int state; + private static final int STATE_INITIAL = 0; + private static final int STATE_IN_XWT_NODE = 1; + private static final int STATE_IN_TEMPLATE_NODE = 2; + private static final int STATE_FINISHED_TEMPLATE_NODE = 3; + + private String nameOfHeaderNodeBeingProcessed; + + Vec nodeStack = new Vec(); ///< stack of Templates whose XML elements we have seen open-tags for but not close-tags + Template t = null; ///< the template we're currently working on + /** parse an XML input stream, building a Template tree off of root */ - void parseit(InputStream is, Template root) throws XML.XMLException, IOException { - rootNodeHasBeenEncountered = false; - templateNodeHasBeenEncountered = false; - staticNodeHasBeenEncountered = false; - templateNodeHasBeenFinished = false; + void parseit(InputStream is, Template root) throws XML.Exn, IOException { + state = STATE_INITIAL; nameOfHeaderNodeBeingProcessed = null; - nodeStack.setSize(0); - preapply.setSize(0); - postapply.setSize(0); - t = root; parse(new InputStreamReader(is)); } - /** parsing state: true iff we have already encountered the open-tag */ - boolean rootNodeHasBeenEncountered = false; - - /** parsing state: true iff we have already encountered the