001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.jasper.compiler;
017:
018: import java.io.IOException;
019: import java.io.PrintWriter;
020:
021: /**
022: * This is what is used to generate servlets.
023: *
024: * @author Anil K. Vijendran
025: * @author Kin-man Chung
026: */
027: public class ServletWriter {
028: public static int TAB_WIDTH = 2;
029: public static String SPACES = " ";
030:
031: // Current indent level:
032: private int indent = 0;
033: private int virtual_indent = 0;
034:
035: // The sink writer:
036: PrintWriter writer;
037:
038: // servlet line numbers start from 1
039: private int javaLine = 1;
040:
041: public ServletWriter(PrintWriter writer) {
042: this .writer = writer;
043: }
044:
045: public void close() throws IOException {
046: writer.close();
047: }
048:
049: // -------------------- Access informations --------------------
050:
051: public int getJavaLine() {
052: return javaLine;
053: }
054:
055: // -------------------- Formatting --------------------
056:
057: public void pushIndent() {
058: virtual_indent += TAB_WIDTH;
059: if (virtual_indent >= 0 && virtual_indent <= SPACES.length())
060: indent = virtual_indent;
061: }
062:
063: public void popIndent() {
064: virtual_indent -= TAB_WIDTH;
065: if (virtual_indent >= 0 && virtual_indent <= SPACES.length())
066: indent = virtual_indent;
067: }
068:
069: /**
070: * Print a standard comment for echo outputed chunk.
071: * @param start The starting position of the JSP chunk being processed.
072: * @param stop The ending position of the JSP chunk being processed.
073: */
074: public void printComment(Mark start, Mark stop, char[] chars) {
075: if (start != null && stop != null) {
076: println("// from=" + start);
077: println("// to=" + stop);
078: }
079:
080: if (chars != null)
081: for (int i = 0; i < chars.length;) {
082: printin();
083: print("// ");
084: while (chars[i] != '\n' && i < chars.length)
085: writer.print(chars[i++]);
086: }
087: }
088:
089: /**
090: * Prints the given string followed by '\n'
091: */
092: public void println(String s) {
093: javaLine++;
094: writer.println(s);
095: }
096:
097: /**
098: * Prints a '\n'
099: */
100: public void println() {
101: javaLine++;
102: writer.println("");
103: }
104:
105: /**
106: * Prints the current indention
107: */
108: public void printin() {
109: writer.print(SPACES.substring(0, indent));
110: }
111:
112: /**
113: * Prints the current indention, followed by the given string
114: */
115: public void printin(String s) {
116: writer.print(SPACES.substring(0, indent));
117: writer.print(s);
118: }
119:
120: /**
121: * Prints the current indention, and then the string, and a '\n'.
122: */
123: public void printil(String s) {
124: javaLine++;
125: writer.print(SPACES.substring(0, indent));
126: writer.println(s);
127: }
128:
129: /**
130: * Prints the given char.
131: *
132: * Use println() to print a '\n'.
133: */
134: public void print(char c) {
135: writer.print(c);
136: }
137:
138: /**
139: * Prints the given int.
140: */
141: public void print(int i) {
142: writer.print(i);
143: }
144:
145: /**
146: * Prints the given string.
147: *
148: * The string must not contain any '\n', otherwise the line count will be
149: * off.
150: */
151: public void print(String s) {
152: writer.print(s);
153: }
154:
155: /**
156: * Prints the given string.
157: *
158: * If the string spans multiple lines, the line count will be adjusted
159: * accordingly.
160: */
161: public void printMultiLn(String s) {
162: int index = 0;
163:
164: // look for hidden newlines inside strings
165: while ((index = s.indexOf('\n', index)) > -1) {
166: javaLine++;
167: index++;
168: }
169:
170: writer.print(s);
171: }
172: }
|