001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.schema2beansdev.gen;
043:
044: import java.util.*;
045: import java.io.*;
046:
047: public class JavaWriter extends IndentingWriter {
048: // Parts of a Java class. If you add another section be sure to
049: // increment defaultSectionCount and put a line into insertSectionAfter
050: public int HEADER_SECTION = 0;
051: public int DECL_SECTION = 1;
052: public int CONSTRUCTOR_SECTION = 2;
053: public int BODY_SECTION = 3;
054: public int TRAILER_SECTION = 4;
055: static final protected int defaultSectionCount = 5;
056:
057: static public final int PUBLIC = 0x0;
058: static public final int PROTECTED = 0x1;
059: static public final int PACKAGE_LEVEL = 0x2;
060: static public final int PRIVATE = 0x3;
061: static public final int ACCESS_MASK = 0x3;
062:
063: static public final int STATIC = 0x10;
064: static public final int FINAL = 0x20;
065:
066: static public final int BEANINFO = 0x100;
067: static public final int IO = 0x200;
068: static public final int UNSUPPORTED = 0x400;
069: static public final int METHOD_SEMANTIC_MASK = 0xf00;
070:
071: protected boolean newlineBeforeCurlyBrace = false;
072: public boolean storeMethods = true;
073:
074: public JavaWriter() {
075: super (defaultSectionCount);
076: privateInit();
077: }
078:
079: public JavaWriter(JavaWriter source) {
080: super (source);
081: HEADER_SECTION = source.HEADER_SECTION;
082: DECL_SECTION = source.DECL_SECTION;
083: CONSTRUCTOR_SECTION = source.CONSTRUCTOR_SECTION;
084: BODY_SECTION = source.BODY_SECTION;
085: TRAILER_SECTION = source.TRAILER_SECTION;
086: newlineBeforeCurlyBrace = source.newlineBeforeCurlyBrace;
087: storeMethods = source.storeMethods;
088: methods = new LinkedHashMap();
089: }
090:
091: /**
092: * Insert a custom section after another section.
093: * eg:
094: * JavaWriter jw = new JavaWriter();
095: * int SPECIAL_SECTION = jw.insertSectionAfter(jw.CONSTRUCTOR_SECTION);
096: */
097: public int insertSectionAfter(int sectionNum) {
098: insertAdditionalBuffers(sectionNum, 1);
099: if (sectionNum < HEADER_SECTION)
100: ++HEADER_SECTION;
101: if (sectionNum < DECL_SECTION)
102: ++DECL_SECTION;
103: if (sectionNum < CONSTRUCTOR_SECTION)
104: ++CONSTRUCTOR_SECTION;
105: if (sectionNum < BODY_SECTION)
106: ++BODY_SECTION;
107: if (sectionNum < TRAILER_SECTION)
108: ++TRAILER_SECTION;
109: return sectionNum + 1;
110: }
111:
112: public void reset() {
113: super .reset();
114: privateInit();
115: }
116:
117: private void privateInit() {
118: for (int i = 0; i < bufferCount; i++) {
119: if (i == HEADER_SECTION)
120: indentLevel[i] = 0;
121: else
122: indentLevel[i] = 1;
123: }
124: methods = new LinkedHashMap();
125: }
126:
127: /**
128: * Send buffers to @param out
129: * Everything is passed thru native2ascii.
130: */
131: public void writeTo(Writer out) throws IOException {
132: Writer n2aout = new BufferedWriter(new JavaUtil.N2AFilter(out));
133: super .writeTo(n2aout);
134: n2aout.flush();
135: }
136:
137: /**
138: * Send buffers to @param out
139: * Everything is passed thru native2ascii.
140: */
141: public void writeTo(OutputStream out) throws IOException {
142: Writer w = new OutputStreamWriter(out);
143: writeTo(w);
144: w.flush();
145: }
146:
147: public void writeTo(GenBuffer o) {
148: super .writeTo(o);
149: if (o instanceof JavaWriter) {
150: JavaWriter out = (JavaWriter) o;
151: if (storeMethods) {
152: out.methods.putAll(methods);
153: }
154: }
155: }
156:
157: public boolean writeOptions(int options) throws IOException {
158: boolean needSpace = writeAccess(options);
159: if ((options & STATIC) == STATIC) {
160: if (needSpace)
161: write(" ");
162: write("static");
163: needSpace = true;
164: }
165: if ((options & FINAL) == FINAL) {
166: if (needSpace)
167: write(" ");
168: write("final");
169: needSpace = true;
170: }
171: return needSpace;
172: }
173:
174: public boolean writeAccess(int accessLevel) throws IOException {
175: switch (accessLevel & ACCESS_MASK) {
176: case PUBLIC:
177: write("public");
178: return true;
179: case PROTECTED:
180: write("protected");
181: return true;
182: case PACKAGE_LEVEL:
183: // write nothing
184: return false;
185: case PRIVATE:
186: write("private");
187: return true;
188: }
189: return false;
190: }
191:
192: /**
193: * Writes a class declaration into the DECL_SECTION.
194: */
195: public void writeClassDecl(String name, String extendsStatement,
196: String implements Statement, int options) throws IOException {
197: pushSelect(HEADER_SECTION);
198: try {
199: if (writeOptions(options))
200: write(" ");
201: write("class ", name, " ");
202: if (extendsStatement != null) {
203: write("extends ", extendsStatement, " ");
204: }
205: if (implements Statement != null) {
206: write("implements ", implements Statement, " ");
207: }
208: begin();
209: popSelect();
210: pushSelect(TRAILER_SECTION);
211: end();
212: } finally {
213: popSelect();
214: }
215: }
216:
217: public void beginMethod(String name) throws IOException {
218: beginMethod(name, "", null);
219: }
220:
221: public void beginMethod(String name, String parameters)
222: throws IOException {
223: beginMethod(name, parameters, null);
224: }
225:
226: public void beginMethod(String name, String parameters,
227: String exceptions) throws IOException {
228: beginMethod(name, parameters, exceptions, "void", PUBLIC);
229: }
230:
231: public void beginMethod(String name, String parameters,
232: String exceptions, String returnType) throws IOException {
233: beginMethod(name, parameters, exceptions, returnType, PUBLIC);
234: }
235:
236: public void beginMethod(String name, String parameters,
237: String exceptions, String returnType, int options)
238: throws IOException {
239: writeMethod(name, parameters, exceptions, returnType, options);
240: write(" ");
241: begin();
242: }
243:
244: public void endMethod() throws IOException {
245: end();
246: cr();
247: }
248:
249: public void writeMethod(String name, String parameters,
250: String exceptions, String returnType, int options)
251: throws IOException {
252: String nameParameters = name + "(" + parameters + ")";
253: if (storeMethods) {
254: addToMethodStore(name, parameters, exceptions, returnType,
255: options);
256: }
257: if (writeOptions(options))
258: write(" ");
259: write(returnType);
260: write(" ");
261: write(nameParameters);
262: if (exceptions != null)
263: write(" throws ", exceptions);
264: }
265:
266: private Map methods; // Map<String, Method>
267:
268: public static class Method implements Comparable {
269: private String name;
270: private String parameters;
271: private String exceptions;
272: private String returnType;
273: private int options;
274:
275: public Method(String name, String parameters,
276: String exceptions, String returnType, int options) {
277: this .name = name;
278: this .parameters = parameters.trim();
279: this .exceptions = exceptions;
280: this .returnType = returnType;
281: this .options = options;
282: }
283:
284: public void beginMethod(JavaWriter out) throws IOException {
285: out.beginMethod(name, parameters, exceptions, returnType,
286: options);
287: }
288:
289: public void writeMethod(JavaWriter out) throws IOException {
290: out.writeMethod(name, parameters, exceptions, returnType,
291: options);
292: }
293:
294: public String getNameParameters() {
295: return name + "(" + parameters + ")";
296: }
297:
298: public String getName() {
299: return name;
300: }
301:
302: public String getReturnType() {
303: return returnType;
304: }
305:
306: public String getParameters() {
307: return parameters;
308: }
309:
310: public String getExceptions() {
311: return exceptions;
312: }
313:
314: public int getOptions() {
315: return options;
316: }
317:
318: public boolean isStatic() {
319: return (options & STATIC) == STATIC;
320: }
321:
322: public boolean isPublic() {
323: return (options & ACCESS_MASK) == PUBLIC;
324: }
325:
326: public boolean isBeanInfo() {
327: return (options & BEANINFO) == BEANINFO;
328: }
329:
330: public boolean isUnsupported() {
331: return (options & UNSUPPORTED) == UNSUPPORTED;
332: }
333:
334: public boolean isConstructor() {
335: return "".equals(getReturnType());
336: }
337:
338: public void writeCall(JavaWriter out) throws IOException {
339: out.write(name);
340: out.write("(");
341: writeParametersNoTypes(out);
342: out.write(")");
343: }
344:
345: public void writeParametersNoTypes(JavaWriter out)
346: throws IOException {
347: boolean writeIt = false;
348: for (int pos = 0; pos < parameters.length(); ++pos) {
349: char c = parameters.charAt(pos);
350: // need to skip the 'final ' modifier before type
351: int endFinal = pos + 6;
352: if ((parameters.length() >= endFinal)
353: && parameters.substring(pos, endFinal).equals(
354: "final ")) {
355: pos = endFinal;
356: c = parameters.charAt(pos);
357: }
358:
359: if (writeIt)
360: out.write(c);
361: boolean skipWS = false;
362: if (Character.isWhitespace(c)) {
363: writeIt = true;
364: skipWS = true;
365: } else if (c == ',') {
366: writeIt = false;
367: skipWS = true;
368: }
369: if (skipWS) {
370: while (pos + 1 < parameters.length()
371: && Character.isWhitespace(parameters
372: .charAt(pos + 1)))
373: ++pos;
374: }
375: }
376: }
377:
378: public int compareTo(Object o) {
379: Method otherMethod = (Method) o;
380: return getNameParameters().compareTo(
381: otherMethod.getNameParameters());
382: }
383: }
384:
385: public void addToMethodStore(String name, String parameters,
386: String exceptions, String returnType) {
387: addToMethodStore(name, parameters, exceptions, returnType,
388: PUBLIC);
389: }
390:
391: public void addToMethodStore(String name, String parameters,
392: String exceptions, String returnType, int options) {
393: Method method = new Method(name, parameters, exceptions,
394: returnType, options);
395: methods.put(method.getNameParameters(), method);
396: }
397:
398: public Collection getStoredMethods() {
399: return methods.values();
400: }
401:
402: public void beginConstructor(String name) throws IOException {
403: beginConstructor(name, "", null, PUBLIC);
404: }
405:
406: public void beginConstructor(String name, String parameters)
407: throws IOException {
408: beginConstructor(name, parameters, null, PUBLIC);
409: }
410:
411: public void beginConstructor(String name, String parameters,
412: String exceptions, int options) throws IOException {
413: select(CONSTRUCTOR_SECTION);
414: if (writeOptions(options))
415: write(" ");
416: write(name);
417: write("(", parameters, ") ");
418: if (exceptions != null)
419: write("throws ", exceptions, " ");
420: begin();
421: addToMethodStore(name, parameters, exceptions, "", options);
422: }
423:
424: public void writePackage(String pkg) throws IOException {
425: pushSelect(HEADER_SECTION);
426: try {
427: writecr("package ", pkg, ";");
428: } finally {
429: popSelect();
430: }
431: }
432:
433: public void writeImport(String pkg) throws IOException {
434: pushSelect(HEADER_SECTION);
435: try {
436: writecr("import ", pkg, ";");
437: } finally {
438: popSelect();
439: }
440: }
441:
442: public void begin() throws IOException {
443: if (newlineBeforeCurlyBrace)
444: cr();
445: writecr("{");
446: indentRight();
447: }
448:
449: public void end() throws IOException {
450: end(true);
451: }
452:
453: public void end(boolean useCr) throws IOException {
454: indentLeft();
455: write("}");
456: if (useCr)
457: cr();
458: }
459:
460: public void eol() throws IOException {
461: eol(true);
462: }
463:
464: public void eol(boolean useCr) throws IOException {
465: write(";");
466: if (useCr)
467: cr();
468: }
469:
470: public void writeEol(String s) throws IOException {
471: write(s);
472: eol();
473: }
474:
475: public void writeEol(String s1, String s2) throws IOException {
476: write(s1, s2);
477: eol();
478: }
479:
480: public void writeEol(String s1, String s2, String s3)
481: throws IOException {
482: write(s1, s2, s3);
483: eol();
484: }
485:
486: public void writeEol(String s1, String s2, String s3, String s4)
487: throws IOException {
488: write(s1, s2, s3, s4);
489: eol();
490: }
491:
492: public void noI18N() throws IOException {
493: writecr(" // NOI18N");
494: }
495:
496: public void eolNoI18N() throws IOException {
497: write(";");
498: noI18N();
499: }
500:
501: public void writeEolNoI18N(String s) throws IOException {
502: write(s);
503: write(";");
504: noI18N();
505: }
506:
507: public void writeEolNoI18N(String s1, String s2) throws IOException {
508: write(s1, s2);
509: write(";");
510: noI18N();
511: }
512:
513: public void writeEolNoI18N(String s1, String s2, String s3)
514: throws IOException {
515: write(s1, s2, s3);
516: write(";");
517: noI18N();
518: }
519:
520: public void writeEolNoI18N(String s1, String s2, String s3,
521: String s4) throws IOException {
522: write(s1, s2, s3, s4);
523: write(";");
524: noI18N();
525: }
526:
527: public void beginTry() throws IOException {
528: write("try ");
529: begin();
530: }
531:
532: public void endCatch(String param) throws IOException {
533: end(false);
534: write(" catch (", param, ") ");
535: begin();
536: }
537:
538: public void endFinallyBegin() throws IOException {
539: end(false);
540: write(" finally ");
541: begin();
542: }
543:
544: public void beginIf(String predicate) throws IOException {
545: write("if (", predicate, ") ");
546: begin();
547: }
548:
549: public void beginIf(String predicate1, String predicate2)
550: throws IOException {
551: write("if (", predicate1, predicate2, ") ");
552: begin();
553: }
554:
555: public void beginIf(String predicate1, String predicate2,
556: String predicate3) throws IOException {
557: write("if (", predicate1, predicate2, predicate3);
558: write(") ");
559: begin();
560: }
561:
562: public void endElse() throws IOException {
563: end(false);
564: write(" else ");
565: }
566:
567: public void endElseBegin() throws IOException {
568: end(false);
569: write(" else ");
570: begin();
571: }
572:
573: public void endElseBeginIf(String predicate) throws IOException {
574: end(false);
575: write(" else ");
576: beginIf(predicate);
577: }
578:
579: public final static int rightMarginColumn = 76;
580:
581: public void beginFor(String init, String predicate, String next)
582: throws IOException {
583: int indentLength;
584: if ("\t".equals(indentString))
585: indentLength = 4;
586: else
587: indentLength = indentString.length();
588: int horizPosition = indentLength * indentLevel[curOut];
589: write("for (");
590: horizPosition += 5;
591: write(init, "; ");
592: horizPosition += init.length() + 2;
593: int nextHorizPosition = horizPosition + predicate.length() + 2;
594: if (nextHorizPosition >= rightMarginColumn) {
595: cr();
596: indentOneLevel();
597: horizPosition = indentLength * indentLevel[curOut];
598: nextHorizPosition = horizPosition + predicate.length() + 2;
599: }
600: write(predicate, "; ");
601: horizPosition = nextHorizPosition;
602: nextHorizPosition = horizPosition + next.length() + 2;
603: if (nextHorizPosition >= rightMarginColumn) {
604: cr();
605: indentOneLevel();
606: }
607: write(next, ") ");
608: horizPosition = nextHorizPosition;
609: begin();
610: }
611:
612: public void beginWhile(String predicate) throws IOException {
613: write("while (");
614: write(predicate);
615: write(") ");
616: begin();
617: }
618:
619: public void writeAssert(String predicate) throws IOException {
620: write("assert ");
621: write(predicate);
622: eol();
623: }
624:
625: public void comment(String msg) throws IOException {
626: write("// ", msg);
627: cr();
628: }
629:
630: public void bigComment(String msg) throws IOException {
631: writecr("/**");
632: // The beginning of every line should hava " * "
633: write(" * ");
634: int length = msg.length();
635: for (int i = 0; i < length; ++i) {
636: char c = msg.charAt(i);
637: if (c == '\n') {
638: cr();
639: write(" * ");
640: } else if (c == '*' && i + 1 < length
641: && msg.charAt(i + 1) == '/') {
642: write("* /");
643: ++i;
644: } else {
645: write(c);
646: }
647: }
648: cr();
649: writecr(" */");
650: }
651: }
|