001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jsp.java;
031:
032: import com.caucho.util.CharBuffer;
033: import com.caucho.vfs.WriteStream;
034: import com.caucho.xml.QName;
035: import com.caucho.xml.XmlChar;
036:
037: import java.io.IOException;
038:
039: /**
040: * Represents static text.
041: */
042: public class StaticText extends JspNode {
043: private static final QName TEXT = new QName("jsp", "text", JSP_NS);
044:
045: private String _text;
046:
047: public StaticText(JavaJspGenerator gen, String text, JspNode parent) {
048: if (gen == null)
049: throw new NullPointerException();
050:
051: setGenerator(gen);
052: setQName(TEXT);
053: setParent(parent);
054:
055: _text = text;
056: }
057:
058: /**
059: * Gets the text.
060: */
061: public String getText() {
062: return _text;
063: }
064:
065: /**
066: * sets the text.
067: */
068: public void setText(String text) {
069: _text = text;
070: }
071:
072: /**
073: * Return true if the node only has static text.
074: */
075: public boolean isStatic() {
076: return true;
077: }
078:
079: /**
080: * Returns the static text.
081: */
082: public void getStaticText(CharBuffer cb) {
083: cb.append(_text);
084: }
085:
086: /**
087: * Returns true if whitespace.
088: */
089: public boolean isWhitespace() {
090: String text = _text;
091:
092: for (int i = text.length() - 1; i >= 0; i--) {
093: if (!XmlChar.isWhitespace(text.charAt(i)))
094: return false;
095: }
096:
097: return true;
098: }
099:
100: /**
101: * Generates the XML text representation for the tag validation.
102: *
103: * @param os write stream to the generated XML.
104: */
105: public void printXml(WriteStream os) throws IOException {
106: os.print("<jsp:text");
107: printJspId(os);
108: os.print(">");
109:
110: printXmlText(os, _text);
111: os.print("</jsp:text>");
112: }
113:
114: /**
115: * Generates the start location.
116: */
117: public void generateStartLocation(JspJavaWriter out)
118: throws IOException {
119: out.setLocation(_filename, _startLine);
120: }
121:
122: /**
123: * Generates the end location.
124: */
125: public void generateEndLocation(JspJavaWriter out)
126: throws IOException {
127: out.setLocation(_filename, _endLine);
128: }
129:
130: /**
131: * Generates the code for the static text
132: *
133: * @param out the output writer for the generated java.
134: */
135: public void generate(JspJavaWriter out) throws Exception {
136: out.addText(_text);
137: }
138:
139: /**
140: * Generates the code for the static text
141: *
142: * @param out the output writer for the generated java.
143: */
144: public void generateStatic(JspJavaWriter out) throws Exception {
145: out.print(_text);
146: }
147:
148: /**
149: * Generates text from a string.
150: *
151: * @param out the output writer for the generated java.
152: * @param string the text to generate.
153: * @param offset the offset into the text.
154: * @param length the length of the text.
155: */
156: private void generateText(JspJavaWriter out, String text,
157: int offset, int length) throws IOException {
158:
159: if (length > 32000) {
160: generateText(out, text, offset, 16 * 1024);
161: generateText(out, text, offset + 16 * 1024,
162: length - 16 * 1024);
163: return;
164: }
165:
166: text = text.substring(offset, offset + length);
167:
168: if (length == 1) {
169: int ch = text.charAt(0);
170:
171: out.print("out.write('");
172: switch (ch) {
173: case '\\':
174: out.print("\\\\");
175: break;
176: case '\'':
177: out.print("\\'");
178: break;
179: case '\n':
180: out.print("\\n");
181: break;
182: case '\r':
183: out.print("\\r");
184: break;
185: default:
186: out.print((char) ch);
187: break;
188: }
189:
190: out.println("');");
191: } else {
192: int index = _gen.addString(text);
193:
194: out.print("out.write(_jsp_string" + index + ", 0, ");
195: out.println("_jsp_string" + index + ".length);");
196: }
197: }
198: }
|