KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > jsfmeta > util > JavaSourceWriter


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.jsfmeta.util;
35
36 import java.io.IOException JavaDoc;
37 import java.io.PrintWriter JavaDoc;
38 import java.io.Writer JavaDoc;
39
40
41 public class JavaSourceWriter extends Writer JavaDoc {
42
43
44     private int currentIndent = 0;
45
46     private int indentAmount = 4;
47
48     private Writer JavaDoc outputWriter = null;
49
50
51     public JavaSourceWriter() {
52
53         outputWriter = new PrintWriter JavaDoc(System.out);
54     }
55
56     public void setOutputWriter(Writer JavaDoc outputWriter) {
57         this.outputWriter = outputWriter;
58     }
59
60     public int getCurrentIndent() {
61         return currentIndent;
62     }
63
64     public void setIndentAmount(int indentAmount) {
65         this.indentAmount = indentAmount;
66     }
67
68     public void indent() {
69         currentIndent += indentAmount;
70     }
71
72     public void outdent() {
73         currentIndent -= indentAmount;
74         if (currentIndent < 0)
75             currentIndent = 0;
76     }
77
78     private void doIndent() throws IOException JavaDoc {
79         for (int i = 0; i < currentIndent; i++){
80             outputWriter.write(32);
81         }
82     }
83
84     public void write(char cbuf[], int off, int len) throws IOException JavaDoc {
85         outputWriter.write(cbuf, off, len);
86     }
87
88     public void flush() throws IOException JavaDoc {
89         outputWriter.flush();
90     }
91
92     public void close() throws IOException JavaDoc {
93         outputWriter.close();
94     }
95
96     public void startSource() throws IOException JavaDoc {
97         currentIndent = 0;
98     }
99
100     public void endSource() throws IOException JavaDoc {
101         currentIndent = 0;
102     }
103
104     public void startJavaDoc() throws IOException JavaDoc {
105         doIndent();
106         outputWriter.write("/**");
107         outputWriter.write(10);
108     }
109
110     public void emitJavaDoc() throws IOException JavaDoc {
111         doIndent();
112         outputWriter.write(" *");
113         outputWriter.write(10);
114     }
115
116     public void emitJavaDoc(String JavaDoc comment) throws IOException JavaDoc {
117         doIndent();
118         outputWriter.write(" * " + comment);
119         emitNewline();
120     }
121
122     public void emitJavaDoc(String JavaDoc comment, int width) throws IOException JavaDoc {
123         int actualWidth = width - (3 + currentIndent);
124         int index = 0;
125         char buffer[] = comment.toCharArray();
126         int pos = 0;
127         while (index < buffer.length) {
128             if (pos == 0) {
129                 doIndent();
130                 outputWriter.write(" * ");
131             }
132             if (pos < actualWidth) {
133                 outputWriter.write(buffer[index++]);
134                 pos++;
135             } else {
136                 while (index < buffer.length && buffer[index] != ' '){
137                     outputWriter.write(buffer[index++]);
138                 }
139                 while (index < buffer.length && buffer[index] == ' '){
140                     outputWriter.write(buffer[index++]);
141                 }
142                 if (index < buffer.length){
143                     emitNewline();
144                 }
145                 pos = 0;
146             }
147         }
148         emitNewline();
149     }
150
151     public void emitJavaDocMultiLine(String JavaDoc comment) throws IOException JavaDoc {
152         if (comment == null)
153             return;
154         boolean start = true;
155         for (int i = 0; i < comment.length(); i++) {
156             char ch = comment.charAt(i);
157             if (ch == '\r')
158                 continue;
159             if (start) {
160                 outputWriter.write(" * ");
161                 start = false;
162             }
163             outputWriter.write(ch);
164             if (ch == '\n')
165                 start = true;
166         }
167
168         if (!start){
169             //new line
170
emitNewline();
171         }
172     }
173
174     public void endJavaDoc() throws IOException JavaDoc {
175         doIndent();
176         outputWriter.write(" */");
177         emitNewline();
178     }
179
180     public void emitNewline() throws IOException JavaDoc {
181         outputWriter.write(10);
182     }
183
184     public void emitPackage(String JavaDoc name) throws IOException JavaDoc {
185         doIndent();
186         outputWriter.write("package " + name + ";");
187         emitNewline();
188     }
189
190         //TODO: pick up from file
191
public void emitLicense() throws IOException JavaDoc{
192             
193         String JavaDoc LICENSE_STRING = "/*\n" +
194             " * Version: MPL 1.1/GPL 2.0/LGPL 2.1\n" +
195             " *\n" +
196             " * \"The contents of this file are subject to the Mozilla Public License\n" +
197             " * Version 1.1 (the \"License\"); you may not use this file except in\n" +
198             " * compliance with the License. You may obtain a copy of the License at\n" +
199             " * http://www.mozilla.org/MPL/\n" +
200             " *\n" +
201             " * Software distributed under the License is distributed on an \"AS IS\"\n" +
202             " * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the\n" +
203             " * License for the specific language governing rights and limitations under\n" +
204             " * the License.\n" +
205             " *\n" +
206             " * The Original Code is ICEfaces 1.5 open source software code, released\n" +
207             " * November 5, 2006. The Initial Developer of the Original Code is ICEsoft\n" +
208             " * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)\n" +
209             " * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.\n" +
210             " *\n" +
211             " * Contributor(s): _____________________.\n" +
212             " *\n" +
213             " * Alternatively, the contents of this file may be used under the terms of\n" +
214             " * the GNU Lesser General Public License Version 2.1 or later (the \"LGPL\"\n" +
215             " * License), in which case the provisions of the LGPL License are\n" +
216             " * applicable instead of those above. If you wish to allow use of your\n" +
217             " * version of this file only under the terms of the LGPL License and not to\n" +
218             " * allow others to use your version of this file under the MPL, indicate\n" +
219             " * your decision by deleting the provisions above and replace them with\n" +
220             " * the notice and other provisions required by the LGPL License. If you do\n" +
221             " * not delete the provisions above, a recipient may use your version of\n" +
222             " * this file under either the MPL or the LGPL License.\"\n" +
223             " *\n" +
224             " */";
225
226         outputWriter.write(LICENSE_STRING);
227         emitNewline();
228     }
229
230     public void emitImport(String JavaDoc name) throws IOException JavaDoc {
231         doIndent();
232         outputWriter.write("import " + name + ";");
233         emitNewline();
234     }
235
236     public void startClass(String JavaDoc name, String JavaDoc superClass, String JavaDoc interfaces[])
237             throws IOException JavaDoc {
238         startClass(name, superClass, interfaces, true, false);
239     }
240
241     public void startClass(String JavaDoc name, String JavaDoc superClass, String JavaDoc interfaces[],
242             boolean _public, boolean _abstract) throws IOException JavaDoc {
243         doIndent();
244         if (_public)
245             outputWriter.write("public ");
246         if (_abstract)
247             outputWriter.write("abstract ");
248         outputWriter.write("class " + name);
249         if (superClass != null)
250             outputWriter.write(" extends " + superClass);
251         if (interfaces != null) {
252             outputWriter.write(" implements");
253             for (int i = 0; i < interfaces.length; i++) {
254                 if (i > 0)
255                     outputWriter.write(44);
256                 outputWriter.write(" " + interfaces[i]);
257             }
258
259         }
260         outputWriter.write(" {");
261         emitNewline();
262         indent();
263     }
264
265     public void endClass() throws IOException JavaDoc {
266         outdent();
267         doIndent();
268         outputWriter.write("}");
269         emitNewline();
270     }
271
272     public void startMethod(String JavaDoc name, String JavaDoc retType, String JavaDoc parmTypes[],
273             String JavaDoc parmNames[]) throws IOException JavaDoc {
274         startMethod(name, retType, parmTypes, parmNames, "public");
275     }
276
277     public void startMethod(String JavaDoc name, String JavaDoc retType, String JavaDoc parmTypes[],
278             String JavaDoc parmNames[], String JavaDoc scope) throws IOException JavaDoc {
279         doIndent();
280         if (scope != null)
281             outputWriter.write(scope);
282         if (retType != null)
283             outputWriter.write(" " + retType);
284         outputWriter.write(" " + name + "(");
285         if (parmTypes != null) {
286             if (parmTypes.length != parmNames.length)
287                 throw new IOException JavaDoc("Oops");
288             for (int i = 0; i < parmTypes.length; i++) {
289                 if (i > 0)
290                     outputWriter.write(44);
291                 outputWriter.write(parmTypes[i] + " " + parmNames[i]);
292             }
293
294         }
295         outputWriter.write(") {");
296         emitNewline();
297         indent();
298     }
299
300     public void endMethod() throws IOException JavaDoc {
301         outdent();
302         doIndent();
303         outputWriter.write("}");
304         emitNewline();
305     }
306
307     public void emitField(String JavaDoc type, String JavaDoc name, String JavaDoc value)
308             throws IOException JavaDoc {
309         doIndent();
310         outputWriter.write("private " + type + " " + name);
311         if (value != null){
312             outputWriter.write(" = \"" + value + "\"");
313         }
314         outputWriter.write(";");
315         emitNewline();
316     }
317
318     public void emitStaticField(boolean isFinal, String JavaDoc type, String JavaDoc name,
319             String JavaDoc expression) throws IOException JavaDoc {
320         doIndent();
321         outputWriter.write("protected static ");
322         if (isFinal){
323             outputWriter.write("final ");
324         }
325         outputWriter.write(type);
326         outputWriter.write(32);
327         outputWriter.write(name);
328         if (expression != null){
329             outputWriter.write(" = " + expression + "");
330         }
331         outputWriter.write(";");
332         emitNewline();
333     }
334
335     public void emitExpression(String JavaDoc expr, boolean newline) throws IOException JavaDoc {
336         doIndent();
337         outputWriter.write(expr);
338         if (newline){
339             emitNewline();
340         }
341     }
342
343     public void emitExpressionPart(String JavaDoc expr) throws IOException JavaDoc {
344         outputWriter.write(expr);
345     }
346
347     public void emitCommentLine() throws IOException JavaDoc {
348         doIndent();
349         outputWriter.write("//");
350         emitNewline();
351     }
352
353     public void emitCommentLine(String JavaDoc comment) throws IOException JavaDoc {
354         doIndent();
355         outputWriter.write("// " + comment);
356         emitNewline();
357     }
358
359     public void emitJavaString(String JavaDoc string) throws IOException JavaDoc {
360         outputWriter.write("\"");
361         boolean eatingWhite = false;
362         for (int i = 0; i < string.length(); i++) {
363             char c = string.charAt(i);
364             if (eatingWhite) {
365                 if (Character.isWhitespace(c))
366                     continue;
367                 eatingWhite = false;
368             }
369             if (c == '"') {
370                 outputWriter.write("\\\"");
371                 continue;
372             }
373             if (c == '\n') {
374                 outputWriter.write(" ");
375                 eatingWhite = true;
376             } else {
377                 outputWriter.write(c);
378             }
379         }
380
381         outputWriter.write("\"");
382     }
383
384     public static String JavaDoc toJavaString(String JavaDoc s) {
385         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s.length() + 10);
386         sb.append("\"");
387         boolean eatingWhite = false;
388         for (int i = 0; i < s.length(); i++) {
389             char c = s.charAt(i);
390             if (eatingWhite) {
391                 if (Character.isWhitespace(c))
392                     continue;
393                 eatingWhite = false;
394             }
395             if (c == '"') {
396                 sb.append("\\\"");
397                 continue;
398             }
399             if (c == '\n') {
400                 sb.append(" ");
401                 eatingWhite = true;
402             } else {
403                 sb.append(c);
404             }
405         }
406
407         sb.append("\"");
408         return sb.toString();
409     }
410
411 }
412
413
Popular Tags