001: /*
002: * Print.java February 2006
003: *
004: * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General
016: * Public License along with this library; if not, write to the
017: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
018: * Boston, MA 02111-1307 USA
019: */
020:
021: package simple.page.translate;
022:
023: /**
024: * The <code>Print</code> object is used to process the JSP print
025: * token. This performs a very simple parsing of the provided token.
026: * The following token is evaluated by this token.
027: * <pre>
028: *
029: * <%= expression %>
030: *
031: * </pre>
032: * This above "expression" is wrapped within a print statement.
033: * This allows the generated page to print expression results.
034: *
035: * @author Niall Gallagher
036: */
037: class Print extends Token {
038:
039: /**
040: * This is used to collect the token parsed from the print.
041: */
042: private TokenBuffer print;
043:
044: /**
045: * Constructor for the <code>Print</code> token. This will
046: * create a buffer, which can be used to accumulate the data
047: * extracted from the supplied print token.
048: */
049: public Print() {
050: this .print = new TokenBuffer();
051: }
052:
053: /**
054: * Constructor for the <code>Print</code> token. This will
055: * create a buffer, which can be used to accumulate the data
056: * extracted from the supplied print token before parsing.
057: *
058: * @param token this is the print token to be parsed
059: */
060: public Print(String token) {
061: this ();
062: parse(token);
063: }
064:
065: /**
066: * This method will supply code to the document definition that
067: * will allow an expression to be printed by the page. The data
068: * inserted into the definition will be displayed in the body.
069: *
070: * @param source this is the source to push the code into
071: * @param builder this is the builder driving the process
072: */
073: public void process(Definition source, Builder builder) {
074: if (print.length() > 0) {
075: source.addContent("out.print(" + print + ");");
076: }
077: }
078:
079: /**
080: * This will clear the print token so that the parse can be reused
081: * by the builder. In practice this method just satisfies the
082: * contract of the token so that this object is not abstract.
083: */
084: protected void init() {
085: print.clear();
086: }
087:
088: /**
089: * This is a very simple parse method which basically extracts the
090: * begining and end values from the token. For instance this will
091: * remove "<%=" and "%>" from the token supplied.
092: */
093: protected void parse() {
094: if (skip("<%=")) {
095: while (off < count) {
096: if (skip("%>")) {
097: break;
098: }
099: print.append(buf[off++]);
100: }
101: }
102: }
103: }
|