1 26 27 package org.objectweb.openccm.pss.generator.hibernate.lib; 28 29 31 import org.objectweb.openccm.ast.api.StorageTypeDecl; 32 import org.objectweb.openccm.ast.api.StorageTypeStateMemberDecl; 33 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 ; 41 import java.util.Enumeration ; 42 import java.util.List ; 43 import java.util.Set ; 44 45 46 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 64 65 public PSDL_JavaTranslator translator_; 66 67 68 public GeneratorBase generator_; 69 70 76 79 public HibernateXmlGenerator(PSDL2JavaGenerator gen) 80 { 81 translator_ = new org.objectweb.openccm.generator.translator.idl2java.lib.PSDL_JavaTranslator(); 83 generator_ = gen; 84 disableLog(); 86 } 87 88 94 103 private void 104 create_file( String base_dir_str, 105 String package_name, 106 String st_name, 107 String id ) 108 { 109 String filename = null; 111 File base_dir = null, 112 target_dir = null; 113 114 base_dir = new File (base_dir_str); 115 target_dir = new File ( 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 ( target_dir.getAbsolutePath() + 126 File.separatorChar + 127 st_name + ".hbm.xml" ); 128 System.out.println( "File '" + filename + "' has been generated"); 129 130 open(filename, id); 132 } 133 134 141 private Set 142 get_user_subclasses( java.util.Properties props, 143 org.objectweb.openccm.ast.api.StorageTypeDecl st ) 144 { 145 Set user_classes = new java.util.HashSet (); 146 String name = translator_.getPackage(st) + "." + st.getName(); 147 148 if ( props.containsValue(name) ) 149 { 150 String key = null; 151 152 for(Enumeration keys = props.keys(); keys.hasMoreElements(); ) 153 { 154 key = (String ) keys.nextElement(); 155 if (props.getProperty(key).compareTo(name) == 0) 156 user_classes.add(key); 157 } 158 } 159 return user_classes; 160 } 161 162 170 private boolean 171 has_user_subclasses( java.util.Properties props, 172 org.objectweb.openccm.ast.api.StorageTypeDecl st ) 173 { 174 return props.containsValue( translator_.getPackage(st) + "." + st.getName() ); 175 } 176 177 184 private String 185 getPackage(String name) 186 { 187 return name.substring(0, name.lastIndexOf(".")); 188 } 189 190 197 private String 198 getShortName(String name) 199 { 200 return name.substring(name.lastIndexOf(".") + 1); 201 } 202 203 209 214 public void 215 initialize( String app_name ) 216 { 217 List list = new java.util.ArrayList (); 218 219 220 221 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 super.init(); 229 230 231 put("which", app_name); 232 } 233 234 239 public void 240 mapStorageType( org.objectweb.openccm.ast.api.StorageTypeDecl st ) 241 { 242 java.util.List members = null; 243 StorageTypeMapping st_mapping = null; 244 StorageTypeStateMemberDecl[] sm_array = null; 245 String 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 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 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 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 293 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 310 public void 311 to_hibernate_xml( String base_dir, 312 List storage_types, 313 String user_properties_file ) 314 throws GenerationException 315 { 316 java.io.File out_dir = null; 317 java.util.Properties user_props = null; 318 java.util.Iterator it = null; 319 StorageTypeDecl st = null; 320 321 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 try { 331 java.io.InputStream propStream = null; 332 333 propStream = new java.io.FileInputStream (user_properties_file); 334 user_props = new java.util.Properties (); 335 user_props.load(propStream); 336 }catch (Exception e) { 337 user_props = null; 339 } 340 341 it = storage_types.iterator(); 343 344 while (it.hasNext()) 345 { 346 st = (StorageTypeDecl) it.next(); 347 348 if (st != null) 349 { 350 String package_name = translator_.getPackage(st); 352 Set subclasses = get_user_subclasses(user_props, st); 353 354 create_file( base_dir, 355 package_name, 356 st.getName(), 357 "out" ); 358 359 put("gen", this); 361 put("translator", translator_); 362 put("class", st); 363 put("package_name", package_name); 364 put("has_subclasses", new Boolean (has_user_subclasses(user_props, st))); 365 put("subclasses", subclasses); 366 map("HIBERNATE_FILE"); 367 } 368 } 369 370 392 } 393 } 394 | Popular Tags |