KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > compiler > ServletWriter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.jasper.compiler;
18
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 /**
23  * This is what is used to generate servlets.
24  *
25  * @author Anil K. Vijendran
26  * @author Kin-man Chung
27  */

28 public class ServletWriter {
29     public static int TAB_WIDTH = 2;
30     public static String JavaDoc SPACES = " ";
31
32     // Current indent level:
33
private int indent = 0;
34     private int virtual_indent = 0;
35
36     // The sink writer:
37
PrintWriter JavaDoc writer;
38     
39     // servlet line numbers start from 1
40
private int javaLine = 1;
41
42
43     public ServletWriter(PrintWriter JavaDoc writer) {
44     this.writer = writer;
45     }
46
47     public void close() throws IOException JavaDoc {
48     writer.close();
49     }
50
51     
52     // -------------------- Access informations --------------------
53

54     public int getJavaLine() {
55         return javaLine;
56     }
57
58
59     // -------------------- Formatting --------------------
60

61     public void pushIndent() {
62     virtual_indent += TAB_WIDTH;
63     if (virtual_indent >= 0 && virtual_indent <= SPACES.length())
64         indent = virtual_indent;
65     }
66
67     public void popIndent() {
68     virtual_indent -= TAB_WIDTH;
69     if (virtual_indent >= 0 && virtual_indent <= SPACES.length())
70         indent = virtual_indent;
71     }
72
73     /**
74      * Print a standard comment for echo outputed chunk.
75      * @param start The starting position of the JSP chunk being processed.
76      * @param stop The ending position of the JSP chunk being processed.
77      */

78     public void printComment(Mark start, Mark stop, char[] chars) {
79         if (start != null && stop != null) {
80             println("// from="+start);
81             println("// to="+stop);
82         }
83         
84         if (chars != null)
85             for(int i = 0; i < chars.length;) {
86                 printin();
87                 print("// ");
88                 while (chars[i] != '\n' && i < chars.length)
89                     writer.print(chars[i++]);
90             }
91     }
92
93     /**
94      * Prints the given string followed by '\n'
95      */

96     public void println(String JavaDoc s) {
97         javaLine++;
98     writer.println(s);
99     }
100
101     /**
102      * Prints a '\n'
103      */

104     public void println() {
105         javaLine++;
106     writer.println("");
107     }
108
109     /**
110      * Prints the current indention
111      */

112     public void printin() {
113     writer.print(SPACES.substring(0, indent));
114     }
115
116     /**
117      * Prints the current indention, followed by the given string
118      */

119     public void printin(String JavaDoc s) {
120     writer.print(SPACES.substring(0, indent));
121     writer.print(s);
122     }
123
124     /**
125      * Prints the current indention, and then the string, and a '\n'.
126      */

127     public void printil(String JavaDoc s) {
128         javaLine++;
129     writer.print(SPACES.substring(0, indent));
130     writer.println(s);
131     }
132
133     /**
134      * Prints the given char.
135      *
136      * Use println() to print a '\n'.
137      */

138     public void print(char c) {
139     writer.print(c);
140     }
141
142     /**
143      * Prints the given int.
144      */

145     public void print(int i) {
146     writer.print(i);
147     }
148
149     /**
150      * Prints the given string.
151      *
152      * The string must not contain any '\n', otherwise the line count will be
153      * off.
154      */

155     public void print(String JavaDoc s) {
156     writer.print(s);
157     }
158
159     /**
160      * Prints the given string.
161      *
162      * If the string spans multiple lines, the line count will be adjusted
163      * accordingly.
164      */

165     public void printMultiLn(String JavaDoc s) {
166         int index = 0;
167
168         // look for hidden newlines inside strings
169
while ((index=s.indexOf('\n',index)) > -1 ) {
170             javaLine++;
171             index++;
172         }
173
174     writer.print(s);
175     }
176 }
177
Popular Tags