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.java.JavaWriter;
033: import com.caucho.util.CharBuffer;
034: import com.caucho.vfs.WriteStream;
035:
036: import java.io.IOException;
037:
038: /**
039: * Writing class for generated Java code.
040: */
041: public class JspJavaWriter extends JavaWriter {
042: // The JSP generator
043: private JavaJspGenerator _gen;
044:
045: private String _filename;
046: private int _line = -1;
047:
048: // Current text node.
049: private CharBuffer _cb = CharBuffer.allocate();
050:
051: public JspJavaWriter(WriteStream os, JavaJspGenerator gen) {
052: super (os);
053:
054: _gen = gen;
055: }
056:
057: /**
058: * Adds text to the current buffer.
059: */
060: public void addText(String text) throws IOException {
061: if (_filename != null && _cb.length() == 0) {
062: super .setLocation(_filename, _line);
063: }
064:
065: _cb.append(text);
066: }
067:
068: /**
069: * Sets the source filename and line.
070: *
071: * @param filename the filename of the source file.
072: * @param line the line of the source file.
073: */
074: public void setLocation(String filename, int line)
075: throws IOException {
076: _filename = filename;
077: _line = line;
078: }
079:
080: /**
081: * Flushes text.
082: */
083:
084: /**
085: * Generates the code for the static text
086: *
087: * @param out the output writer for the generated java.
088: */
089: protected void flushText() throws IOException {
090: String filename = _filename;
091: int line = _line;
092: _filename = null;
093:
094: if (_cb.length() > 0) {
095: int length = _cb.length();
096: _cb.clear();
097: generateText(_cb.getBuffer(), 0, length);
098: }
099:
100: if (filename != null)
101: super .setLocation(filename, line);
102: }
103:
104: /**
105: * Generates text from a string.
106: *
107: * @param out the output writer for the generated java.
108: * @param string the text to generate.
109: * @param offset the offset into the text.
110: * @param length the length of the text.
111: */
112: private void generateText(char[] text, int offset, int length)
113: throws IOException {
114: if (length > 32000) {
115: generateText(text, offset, 16 * 1024);
116: generateText(text, offset + 16 * 1024, length - 16 * 1024);
117: return;
118: }
119:
120: if (length == 1) {
121: int ch = text[offset];
122:
123: print("out.write('");
124: switch (ch) {
125: case '\\':
126: print("\\\\");
127: break;
128: case '\'':
129: print("\\'");
130: break;
131: case '\n':
132: print("\\n");
133: break;
134: case '\r':
135: print("\\r");
136: break;
137: default:
138: print((char) ch);
139: break;
140: }
141:
142: println("');");
143: } else {
144: int index = _gen
145: .addString(new String(text, offset, length));
146:
147: print("out.write(_jsp_string" + index + ", 0, ");
148: println("_jsp_string" + index + ".length);");
149: }
150: }
151:
152: /**
153: * Prints a Java escaped string
154: */
155: public void printJavaString(String s) throws IOException {
156: flushText();
157:
158: super .printJavaString(s);
159: }
160:
161: /**
162: * Pushes an indentation depth.
163: */
164: public void pushDepth() throws IOException {
165: flushText();
166:
167: super .pushDepth();
168: }
169:
170: /**
171: * Pops an indentation depth.
172: */
173: public void popDepth() throws IOException {
174: flushText();
175:
176: super .popDepth();
177: }
178:
179: /**
180: * Prints a string
181: */
182: public void print(String s) throws IOException {
183: flushText();
184:
185: super .print(s);
186: }
187:
188: /**
189: * Prints a character.
190: */
191: public void print(char ch) throws IOException {
192: flushText();
193:
194: super .print(ch);
195: }
196:
197: /**
198: * Prints a boolean.
199: */
200: public void print(boolean b) throws IOException {
201: flushText();
202:
203: super .print(b);
204: }
205:
206: /**
207: * Prints an integer.
208: */
209: public void print(int i) throws IOException {
210: flushText();
211:
212: super .print(i);
213: }
214:
215: /**
216: * Prints an long
217: */
218: public void print(long l) throws IOException {
219: flushText();
220:
221: super .print(l);
222: }
223:
224: /**
225: * Prints an object.
226: */
227: public void print(Object o) throws IOException {
228: flushText();
229:
230: super .print(o);
231: }
232:
233: /**
234: * Prints a string with a new line
235: */
236: public void println(String s) throws IOException {
237: flushText();
238:
239: super .println(s);
240: }
241:
242: /**
243: * Prints a boolean with a new line
244: */
245: public void println(boolean v) throws IOException {
246: flushText();
247:
248: super .println(v);
249: }
250:
251: /**
252: * Prints a character.
253: */
254: public void println(char ch) throws IOException {
255: flushText();
256:
257: super .println(ch);
258: }
259:
260: /**
261: * Prints an integer with a new line
262: */
263: public void println(int v) throws IOException {
264: flushText();
265:
266: super .println(v);
267: }
268:
269: /**
270: * Prints an long with a new line
271: */
272: public void println(long v) throws IOException {
273: flushText();
274:
275: super .println(v);
276: }
277:
278: /**
279: * Prints an object with a new line
280: */
281: public void println(Object v) throws IOException {
282: flushText();
283:
284: super .println(v);
285: }
286:
287: /**
288: * Prints a newline
289: */
290: public void println() throws IOException {
291: flushText();
292:
293: super.println();
294: }
295: }
|