X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FTemplate.java;h=e7e72b5b5e88a3a6f9c473b3ba4f1cc8ed61247e;hb=81c4e615c6f9c9daddd24f4696bbd911a13c958d;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..e7e72b5 100644 --- a/src/org/xwt/Template.java +++ b/src/org/xwt/Template.java @@ -21,107 +21,75 @@ 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 JS.CompiledFunction 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 JS.Scope staticScope = null; ///< the scope in which the static block is executed + private JS.CompiledFunction 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 content_lines = 0; ///< number of lines in content + private int startLine = -1; ///< the line number that this element starts on + private final Res r; ///< the resource we came from // Static data/methods /////////////////////////////////////////////////////////////////// - private Template(String fileName) { this.fileName = fileName; } - public static Template getTemplate(Res r) { 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()); + } catch (Exception e) { + if (Log.on) Log.log(r.t.fileName, e); 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); + } + } + + public static Res resolveStringToResource(String str, XWT xwt, boolean permitAbsolute) { + // URL + if (str.indexOf("://") != -1) { + if (permitAbsolute) return Res.stringToRes(str); + Log.log(Template.class, "absolute URL " + str + " not permitted here"); return null; } + + // root-relative + Res ret = xwt.rr; + while(str.indexOf('.') != -1) { + String path = str.substring(0, str.indexOf('.')); + str = str.substring(str.indexOf('.') + 1); + ret = (Res)ret.get(path); + } + ret = (Res)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 && iroot */ void parseit(InputStream is, Template root) throws XML.XMLException, IOException { - rootNodeHasBeenEncountered = false; - templateNodeHasBeenEncountered = false; - staticNodeHasBeenEncountered = false; - templateNodeHasBeenFinished = false; + 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