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