X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FTemplate.java;h=a9ea9b91502a90352a6c92d33b17de5d7435181e;hb=69ecee0bc41e6bd410572817e7c59d51f1b5b629;hp=0af2e0a4d87eee54f217aa6097ef21fbf678ac72;hpb=6242c991f365dbd67eba62ecfa5df769a83fcbc6;p=org.ibex.core.git
diff --git a/src/org/xwt/Template.java b/src/org/xwt/Template.java
index 0af2e0a..a9ea9b9 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.Script staticscript = null;
/** the script on this node */
- private Script script = null;
+ private JS.Script script = null;
/** during XML parsing, this holds the list of currently-parsed children; null otherwise */
private Vec childvect = new Vec();
@@ -90,6 +90,8 @@ public class Template {
/** number of lines in content */
private int content_lines = 0;
+ /** the line number that this element starts on */
+ private int startLine = -1;
// Static data/methods ///////////////////////////////////////////////////////////////////
@@ -113,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");
@@ -137,11 +139,13 @@ public class Template {
// Methods to apply templates ////////////////////////////////////////////////////////
- private Template() { }
- private Template(InputStream is, String nodeName) throws XML.SAXException, IOException {
+ private Template(String nodeName) {
this.nodeName = nodeName;
cache.put(nodeName, this);
- new TemplateHelper().parseit(is, this);
+ }
+ private Template(InputStream is, String nodeName, TemplateHelper th) throws XML.XMLException, IOException {
+ this(nodeName);
+ 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 */
@@ -161,7 +165,9 @@ 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) {
+ void apply(Box b, Vec pboxes, Vec ptemplates, JS.Function callback, int numerator, int denominator) {
+
+ int original_numerator = numerator;
if (pboxes == null) {
pboxes = new Vec();
@@ -172,10 +178,10 @@ public class Template {
for(int i=0; ichange as needed */
@@ -326,19 +363,25 @@ public class Template {
private void link(boolean force) {
if (staticscript != null) try {
- Scriptable s = Static.getStatic(nodeName);
+ JS.Scope s = Static.createStatic(nodeName, false);
if (staticscript != null) {
- Script temp = staticscript;
+ JS.Script 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);
+ } };
+
+ JS.Array args = new JS.Array();
+ args.addElement(varScope);
+ temp.call(args);
}
- } 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;
@@ -367,16 +410,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 */
@@ -410,78 +464,78 @@ 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 node");
+ throw new XML.SchemaException("no elements may appear after the node");
} else if (!rootNodeHasBeenEncountered) {
- if (!"xwt".equals(name)) throw new XML.SAXException("root element was not ");
- if (keys.length != 0) throw new XML.SAXException("root element must not have attributes");
+ if (!"xwt".equals(c.localName)) throw new XML.SchemaException("root element was not ");
+ if (c.len != 0) throw new XML.SchemaException("root element must not have attributes");
rootNodeHasBeenEncountered = true;
return;
} else if (!templateNodeHasBeenEncountered) {
- if (nameOfHeaderNodeBeingProcessed != null) throw new XML.SAXException("can't nest header nodes");
- nameOfHeaderNodeBeingProcessed = name;
+ if (nameOfHeaderNodeBeingProcessed != null) throw new XML.SchemaException("can't nest header nodes");
+ nameOfHeaderNodeBeingProcessed = c.localName;
- if (name.equals("import")) {
- if (keys.length != 1 || !keys[0].equals("name"))
- throw new XML.SAXException(" node must have exactly one attribute, which must be called 'name'");
- String importpackage = vals[0].toString();
+ if (c.localName.equals("import")) {
+ if (c.len != 1 || !c.keys[0].equals("name"))
+ throw new XML.SchemaException(" node must have exactly one attribute, which must be called 'name'");
+ String importpackage = c.vals[0].toString();
if (importpackage.endsWith(".*")) importpackage = importpackage.substring(0, importpackage.length() - 2);
importlist.addElement(importpackage);
return;
- } else if (name.equals("redirect")) {
- if (keys.length != 1 || !keys[0].equals("target"))
- throw new XML.SAXException(" node must have exactly one attribute, which must be called 'target'");
+ } else if (c.localName.equals("redirect")) {
+ if (c.len != 1 || !c.keys[0].equals("target"))
+ throw new XML.SchemaException(" node must have exactly one attribute, which must be called 'target'");
if (t.redirect != null)
- throw new XML.SAXException("the header element may not appear more than once");
- t.redirect = vals[0].toString();
+ throw new XML.SchemaException("the header element may not appear more than once");
+ t.redirect = c.vals[0].toString();
return;
- } else if (name.equals("preapply")) {
- if (keys.length != 1 || !keys[0].equals("name"))
- throw new XML.SAXException(" node must have exactly one attribute, which must be called 'name'");
- preapply.addElement(vals[0]);
+ } else if (c.localName.equals("preapply")) {
+ if (c.len != 1 || !c.keys[0].equals("name"))
+ throw new XML.SchemaException(" node must have exactly one attribute, which must be called 'name'");
+ preapply.addElement(c.vals[0]);
return;
- } else if (name.equals("postapply")) {
- if (keys.length != 1 || !keys[0].equals("name"))
- throw new XML.SAXException(" node must have exactly one attribute, which must be called 'name'");
- postapply.addElement(vals[0]);
+ } else if (c.localName.equals("postapply")) {
+ if (c.len != 1 || !c.keys[0].equals("name"))
+ throw new XML.SchemaException(" node must have exactly one attribute, which must be called 'name'");
+ postapply.addElement(c.vals[0]);
return;
- } else if (name.equals("static")) {
+ } else if (c.localName.equals("static")) {
if (staticNodeHasBeenEncountered)
- throw new XML.SAXException("the header node may not appear more than once");
- if (keys.length > 0)
- throw new XML.SAXException("the node may not have attributes");
+ throw new XML.SchemaException("the header node may not appear more than once");
+ if (c.len > 0)
+ throw new XML.SchemaException("the node may not have attributes");
staticNodeHasBeenEncountered = true;
return;
- } else if (name.equals("preserve")) {
- if (keys.length != 1 || !keys[0].equals("attributes"))
- throw new XML.SAXException(" node must have exactly one attribute, which must be called 'attributes'");
+ } else if (c.localName.equals("preserve")) {
+ if (c.len != 1 || !c.keys[0].equals("attributes"))
+ throw new XML.SchemaException(" node must have exactly one attribute, which must be called 'attributes'");
if (t.preserve != null)
- throw new XML.SAXException(" header element may not appear more than once");
+ throw new XML.SchemaException(" header element may not appear more than once");
- StringTokenizer tok = new StringTokenizer(vals[0].toString(), ",", false);
+ StringTokenizer tok = new StringTokenizer(c.vals[0].toString(), ",", false);
t.preserve = new String[tok.countTokens()];
for(int i=0; i 0) preapply.copyInto(t.preapply = new String[preapply.size()]);
if (postapply.size() > 0) postapply.copyInto(t.postapply = new String[postapply.size()]);
- importlist = preapply = postapply = null;
+ importlist.setSize(0); preapply.setSize(0); postapply.setSize(0);
templateNodeHasBeenEncountered = true;
} else {
- throw new XML.SAXException("unrecognized header node \"" + name + "\"");
+ throw new XML.SchemaException("unrecognized header node \"" + c.localName + "\"");
}
@@ -491,31 +545,33 @@ public class Template {
nodeStack.addElement(t);
// instantiate a new node, and set its nodeName/importlist/preapply
- Template t2 = new Template();
- t2.nodeName = t.nodeName + "." + t.childvect.size();
+ Template t2 = new Template(t.nodeName + "." + t.childvect.size());
t2.importlist = t.importlist;
- if (!name.equals("box")) t2.preapply = new String[] { name };
+ t2.startLine = getLine();
+ if (!c.localName.equals("box")) t2.preapply = new String[] { c.localName };
// make the new node the current node
t = t2;
}
- t.keys = keys;
- t.vals = vals;
-
+ // TODO: Sort contents straight from one array to another
+ t.keys = new String[c.len];
+ t.vals = new Object[c.len];
+ System.arraycopy(c.keys, 0, t.keys, 0, c.len);
+ System.arraycopy(c.vals, 0, t.vals, 0, c.len);
quickSortAttributes(0, t.keys.length - 1);
for(int i=0; i 0 && !hasNonNumeral) vals[i] = new Double(valString);
- else vals[i] = valString.intern();
+ if (valString.length() > 0 && !hasNonNumeral) t.vals[i] = new Double(valString);
+ else t.vals[i] = valString.intern();
}
// bump thisbox to the front of the pack
@@ -566,7 +619,7 @@ public class Template {
o = t.vals[right]; t.vals[right] = t.vals[i]; t.vals[i] = o;
return i;
}
-
+
/** simple quicksort, from http://sourceforge.net/snippet/detail.php?type=snippet&id=100240 */
private void quickSortAttributes(int left, int right) {
if (left >= right) return;
@@ -574,61 +627,17 @@ public class Template {
quickSortAttributes(left, p - 1);
quickSortAttributes(p + 1, right);
}
-
- public void endElement(String name, int line, int col) throws XML.SAXException {
-
- boolean hasNonWhitespace = false;
-
- int len = t == null || t.content == null ? 0 : t.content.length();
- for(int i=0; t.content != null && i
@@ -645,22 +654,15 @@ public class Template {
}
}
- private Script genscript(boolean isstatic) {
- Script thisscript = null;
- Context cx = Context.enter();
- cx.setOptimizationLevel(-1);
-
+ private JS.Script genscript(boolean isstatic) {
+ JS.Script thisscript = null;
try {
- thisscript = cx.compileReader(null, new StringReader(t.content.toString()), t.nodeName + (isstatic ? "._" : ""), t.content_start, null);
- } catch (EcmaError ee) {
- if (Log.on) Log.log(this, ee.getMessage() + " at " + ee.getSourceName() + ":" + ee.getLineNumber());
- thisscript = null;
- } catch (EvaluatorException ee) {
+ thisscript = JS.Script.parse(new StringReader(t.content.toString()), t.nodeName + (isstatic ? "._" : ""), t.content_start);
+ } catch (JS.Exn ee) {
if (Log.on) Log.log(this, " ERROR: " + ee.getMessage());
thisscript = null;
} catch (IOException ioe) {
- if (Log.on) Log.log(this, "IOException while compiling script; this should never happen");
- if (Log.on) Log.log(this, ioe);
+ if (Log.on) Log.log(this, " ERROR: " + ioe.getMessage());
thisscript = null;
}
@@ -670,74 +672,28 @@ public class Template {
return thisscript;
}
- public void content(char[] ch, int start, int length, int line, int col) throws XML.SAXException {
- if ("static".equals(nameOfHeaderNodeBeingProcessed) || templateNodeHasBeenEncountered) {
- int contentlines = 0;
- for(int i=start; ii; i++) if (ch[start+i] == '\t') throw new XML.SchemaException(
+ t.nodeName+ ":" + getLine() + "," + getCol() + ": tabs are not allowed in XWT files");
+ if ("static".equals(nameOfHeaderNodeBeingProcessed) || templateNodeHasBeenEncountered) {
if (t.content == null) {
- t.content_start = line;
+ t.content_start = getLine();
t.content_lines = 0;
t.content = new StringBuffer();
}
- for(int i=t.content_start + t.content_lines; i cannot have text content");
-
+ throw new XML.SchemaException("header node <" + nameOfHeaderNodeBeingProcessed + "> cannot have text content");
}
-
}
- }
-
- /** a filtering reader that watches for tabs and long lines */
- private static class TabAndMaxColumnEnforcingReader extends FilterReader {
- private int MAX_COLUMN = 150;
- private int column = 0;
- private int line = 1;
- private boolean lastCharWasCR = false;
- private String filename;
- public TabAndMaxColumnEnforcingReader(Reader r, String filename) { super(r); this.filename = filename; }
- public int read() {
- if (Log.on) Log.log(this, this.getClass().getName() + ".read() not supported, this should never happen");
- return -1;
+ public void whitespace(char[] ch, int start, int length) throws XML.SchemaException {
}
- public long skip(long numskip) {
- if (Log.on) Log.log(this, this.getClass().getName() + ".skip() not supported; this should never happen");
- return numskip;
- }
- public int read(char[] buf, int off, int len) throws IOException {
- int ret = super.read(buf, off, len);
- for(int i=off; i MAX_COLUMN) {
- throw new TemplateException(filename + ":" + line + ": lines longer than " + MAX_COLUMN + " characters not allowed");
- } else {
- lastCharWasCR = false;
- }
- return ret;
- }
- }
-
- private static class TemplateException extends IOException {
- TemplateException(String s) { super(s); }
}
}