KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsp.java;
30
31 import com.caucho.vfs.WriteStream;
32 import com.caucho.xml.QName;
33 import com.caucho.xml.XmlChar;
34
35 import java.io.IOException JavaDoc;
36
37 /**
38  * Represents static text.
39  */

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

58   public String JavaDoc getText()
59   {
60     return _text;
61   }
62
63   /**
64    * sets the text.
65    */

66   public void setText(String JavaDoc text)
67   {
68     _text = text;
69   }
70   
71   /**
72    * Return true if the node only has static text.
73    */

74   public boolean isStatic()
75   {
76     return true;
77   }
78
79   /**
80    * Returns true if whitespace.
81    */

82   public boolean isWhitespace()
83   {
84     String JavaDoc text = _text;
85       
86     for (int i = text.length() - 1; i >= 0; i--) {
87       if (! XmlChar.isWhitespace(text.charAt(i)))
88     return false ;
89     }
90
91     return true;
92   }
93
94   /**
95    * Generates the XML text representation for the tag validation.
96    *
97    * @param os write stream to the generated XML.
98    */

99   public void printXml(WriteStream os)
100     throws IOException JavaDoc
101   {
102     printXmlText(os, _text);
103   }
104
105   /**
106    * Generates the code for the static text
107    *
108    * @param out the output writer for the generated java.
109    */

110   public void generate(JspJavaWriter out)
111     throws Exception JavaDoc
112   {
113     out.addText(_text);
114   }
115
116   /**
117    * Generates the code for the static text
118    *
119    * @param out the output writer for the generated java.
120    */

121   public void generateStatic(JspJavaWriter out)
122     throws Exception JavaDoc
123   {
124     out.print(_text);
125   }
126
127   /**
128    * Generates text from a string.
129    *
130    * @param out the output writer for the generated java.
131    * @param string the text to generate.
132    * @param offset the offset into the text.
133    * @param length the length of the text.
134    */

135   private void generateText(JspJavaWriter out, String JavaDoc text,
136                             int offset, int length)
137     throws IOException JavaDoc
138   {
139     
140     if (length > 32000) {
141       generateText(out, text, offset, 16 * 1024);
142       generateText(out, text, offset + 16 * 1024, length - 16 * 1024);
143       return;
144     }
145
146     text = text.substring(offset, offset + length);
147     int index = _gen.addString(text);
148     
149     out.print("out.write(_jsp_string" + index + ", 0, ");
150     out.println("_jsp_string" + index + ".length);");
151   }
152 }
153
Popular Tags