X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Forg%2Fxwt%2FTemplate.java;h=2cc62420e61021993a5953448a3eb93b2588140c;hb=16c24a73c1c1b2955db0bbbaf5a940215329bca1;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..2cc6242 100644 --- a/src/org/xwt/Template.java +++ b/src/org/xwt/Template.java @@ -1,4 +1,4 @@ -// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] package org.xwt; import java.io.*; @@ -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,316 +22,226 @@ 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; - - /** values to be "put" to instances of this template; elements correspond to those of keys */ - private Object[] vals; - - /** child template objects */ - private Template[] children; - - /** see numUnits(); -1 means that this value has not yet been computed */ - private int numunits = -1; - - /** 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"; + 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 String[] urikeys; + private String[] urivals; + private Vec children = new Vec(); ///< during XML parsing, this holds the list of currently-parsed children; null otherwise + private JS script = null; ///< the script on this node + Template prev; + JSScope staticScope = null; ///< the scope in which the static block is executed // 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 XWT xwt; // Static data/methods /////////////////////////////////////////////////////////////////// - private Template(String fileName) { this.fileName = fileName; } - - public static Template getTemplate(Res r) { - try { - if (r.t != null) return r.t; - r.t = new Template(r.getDescriptiveName()); - 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; - } - } - + // for non-root nodes + private Template(Template t, int startLine) { prev = t; this.xwt = t.xwt; this.startLine = startLine; } + private Template(XWT xwt) { this.xwt = xwt; } + // 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 = xwt.resolveString(((String)val).substring(1), false); + // FIXME: url case + // FIXME: should we be resolving all of these in the XML-parsing code? + } + } - for(int i=0; postapply != null && iroot */ - void parseit(InputStream is, Template root) throws XML.XMLException, IOException { - rootNodeHasBeenEncountered = false; - templateNodeHasBeenEncountered = false; - staticNodeHasBeenEncountered = false; - templateNodeHasBeenFinished = false; - nameOfHeaderNodeBeingProcessed = null; - - nodeStack.setSize(0); - preapply.setSize(0); - postapply.setSize(0); - - t = root; - parse(new InputStreamReader(is)); + public static Template buildTemplate(InputStream is, XWT xwt) { + try { + return new TemplateHelper(is, xwt).t; + } catch (Exception e) { + Log.error(Template.class, e); + return null; } + } - /** parsing state: true iff we have already encountered the open-tag */ - boolean rootNodeHasBeenEncountered = false; - - /** parsing state: true iff we have already encountered the