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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsp.java;
030:
031: import com.caucho.vfs.WriteStream;
032: import com.caucho.xml.QName;
033: import com.caucho.xml.XmlChar;
034:
035: import java.io.IOException;
036:
037: /**
038: * Represents static text.
039: */
040: public class JspTextNode extends JspNode {
041: private String _text;
042:
043: public JspTextNode(JavaJspGenerator gen, String text, JspNode parent) {
044: if (gen == null)
045: throw new NullPointerException();
046:
047: setGenerator(gen);
048: setQName(new QName("jsp", "text", JSP_NS));
049: setParent(parent);
050:
051: _text = text;
052: }
053:
054: /**
055: * Gets the text.
056: */
057: public String getText() {
058: return _text;
059: }
060:
061: /**
062: * sets the text.
063: */
064: public void setText(String text) {
065: _text = text;
066: }
067:
068: /**
069: * Return true if the node only has static text.
070: */
071: public boolean isStatic() {
072: return true;
073: }
074:
075: /**
076: * Returns true if whitespace.
077: */
078: public boolean isWhitespace() {
079: String text = _text;
080:
081: for (int i = text.length() - 1; i >= 0; i--) {
082: if (!XmlChar.isWhitespace(text.charAt(i)))
083: return false;
084: }
085:
086: return true;
087: }
088:
089: /**
090: * Generates the XML text representation for the tag validation.
091: *
092: * @param os write stream to the generated XML.
093: */
094: public void printXml(WriteStream os) throws IOException {
095: printXmlText(os, _text);
096: }
097:
098: /**
099: * Generates the code for the static text
100: *
101: * @param out the output writer for the generated java.
102: */
103: public void generate(JspJavaWriter out) throws Exception {
104: out.addText(_text);
105: }
106:
107: /**
108: * Generates the code for the static text
109: *
110: * @param out the output writer for the generated java.
111: */
112: public void generateStatic(JspJavaWriter out) throws Exception {
113: out.print(_text);
114: }
115:
116: /**
117: * Generates text from a string.
118: *
119: * @param out the output writer for the generated java.
120: * @param string the text to generate.
121: * @param offset the offset into the text.
122: * @param length the length of the text.
123: */
124: private void generateText(JspJavaWriter out, String text,
125: int offset, int length) throws IOException {
126:
127: if (length > 32000) {
128: generateText(out, text, offset, 16 * 1024);
129: generateText(out, text, offset + 16 * 1024,
130: length - 16 * 1024);
131: return;
132: }
133:
134: text = text.substring(offset, offset + length);
135: int index = _gen.addString(text);
136:
137: out.print("out.write(_jsp_string" + index + ", 0, ");
138: out.println("_jsp_string" + index + ".length);");
139: }
140: }
|