X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FTemplate.java;h=22c994c2f78586a6640dd2f515392ece4e8acb5c;hb=bc0d2f2d898adb7b0e207ac4d9ddfde1b408230a;hp=7ff09ce1033ab932484760c98a4b9804303c8f76;hpb=fea7f27cc252c8feaafb8a5c0d4b9f391f03975d;p=org.ibex.core.git
diff --git a/src/org/xwt/Template.java b/src/org/xwt/Template.java
index 7ff09ce..22c994c 100644
--- a/src/org/xwt/Template.java
+++ b/src/org/xwt/Template.java
@@ -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, Function callback, int numerator, int denominator) {
+
+ int original_numerator = numerator;
if (pboxes == null) {
pboxes = new Vec();
@@ -185,10 +191,15 @@ public class Template {
link();
for(int i=0; _preapply != null && i<_preapply.length; i++)
- if (_preapply[i] != null) _preapply[i].apply(b, null, null);
+ if (_preapply[i] != null) {
+ _preapply[i].apply(b, null, null, callback, numerator, denominator);
+ numerator += _preapply[i].numUnits();
+ }
- for (int i=0; children != null && 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 */
@@ -411,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 + "\"");
}
@@ -492,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
@@ -567,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;
@@ -575,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
@@ -660,8 +668,7 @@ public class Template {
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;
}
@@ -671,74 +678,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); }
}
}