KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > pss > generator > jdo > lib > JdoXmlGenerator


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.jdo.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.generator.translator.idl2java.api.PSDL_JavaTranslator;
35 import org.objectweb.openccm.generator.common.lib.GenerationException;
36 import org.objectweb.openccm.pss.generator.common.api.PSDL2JavaGenerator;
37 import org.objectweb.openccm.pss.generator.common.api.StorageTypeMapping;
38
39 import java.io.File JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.ArrayList JavaDoc;
42
43
44 /**
45  * This class generates xml JDO files for storage types.
46  *
47  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
48  *
49  * @version 0.1
50  */

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

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

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

78
79     public JdoXmlGenerator(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 package name of the jdo file.
100      * @param id - An identifiant for this stream.
101      **/

102     private void
103     create_file( String JavaDoc base_dir_str,
104                  String JavaDoc package_name,
105                  String JavaDoc id )
106     {
107         // Create the target directory
108
String JavaDoc filename = null;
109         File JavaDoc base_dir = null,
110              target_dir = null;
111
112         base_dir = new File JavaDoc(base_dir_str);
113         target_dir = new File JavaDoc( base_dir.getAbsolutePath() +
114                                File.separatorChar +
115                                translator_.getAsDirectory(package_name) );
116         try{
117             org.objectweb.openccm.generator.common.lib.FileManager.mkdir(target_dir);
118         }catch(GenerationException ex){
119             System.err.println(ex.getMessage());
120             return;
121         }
122
123         package_name = package_name.substring(package_name.lastIndexOf('.')+1);
124         filename = new String JavaDoc( target_dir.getAbsolutePath() +
125                                File.separatorChar +
126                                package_name + ".jdo" );
127         System.out.println( "File '" + filename + "' has been generated");
128
129         // create the target file and map
130
open(filename, id);
131     }
132
133     /**
134      * Get user classes to define in the jdo file for a package.
135      * This methode iterates on user properties and check the user class package.
136      *
137      * @param props - The user properties.
138      * @param package_name - The package name of the jdo file.
139      *
140      * @return A map of classes (user_class - super_class).
141      **/

142     private java.util.Map JavaDoc
143     get_user_classes( java.util.Properties JavaDoc props,
144                       String JavaDoc package_name)
145     {
146         java.util.Enumeration JavaDoc keys = null;
147         java.util.Map JavaDoc user_classes = null;
148
149         user_classes = new java.util.HashMap JavaDoc();
150
151         if (props != null)
152         {
153             String JavaDoc key = null;
154
155             keys = props.keys();
156
157             while (keys.hasMoreElements())
158             {
159                 key = (String JavaDoc) keys.nextElement();
160
161                 if ( key.startsWith(package_name) )
162                 {
163                     String JavaDoc class_name = key.substring(package_name.length()+1);
164                     if (class_name.indexOf(".") == -1)
165                         user_classes.put(class_name, props.getProperty(key) );
166                 }
167             }
168         }
169         return user_classes;
170     }
171
172     // ==================================================================
173
//
174
// Public methods.
175
//
176
// ==================================================================
177

178     /**
179      * Initialize the generator.
180      *
181      * @param app_name - The application's name.
182      **/

183     public void
184     initialize( String JavaDoc app_name )
185     {
186         List JavaDoc list = new java.util.ArrayList JavaDoc();
187
188         /** Initialize environment **/
189
190         // Templates to use.
191
list.clear();
192         list.add("org/objectweb/openccm/pss/generator/common/common.vm");
193         list.add("org/objectweb/openccm/pss/generator/jdo/xml_jdo.vm");
194         setLibrary(list);
195
196         // call the common generator init method
197
super.init();
198
199         /** Initialize velocity context **/
200         put("which", app_name);
201     }
202
203     /**
204      * Map a storage type to a JDO persistent capable classe in the xml file.
205      *
206      * @param st - The storage type to map.
207      **/

208     public void
209     mapStorageType( StorageTypeDecl st )
210     {
211         java.util.List JavaDoc members = null;
212         StorageTypeMapping st_mapping = null;
213         StorageTypeStateMemberDecl[] sm_array = null;
214         String JavaDoc name = null,
215                type = null;
216
217         st_mapping = new org.objectweb.openccm.pss.generator.jdo.lib.StorageTypeMapping();
218         st_mapping.setStorageType(st);
219
220         // Map native state members
221
members = st_mapping.getStatesToNative(generator_);
222         sm_array = (StorageTypeStateMemberDecl[])members.toArray(new StorageTypeStateMemberDecl[0]);
223         for (int i=0; i< members.size(); i++)
224         {
225             name = StateMemberMapping.mapStateName( sm_array[i].getName() );
226             put("field_name", name);
227             type = translator_.toJava( sm_array[i].getType() );
228             if (type.compareTo("byte[]") == 0)
229                 map("JDO_FIELD_BLOB");
230             else
231                 map("JDO_FIELD");
232         }
233
234         // Map state members to IOR
235
members = st_mapping.getStatesToIOR(generator_);
236         sm_array = (StorageTypeStateMemberDecl[])members.toArray(new StorageTypeStateMemberDecl[0]);
237         for (int i=0; i< members.size(); i++)
238         {
239             name = StateMemberMapping.mapMarshalledStateName( sm_array[i].getName() );
240             put("field_name", name);
241             map("JDO_FIELD_CLOB");
242         }
243
244         // Map state members to byte[]
245
members = st_mapping.getStatesToCodec(generator_);
246         sm_array = (StorageTypeStateMemberDecl[])members.toArray(new StorageTypeStateMemberDecl[0]);
247         for (int i=0; i< members.size(); i++)
248         {
249             name = StateMemberMapping.mapMarshalledStateName( sm_array[i].getName() );
250             put("field_name", name);
251             map("JDO_FIELD_BLOB");
252         }
253         // Map the SH RID state
254
put("field_name", "sh_rid");
255         // If SH RID can't be fed in a varchar type
256
//map("JDO_FIELD_CLOB");
257
map("JDO_FIELD");
258
259         // Map state member to Class
260
members = st_mapping.getStatesToClass(generator_);
261         sm_array = (StorageTypeStateMemberDecl[])members.toArray(new StorageTypeStateMemberDecl[0]);
262         for (int i=0; i< members.size(); i++)
263         {
264             name = StateMemberMapping.mapMarshalledStateName( sm_array[i].getName() );
265             put("field_name", name);
266             map("JDO_FIELD");
267         }
268     }
269
270     /**
271      * Generate xml JDO files.
272      *
273      * @param base_dir - The base directory where files will be generated.
274      * @param storage_types - Storage Types to include in xml JDO files.
275      * @param user_properties_file - The name of user jdo properties file.
276      **/

277     public void
278     to_jdo_xml(String JavaDoc base_dir,
279                List JavaDoc storage_types,
280                String JavaDoc user_properties_file)
281     throws GenerationException
282     {
283         java.io.File JavaDoc out_dir = null;
284         StorageTypeDecl[] st_array = null;
285         java.util.Map JavaDoc packages = null;
286         java.util.List JavaDoc childs = null;
287         String JavaDoc parent_id = null;
288
289         // Check and create the output directory
290
try{
291             out_dir = org.objectweb.openccm.generator.common.lib.FileManager.mkdir(base_dir);
292         }catch(GenerationException ex){
293             System.err.println(ex.getMessage());
294             return;
295         }
296
297         // Group storage types by package
298
st_array = (StorageTypeDecl[])storage_types.toArray(new StorageTypeDecl[0]);
299         packages = new java.util.HashMap JavaDoc();
300         for (int i=0; i<st_array.length; i++)
301         {
302             parent_id = translator_.getPackage(st_array[i]);
303             childs = (ArrayList JavaDoc) packages.get(parent_id);
304
305             if (childs == null)
306             {
307                 childs = new java.util.ArrayList JavaDoc();
308             }
309             childs.add(st_array[i]);
310             packages.put(parent_id, childs);
311         }
312
313         // Retrieve JDO user properties
314
java.util.Properties JavaDoc user_props = null;
315
316         try {
317             java.io.InputStream JavaDoc propStream = null;
318
319             propStream = new java.io.FileInputStream JavaDoc(user_properties_file);
320             user_props = new java.util.Properties JavaDoc();
321             user_props.load(propStream);
322         }catch (Exception JavaDoc e) {
323             // No user properties
324
user_props = null;
325         }
326
327         // Add user packages to the package list
328
java.util.Enumeration JavaDoc keys = null;
329
330         if (user_props != null)
331         {
332             String JavaDoc package_name = null;
333
334             keys = user_props.keys();
335
336             while (keys.hasMoreElements())
337             {
338                 package_name = (String JavaDoc) keys.nextElement();
339                 package_name = package_name.substring(0, package_name.lastIndexOf('.'));
340                 if ( !packages.containsKey(package_name) )
341                     packages.put(package_name, null);
342             }
343         }
344
345
346         // For each Package, generate a JDO xml file
347
java.util.Iterator JavaDoc it = null;
348         String JavaDoc key = null;
349         java.util.Map JavaDoc user_classes = null;
350
351         it = packages.keySet().iterator();
352         while (it.hasNext())
353         {
354             key = (String JavaDoc) it.next();
355             childs = (ArrayList JavaDoc) packages.get(key);
356             user_classes = get_user_classes(user_props, key);
357
358             if ( (childs != null)
359                  || (user_classes.size()>0) )
360             {
361                 create_file(base_dir, key, "out");
362                 // Complete the file
363
put("gen", this);
364                 put("translator", translator_);
365                 put("package_name", key);
366                 put("classes", childs);
367                 put("user_classes", user_classes);
368                 map("JDO_FILE");
369             }
370         }
371     }
372 }
373
Popular Tags