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.rimfaxe.webserver.compiler.jsp;
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: * @author Kin-man Chung
067: * @author Lars Andersen
068: */
069: public class ServletWriter {
070: public static int TAB_WIDTH = 2;
071: public static String SPACES = " ";
072:
073: // Current indent level:
074: private int indent = 0;
075: private int virtual_indent = 0;
076:
077: // The sink writer:
078: PrintWriter writer;
079:
080: // servlet line numbers start from 1, but we pre-increment
081: private int javaLine = 0;
082:
083: public ServletWriter(PrintWriter writer) {
084: this .writer = writer;
085: }
086:
087: public void close() throws IOException {
088: writer.close();
089: }
090:
091: // -------------------- Access informations --------------------
092:
093: public int getJavaLine() {
094: return javaLine;
095: }
096:
097: // -------------------- Formatting --------------------
098:
099: public void pushIndent() {
100: virtual_indent += TAB_WIDTH;
101: if (virtual_indent >= 0 && virtual_indent <= SPACES.length())
102: indent = virtual_indent;
103: }
104:
105: public void popIndent() {
106: virtual_indent -= TAB_WIDTH;
107: if (virtual_indent >= 0 && virtual_indent <= SPACES.length())
108: indent = virtual_indent;
109: }
110:
111: /**
112: * Print a standard comment for echo outputed chunk.
113: * @param start The starting position of the JSP chunk being processed.
114: * @param stop The ending position of the JSP chunk being processed.
115: */
116: public void printComment(Mark start, Mark stop, char[] chars) {
117: if (start != null && stop != null) {
118: println("// from=" + start);
119: println("// to=" + stop);
120: }
121:
122: if (chars != null)
123: for (int i = 0; i < chars.length;) {
124: printin();
125: print("// ");
126: while (chars[i] != '\n' && i < chars.length)
127: writer.print(chars[i++]);
128: }
129: }
130:
131: /**
132: * Prints the given string followed by '\n'
133: */
134: public void println(String s) {
135: javaLine++;
136: writer.println(s);
137: }
138:
139: /**
140: * Prints a '\n'
141: */
142: public void println() {
143: javaLine++;
144: writer.println("");
145: }
146:
147: /**
148: * Prints the current indention
149: */
150: public void printin() {
151: writer.print(SPACES.substring(0, indent));
152: }
153:
154: /**
155: * Prints the current indention, followed by the given string
156: */
157: public void printin(String s) {
158: writer.print(SPACES.substring(0, indent));
159: writer.print(s);
160: }
161:
162: /**
163: * Prints the current indention, and then the string, and a '\n'.
164: */
165: public void printil(String s) {
166: javaLine++;
167: writer.print(SPACES.substring(0, indent));
168: writer.println(s);
169: }
170:
171: /**
172: * Prints the given char.
173: *
174: * Use println() to print a '\n'.
175: */
176: public void print(char c) {
177: writer.print(c);
178: }
179:
180: /**
181: * Prints the given string.
182: *
183: * The string must not contain any '\n', otherwise the line count will be
184: * off.
185: */
186: public void print(String s) {
187: writer.print(s);
188: }
189:
190: /**
191: * Prints the given string.
192: *
193: * If the string spans multiple lines, the line count will be adjusted
194: * accordingly.
195: */
196: public void printMultiLn(String s) {
197: int index = 0;
198:
199: // look for hidden newlines inside strings
200: while ((index = s.indexOf('\n', index)) > -1) {
201: javaLine++;
202: index++;
203: }
204:
205: writer.print(s);
206: }
207: }
|