KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > java > JspJavaWriter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp.java;
31
32 import com.caucho.java.JavaWriter;
33 import com.caucho.util.CharBuffer;
34 import com.caucho.vfs.WriteStream;
35
36 import java.io.IOException JavaDoc;
37
38 /**
39  * Writing class for generated Java code.
40  */

41 public class JspJavaWriter extends JavaWriter {
42   // The JSP generator
43
private JavaJspGenerator _gen;
44
45   private String JavaDoc _filename;
46   private int _line = -1;
47   
48   // Current text node.
49
private CharBuffer _cb = CharBuffer.allocate();
50
51   public JspJavaWriter(WriteStream os, JavaJspGenerator gen)
52   {
53     super(os);
54
55     _gen = gen;
56   }
57
58   /**
59    * Adds text to the current buffer.
60    */

61   public void addText(String JavaDoc text)
62     throws IOException JavaDoc
63   {
64     if (_filename != null && _cb.length() == 0) {
65       super.setLocation(_filename, _line);
66     }
67     
68     _cb.append(text);
69   }
70
71   /**
72    * Sets the source filename and line.
73    *
74    * @param filename the filename of the source file.
75    * @param line the line of the source file.
76    */

77   public void setLocation(String JavaDoc filename, int line)
78     throws IOException JavaDoc
79   {
80     _filename = filename;
81     _line = line;
82   }
83
84   /**
85    * Flushes text.
86    */

87
88   /**
89    * Generates the code for the static text
90    *
91    * @param out the output writer for the generated java.
92    */

93   protected void flushText()
94     throws IOException JavaDoc
95   {
96     String JavaDoc filename = _filename;
97     int line = _line;
98     _filename = null;
99     
100     if (_cb.length() > 0) {
101       int length = _cb.length();
102       _cb.clear();
103       generateText(_cb.getBuffer(), 0, length);
104     }
105
106     if (filename != null)
107       super.setLocation(filename, line);
108   }
109
110   /**
111    * Generates text from a string.
112    *
113    * @param out the output writer for the generated java.
114    * @param string the text to generate.
115    * @param offset the offset into the text.
116    * @param length the length of the text.
117    */

118   private void generateText(char []text, int offset, int length)
119     throws IOException JavaDoc
120   {
121     if (length > 32000) {
122       generateText(text, offset, 16 * 1024);
123       generateText(text, offset + 16 * 1024, length - 16 * 1024);
124       return;
125     }
126
127     if (length == 1) {
128       int ch = text[offset];
129       
130       print("out.write('");
131       switch (ch) {
132       case '\\':
133         print("\\\\");
134     break;
135       case '\'':
136         print("\\'");
137     break;
138       case '\n':
139         print("\\n");
140     break;
141       case '\r':
142         print("\\r");
143     break;
144       default:
145     print((char) ch);
146     break;
147       }
148
149       println("');");
150     }
151     else {
152       int index = _gen.addString(new String JavaDoc(text, offset, length));
153     
154       print("out.write(_jsp_string" + index + ", 0, ");
155       println("_jsp_string" + index + ".length);");
156     }
157   }
158
159   /**
160    * Prints a Java escaped string
161    */

162   public void printJavaString(String JavaDoc s)
163     throws IOException JavaDoc
164   {
165     flushText();
166
167     super.printJavaString(s);
168   }
169
170   /**
171    * Pushes an indentation depth.
172    */

173   public void pushDepth()
174     throws IOException JavaDoc
175   {
176     flushText();
177
178     super.pushDepth();
179   }
180
181   /**
182    * Pops an indentation depth.
183    */

184   public void popDepth()
185     throws IOException JavaDoc
186   {
187     flushText();
188
189     super.popDepth();
190   }
191
192   /**
193    * Prints a string
194    */

195   public void print(String JavaDoc s)
196     throws IOException JavaDoc
197   {
198     flushText();
199
200     super.print(s);
201   }
202
203   /**
204    * Prints a character.
205    */

206   public void print(char ch)
207     throws IOException JavaDoc
208   {
209     flushText();
210
211     super.print(ch);
212   }
213
214   /**
215    * Prints a boolean.
216    */

217   public void print(boolean b)
218     throws IOException JavaDoc
219   {
220     flushText();
221
222     super.print(b);
223   }
224
225   /**
226    * Prints an integer.
227    */

228   public void print(int i)
229     throws IOException JavaDoc
230   {
231     flushText();
232
233     super.print(i);
234   }
235
236   /**
237    * Prints an long
238    */

239   public void print(long l)
240     throws IOException JavaDoc
241   {
242     flushText();
243
244     super.print(l);
245   }
246
247   /**
248    * Prints an object.
249    */

250   public void print(Object JavaDoc o)
251     throws IOException JavaDoc
252   {
253     flushText();
254
255     super.print(o);
256   }
257
258   /**
259    * Prints a string with a new line
260    */

261   public void println(String JavaDoc s)
262     throws IOException JavaDoc
263   {
264     flushText();
265
266     super.println(s);
267   }
268
269   /**
270    * Prints a boolean with a new line
271    */

272   public void println(boolean v)
273     throws IOException JavaDoc
274   {
275     flushText();
276
277     super.println(v);
278   }
279
280   /**
281    * Prints a character.
282    */

283   public void println(char ch)
284     throws IOException JavaDoc
285   {
286     flushText();
287
288     super.println(ch);
289   }
290
291   /**
292    * Prints an integer with a new line
293    */

294   public void println(int v)
295     throws IOException JavaDoc
296   {
297     flushText();
298
299     super.println(v);
300   }
301
302   /**
303    * Prints an long with a new line
304    */

305   public void println(long v)
306     throws IOException JavaDoc
307   {
308     flushText();
309
310     super.println(v);
311   }
312
313   /**
314    * Prints an object with a new line
315    */

316   public void println(Object JavaDoc v)
317     throws IOException JavaDoc
318   {
319     flushText();
320
321     super.println(v);
322   }
323
324   /**
325    * Prints a newline
326    */

327   public void println()
328     throws IOException JavaDoc
329   {
330     flushText();
331
332     super.println();
333   }
334 }
335
Popular Tags