001: /*
002: * Insert.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>Insert</code> object is used to parse the insert token
025: * from the JSP. This will parse the token in such a way the data
026: * from the model can be inserted into the JSP page. The following
027: * token is parsed using this token implementation.
028: * <pre>
029: *
030: * <%@ insert name="example" %>
031: *
032: * </pre>
033: * This will push contents into the document definition for getting
034: * the named attribute from the page model and displaying it. The
035: * result is that this token allows attributes to be printed easily.
036: *
037: * @author Niall Gallagher
038: */
039: class Insert extends Token {
040:
041: /**
042: * This is used to collect the token parsed from the insert.
043: */
044: private TokenBuffer name;
045:
046: /**
047: * Constructor for the <code>Insert</code> token. This will
048: * create a buffer, which can be used to accumulate the data
049: * extracted from the supplied insert token.
050: */
051: public Insert() {
052: this .name = new TokenBuffer();
053: }
054:
055: /**
056: * Constructor for the <code>Insert</code> token. This will
057: * create a buffer, which can be used to accumulate the data
058: * extracted from the supplied insert token before parsing.
059: *
060: * @param token this is the insert token to be parsed
061: */
062: public Insert(String token) {
063: this ();
064: parse(token);
065: }
066:
067: /**
068: * This method will supply code to the document definition that
069: * will allow an attribute to be printed by the page. The data
070: * inserted into the definition will be displayed in the body.
071: *
072: * @param source this is the source to push the code into
073: * @param builder this is the builder driving the process
074: */
075: public void process(Definition source, Builder builder) {
076: source.addContent("model.write(out, \"" + name + "\");");
077: }
078:
079: /**
080: * This will clear the name 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: name.clear();
086: }
087:
088: /**
089: * This does not verify the token type, instead this will seek
090: * the '=' character. Once the '=' character has been encountered
091: * the name is extracted as a quoted string, for example "name".
092: */
093: protected void parse() {
094: scrap();
095: name();
096: }
097:
098: /**
099: * This is a quick and dirty means of parsing the token. This
100: * will basically seek the '=' character such the offset of
101: * the buffer is on the start of the name, like "[n]ame".
102: */
103: private void scrap() {
104: while (off < count) {
105: char next = buf[off];
106:
107: if (next == '=') {
108: while (off < count) {
109: next = buf[off++];
110: if (quote(next)) {
111: break;
112: }
113: }
114: break;
115: }
116: off++;
117: }
118: }
119:
120: /**
121: * This method will read all characters up to a space or the
122: * next quotation chatacter, for example "'" or '"' will be
123: * considered a terminal. Once this has finished the name will
124: * be stored in the internal name buffer for processing.
125: */
126: private void name() {
127: while (off < count) {
128: char next = buf[off++];
129:
130: if (space(next)) {
131: break;
132: } else if (quote(next)) {
133: break;
134: }
135: name.append(next);
136: }
137: }
138:
139: /**
140: * This is used to determine when the start and end of the name
141: * has been encountered. The terminals are '"' and '"', which
142: * are legal quotations within the JSP syntax.
143: *
144: * @param ch this is the character to be evaluated
145: *
146: * @return this returns true if the character is a quote
147: */
148: private boolean quote(char ch) {
149: return ch == '"' || ch == '\'';
150: }
151: }
|