1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
5 import java.util.zip.*;
8 import org.mozilla.javascript.*;
12 * Encapsulates a template node (the <template/> element of a
13 * .xwt file, or any child element thereof). Each instance of
14 * Template has a <tt>nodeName</tt> -- this is the resource name of
15 * the file that the template node occurs in, concatenated with the
16 * path from the root element to this node, each step of which is in
17 * the form .n for some integer n. Static nodes use the string "._"
20 * Note that the Template instance corresponding to the
21 * <template/> node carries all the header information -- hence
22 * some of the instance members are not meaningful on non-root
23 * Template instances. We refer to these non-root instances as
24 * <i>anonymous templates</i>.
26 * See the XWT reference for information on the order in which
27 * templates are applied, attributes are put, and scripts are run.
29 public class Template {
31 // Instance Members ///////////////////////////////////////////////////////
33 /** this instance's nodeName */
36 /** the id of the redirect target; only meaningful on a root node */
37 String redirect = null;
39 /** templates that should be preapplied (in the order of application); only meaningful on a root node */
40 private String[] preapply;
42 /** 'linked' form of preapply -- the String references have been resolved into instance references */
43 private Template[] _preapply = null;
45 /** templates that should be postapplied (in the order of application); only meaningful on a root node */
46 private String[] postapply;
48 /** 'linked' form of postapply -- the String references have been resolved into instance references */
49 private Template[] _postapply = null;
51 /** keys to be "put" to instances of this template; elements correspond to those of vals */
52 private String[] keys;
54 /** values to be "put" to instances of this template; elements correspond to those of keys */
55 private Object[] vals;
57 /** array of strings representing the importlist for this template */
58 private String[] importlist;
60 /** child template objects */
61 private Template[] children;
63 /** an array of the names of properties to be preserved when retheming; only meaningful on a root node */
64 private String[] preserve = null;
66 /** the <tt>id</tt> attribute on this node */
67 private String id = "";
69 /** see numUnits(); -1 means that this value has not yet been computed */
70 private int numunits = -1;
72 /** true iff the resolution of this template's preapply/postapply sets changed as a result of the most recent call to retheme() */
73 private boolean changed = false;
75 /** the script on the static node of this template, null if it has already been executed */
76 private Script staticscript = null;
78 /** the script on this node */
79 private Script script = null;
81 /** during XML parsing, this holds the list of currently-parsed children; null otherwise */
82 private Vec childvect = new Vec();
84 /** during XML parsing, this holds partially-read character data; null otherwise */
85 private StringBuffer content = null;
87 /** line number of the first line of <tt>content</tt> */
88 private int content_start = 0;
90 /** number of lines in <tt>content</tt> */
91 private int content_lines = 0;
94 // Static data/methods ///////////////////////////////////////////////////////////////////
96 /** a template cache so that only one Template object is created for each xwt */
97 private static Hashtable cache = new Hashtable(1000);
99 /** The default importlist; in future revisions this will contain "xwt.*" */
100 public static final String[] defaultImportList = new String[] { };
102 /** returns the appropriate template, resolving and theming as needed */
103 public static Template getTemplate(String name, String[] importlist) {
104 String resolved = Resources.resolve(name + ".xwt", importlist);
105 Template t = resolved == null ? null : (Template)cache.get(resolved.substring(0, resolved.length() - 4));
106 if (t != null) return t;
107 if (resolved == null) return null;
109 // note that Templates in xwar's are instantiated as read in via loadStream() --
110 // the following code only runs when XWT is reading templates from a filesystem.
111 ByteArrayInputStream bais = new ByteArrayInputStream(Resources.getResource(resolved));
112 return buildTemplate(bais, resolved.substring(0, resolved.length() - 4));
115 public static Template buildTemplate(InputStream is, String nodeName) {
117 return new Template(is, nodeName);
118 } catch (XML.SAXParseException e) {
119 if (Log.on) Log.log(Template.class, "error parsing template at " + nodeName + ":" + e.getLineNumber() + "," + e.getColumnNumber());
120 if (Log.on) Log.log(Template.class, e);
122 } catch (XML.SAXException e) {
123 if (Log.on) Log.log(Template.class, "error parsing template " + nodeName);
124 if (Log.on) Log.log(Template.class, e);
126 } catch (TemplateException te) {
127 if (Log.on) Log.log(Template.class, "error parsing template " + nodeName);
128 if (Log.on) Log.log(Template.class, te);
130 } catch (IOException e) {
131 if (Log.on) Log.log(Template.class, "IOException while parsing template " + nodeName + " -- this should never happen");
132 if (Log.on) Log.log(Template.class, e);
138 // Methods to apply templates ////////////////////////////////////////////////////////
140 private Template() { }
141 private Template(InputStream is, String nodeName) throws XML.SAXException, IOException {
142 this.nodeName = nodeName;
143 cache.put(nodeName, this);
144 new TemplateHelper().parseit(is, this);
147 /** calculates, caches, and returns an integer approximation of how long it will take to apply this template, including pre/post and children */
150 if (numunits != -1) return numunits;
152 for(int i=0; _preapply != null && i<_preapply.length; i++) if (_preapply[i] != null) numunits += _preapply[i].numUnits();
153 for(int i=0; _postapply != null && i<_postapply.length; i++) if (_postapply[i] != null) numunits += _postapply[i].numUnits();
154 if (script != null) numunits += 10;
155 numunits += keys == null ? 0 : keys.length;
156 for(int i=0; children != null && i<children.length; i++) numunits += children[i].numUnits();
160 /** Applies the template to Box b
161 * @param pboxes a vector of all box parents on which to put $-references
162 * @param ptemplates a vector of the nodeNames to recieve private references on the pboxes
164 void apply(Box b, Vec pboxes, Vec ptemplates) {
166 if (pboxes == null) {
168 ptemplates = new Vec();
171 if (id != null && !id.equals(""))
172 for(int i=0; i<pboxes.size(); i++) {
173 Box parent = (Box)pboxes.elementAt(i);
174 String parentNodeName = (String)ptemplates.elementAt(i);
175 parent.putPrivately("$" + id, b, parentNodeName);
178 if (script != null || (redirect != null && !"self".equals(redirect))) {
179 pboxes.addElement(b);
180 ptemplates.addElement(nodeName);
183 int numids = pboxes.size();
187 for(int i=0; _preapply != null && i<_preapply.length; i++)
188 if (_preapply[i] != null) _preapply[i].apply(b, null, null);
190 for (int i=0; children != null && i<children.length; i++)
191 b.put(Integer.MAX_VALUE, null, new Box(children[i], pboxes, ptemplates));
193 // whom to redirect to; doesn't take effect until after script runs
195 if (redirect != null && !"self".equals(redirect))
196 redir = (Box)b.getPrivately("$" + redirect, nodeName);
198 if (script != null) try {
199 Context cx = Context.enter();
201 } catch (EcmaError e) {
202 if (Log.on) Log.log(this, "WARNING: uncaught interpreter exception: " + e.getMessage());
203 if (Log.on) Log.log(this, " thrown while instantiating " + nodeName + " at " + e.getSourceName() + ":" + e.getLineNumber());
204 } catch (JavaScriptException e) {
205 if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e.getMessage());
206 if (Log.on) Log.log(this, " thrown while instantiating " + nodeName + " at " + e.sourceFile + ":" + e.line);
209 for(int i=0; keys != null && i<keys.length; i++)
210 if (keys[i] == null) { }
211 else if (keys[i].equals("border") || keys[i].equals("image")) {
212 if (vals[i].startsWith("http://") || vals[i].startsWith("https://")) {
213 b.put(keys[i], null, s.substring(0, s.length() - 4));
215 String s = Resources.resolve(vals[i].toString() + ".png", importlist);
216 if (s != null) b.put(keys[i], null, s.substring(0, s.length() - 4));
217 else if (Log.on) Log.log(this, "unable to resolve image " + vals[i].toString() + " referenced in attributes of " + nodeName);
220 else b.put(keys[i], null, vals[i]);
222 if (redirect != null && !"self".equals(redirect)) b.redirect = redir;
224 for(int i=0; _postapply != null && i<_postapply.length; i++)
225 if (_postapply[i] != null) _postapply[i].apply(b, null, null);
227 pboxes.setSize(numids);
228 ptemplates.setSize(numids);
230 Main.instantiatedUnits += 1 + (script == null ? 0 : 10) + (keys == null ? 0 : keys.length);
231 Main.updateSplashScreen();
235 // Theming Logic ////////////////////////////////////////////////////////////
237 /** helper method to recursively gather up the list of keys to be preserved */
238 private void gatherPreserves(Vec v) {
239 for(int i=0; preserve != null && i<preserve.length; i++) v.addElement(preserve[i]);
240 for(int i=0; _preapply != null && i<_preapply.length; i++) if (_preapply[i] != null) _preapply[i].gatherPreserves(v);
241 for(int i=0; _postapply != null && i<_postapply.length; i++) if (_postapply[i] != null) _postapply[i].gatherPreserves(v);
244 /** adds a theme mapping, retemplatizing as needed */
245 public static void retheme(String from, String to) {
246 if (Log.on) Log.log(Template.class, "retheming from " + from + " to " + to);
248 Resources.mapFrom.addElement(from);
249 Resources.mapTo.addElement(to);
251 // clear changed marker and relink
252 Template[] t = new Template[cache.size()];
253 Enumeration e = cache.elements();
254 for(int i=0; e.hasMoreElements(); i++) t[i] = (Template)e.nextElement();
255 for(int i=0; i<t.length; i++) {
256 t[i].changed = false;
261 for(int i=0; i<Surface.allSurfaces.size(); i++) {
262 Box b = ((Surface)Surface.allSurfaces.elementAt(i)).root;
263 if (b != null) reapply(b);
267 /** template reapplication procedure */
268 private static void reapply(Box b) {
270 // Ref 7.5.1: check if we need to retemplatize
271 boolean retemplatize = false;
272 if (b.templatename != null) {
273 Template t = getTemplate(b.templatename, b.importlist);
274 if (t != b.template) retemplatize = true;
277 if (b.template != null && b.template.changed)
282 // Ref 7.5.2: "Preserve all properties on the box mentioned in the <preserve> elements of any
283 // of the templates which would be applied in step 7."
284 Vec keys = new Vec();
285 b.template.gatherPreserves(keys);
286 Object[] vals = new Object[keys.size()];
287 for(int i=0; i<keys.size(); i++) vals[i] = b.get(((String)keys.elementAt(i)), null);
289 // Ref 7.5.3: "Remove and save all children of the box, or its redirect target, if it has one"
291 if (b.redirect != null) {
292 kids = new Box[b.redirect.numChildren()];
293 for(int i=b.redirect.numChildren() - 1; i >= 0; i--) {
294 kids[i] = b.redirect.getChild(i);
299 // Ref 7.5.4: "Set the box's redirect target to self"
302 // Ref 7.5.5: "Remove all of the box's immediate children"
303 for(Box cur = b.getChild(b.numChildren() - 1); cur != null;) {
305 cur = cur.prevSibling();
309 // Ref 7.5.6: "Remove all traps set by scripts run during the application of any template to this box"
310 Trap.removeAllTrapsByBox(b);
312 // Ref 7.5.7: "Apply the template to the box according to the usual application procedure"
313 b.template.apply(b, null, null);
315 // Ref 7.5.8: "Re-add the saved children which were removed in step 3"
316 for(int i=0; kids != null && i<kids.length; i++) b.put(Integer.MAX_VALUE, null, kids[i]);
318 // Ref 7.5.9: "Re-put any property values which were preserved in step 2"
319 for(int i=0; i<keys.size(); i++) b.put((String)keys.elementAt(i), null, vals[i]);
323 for(Box j = b.getChild(0); j != null; j = j.nextSibling()) reapply(j);
326 /** runs statics, resolves string references to other templates into actual Template instance references, and sets <tt>change</tt> as needed */
327 void link() { link(false); }
329 /** same as link(), except that with a true value, it will force a re-link */
330 private void link(boolean force) {
332 if (staticscript != null) try {
333 Scriptable s = Static.getStatic(nodeName);
334 if (staticscript != null) {
335 Script temp = staticscript;
336 ((InterpretedScript)temp).setParentScope(s); // so we know how to handle Static.get("xwt")
338 temp.exec(Context.enter(), s);
340 } catch (EcmaError e) {
341 if (Log.on) Log.log(this, "WARNING: uncaught interpreter exception: " + e.getMessage());
342 if (Log.on) Log.log(this, " thrown while executing <static/> block for " + nodeName +
343 " at " + e.getSourceName() + ":" + e.getLineNumber());
344 } catch (JavaScriptException e) {
345 if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e.getMessage());
346 if (Log.on) Log.log(this, " thrown while executing <static/> block for " + nodeName + " at " + e.sourceFile + ":" + e.line);
349 if (!(force || (preapply != null && _preapply == null) || (postapply != null && _postapply == null))) return;
351 if (preapply != null) {
352 if (_preapply == null) _preapply = new Template[preapply.length];
353 for(int i=0; i<_preapply.length; i++) {
354 Template t = getTemplate(preapply[i], importlist);
355 if (t != _preapply[i]) changed = true;
359 if (postapply != null) {
360 if (_postapply == null) _postapply = new Template[postapply.length];
361 for(int i=0; i<_postapply.length; i++) {
362 Template t = getTemplate(postapply[i], importlist);
363 if (t != _postapply[i]) changed = true;
368 for(int i=0; children != null && i<children.length; i++) children[i].link(force);
372 // XML Parsing /////////////////////////////////////////////////////////////////
374 /** handles XML parsing; builds a Template tree as it goes */
375 private static class TemplateHelper extends XML {
378 for(int i=0; i<defaultImportList.length; i++) importlist.addElement(defaultImportList[i]);
381 /** parse an XML input stream, building a Template tree off of <tt>root</tt> */
382 void parseit(InputStream is, Template root) throws XML.SAXException, IOException {
384 parse(new TabAndMaxColumnEnforcingReader(new InputStreamReader(is), root.nodeName));
387 /** parsing state: true iff we have already encountered the <xwt> open-tag */
388 boolean rootNodeHasBeenEncountered = false;
390 /** parsing state: true iff we have already encountered the <template> open-tag */
391 boolean templateNodeHasBeenEncountered = false;
393 /** parsing state: true iff we have already encountered the <static> open-tag */
394 boolean staticNodeHasBeenEncountered = false;
396 /** parsing state: true iff we have already encountered the <template> close-tag */
397 boolean templateNodeHasBeenFinished = false;
399 /** parsing state: If we have encountered the open tag of a header node, but not the close tag, this is the name of
400 * that tag; otherwise, it is null. */
401 String nameOfHeaderNodeBeingProcessed = null;
403 /** stack of Templates whose XML elements we have seen open-tags for but not close-tags */
404 Vec nodeStack = new Vec();
406 /** builds up the list of imports */
407 Vec importlist = new Vec();
409 /** builds up the list of preapplies */
410 Vec preapply = new Vec();
412 /** builds up the list of postapplies */
413 Vec postapply = new Vec();
415 /** the template we're currently working on */
418 public void startElement(String name, String[] keys, Object[] vals, int line, int col) throws XML.SAXException {
420 if (templateNodeHasBeenFinished) {
421 throw new XML.SAXException("no elements may appear after the <template> node");
423 } else if (!rootNodeHasBeenEncountered) {
424 if (!"xwt".equals(name)) throw new XML.SAXException("root element was not <xwt>");
425 if (keys.length != 0) throw new XML.SAXException("root element must not have attributes");
426 rootNodeHasBeenEncountered = true;
429 } else if (!templateNodeHasBeenEncountered) {
430 if (nameOfHeaderNodeBeingProcessed != null) throw new XML.SAXException("can't nest header nodes");
431 nameOfHeaderNodeBeingProcessed = name;
433 if (name.equals("import")) {
434 if (keys.length != 1 || !keys[0].equals("name"))
435 throw new XML.SAXException("<import> node must have exactly one attribute, which must be called 'name'");
436 String importpackage = vals[0].toString();
437 if (importpackage.endsWith(".*")) importpackage = importpackage.substring(0, importpackage.length() - 2);
438 importlist.addElement(importpackage);
441 } else if (name.equals("redirect")) {
442 if (keys.length != 1 || !keys[0].equals("target"))
443 throw new XML.SAXException("<redirect> node must have exactly one attribute, which must be called 'target'");
444 if (t.redirect != null)
445 throw new XML.SAXException("the <redirect> header element may not appear more than once");
446 t.redirect = vals[0].toString();
449 } else if (name.equals("preapply")) {
450 if (keys.length != 1 || !keys[0].equals("name"))
451 throw new XML.SAXException("<preapply> node must have exactly one attribute, which must be called 'name'");
452 preapply.addElement(vals[0]);
455 } else if (name.equals("postapply")) {
456 if (keys.length != 1 || !keys[0].equals("name"))
457 throw new XML.SAXException("<postapply> node must have exactly one attribute, which must be called 'name'");
458 postapply.addElement(vals[0]);
461 } else if (name.equals("static")) {
462 if (staticNodeHasBeenEncountered)
463 throw new XML.SAXException("the <static> header node may not appear more than once");
465 throw new XML.SAXException("the <static> node may not have attributes");
466 staticNodeHasBeenEncountered = true;
469 } else if (name.equals("preserve")) {
470 if (keys.length != 1 || !keys[0].equals("attributes"))
471 throw new XML.SAXException("<preserve> node must have exactly one attribute, which must be called 'attributes'");
472 if (t.preserve != null)
473 throw new XML.SAXException("<preserve> header element may not appear more than once");
475 StringTokenizer tok = new StringTokenizer(vals[0].toString(), ",", false);
476 t.preserve = new String[tok.countTokens()];
477 for(int i=0; i<t.preserve.length; i++) t.preserve[i] = tok.nextToken();
480 } else if (name.equals("template")) {
481 // finalize importlist/preapply/postapply, since they can't change from here on
482 importlist.toArray(t.importlist = new String[importlist.size()]);
483 if (preapply.size() > 0) preapply.copyInto(t.preapply = new String[preapply.size()]);
484 if (postapply.size() > 0) postapply.copyInto(t.postapply = new String[postapply.size()]);
485 importlist = preapply = postapply = null;
486 templateNodeHasBeenEncountered = true;
489 throw new XML.SAXException("unrecognized header node \"" + name + "\"");
495 // push the last node we were in onto the stack
496 nodeStack.addElement(t);
498 // instantiate a new node, and set its nodeName/importlist/preapply
499 Template t2 = new Template();
500 t2.nodeName = t.nodeName + "." + t.childvect.size();
501 t2.importlist = t.importlist;
502 if (!name.equals("box")) t2.preapply = new String[] { name };
504 // make the new node the current node
512 quickSortAttributes(0, t.keys.length - 1);
514 for(int i=0; i<t.keys.length; i++) {
515 if (t.keys[i].equals("id")) {
516 t.id = vals[i].toString().intern();
521 t.keys[i] = t.keys[i].intern();
523 String valString = vals[i].toString();
525 if (valString.equals("true")) t.vals[i] = Boolean.TRUE;
526 else if (valString.equals("false")) t.vals[i] = Boolean.FALSE;
527 else if (valString.equals("null")) t.vals[i] = null;
529 boolean hasNonNumeral = false;
530 boolean periodUsed = false;
531 for(int j=0; j<valString.length(); j++)
532 if (j == 0 && valString.charAt(j) == '-') {
533 } else if (valString.charAt(j) == '.' && !periodUsed && j != valString.length() - 1) {
535 } else if (!Character.isDigit(valString.charAt(j))) {
536 hasNonNumeral = true;
539 if (valString.length() > 0 && !hasNonNumeral) vals[i] = new Double(valString);
540 else vals[i] = valString.intern();
543 // bump thisbox to the front of the pack
544 if (t.keys[i].equals("thisbox")) {
545 t.keys[i] = t.keys[0];
546 t.keys[0] = "thisbox";
547 Object o = t.vals[0];
548 t.vals[0] = t.vals[i];
554 /** simple quicksort, from http://sourceforge.net/snippet/detail.php?type=snippet&id=100240 */
555 private int partitionAttributes(int left, int right) {
557 middle = (left + right) / 2;
558 String s = t.keys[right]; t.keys[right] = t.keys[middle]; t.keys[middle] = s;
559 Object o = t.vals[right]; t.vals[right] = t.vals[middle]; t.vals[middle] = o;
560 for (i = left - 1, j = right; ; ) {
561 while (t.keys[++i].compareTo(t.keys[right]) < 0);
562 while (j > left && t.keys[--j].compareTo(t.keys[right]) > 0);
564 s = t.keys[i]; t.keys[i] = t.keys[j]; t.keys[j] = s;
565 o = t.vals[i]; t.vals[i] = t.vals[j]; t.vals[j] = o;
567 s = t.keys[right]; t.keys[right] = t.keys[i]; t.keys[i] = s;
568 o = t.vals[right]; t.vals[right] = t.vals[i]; t.vals[i] = o;
572 /** simple quicksort, from http://sourceforge.net/snippet/detail.php?type=snippet&id=100240 */
573 private void quickSortAttributes(int left, int right) {
574 if (left >= right) return;
575 int p = partitionAttributes(left, right);
576 quickSortAttributes(left, p - 1);
577 quickSortAttributes(p + 1, right);
580 public void endElement(String name, int line, int col) throws XML.SAXException {
582 boolean hasNonWhitespace = false;
584 int len = t == null || t.content == null ? 0 : t.content.length();
585 for(int i=0; t.content != null && i<len; i++)
587 // ignore double-slash comment blocks
588 if (t.content.charAt(i) == '/' && t.content.charAt(i + 1) == '/') {
589 while(t.content.charAt(i) != '\n' && i<len) i++;
592 // ignore /* .. */ comment blocks
593 } else if (i<len - 1 && t.content.charAt(i) == '/' && t.content.charAt(i + 1) == '*') {
595 while(i<len - 1 && !(t.content.charAt(i) == '*' && t.content.charAt(i + 1) == '/')) i++;
596 if (i<len - 1 && t.content.charAt(i) == '*' && t.content.charAt(i + 1) == '/') i += 2;
599 // check for named functions
600 } else if (i + 8 <= len && t.content.charAt(i) == 'f' && t.content.charAt(i+1) == 'u' &&
601 t.content.charAt(i+2) == 'n' && t.content.charAt(i+3) == 'c' && t.content.charAt(i+4) == 't' &&
602 t.content.charAt(i+5) == 'i' && t.content.charAt(i+6) == 'o' && t.content.charAt(i+7) == 'n') {
604 while(j<len && Character.isWhitespace(t.content.charAt(j))) j++;
605 if (j<len && t.content.charAt(j) != '(')
606 throw new XML.SAXException("named functions are not permitted in XWT -- instead of \"function foo() { ... }\"," +
607 " use \"foo = function() { ... }\"");
609 // replace " and " with " && "
610 } else if (i + 5 < len && Character.isWhitespace(t.content.charAt(i)) &&
611 t.content.charAt(i+1) == 'a' && t.content.charAt(i+2) == 'n' && t.content.charAt(i+3) == 'd' &&
612 Character.isWhitespace(t.content.charAt(i + 4))) {
613 t.content.setCharAt(i+1, '&');
614 t.content.setCharAt(i+2, '&');
615 t.content.setCharAt(i+3, ' ');
616 hasNonWhitespace = true;
618 // generic check for nonwhitespace
619 } else if (!Character.isWhitespace(t.content.charAt(i))) {
620 hasNonWhitespace = true;
624 if (rootNodeHasBeenEncountered && !templateNodeHasBeenEncountered) {
625 if ("static".equals(nameOfHeaderNodeBeingProcessed) && hasNonWhitespace) t.staticscript = genscript(true);
626 nameOfHeaderNodeBeingProcessed = null;
628 } else if (templateNodeHasBeenEncountered && !templateNodeHasBeenFinished) {
630 // turn our childvect into a Template[]
631 t.childvect.copyInto(t.children = new Template[t.childvect.size()]);
633 if (hasNonWhitespace) t.script = genscript(false);
635 if (nodeStack.size() == 0) {
637 templateNodeHasBeenFinished = true;
640 // add this template as a child of its parent
642 t = (Template)nodeStack.lastElement();
643 nodeStack.setSize(nodeStack.size() - 1);
644 t.childvect.addElement(oldt);
650 private Script genscript(boolean isstatic) {
651 Script thisscript = null;
652 Context cx = Context.enter();
653 cx.setOptimizationLevel(-1);
656 thisscript = cx.compileReader(null, new StringReader(t.content.toString()), t.nodeName + (isstatic ? "._" : ""), t.content_start, null);
657 } catch (EcmaError ee) {
658 if (Log.on) Log.log(this, ee.getMessage() + " at " + ee.getSourceName() + ":" + ee.getLineNumber());
660 } catch (EvaluatorException ee) {
661 if (Log.on) Log.log(this, " ERROR: " + ee.getMessage());
663 } catch (IOException ioe) {
664 if (Log.on) Log.log(this, "IOException while compiling script; this should never happen");
665 if (Log.on) Log.log(this, ioe);
675 public void content(char[] ch, int start, int length, int line, int col) throws XML.SAXException {
676 if ("static".equals(nameOfHeaderNodeBeingProcessed) || templateNodeHasBeenEncountered) {
677 int contentlines = 0;
678 for(int i=start; i<start + length; i++) if (ch[i] == '\n') contentlines++;
679 line -= contentlines;
681 if (t.content == null) {
682 t.content_start = line;
684 t.content = new StringBuffer();
687 for(int i=t.content_start + t.content_lines; i<line; i++) {
688 t.content.append('\n');
692 t.content.append(ch, start, length);
693 t.content_lines += contentlines;
695 } else if (nameOfHeaderNodeBeingProcessed != null) {
696 throw new XML.SAXException("header node <" + nameOfHeaderNodeBeingProcessed + "> cannot have text content");
704 /** a filtering reader that watches for tabs and long lines */
705 private static class TabAndMaxColumnEnforcingReader extends FilterReader {
706 private int MAX_COLUMN = 150;
707 private int column = 0;
708 private int line = 1;
709 private boolean lastCharWasCR = false;
710 private String filename;
711 public TabAndMaxColumnEnforcingReader(Reader r, String filename) { super(r); this.filename = filename; }
713 if (Log.on) Log.log(this, this.getClass().getName() + ".read() not supported, this should never happen");
716 public long skip(long numskip) {
717 if (Log.on) Log.log(this, this.getClass().getName() + ".skip() not supported; this should never happen");
720 public int read(char[] buf, int off, int len) throws IOException {
721 int ret = super.read(buf, off, len);
722 for(int i=off; i<off + ret; i++)
723 if (buf[i] == '\t') {
724 throw new TemplateException(filename + ":" + line + "," + column + ": tabs are not allowed in XWT files");
725 } else if (buf[i] == '\r') {
728 lastCharWasCR = true;
729 } else if (buf[i] == '\n') {
731 if (!lastCharWasCR) line++;
732 } else if (++column > MAX_COLUMN) {
733 throw new TemplateException(filename + ":" + line + ": lines longer than " + MAX_COLUMN + " characters not allowed");
735 lastCharWasCR = false;
741 private static class TemplateException extends IOException {
742 TemplateException(String s) { super(s); }