KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > generator > java > core > lib > JavaGenerator


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Christophe Demarey.
23 Contributor(s): ________________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.generator.java.core.lib;
28
29 // Package dependencies
30
import org.objectweb.openccm.generator.java.ast.api.*;
31 import org.objectweb.openccm.generator.translator.idl2java.api.IDL_JavaTranslator;
32 import org.objectweb.openccm.generator.common.lib.GenerationException;
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35 import java.io.File JavaDoc;
36
37
38 /**
39  * This class is a java generator. Generation is done from a java AST.
40  *
41  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</A>
42  */

43
44 public class JavaGenerator
45      extends org.objectweb.openccm.generator.common.lib.Generator
46   implements org.objectweb.openccm.generator.java.core.api.JavaGenerator
47 {
48     // ==================================================================
49
//
50
// Internal state.
51
//
52
// ==================================================================
53

54     /** The output base directory */
55     private File JavaDoc base_dir_;
56
57     /** Utility class to convert types in Java*/
58     public IDL_JavaTranslator translator_;
59
60     // ==================================================================
61
//
62
// Constructor.
63
//
64
// ==================================================================
65

66     /** The default constructor. */
67     public
68     JavaGenerator()
69     {
70         super();
71         disableLog();
72         // Init internal state
73
translator_ = new org.objectweb.openccm.generator.translator.idl2java.lib.IDL_JavaTranslator();
74     }
75
76     // ==================================================================
77
//
78
// Internal methods.
79
//
80
// ==================================================================
81

82     /**
83      * Generate a java class.
84      *
85      * @param rep - The repository to generate.
86      **/

87     private void
88     map_class(ClassObject clazz)
89     {
90         // Map header
91
create_file(clazz, "out");
92         put("package_name", clazz.getPackage());
93         map("FILE_HEADER");
94
95         // Map class
96
put("modifier", compute_class_prefix(clazz));
97         put("class_imports", clazz.getImports());
98         put("class_comments", clazz.getComments());
99         put("class_name", clazz.getName());
100         put("extends_list", clazz.getInheritedObjects());
101         put("impl_list", clazz.getImplementedObjects());
102         put("att_list", clazz.getAttributes());
103         put("constructor_list", clazz.getConstructors());
104         put("method_list", clazz.getMethods());
105         map("CLASS");
106
107         // End of mapping
108
close("out");
109     }
110
111     /**
112      * Generate a java interface.
113      *
114      * @param rep - The repository to generate.
115      **/

116     private void
117     map_interface(InterfaceObject itf)
118     {
119         // Map header
120
create_file(itf, "out");
121         put("package_name", itf.getPackage());
122         map("FILE_HEADER");
123
124         // Map interface
125
put("modifier", compute_class_prefix(itf));
126         put("itf_imports", itf.getImports());
127         put("itf_comments", itf.getComments());
128         put("itf_name", itf.getName());
129         put("extends_list", itf.getInheritedObjects());
130         put("itf_method_list", itf.getMethods());
131         map("INTERFACE");
132
133         // End of mapping
134
close("out");
135     }
136
137     // ==================================================================
138
//
139
// Public methods.
140
//
141
// ==================================================================
142

143     /**
144      * Initialize the generator.
145      *
146      * @param app_name - The application's name.
147      * @param templates - Templates files to use.
148      **/

149     public void
150     initialize( String JavaDoc app_name,
151                 List JavaDoc templates )
152     {
153         List JavaDoc list = new java.util.ArrayList JavaDoc();
154
155         /** Initialize environment **/
156
157         // Templates to use.
158
list.clear();
159         list.add("org/objectweb/openccm/generator/common/common.vm");
160         list.add("org/objectweb/openccm/generator/java/core/java.vm");
161         list.addAll(templates);
162         setLibrary(list);
163
164         // call the common generator init method
165
super.init();
166
167         /** Initialize velocity context **/
168         put("which", app_name);
169         put("gen", this);
170     }
171
172     /**
173      * Generate java files from the repository.
174      *
175      * @param base_dir - The base directory where files will be generated.
176      **/

177     public void
178     generate(Repository rep, String JavaDoc base_dir)
179     {
180         ArrayList JavaDoc decls = null;
181
182         // Check and/or create the output base dir
183
// Check and create the output directory
184
try{
185             base_dir_ = org.objectweb.openccm.generator.common.lib.FileManager.mkdir(base_dir);
186         }catch(GenerationException ex){
187             System.err.println(ex.getMessage());
188             return;
189         }
190
191         // Get Repository contents
192
decls = rep.getContents();
193
194         for (int i=0; i<decls.size(); i++)
195         {
196             // Map a class
197
try{
198                 ClassObject clazz = (ClassObject)decls.get(i);
199                 map_class(clazz);
200             }catch(ClassCastException JavaDoc ex1){
201                 // Map an interface
202
try{
203                     InterfaceObject itf = (InterfaceObject)decls.get(i);
204                     map_interface(itf);
205                 }catch(ClassCastException JavaDoc ex2){
206                     // Should not happen
207
ex2.printStackTrace();
208                 }
209             }
210         }
211     }
212
213     /**
214      * Create the output file.
215      * This include creation of package directories if necessary.
216      *
217      * @param object - The class or interface to generate.
218      * @param id - An identifiant for this stream.
219      **/

220     public void
221     create_file(InterfaceObject object, String JavaDoc id)
222     {
223         // Create the target directory
224
String JavaDoc filename = null;
225         File JavaDoc target_dir = null;
226
227         target_dir = new File JavaDoc( base_dir_.getAbsolutePath() +
228                                File.separatorChar +
229                                translator_.getAsDirectory(object.getPackage()) );
230         try{
231             org.objectweb.openccm.generator.common.lib.FileManager.mkdir(target_dir);
232         }catch(GenerationException ex){
233             System.err.println(ex.getMessage());
234             return;
235         }
236
237         filename = new String JavaDoc( target_dir.getAbsolutePath() +
238                                File.separatorChar +
239                                object.getName() + ".java" );
240         System.out.println( "File '" + filename + "' has been generated");
241
242         // create the target file and map
243
open(filename, id);
244     }
245
246     // ==================================================================
247
//
248
// Useful methods for java mapping.
249
//
250
// ==================================================================
251

252     public String JavaDoc
253     compute_class_prefix(InterfaceObject obj)
254     {
255         String JavaDoc result = "";
256
257         result = obj.getModifier().toString();
258         if (obj.isAbstract())
259             result += " abstract";
260         else if(obj.isFinal())
261             result += " final";
262
263         return result;
264     }
265 }
266
Popular Tags