KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > pss > generator > hibernate > lib > HibernateXmlGenerator


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): Alex Andrushchak.
23 Contributor(s): Christophe Demarey.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.pss.generator.hibernate.lib;
28
29 // Package dependencies
30
/** To access AST. */
31 import org.objectweb.openccm.ast.api.StorageTypeDecl;
32 import org.objectweb.openccm.ast.api.StorageTypeStateMemberDecl;
33 /** Others. */
34 import org.objectweb.openccm.pss.generator.common.api.StorageTypeMapping;
35 import org.objectweb.openccm.pss.generator.common.api.PSDL2JavaGenerator;
36 import org.objectweb.openccm.pss.generator.common.lib.StateMemberMapping;
37 import org.objectweb.openccm.generator.common.api.GeneratorBase;
38 import org.objectweb.openccm.generator.common.lib.GenerationException;
39 import org.objectweb.openccm.generator.translator.idl2java.api.PSDL_JavaTranslator;
40 import java.io.File JavaDoc;
41 import java.util.Enumeration JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Set JavaDoc;
44
45
46 /**
47  * This class generates xml Hibernate files for storage types.
48  *
49  * @author <a HREF="mailto:pretend@ukr.net">Alex Andrushchak</a>
50  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
51  */

52
53 public class HibernateXmlGenerator
54      extends org.objectweb.openccm.generator.common.lib.Generator
55   implements org.objectweb.openccm.pss.generator.hibernate.api.HibernateXmlGenerator
56 {
57
58     // ==================================================================
59
//
60
// Internal state.
61
//
62
// ==================================================================
63

64     /** Utility class to convert types in Java */
65     public PSDL_JavaTranslator translator_;
66
67     /** A generator to get declarations */
68     public GeneratorBase generator_;
69
70     // ==================================================================
71
//
72
// Constructors.
73
//
74
// ==================================================================
75

76     /**
77      * The default constructor.
78      */

79     public HibernateXmlGenerator(PSDL2JavaGenerator gen)
80     {
81         // Init internal state
82
translator_ = new org.objectweb.openccm.generator.translator.idl2java.lib.PSDL_JavaTranslator();
83         generator_ = gen;
84         // No log
85
disableLog();
86     }
87
88     // ==================================================================
89
//
90
// Internal methods.
91
//
92
// ==================================================================
93

94     /**
95      * Create the output file.
96      * This include creation of package directories if necessary.
97      *
98      * @param base_dir_str - The base directory where files will be generated.
99      * @param package_name - The Storage Type package name.
100      * @param st_name - The Storage Type class name.
101      * @param id - An identifiant for this stream.
102      **/

103     private void
104     create_file( String JavaDoc base_dir_str,
105                  String JavaDoc package_name,
106                  String JavaDoc st_name,
107                  String JavaDoc id )
108     {
109         // Create the target directory
110
String JavaDoc filename = null;
111         File JavaDoc base_dir = null,
112              target_dir = null;
113
114         base_dir = new File JavaDoc(base_dir_str);
115         target_dir = new File JavaDoc( base_dir.getAbsolutePath() +
116                                File.separatorChar +
117                                translator_.getAsDirectory(package_name) );
118         try{
119             org.objectweb.openccm.generator.common.lib.FileManager.mkdir(target_dir);
120         }catch(GenerationException ex){
121             System.err.println(ex.getMessage());
122             return;
123         }
124
125         filename = new String JavaDoc( target_dir.getAbsolutePath() +
126                                File.separatorChar +
127                                st_name + ".hbm.xml" );
128         System.out.println( "File '" + filename + "' has been generated");
129
130         // create the target file and map
131
open(filename, id);
132     }
133     
134     /**
135      * Get User sub-classes of the Storage Type ST.
136      * They will be included in mapping files.
137      *
138      * @param props - User properties.
139      * @param st - A Storage Type.
140      */

141     private Set JavaDoc
142     get_user_subclasses( java.util.Properties JavaDoc props,
143                          org.objectweb.openccm.ast.api.StorageTypeDecl st )
144     {
145         Set JavaDoc user_classes = new java.util.HashSet JavaDoc();
146         String JavaDoc name = translator_.getPackage(st) + "." + st.getName();
147
148         if ( props.containsValue(name) )
149         {
150             String JavaDoc key = null;
151             
152             for(Enumeration JavaDoc keys = props.keys(); keys.hasMoreElements(); )
153             {
154                 key = (String JavaDoc) keys.nextElement();
155                 if (props.getProperty(key).compareTo(name) == 0)
156                     user_classes.add(key);
157             }
158         }
159         return user_classes;
160     }
161
162     /**
163      * Has this Storage Type user sub-classes?
164      *
165      * @param props - User properties.
166      * @param st - A Storage Type.
167      *
168      * @return True if st has user sub-classes, else false.
169      */

170     private boolean
171     has_user_subclasses( java.util.Properties JavaDoc props,
172                          org.objectweb.openccm.ast.api.StorageTypeDecl st )
173     {
174         return props.containsValue( translator_.getPackage(st) + "." + st.getName() );
175     }
176     
177     /**
178      * Get the package of a declaration.
179      *
180      * @param name - The declaration name.
181      *
182      * @return The declaration's package.
183      */

184     private String JavaDoc
185     getPackage(String JavaDoc name)
186     {
187         return name.substring(0, name.lastIndexOf("."));
188     }
189     
190     /**
191      * Get the short name of a declaration.
192      *
193      * @param name - The full declaration name.
194      *
195      * @return The declaration's short name.
196      */

197     private String JavaDoc
198     getShortName(String JavaDoc name)
199     {
200         return name.substring(name.lastIndexOf(".") + 1);
201     }
202     
203     // ==================================================================
204
//
205
// Public methods.
206
//
207
// ==================================================================
208

209     /**
210      * Initialize the generator.
211      *
212      * @param app_name - The application's name.
213      **/

214     public void
215     initialize( String JavaDoc app_name )
216     {
217         List JavaDoc list = new java.util.ArrayList JavaDoc();
218
219         /** Initialize environment **/
220
221         // Templates to use.
222
list.clear();
223         list.add("org/objectweb/openccm/pss/generator/common/common.vm");
224         list.add("org/objectweb/openccm/pss/generator/hibernate/xml_hibernate.vm");
225         setLibrary(list);
226
227         // call the common generator init method
228
super.init();
229
230         /** Initialize velocity context **/
231         put("which", app_name);
232     }
233
234     /**
235      * Map a storage type to a Hibernate persistent capable classe in the xml file.
236      *
237      * @param st - The storage type to map.
238      **/

239     public void
240     mapStorageType( org.objectweb.openccm.ast.api.StorageTypeDecl st )
241     {
242         java.util.List JavaDoc members = null;
243         StorageTypeMapping st_mapping = null;
244         StorageTypeStateMemberDecl[] sm_array = null;
245         String JavaDoc name = null,
246                type = null;
247
248         st_mapping = new org.objectweb.openccm.pss.generator.hibernate.lib.StorageTypeMapping();
249         st_mapping.setStorageType(st);
250
251         // Map native state members
252
members = st_mapping.getStatesToNative(generator_);
253         sm_array = (StorageTypeStateMemberDecl[])members.toArray(new StorageTypeStateMemberDecl[0]);
254         for (int i=0; i< members.size(); i++)
255         {
256             put("field_name", StateMemberMapping.mapStateName(sm_array[i].getName()));
257             type = translator_.toJava( sm_array[i].getType() );
258             put("field_type", type.toLowerCase());
259             if (type.compareTo("byte[]") == 0) {
260                 map("HIBERNATE_FIELD_BLOB");
261             } else {
262                 map("HIBERNATE_FIELD");
263             }
264         }
265
266         // Map state members to IOR
267
members = st_mapping.getStatesToIOR(generator_);
268         sm_array = (StorageTypeStateMemberDecl[])members.toArray(new StorageTypeStateMemberDecl[0]);
269         for (int i=0; i< members.size(); i++)
270         {
271             name = StateMemberMapping.mapMarshalledStateName( sm_array[i].getName() );
272             put("field_name", name);
273             map("HIBERNATE_FIELD_CLOB");
274         }
275
276         // Map state members to byte[]
277
members = st_mapping.getStatesToCodec(generator_);
278         sm_array = (StorageTypeStateMemberDecl[])members.toArray(new StorageTypeStateMemberDecl[0]);
279         for (int i=0; i< members.size(); i++)
280         {
281             name = StateMemberMapping.mapMarshalledStateName( sm_array[i].getName() );
282             put("field_name", name);
283             map("HIBERNATE_FIELD_BLOB");
284         }
285
286         // Map the SH RID state
287
/*
288         put("field_name", "sh_rid");
289         // If SH RID can't be fed in a varchar type
290         //map("HIBERNATE_FIELD_CLOB");
291         map("HIBERNATE_FIELD");
292 */

293         // Map state member to Class
294
members = st_mapping.getStatesToClass(generator_);
295         sm_array = (StorageTypeStateMemberDecl[])members.toArray(new StorageTypeStateMemberDecl[0]);
296         for (int i=0; i< members.size(); i++)
297         {
298             name = StateMemberMapping.mapMarshalledStateName( sm_array[i].getName() );
299             put("field_name", name);
300             map("HIBERNATE_FIELD");
301         }
302     }
303
304     /**
305      * Generate xml Hibernate files.
306      *
307      * @param base_dir - The base directory where files will be generated.
308      * @param storage_types - Storage Types to include in xml Hibernate files.
309      **/

310     public void
311     to_hibernate_xml( String JavaDoc base_dir,
312                       List JavaDoc storage_types,
313                       String JavaDoc user_properties_file )
314     throws GenerationException
315     {
316         java.io.File JavaDoc out_dir = null;
317         java.util.Properties JavaDoc user_props = null;
318         java.util.Iterator JavaDoc it = null;
319         StorageTypeDecl st = null;
320
321         // Check and create the output directory
322
try{
323             out_dir = org.objectweb.openccm.generator.common.lib.FileManager.mkdir(base_dir);
324         }catch(GenerationException ex){
325             System.err.println(ex.getMessage());
326             return;
327         }
328
329         // Retrieve user properties
330
try {
331             java.io.InputStream JavaDoc propStream = null;
332
333             propStream = new java.io.FileInputStream JavaDoc(user_properties_file);
334             user_props = new java.util.Properties JavaDoc();
335             user_props.load(propStream);
336         }catch (Exception JavaDoc e) {
337             // No user properties
338
user_props = null;
339         }
340         
341         // For each Storage type, generate an Hibernate xml file
342
it = storage_types.iterator();
343         
344         while (it.hasNext())
345         {
346             st = (StorageTypeDecl) it.next();
347             
348             if (st != null)
349             {
350                 // Prepare generation
351
String JavaDoc package_name = translator_.getPackage(st);
352                 Set JavaDoc subclasses = get_user_subclasses(user_props, st);
353                              
354                 create_file( base_dir,
355                              package_name,
356                              st.getName(),
357                              "out" );
358                              
359                 // Complete the file
360
put("gen", this);
361                 put("translator", translator_);
362                 put("class", st);
363                 put("package_name", package_name);
364                 put("has_subclasses", new Boolean JavaDoc(has_user_subclasses(user_props, st)));
365                 put("subclasses", subclasses);
366                 map("HIBERNATE_FILE");
367             }
368         }
369         
370         // For each User class, generate an Hibernate xml file
371
// The following code is valid but we need to correct a bug in hibernate before use it.
372
// (Hibernate don't try to load the hbm.xml file of the super class)
373
/* for(Enumeration e = user_props.keys(); e.hasMoreElements(); )
374         {
375             String user_class = (String) e.nextElement();
376             
377             if (user_class != null)
378             {
379                 create_file( base_dir,
380                              getPackage(user_class),
381                              getShortName(user_class),
382                              "out" );
383                              
384                 // Complete the file
385                 put("class", user_class);
386                 put("super_class", user_props.getProperty(user_class) );
387                 put("is_subclasses", new Boolean(true));
388                 map("HIBERNATE_FILE");
389             }
390         }
391 */

392     }
393 }
394
Popular Tags