KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > pss > generator > common > lib > PSDL2JavaGenerator


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.pss.generator.common.lib;
28
29 // Package dependencies
30
/** To access AST. */
31 import org.objectweb.openccm.ast.api.AbstractStorageTypeDecl;
32 import org.objectweb.openccm.ast.api.StorageTypeDecl;
33 import org.objectweb.openccm.ast.api.AbstractStorageHomeDecl;
34 import org.objectweb.openccm.ast.api.StorageHomeDecl;
35 import org.objectweb.openccm.ast.api.DeclarationKind;
36 import org.objectweb.openccm.ast.api.Scope;
37 import org.objectweb.openccm.ast.api.AST;
38 /** To access Java AST. */
39 import org.objectweb.openccm.generator.java.core.lib.*;
40 import org.objectweb.openccm.generator.java.ast.api.*;
41 import org.objectweb.openccm.generator.java.ast.lib.*;
42 /** Others. */
43 import org.objectweb.openccm.generator.common.lib.GenerationException;
44 import org.objectweb.openccm.generator.translator.idl2java.api.PSDL_JavaTranslator;
45 import org.objectweb.openccm.pss.generator.hibernate.api.HibernateXmlGenerator;
46 import org.objectweb.openccm.pss.generator.jdo.api.JdoXmlGenerator;
47
48 import java.util.List JavaDoc;
49
50
51 /**
52  * This class generates PSDL to Java Mappings.
53  *
54  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
55  *
56  * @version 0.1
57  */

