X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FTemplate.java;h=6ee6f0b605214e7ee1fff5a7cdfd5b137ab4da23;hb=52ffc2a48fc74d013ae7fa401d9d0d405543c38e;hp=324fea1b37057d8233a5a9207a9add789f58b9fc;hpb=66fa691f3f037006be09e5abfe747bcbbe4fe2c1;p=org.ibex.core.git diff --git a/src/org/xwt/Template.java b/src/org/xwt/Template.java index 324fea1..6ee6f0b 100644 --- a/src/org/xwt/Template.java +++ b/src/org/xwt/Template.java @@ -5,7 +5,7 @@ import java.io.*; import java.util.zip.*; import java.util.*; import java.lang.*; -import org.mozilla.javascript.*; +import org.xwt.js.*; import org.xwt.util.*; /** @@ -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 Script staticscript = null; + private JS.CompiledFunction staticscript = null; /** the script on this node */ - private 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(); @@ -115,19 +115,19 @@ public class Template { } public static Template buildTemplate(InputStream is, String nodeName) { + return buildTemplate(is, nodeName, new TemplateHelper()); + } + + public static Template buildTemplate(InputStream is, String nodeName, TemplateHelper t) { try { - return new Template(is, nodeName); - } catch (XML.SAXParseException e) { - if (Log.on) Log.log(Template.class, "error parsing template at " + nodeName + ":" + e.getLineNumber() + "," + e.getColumnNumber()); - if (Log.on) Log.log(Template.class, e); - return null; - } catch (XML.SAXException e) { + return new Template(is, nodeName, t); + } catch (XML.SchemaException e) { if (Log.on) Log.log(Template.class, "error parsing template " + nodeName); - if (Log.on) Log.log(Template.class, e); + if (Log.on) Log.log(Template.class, e.getMessage()); return null; - } catch (TemplateException te) { - if (Log.on) Log.log(Template.class, "error parsing template " + nodeName); - if (Log.on) Log.log(Template.class, te); + } catch (XML.XMLException e) { + if (Log.on) Log.log(Template.class, "error parsing template at " + nodeName + ":" + 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 " + nodeName + " -- this should never happen"); @@ -143,9 +143,9 @@ public class Template { this.nodeName = nodeName; cache.put(nodeName, this); } - private Template(InputStream is, String nodeName) throws XML.SAXException, IOException { + private Template(InputStream is, String nodeName, TemplateHelper th) throws XML.XMLException, IOException { this(nodeName); - new TemplateHelper().parseit(is, this); + th.parseit(is, this); } /** calculates, caches, and returns an integer approximation of how long it will take to apply this template, including pre/post and children */ @@ -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, 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; @@ -178,7 +178,7 @@ public class Template { for(int i=0; ichange as needed */ @@ -368,20 +361,23 @@ public class Template { private void link(boolean force) { if (staticscript != null) try { - Scriptable s = Static.createStatic(nodeName, false); + JS.Scope s = Static.createStatic(nodeName, false); if (staticscript != null) { - Script temp = staticscript; - ((InterpretedScript)temp).setParentScope(s); // so we know how to handle Static.get("xwt") + JS.CompiledFunction temp = staticscript; staticscript = null; - temp.exec(Context.enter(), s); + + // 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 (EcmaError e) { - if (Log.on) Log.log(this, "WARNING: uncaught interpreter exception: " + e.getMessage()); - if (Log.on) Log.log(this, " thrown while executing block for " + nodeName + - " at " + e.getSourceName() + ":" + e.getLineNumber()); - } catch (JavaScriptException e) { + } catch (JS.Exn e) { if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e.getMessage()); - if (Log.on) Log.log(this, " thrown while executing block for " + nodeName + " at " + e.sourceFile + ":" + e.line); } if (!(force || (preapply != null && _preapply == null) || (postapply != null && _postapply == null))) return; @@ -410,16 +406,27 @@ public class Template { // XML Parsing ///////////////////////////////////////////////////////////////// /** handles XML parsing; builds a Template tree as it goes */ - private static class TemplateHelper extends XML { + static final class TemplateHelper extends XML { - TemplateHelper() { - for(int i=0; iroot */ - void parseit(InputStream is, Template root) throws XML.SAXException, IOException { + void parseit(InputStream is, Template root) throws XML.XMLException, IOException { + rootNodeHasBeenEncountered = false; + templateNodeHasBeenEncountered = false; + staticNodeHasBeenEncountered = false; + templateNodeHasBeenFinished = false; + nameOfHeaderNodeBeingProcessed = null; + + nodeStack.setSize(0); + importlist.setSize(0); + preapply.setSize(0); + postapply.setSize(0); + + importlist.fromArray(defaultImportList); + t = root; - parse(new TabAndMaxColumnEnforcingReader(new InputStreamReader(is), root.nodeName)); + parse(new InputStreamReader(is)); } /** parsing state: true iff we have already encountered the open-tag */ @@ -453,79 +460,79 @@ public class Template { /** the template we're currently working on */ Template t = null; - public void startElement(String name, String[] keys, Object[] vals, int line, int col) throws XML.SAXException { - + public void startElement(XML.Element c) throws XML.SchemaException { if (templateNodeHasBeenFinished) { - throw new XML.SAXException("no elements may appear after the