KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > codegen > JavaClass


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra 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 on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
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
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: JavaClass.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.codegen;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.TreeMap JavaDoc;
30 import java.util.TreeSet JavaDoc;
31
32 import org.enhydra.xml.xmlc.misc.ReadIterator;
33
34 // FIXME: Add trackin of source files to this file.
35

36 /**
37  * Object that is used to assemble the Java source for a class. Able to
38  * generate either a stand-alone class, or an interface and an implementation
39  * of that interface
40  */

41 public final class JavaClass {
42     /** Attributes of the class */
43     protected String JavaDoc fPackageName;
44     protected String JavaDoc fName;
45     protected int fModifiers;
46     protected String JavaDoc[] fDoc;
47
48     /**
49      * Imports to use for the class but not the interface,
50      */

51     private TreeSet JavaDoc fClassImports = new TreeSet JavaDoc();
52
53     /**
54      * Imports to for both class and interface.
55      */

56     private TreeSet JavaDoc fImports = new TreeSet JavaDoc();
57
58     /**
59      * Class that this class extends, or null.
60      */

61     private String JavaDoc fExtends;
62
63     /**
64      * If the class is also going to have an associated generated
65      * interface, this is its name.
66      */

67     private String JavaDoc fInterface;
68
69     /**
70      * List of interfaces that this class extends.
71      */

72     private TreeSet JavaDoc fImplements = new TreeSet JavaDoc();
73
74     /**
75      * Fields associated with the class.
76      */

77     private TreeMap JavaDoc fFields = new TreeMap JavaDoc();
78
79     /**
80      * Class initializer.
81      */

82     private JavaCode fClassInitializer = new JavaCode();;
83
84     /**
85      * Constructors associated with the class.
86      */

87     private ArrayList JavaDoc fConstructors = new ArrayList JavaDoc();
88
89     /**
90      * Methods associated with the class.
91      */

92     private TreeMap JavaDoc fMethods = new TreeMap JavaDoc();
93
94     /**
95      * Constructor.
96      * @param packageName package for the class, or null for default package.
97      * @param name class name
98      * @param modifiers class modifier bit set.
99      * @param doc Class documentation, each row becomes a line of
100      * documentation.
101      * @see JavaModifiers
102      */

103     public JavaClass(String JavaDoc packageName,
104                      String JavaDoc name,
105                      int modifiers,
106                      String JavaDoc[] doc) {
107         fPackageName = packageName;
108         if (fPackageName.length() == 0) {
109             fPackageName = null; // Force default
110
}
111         fName = name;
112         fModifiers = modifiers;
113         fDoc = (String JavaDoc[])doc.clone();
114     }
115
116     /**
117      * Set the interface name for the associated generated interface.
118      */

119     public void setInterface(String JavaDoc interfaceName) {
120         fInterface = interfaceName;
121     }
122
123     /**
124      * Add an class imports to the class. These will not be in
125      * any generated interface.
126      * @param importSpec Name package, class, or package.* to import.
127      * Should not include import keyword. Duplicates will be discarded.
128      */

129     public void addClassImport(String JavaDoc importSpec) {
130         fClassImports.add(importSpec);
131     }
132
133     /**
134      * Add a set of class imports. These will not be in
135      * any generated interface.
136      * @param importSpecs Names of packages, classes, or package.* to import.
137      * Should not include import keyword.
138      */

139     public void addClassImports(String JavaDoc[] importSpecs) {
140         for (int idx = 0; idx < importSpecs.length; idx++) {
141             addClassImport(importSpecs[idx]);
142         }
143     }
144
145     /**
146      * Add an import to the class and interface.
147      * @param importSpec Name package, class, or package.* to import.
148      * Should not include import keyword. Duplicates will be discarded.
149      */

150     public void addImport(String JavaDoc importSpec) {
151         fImports.add(importSpec);
152     }
153
154     /**
155      * Add a set of imports.
156      * @param importSpecs Names of packages, classes, or package.* to import.
157      * Should not include import keyword. Duplicates will be discarded.
158      */

159     public void addImports(String JavaDoc[] importSpecs) {
160         for (int idx = 0; idx < importSpecs.length; idx++) {
161             addImport(importSpecs[idx]);
162         }
163     }
164
165     /**
166      * Set the class that this class extends.
167      */

168     public void setExtends(String JavaDoc extendsName) {
169         fExtends = extendsName;
170     }
171
172     /**
173      * Add a interface that the class will implement.
174      */

175     public void addImplements(String JavaDoc implementsName) {
176         fImplements.add(implementsName);
177     }
178
179     /**
180      * Add a list of interface for this class to implement.
181      */

182     public void addImplements(String JavaDoc[] implementsName) {
183         for (int idx = 0; idx < implementsName.length; idx++) {
184             fImplements.add(implementsName[idx]);
185         }
186     }
187
188     /**
189      * Add a field to the class.
190      */

191     public void addField(JavaField field) {
192         fFields.put(field.getName(), field);
193     }
194
195     /**
196      * Add a constructor.
197      */

198     public void addConstructor(JavaMethod method) {
199         fConstructors.add(method);
200     }
201
202     /**
203      * Add a method.
204      */

205     public void addMethod(JavaMethod method) {
206         fMethods.put(method.getName(), method);
207     }
208
209     /**
210      * Get the name.
211      */

212     public String JavaDoc getName() {
213         return fName;
214     }
215
216     /**
217      * Get the modifiers.
218      */

219     public int getModifiers() {
220         return fModifiers;
221     }
222
223     /**
224      * Get the documentation, or null if there is none.
225      */

226     public String JavaDoc[] getDoc() {
227         return (fDoc == null) ? null : (String JavaDoc[])fDoc.clone();
228     }
229
230     /**
231      * Get the fields, sorted by field name.
232      */

233     public Iterator JavaDoc getFields() {
234         return new ReadIterator(fFields.values().iterator());
235     }
236
237     /**
238      * Get the class initializer.
239      */

240     public JavaCode getClassInitializer() {
241         return fClassInitializer;
242     }
243
244     /**
245      * Get the constructors
246      */

247     public Iterator JavaDoc getConstructors() {
248         return new ReadIterator(fConstructors.listIterator());
249     }
250     
251     /**
252      * Get the methods, sorted by method name.
253      */

254     public Iterator JavaDoc getMethods() {
255         return new ReadIterator(fMethods.values().iterator());
256     }
257
258     /**
259      * Output a set of imports.
260      */

261     private void printImports(IndentWriter out,
262                               Set JavaDoc importSet) {
263         Iterator JavaDoc imports = importSet.iterator();
264         while (imports.hasNext()) {
265             out.print("import ");
266             out.print((String JavaDoc)imports.next());
267             out.println(';');
268         }
269     }
270     
271     /**
272      * Print the package, imports and comment.
273      */

274     private void printStart(IndentWriter out,
275                             boolean forInterface) {
276         // Package and imports.
277
if (fPackageName != null) {
278             out.print("package ");
279             out.print(fPackageName);
280             out.println(';');
281             out.println();
282         }
283
284         printImports(out, fImports);
285         if (!forInterface) {
286             printImports(out, fClassImports);
287         }
288         out.println();
289
290         // Print class comment
291
out.println("/**");
292         for (int idx = 0; idx < fDoc.length; idx++) {
293             out.print(" * ");
294             out.println(fDoc[idx]);
295         }
296         out.println(" */");
297     }
298
299     /**
300      * Generate `implements' clause for classes or `extends' clause for
301      * interfaces.
302      */

303     private void printImplExtends(IndentWriter out,
304                                   String JavaDoc keyword,
305                                   String JavaDoc single,
306                                   Set JavaDoc set) {
307         boolean printedKeyword = false;
308         if (single != null) {
309             out.print(' ');
310             out.print(keyword);
311             out.print(' ');
312             printedKeyword = true;
313             out.print(single);
314         }
315         Iterator JavaDoc items = set.iterator();
316         while (items.hasNext()) {
317             if (!printedKeyword) {
318                 out.print(' ');
319                 out.print(keyword);
320                 out.print(' ');
321                 printedKeyword = true;
322             } else {
323                 out.print(", ");
324             }
325             out.print((String JavaDoc)items.next());
326         }
327     }
328     
329     /**
330      * Print the class declaration.
331      */

332     private void printClassDecl(IndentWriter out) {
333         out.print(JavaModifiers.toDecl(fModifiers));
334         out.print("class ");
335         out.print(fName);
336         if (fExtends != null) {
337             out.print(" extends ");
338             out.print(fExtends);
339         }
340
341         printImplExtends(out, "implements", fInterface, fImplements);
342         out.println(" {");
343     }
344
345     /**
346      * Print the interface declaration.
347      */

348     private void printInterfaceDecl(IndentWriter out) {
349         out.print(JavaModifiers.toDecl(fModifiers));
350         out.print("interface ");
351         out.print(fInterface);
352
353         printImplExtends(out, "extends", null, fImplements);
354         out.println(" {");
355     }
356
357     /**
358      * Print the fields
359      */

360     private void printFields(IndentWriter out,
361                              int omitFlags) {
362         Iterator JavaDoc fields = getFields();
363         while (fields.hasNext()) {
364             JavaField field = (JavaField)fields.next();
365             if ((field.getModifiers() & omitFlags) == 0) {
366                 field.print(out);
367             }
368         }
369     }
370
371     /**
372      * Print the class initializer
373      */

374     private void printClassInitializer(IndentWriter out,
375                                        boolean printMethodBodies) {
376         if (printMethodBodies && !fClassInitializer.isEmpty()) {
377             out.println("/*");
378             out.println(" * Class initializer.");
379             out.println(" */");
380             out.println("static {");
381             out.enter();
382             fClassInitializer.print(out);
383             out.leave();
384             out.println("}");
385             out.println();
386         }
387     }
388
389     /**
390      * Print the constructors
391      */

392     private void printConstructors(IndentWriter out,
393                                    boolean printMethodBodies,
394                                    int omitFlags) {
395         Iterator JavaDoc constrs = getConstructors();
396         while (constrs.hasNext()) {
397             JavaMethod method = (JavaMethod)constrs.next();
398             if ((method.getModifiers() & omitFlags) == 0) {
399                 method.print(out, printMethodBodies);
400             }
401         }
402     }
403   
404     /**
405      * Print the methods
406      */

407     private void printMethods(IndentWriter out,
408                               boolean printMethodBodies,
409                               int omitFlags) {
410         Iterator JavaDoc methods = getMethods();
411         while (methods.hasNext()) {
412             JavaMethod method = (JavaMethod)methods.next();
413             if ((method.getModifiers() & omitFlags) == 0) {
414                 method.print(out, printMethodBodies);
415             }
416         }
417     }
418   
419     /**
420      * Print the body of a class, interface, or implementation.
421      * @param printMethodBodies If true, print the body of methods, if false
422      * don't print the bodies.
423      * @param omitFlags If any of these modifier flags are set, the
424      * method or field is omitted.
425      */

426     private void printBody(IndentWriter out,
427                            boolean printMethodBodies,
428                            int omitFlags) {
429         out.enter();
430         printFields(out, omitFlags);
431         printClassInitializer(out, printMethodBodies);
432         printConstructors(out, printMethodBodies, omitFlags);
433         printMethods(out, printMethodBodies, omitFlags);
434         out.leave();
435         out.println('}');
436     }
437
438     /**
439      * Output a generated class which doesn't have a corresponding
440      * interface.
441      */

442     public void printClass(IndentWriter out) {
443         printStart(out, false);
444         printClassDecl(out);
445         printBody(out, true, 0);
446     }
447
448     /**
449      * Output a generated interface.
450      */

451     public void printInterface(IndentWriter out) {
452         printStart(out, true);
453         printInterfaceDecl(out);
454         printBody(out, false, JavaModifiers.OMIT_INTERFACE);
455     }
456
457     /**
458      * Output a generated class which has an interface.
459      */

460     public void printImplementation(IndentWriter out) {
461         printStart(out, false);
462         printClassDecl(out);
463         printBody(out, true, JavaModifiers.OMIT_IMPLEMENTATION);
464     }
465
466 }
467
Popular Tags