001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055: package com.sun.portal.providers.jsp.jasper3.jasper.compiler;
056:
057: import java.io.PrintWriter;
058: import java.io.BufferedReader;
059: import java.io.StringReader;
060: import java.io.IOException;
061:
062: /**
063: * This is what is used to generate servlets.
064: *
065: * @author Anil K. Vijendran
066: */
067: public class ServletWriter {
068: public static int TAB_WIDTH = 4;
069: public static String SPACES = " ";
070:
071: // Current indent level:
072: int indent = 0;
073:
074: // The sink writer:
075: PrintWriter writer;
076:
077: public ServletWriter(PrintWriter writer) {
078: this .writer = writer;
079: }
080:
081: public void close() throws IOException {
082: writer.close();
083: }
084:
085: public void pushIndent() {
086: if ((indent += TAB_WIDTH) > SPACES.length())
087: indent = SPACES.length();
088: }
089:
090: public void popIndent() {
091: if ((indent -= TAB_WIDTH) <= 0)
092: indent = 0;
093: }
094:
095: /**
096: * Print a standard comment for echo outputed chunk.
097: * @param start The starting position of the JSP chunk being processed.
098: * @param stop The ending position of the JSP chunk being processed.
099: */
100: public void printComment(Mark start, Mark stop, char[] chars) {
101: if (start != null && stop != null) {
102: println("// from=" + start);
103: println("// to=" + stop);
104: }
105:
106: if (chars != null)
107: for (int i = 0; i < chars.length;) {
108: indent();
109: print("// ");
110: while (chars[i] != '\n' && i < chars.length)
111: writer.print(chars[i++]);
112: }
113: }
114:
115: /**
116: * Quote the given string to make it appear in a chunk of java code.
117: * @param s The string to quote.
118: * @return The quoted string.
119: */
120:
121: public String quoteString(String s) {
122: // Turn null string into quoted empty strings:
123: if (s == null)
124: return "null";
125: // Hard work:
126: if (s.indexOf('"') < 0 && s.indexOf('\\') < 0
127: && s.indexOf('\n') < 0 && s.indexOf('\r') < 0)
128: return "\"" + s + "\"";
129: StringBuffer sb = new StringBuffer();
130: int len = s.length();
131: sb.append('"');
132: for (int i = 0; i < len; i++) {
133: char ch = s.charAt(i);
134: if (ch == '\\' && i + 1 < len) {
135: sb.append('\\');
136: sb.append('\\');
137: sb.append(s.charAt(++i));
138: } else if (ch == '"') {
139: sb.append('\\');
140: sb.append('"');
141: } else if (ch == '\n') {
142: sb.append("\\n");
143: } else if (ch == '\r') {
144: sb.append("\\r");
145: } else {
146: sb.append(ch);
147: }
148: }
149: sb.append('"');
150: return sb.toString();
151: }
152:
153: public void println(String line) {
154: writer.println(SPACES.substring(0, indent) + line);
155: }
156:
157: public void println() {
158: writer.println("");
159: }
160:
161: public void indent() {
162: writer.print(SPACES.substring(0, indent));
163: }
164:
165: public void print(String s) {
166: writer.print(s);
167: }
168:
169: public void printMultiLn(String multiline) {
170: // Try to be smart (i.e. indent properly) at generating the code:
171: BufferedReader reader = new BufferedReader(new StringReader(
172: multiline));
173: try {
174: for (String line = null; (line = reader.readLine()) != null;)
175: // println(SPACES.substring(0, indent)+line);
176: println(line);
177: } catch (IOException ex) {
178: // Unlikely to happen, since we're acting on strings
179: }
180: }
181:
182: }
|