KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.CharBuffer;
33 import com.caucho.vfs.WriteStream;
34 import com.caucho.xml.QName;
35 import com.caucho.xml.XmlChar;
36
37 import java.io.IOException JavaDoc;
38
39 /**
40  * Represents static text.
41  */

42 public class StaticText extends JspNode {
43   private static final QName TEXT = new QName("jsp", "text", JSP_NS);
44     
45   private String JavaDoc _text;
46   
47   public StaticText(JavaJspGenerator gen, String JavaDoc text, JspNode parent)
48   {
49     if (gen == null)
50       throw new NullPointerException JavaDoc();
51     
52     setGenerator(gen);
53     setQName(TEXT);
54     setParent(parent);
55
56     _text = text;
57   }
58
59   /**
60    * Gets the text.
61    */

62   public String JavaDoc getText()
63   {
64     return _text;
65   }
66
67   /**
68    * sets the text.
69    */

70   public void setText(String JavaDoc text)
71   {
72     _text = text;
73   }
74   
75   /**
76    * Return true if the node only has static text.
77    */

78   public boolean isStatic()
79   {
80     return true;
81   }
82
83   /**
84    * Returns the static text.
85    */

86   public void getStaticText(CharBuffer cb)
87   {
88     cb.append(_text);
89   }
90
91   /**
92    * Returns true if whitespace.
93    */

94   public boolean isWhitespace()
95   {
96     String JavaDoc text = _text;
97       
98     for (int i = text.length() - 1; i >= 0; i--) {
99       if (! XmlChar.isWhitespace(text.charAt(i)))
100     return false ;
101     }
102
103     return true;
104   }
105
106   /**
107    * Generates the XML text representation for the tag validation.
108    *
109    * @param os write stream to the generated XML.
110    */

111   public void printXml(WriteStream os)
112     throws IOException JavaDoc
113   {
114     os.print("<jsp:text");
115     printJspId(os);
116     os.print(">");
117     
118     printXmlText(os, _text);
119     os.print("</jsp:text>");
120   }
121
122   /**
123    * Generates the start location.
124    */

125   public void generateStartLocation(JspJavaWriter out)
126     throws IOException JavaDoc
127   {
128     out.setLocation(_filename, _startLine);
129   }
130
131   /**
132    * Generates the end location.
133    */

134   public void generateEndLocation(JspJavaWriter out)
135     throws IOException JavaDoc
136   {
137     out.setLocation(_filename, _endLine);
138   }
139
140   /**
141    * Generates the code for the static text
142    *
143    * @param out the output writer for the generated java.
144    */

145   public void generate(JspJavaWriter out)
146     throws Exception JavaDoc
147   {
148     out.addText(_text);
149   }
150
151   /**
152    * Generates the code for the static text
153    *
154    * @param out the output writer for the generated java.
155    */

156   public void generateStatic(JspJavaWriter out)
157     throws Exception JavaDoc
158   {
159     out.print(_text);
160   }
161
162   /**
163    * Generates text from a string.
164    *
165    * @param out the output writer for the generated java.
166    * @param string the text to generate.
167    * @param offset the offset into the text.
168    * @param length the length of the text.
169    */

170   private void generateText(JspJavaWriter out, String JavaDoc text,
171                             int offset, int length)
172     throws IOException JavaDoc
173   {
174     
175     if (length > 32000) {
176       generateText(out, text, offset, 16 * 1024);
177       generateText(out, text, offset + 16 * 1024, length - 16 * 1024);
178       return;
179     }
180
181     text = text.substring(offset, offset + length);
182
183     if (length == 1) {
184       int ch = text.charAt(0);
185       
186       out.print("out.write('");
187       switch (ch) {
188       case '\\':
189         out.print("\\\\");
190     break;
191       case '\'':
192         out.print("\\'");
193     break;
194       case '\n':
195         out.print("\\n");
196     break;
197       case '\r':
198         out.print("\\r");
199     break;
200       default:
201     out.print((char) ch);
202     break;
203       }
204
205       out.println("');");
206     }
207     else {
208       int index = _gen.addString(text);
209     
210       out.print("out.write(_jsp_string" + index + ", 0, ");
211       out.println("_jsp_string" + index + ".length);");
212     }
213   }
214 }
215
Popular Tags