KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > rebind > ClassSourceFileComposer


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

16 package com.google.gwt.user.rebind;
17
18 import com.google.gwt.core.ext.GeneratorContext;
19 import com.google.gwt.core.ext.TreeLogger;
20 import com.google.gwt.user.rebind.ClassSourceFileComposerFactory.JavaSourceCategory;
21
22 import java.io.PrintWriter JavaDoc;
23
24 class ClassSourceFileComposer implements SourceWriter {
25
26   /**
27    * For the interior of a '*' style comment.
28    */

29   private static final String JavaDoc STAR_COMMENT_LINE = " * ";
30
31   private boolean atStart;
32
33   /**
34    * Either STAR/BLOCK comment line, not pulled out into a ENUM class because
35    * only used by this class.
36    */

37   private String JavaDoc commentIndicator;
38
39   private final GeneratorContext ctx;
40
41   /**
42    * Are you currently in a comment?
43    */

44   private boolean inComment;
45
46   private int indent;
47
48   private final PrintWriter JavaDoc printWriter;
49
50   ClassSourceFileComposer(GeneratorContext ctx, PrintWriter JavaDoc printWriter,
51       String JavaDoc targetPackageName, String JavaDoc targetClassShortName,
52       String JavaDoc superClassName, String JavaDoc[] interfaceNames, String JavaDoc[] imports,
53       JavaSourceCategory category, String JavaDoc classJavaDocComment) {
54     this.ctx = ctx;
55     this.printWriter = printWriter;
56     if (targetPackageName == null) {
57       throw new IllegalArgumentException JavaDoc("Cannot supply a null package name to"
58           + targetClassShortName);
59     }
60     // Inlined header to only have one method with a huge number of methods.
61
println("package " + targetPackageName + ";");
62     println();
63     if (imports != null && imports.length > 0) {
64       for (int i = 0, n = imports.length; i < n; ++i) {
65         println("import " + imports[i] + ";");
66       }
67       println();
68     }
69     if (classJavaDocComment != null) {
70       beginJavaDocComment();
71       print(classJavaDocComment);
72       endJavaDocComment();
73     }
74     if (category == JavaSourceCategory.CLASS) {
75       emitClassDecl(targetClassShortName, superClassName, interfaceNames);
76     } else {
77       emitInterfaceDecl(targetClassShortName, superClassName, interfaceNames);
78     }
79     println(" {");
80     indent();
81   }
82
83   /**
84    * Begin emitting a JavaDoc comment.
85    */

86   public void beginJavaDocComment() {
87     println("\n/**");
88     inComment = true;
89     commentIndicator = STAR_COMMENT_LINE;
90   }
91
92   public void commit(TreeLogger logger) {
93     outdent();
94     println("}");
95     printWriter.close();
96     // If generating a class on the command line, may not have a
97
if (ctx != null) {
98       ctx.commit(logger, printWriter);
99     }
100   }
101
102   /**
103    * End emitting a JavaDoc comment.
104    */

105   public void endJavaDocComment() {
106     inComment = false;
107     println("\n */");
108   }
109
110   public void indent() {
111     ++indent;
112   }
113
114   public void indentln(String JavaDoc s) {
115     indent();
116     println(s);
117     outdent();
118   }
119
120   public void outdent() {
121     --indent;
122   }
123
124   public void print(String JavaDoc s) {
125     // If we just printed a newline, print an indent.
126
//
127
if (atStart) {
128       for (int j = 0; j < indent; ++j) {
129         printWriter.print(" ");
130       }
131       if (inComment) {
132         printWriter.print(commentIndicator);
133       }
134       atStart = false;
135     }
136     // Now print up to the end of the string or the next newline.
137
//
138
String JavaDoc rest = null;
139     int i = s.indexOf("\n");
140     if (i > -1 && i < s.length() - 1) {
141       rest = s.substring(i + 1);
142       s = s.substring(0, i + 1);
143     }
144     printWriter.print(s);
145     // If rest is non-null, then s ended with a newline and we recurse.
146
//
147
if (rest != null) {
148       atStart = true;
149       print(rest);
150     }
151   }
152
153   public void println() {
154     print("\n");
155     atStart = true;
156   }
157
158   public void println(String JavaDoc s) {
159     print(s + "\n");
160     atStart = true;
161   }
162
163   private void emitClassDecl(String JavaDoc targetClassShortName,
164       String JavaDoc superClassName, String JavaDoc[] interfaceNames) {
165     print("public class " + targetClassShortName);
166     if (superClassName != null) {
167       print(" extends " + superClassName);
168     }
169     if (interfaceNames != null && interfaceNames.length > 0) {
170       print(" implements ");
171       for (int i = 0, n = interfaceNames.length; i < n; ++i) {
172         if (i > 0) {
173           print(", ");
174         }
175         print(interfaceNames[i]);
176       }
177     }
178   }
179
180   private void emitInterfaceDecl(String JavaDoc targetClassShortName,
181       String JavaDoc superClassName, String JavaDoc[] interfaceNames) {
182     if (superClassName != null) {
183       throw new IllegalArgumentException JavaDoc("Cannot set superclass name "
184           + superClassName + " on a interface.");
185     }
186     print("public interface " + targetClassShortName);
187     if (interfaceNames != null && interfaceNames.length > 0) {
188       print(" extends ");
189       for (int i = 0; i < interfaceNames.length; ++i) {
190         if (i > 0) {
191           print(", ");
192         }
193         print(interfaceNames[i]);
194       }
195     }
196   }
197 }
198
Popular Tags