001: /*
002: * Text.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: import java.util.ArrayList;
024: import java.util.List;
025:
026: /**
027: * The <code>Text</code> token is used to parse plain text from the
028: * JSP source. This will basically break the provided token up into
029: * individual lines and push print statements into the definition.
030: * Ther is no specific syntax for the text token, it is simply text.
031: *
032: * @author Niall Gallagher
033: */
034: class Text extends Token {
035:
036: /**
037: * This is used to collect the data parsed from the lines.
038: */
039: private TokenBuffer line;
040:
041: /**
042: * This is used to collect the lines parsed from the text.
043: */
044: private List list;
045:
046: /**
047: * Constructor for the <code>Text</code> token. This will
048: * create a buffer, which can be used to accumulate the data
049: * extracted from the supplied text token.
050: */
051: public Text() {
052: this .line = new TokenBuffer();
053: this .list = new ArrayList();
054: }
055:
056: /**
057: * Constructor for the <code>Text</code> token. This will
058: * create a buffer, which can be used to accumulate the data
059: * extracted from the supplied text token before parsing.
060: *
061: * @param token this is the text token to be parsed
062: */
063: public Text(String token) {
064: this ();
065: parse(token);
066: }
067:
068: /**
069: * This is used to push the lines from the text token into the
070: * document definition. This will iterate over the lines and add
071: * them in the order they were encounterd within the source text.
072: *
073: * @param source this is the document definition to populate
074: * @param builder this is the builder driving the process
075: */
076: public void process(Definition source, Builder builder) {
077: for (int i = 0; i < list.size(); i++) {
078: source.addContent("out.print(\"" + list.get(i) + "\");");
079: }
080: }
081:
082: /**
083: * This will clear the line token so that the parse can be reused
084: * by the builder. In practice this method just satisfies the
085: * contract of the token so that this object is not abstract.
086: */
087: protected void init() {
088: line.clear();
089: list.clear();
090: }
091:
092: /**
093: * This method is used to break the text up into individual lines
094: * using the CR or LF to delimit the end of the line. Once all
095: * lines have be extracted from the text this object will contain
096: * a list of lines equivelant to the text extracted.
097: */
098: protected void parse() {
099: while (off < count) {
100: line();
101: insert();
102: }
103: }
104:
105: /**
106: * This will seek a CR or LF from the text token. Once either a
107: * CR, '\r', or LF, '\n', has been extracted it is stored within
108: * the line buffer and inserted into the list of lines.
109: */
110: private void line() {
111: while (off < count) {
112: char next = buf[off++];
113:
114: if (next == '"') {
115: line.append("\\\"");
116: } else if (next == '\\') {
117: line.append("\\\\");
118: } else if (next == '\r') {
119: line.append("\\r");
120: break;
121: } else if (next == '\n') {
122: line.append("\\n");
123: break;
124: } else {
125: line.append(next);
126: }
127: }
128: }
129:
130: /**
131: * This will insert the line buffer into the list of lines for the
132: * text. A line is entered only if that line is greater than zero,
133: * once inserted into the list the line buffer is cleared.
134: */
135: private void insert() {
136: if (line.length() > 0) {
137: list.add(line.text());
138: line.clear();
139: }
140: }
141: }
|