58
59 public class PSDL2JavaGenerator
60      extends org.objectweb.openccm.generator.common.lib.GeneratorBase
61   implements org.objectweb.openccm.pss.generator.common.api.PSDL2JavaGenerator
62 {
63
64     // ==================================================================
65
//
66
// Constants.
67
//
68
// ==================================================================
69

70     /** Constant to refer a JDO Back-end */
71     private static final int JDO_BACKEND = 0;
72
73     /** Constant to refer an Hibernate Back-end */
74     private static final int HIBERNATE_BACKEND = 1;
75
76     // ==================================================================
77
//
78
// Internal states.
79
//
80
// ==================================================================
81

82     /** The java repository */
83     private Repository repository_;
84
85     /** The backend to used for mapping */
86     private int backend_;
87
88     /** Utility class to convert types in Java */
89     public PSDL_JavaTranslator translator_;
90     
91     // ==================================================================
92
//
93
// Constructors.
94
//
95
// ==================================================================
96

97     /**
98      * The default constructor.
99      *
100      * @param ast - The Abstract Syntax Tree.
101      */

102
103     public PSDL2JavaGenerator(AST ast)
104     {
105         super(ast);
106         // Init internal state
107
repository_ = null;
108         translator_ = new org.objectweb.openccm.generator.translator.idl2java.lib.PSDL_JavaTranslator();
109         backend_ = JDO_BACKEND;
110     }
111
112     // ==================================================================
113
//
114
// Internal methods.
115
//
116
// ==================================================================
117

118     // ==================================================================
119
//
120
// Public methods.
121
//
122
// ==================================================================
123

124     // ==================================================================
125
//
126
// Useful methods.
127
//
128
// ==================================================================
129

130
131     // ==================================================================
132
//
133
// Others methods.
134
//
135
// ==================================================================
136

137     /**
138      * Get the generator translator utility class.
139      *
140      * @return The translator.
141      **/

142     public PSDL_JavaTranslator
143     getTranslator()
144     {
145         return translator_;
146     }
147
148     /**
149      * Get the java Repository.
150      *
151      * @return The java repository.
152      **/

153     public Repository
154     getJavaRepository()
155     {
156         return repository_;
157     }
158
159
160     /**
161      * Generate java files for the PSDL JAVA Mappings.
162      *
163      * @param base_dir - The base directory where files will be generated.
164      * @param scope - The scope to map.
165      * @param user_properties_file - The user properties file name.
166      * @param backend - The target persistent framework.
167      **/

168     public void
169     psdl_to_java(String JavaDoc base_dir,
170                  Scope scope,
171                  String JavaDoc user_properties_file,
172                  String JavaDoc backend)
173     throws GenerationException
174     {
175         java.io.File JavaDoc out_dir = null;
176         repository_ = new RepositoryImpl();
177         List JavaDoc vect = null;
178         AbstractStorageTypeDecl[] ast_array = null;
179         StorageTypeDecl[] st_array = null;
180         AbstractStorageHomeDecl[] ash_array = null;
181         StorageHomeDecl[] sh_array = null;
182
183         // Check and create the output directory
184
try{
185             out_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         // Fix persistent framework properties
192
System.getProperties().put("openccm.pss.pf", backend);
193         if (backend.compareTo("jdo") == 0)
194         {
195             backend_ = JDO_BACKEND;
196         }
197         else if (backend.compareTo("hibernate") == 0)
198         {
199             backend_ = HIBERNATE_BACKEND;
200         }
201
202
203         /** Get all Abstract Storage Types contained in the file scope and map **/
204         vect = getAllDeclarations(scope, DeclarationKind.dk_abstract_storage_type);
205         ast_array = (AbstractStorageTypeDecl[])vect.toArray(new AbstractStorageTypeDecl[0]);
206         for (int i=0; i<ast_array.length; i++)
207         {
208             org.objectweb.openccm.pss.generator.common.api.AbstractStorageTypeMapping ast = null;
209             
210             ast = (org.objectweb.openccm.pss.generator.common.api.AbstractStorageTypeMapping)
211                             MappingObjectFactory.getClass("AbstractStorageTypeMapping");
212             ast.setAbstractStorageType(ast_array[i]);
213             ast.toJava(this);
214         }
215
216         /** Get all Abstract Storage Homes contained in the file scope and map **/
217         vect = getAllDeclarations(scope, DeclarationKind.dk_abstract_storage_home);
218         ash_array = (AbstractStorageHomeDecl[])vect.toArray(new AbstractStorageHomeDecl[0]);
219         for (int i=0; i<ash_array.length; i++)
220         {
221             org.objectweb.openccm.pss.generator.common.api.AbstractStorageHomeMapping ash = null;
222             
223             ash = (org.objectweb.openccm.pss.generator.common.api.AbstractStorageHomeMapping)
224                             MappingObjectFactory.getClass("AbstractStorageHomeMapping");
225             ash.setAbstractStorageHome(ash_array[i]);
226             ash.toJava(this);
227         }
228
229         /** Get all Storage Types contained in the file scope and map **/
230         vect = getAllDeclarations(scope, DeclarationKind.dk_storage_type);
231         st_array = (StorageTypeDecl[])vect.toArray(new StorageTypeDecl[0]);
232         for (int i=0; i<st_array.length; i++)
233         {
234             org.objectweb.openccm.pss.generator.common.api.StorageTypeMapping st = null;
235
236             st = (org.objectweb.openccm.pss.generator.common.api.StorageTypeMapping)
237                             MappingObjectFactory.getClass("StorageTypeMapping");
238             st.setStorageType(st_array[i]);
239             st.toJava(true, this);
240         }
241         // Generate associated xml file
242
if (backend_ == JDO_BACKEND)
243         {
244             JdoXmlGenerator jdo_xml_gen = null;
245             
246             jdo_xml_gen = new org.objectweb.openccm.pss.generator.jdo.lib.JdoXmlGenerator(this);
247             jdo_xml_gen.initialize("psdl_java");
248             jdo_xml_gen.to_jdo_xml(base_dir, vect, user_properties_file);
249         }
250         else
251         {
252             HibernateXmlGenerator hibernate_xml_gen = null;
253             
254             hibernate_xml_gen = new org.objectweb.openccm.pss.generator.hibernate.lib.HibernateXmlGenerator(this);
255             hibernate_xml_gen.initialize("psdl_java");
256             hibernate_xml_gen.to_hibernate_xml(base_dir, vect, user_properties_file);
257         }
258
259         /** Get all Storage Homes contained in the file scope and map **/
260         vect = getAllDeclarations(scope, DeclarationKind.dk_storage_home);
261         sh_array = (StorageHomeDecl[])vect.toArray(new StorageHomeDecl[0]);
262         for (int i=0; i<sh_array.length; i++)
263         {
264             org.objectweb.openccm.pss.generator.common.api.StorageHomeMapping sh = null;
265
266             sh = (org.objectweb.openccm.pss.generator.common.api.StorageHomeMapping)
267                             MappingObjectFactory.getClass("StorageHomeMapping");
268             sh.setStorageHome(sh_array[i]);
269             sh.toJava(this);
270         }
271
272         // Generate Java Files
273
org.objectweb.openccm.generator.java.core.lib.JavaGenerator java_gen = new JavaGenerator();
274         java.util.List JavaDoc v = new java.util.ArrayList JavaDoc();
275         v.add("org/objectweb/openccm/pss/generator/common/psdl2java_common.vm");
276         if (backend_ == JDO_BACKEND)
277         {
278             v.add("org/objectweb/openccm/pss/generator/jdo/psdl2java_jdo.vm");
279         }
280         else
281         {
282             v.add("org/objectweb/openccm/pss/generator/hibernate/psdl2java_hibernate.vm");
283         }
284         java_gen.initialize("PSDL2JAVA", v);
285         java_gen.generate(repository_, base_dir);
286     }
287 }
288
Popular Tags