001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.jsfmeta.util;
035:
036: import java.io.IOException;
037: import java.io.PrintWriter;
038: import java.io.Writer;
039:
040: public class JavaSourceWriter extends Writer {
041:
042: private int currentIndent = 0;
043:
044: private int indentAmount = 4;
045:
046: private Writer outputWriter = null;
047:
048: public JavaSourceWriter() {
049:
050: outputWriter = new PrintWriter(System.out);
051: }
052:
053: public void setOutputWriter(Writer outputWriter) {
054: this .outputWriter = outputWriter;
055: }
056:
057: public int getCurrentIndent() {
058: return currentIndent;
059: }
060:
061: public void setIndentAmount(int indentAmount) {
062: this .indentAmount = indentAmount;
063: }
064:
065: public void indent() {
066: currentIndent += indentAmount;
067: }
068:
069: public void outdent() {
070: currentIndent -= indentAmount;
071: if (currentIndent < 0)
072: currentIndent = 0;
073: }
074:
075: private void doIndent() throws IOException {
076: for (int i = 0; i < currentIndent; i++) {
077: outputWriter.write(32);
078: }
079: }
080:
081: public void write(char cbuf[], int off, int len) throws IOException {
082: outputWriter.write(cbuf, off, len);
083: }
084:
085: public void flush() throws IOException {
086: outputWriter.flush();
087: }
088:
089: public void close() throws IOException {
090: outputWriter.close();
091: }
092:
093: public void startSource() throws IOException {
094: currentIndent = 0;
095: }
096:
097: public void endSource() throws IOException {
098: currentIndent = 0;
099: }
100:
101: public void startJavaDoc() throws IOException {
102: doIndent();
103: outputWriter.write("/**");
104: outputWriter.write(10);
105: }
106:
107: public void emitJavaDoc() throws IOException {
108: doIndent();
109: outputWriter.write(" *");
110: outputWriter.write(10);
111: }
112:
113: public void emitJavaDoc(String comment) throws IOException {
114: doIndent();
115: outputWriter.write(" * " + comment);
116: emitNewline();
117: }
118:
119: public void emitJavaDoc(String comment, int width)
120: throws IOException {
121: int actualWidth = width - (3 + currentIndent);
122: int index = 0;
123: char buffer[] = comment.toCharArray();
124: int pos = 0;
125: while (index < buffer.length) {
126: if (pos == 0) {
127: doIndent();
128: outputWriter.write(" * ");
129: }
130: if (pos < actualWidth) {
131: outputWriter.write(buffer[index++]);
132: pos++;
133: } else {
134: while (index < buffer.length && buffer[index] != ' ') {
135: outputWriter.write(buffer[index++]);
136: }
137: while (index < buffer.length && buffer[index] == ' ') {
138: outputWriter.write(buffer[index++]);
139: }
140: if (index < buffer.length) {
141: emitNewline();
142: }
143: pos = 0;
144: }
145: }
146: emitNewline();
147: }
148:
149: public void emitJavaDocMultiLine(String comment) throws IOException {
150: if (comment == null)
151: return;
152: boolean start = true;
153: for (int i = 0; i < comment.length(); i++) {
154: char ch = comment.charAt(i);
155: if (ch == '\r')
156: continue;
157: if (start) {
158: outputWriter.write(" * ");
159: start = false;
160: }
161: outputWriter.write(ch);
162: if (ch == '\n')
163: start = true;
164: }
165:
166: if (!start) {
167: //new line
168: emitNewline();
169: }
170: }
171:
172: public void endJavaDoc() throws IOException {
173: doIndent();
174: outputWriter.write(" */");
175: emitNewline();
176: }
177:
178: public void emitNewline() throws IOException {
179: outputWriter.write(10);
180: }
181:
182: public void emitPackage(String name) throws IOException {
183: doIndent();
184: outputWriter.write("package " + name + ";");
185: emitNewline();
186: }
187:
188: //TODO: pick up from file
189: public void emitLicense() throws IOException {
190:
191: String LICENSE_STRING = "/*\n"
192: + " * Version: MPL 1.1/GPL 2.0/LGPL 2.1\n"
193: + " *\n"
194: + " * \"The contents of this file are subject to the Mozilla Public License\n"
195: + " * Version 1.1 (the \"License\"); you may not use this file except in\n"
196: + " * compliance with the License. You may obtain a copy of the License at\n"
197: + " * http://www.mozilla.org/MPL/\n"
198: + " *\n"
199: + " * Software distributed under the License is distributed on an \"AS IS\"\n"
200: + " * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the\n"
201: + " * License for the specific language governing rights and limitations under\n"
202: + " * the License.\n"
203: + " *\n"
204: + " * The Original Code is ICEfaces 1.5 open source software code, released\n"
205: + " * November 5, 2006. The Initial Developer of the Original Code is ICEsoft\n"
206: + " * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)\n"
207: + " * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.\n"
208: + " *\n"
209: + " * Contributor(s): _____________________.\n"
210: + " *\n"
211: + " * Alternatively, the contents of this file may be used under the terms of\n"
212: + " * the GNU Lesser General Public License Version 2.1 or later (the \"LGPL\"\n"
213: + " * License), in which case the provisions of the LGPL License are\n"
214: + " * applicable instead of those above. If you wish to allow use of your\n"
215: + " * version of this file only under the terms of the LGPL License and not to\n"
216: + " * allow others to use your version of this file under the MPL, indicate\n"
217: + " * your decision by deleting the provisions above and replace them with\n"
218: + " * the notice and other provisions required by the LGPL License. If you do\n"
219: + " * not delete the provisions above, a recipient may use your version of\n"
220: + " * this file under either the MPL or the LGPL License.\"\n"
221: + " *\n" + " */";
222:
223: outputWriter.write(LICENSE_STRING);
224: emitNewline();
225: }
226:
227: public void emitImport(String name) throws IOException {
228: doIndent();
229: outputWriter.write("import " + name + ";");
230: emitNewline();
231: }
232:
233: public void startClass(String name, String super Class,
234: String interfaces[]) throws IOException {
235: startClass(name, super Class, interfaces, true, false);
236: }
237:
238: public void startClass(String name, String super Class,
239: String interfaces[], boolean _public, boolean _abstract)
240: throws IOException {
241: doIndent();
242: if (_public)
243: outputWriter.write("public ");
244: if (_abstract)
245: outputWriter.write("abstract ");
246: outputWriter.write("class " + name);
247: if (super Class != null)
248: outputWriter.write(" extends " + super Class);
249: if (interfaces != null) {
250: outputWriter.write(" implements");
251: for (int i = 0; i < interfaces.length; i++) {
252: if (i > 0)
253: outputWriter.write(44);
254: outputWriter.write(" " + interfaces[i]);
255: }
256:
257: }
258: outputWriter.write(" {");
259: emitNewline();
260: indent();
261: }
262:
263: public void endClass() throws IOException {
264: outdent();
265: doIndent();
266: outputWriter.write("}");
267: emitNewline();
268: }
269:
270: public void startMethod(String name, String retType,
271: String parmTypes[], String parmNames[]) throws IOException {
272: startMethod(name, retType, parmTypes, parmNames, "public");
273: }
274:
275: public void startMethod(String name, String retType,
276: String parmTypes[], String parmNames[], String scope)
277: throws IOException {
278: doIndent();
279: if (scope != null)
280: outputWriter.write(scope);
281: if (retType != null)
282: outputWriter.write(" " + retType);
283: outputWriter.write(" " + name + "(");
284: if (parmTypes != null) {
285: if (parmTypes.length != parmNames.length)
286: throw new IOException("Oops");
287: for (int i = 0; i < parmTypes.length; i++) {
288: if (i > 0)
289: outputWriter.write(44);
290: outputWriter.write(parmTypes[i] + " " + parmNames[i]);
291: }
292:
293: }
294: outputWriter.write(") {");
295: emitNewline();
296: indent();
297: }
298:
299: public void endMethod() throws IOException {
300: outdent();
301: doIndent();
302: outputWriter.write("}");
303: emitNewline();
304: }
305:
306: public void emitField(String type, String name, String value)
307: throws IOException {
308: doIndent();
309: outputWriter.write("private " + type + " " + name);
310: if (value != null) {
311: outputWriter.write(" = \"" + value + "\"");
312: }
313: outputWriter.write(";");
314: emitNewline();
315: }
316:
317: public void emitStaticField(boolean isFinal, String type,
318: String name, String expression) throws IOException {
319: doIndent();
320: outputWriter.write("protected static ");
321: if (isFinal) {
322: outputWriter.write("final ");
323: }
324: outputWriter.write(type);
325: outputWriter.write(32);
326: outputWriter.write(name);
327: if (expression != null) {
328: outputWriter.write(" = " + expression + "");
329: }
330: outputWriter.write(";");
331: emitNewline();
332: }
333:
334: public void emitExpression(String expr, boolean newline)
335: throws IOException {
336: doIndent();
337: outputWriter.write(expr);
338: if (newline) {
339: emitNewline();
340: }
341: }
342:
343: public void emitExpressionPart(String expr) throws IOException {
344: outputWriter.write(expr);
345: }
346:
347: public void emitCommentLine() throws IOException {
348: doIndent();
349: outputWriter.write("//");
350: emitNewline();
351: }
352:
353: public void emitCommentLine(String comment) throws IOException {
354: doIndent();
355: outputWriter.write("// " + comment);
356: emitNewline();
357: }
358:
359: public void emitJavaString(String string) throws IOException {
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 toJavaString(String s) {
385: StringBuffer sb = new StringBuffer(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: }
|