1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.struts2.jasper.compiler;
18
19 import java.io.IOException;
20 import java.io.PrintWriter;
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 SPACES = " ";
31
32
33 private int indent = 0;
34 private int virtual_indent = 0;
35
36
37 PrintWriter writer;
38
39
40 private int javaLine = 1;
41
42
43 public ServletWriter(PrintWriter writer) {
44 this.writer = writer;
45 }
46
47 public void close() throws IOException {
48 writer.close();
49 }
50
51
52
53
54 public int getJavaLine() {
55 return javaLine;
56 }
57
58
59
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 *
76 * @param start The starting position of the JSP chunk being processed.
77 * @param stop The ending position of the JSP chunk being processed.
78 */
79 public void printComment(Mark start, Mark stop, char[] chars) {
80 if (start != null && stop != null) {
81 println("// from=" + start);
82 println("// to=" + stop);
83 }
84
85 if (chars != null)
86 for (int i = 0; i < chars.length;) {
87 printin();
88 print("// ");
89 while (chars[i] != '\n' && i < chars.length)
90 writer.print(chars[i++]);
91 }
92 }
93
94 /***
95 * Prints the given string followed by '\n'
96 */
97 public void println(String s) {
98 javaLine++;
99 writer.println(s);
100 }
101
102 /***
103 * Prints a '\n'
104 */
105 public void println() {
106 javaLine++;
107 writer.println("");
108 }
109
110 /***
111 * Prints the current indention
112 */
113 public void printin() {
114 writer.print(SPACES.substring(0, indent));
115 }
116
117 /***
118 * Prints the current indention, followed by the given string
119 */
120 public void printin(String s) {
121 writer.print(SPACES.substring(0, indent));
122 writer.print(s);
123 }
124
125 /***
126 * Prints the current indention, and then the string, and a '\n'.
127 */
128 public void printil(String s) {
129 javaLine++;
130 writer.print(SPACES.substring(0, indent));
131 writer.println(s);
132 }
133
134 /***
135 * Prints the given char.
136 * <p/>
137 * Use println() to print a '\n'.
138 */
139 public void print(char c) {
140 writer.print(c);
141 }
142
143 /***
144 * Prints the given int.
145 */
146 public void print(int i) {
147 writer.print(i);
148 }
149
150 /***
151 * Prints the given string.
152 * <p/>
153 * The string must not contain any '\n', otherwise the line count will be
154 * off.
155 */
156 public void print(String s) {
157 writer.print(s);
158 }
159
160 /***
161 * Prints the given string.
162 * <p/>
163 * If the string spans multiple lines, the line count will be adjusted
164 * accordingly.
165 */
166 public void printMultiLn(String s) {
167 int index = 0;
168
169
170 while ((index = s.indexOf('\n', index)) > -1) {
171 javaLine++;
172 index++;
173 }
174
175 writer.print(s);
176 }
177 }