1 26 27 package org.objectweb.openccm.pss.generator.jdo.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.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 ; 40 import java.util.List ; 41 import java.util.ArrayList ; 42 43 44 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 63 64 public PSDL_JavaTranslator translator_; 65 66 67 public org.objectweb.openccm.generator.common.api.GeneratorBase generator_; 68 69 75 78 79 public JdoXmlGenerator(PSDL2JavaGenerator gen) 80 { 81 translator_ = new org.objectweb.openccm.generator.translator.idl2java.lib.PSDL_JavaTranslator(); 83 generator_ = gen; 84 disableLog(); 86 } 87 88 94 102 private void 103 create_file( String base_dir_str, 104 String package_name, 105 String id ) 106 { 107 String filename = null; 109 File base_dir = null, 110 target_dir = null; 111 112 base_dir = new File (base_dir_str); 113 target_dir = new File ( 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 ( target_dir.getAbsolutePath() + 125 File.separatorChar + 126 package_name + ".jdo" ); 127 System.out.println( "File '" + filename + "' has been generated"); 128 129 open(filename, id); 131 } 132 133 142 private java.util.Map 143 get_user_classes( java.util.Properties props, 144 String package_name) 145 { 146 java.util.Enumeration keys = null; 147 java.util.Map user_classes = null; 148 149 user_classes = new java.util.HashMap (); 150 151 if (props != null) 152 { 153 String key = null; 154 155 keys = props.keys(); 156 157 while (keys.hasMoreElements()) 158 { 159 key = (String ) keys.nextElement(); 160 161 if ( key.startsWith(package_name) ) 162 { 163 String 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 178 183 public void 184 initialize( String app_name ) 185 { 186 List list = new java.util.ArrayList (); 187 188 189 190 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 super.init(); 198 199 200 put("which", app_name); 201 } 202 203 208 public void 209 mapStorageType( StorageTypeDecl st ) 210 { 211 java.util.List members = null; 212 StorageTypeMapping st_mapping = null; 213 StorageTypeStateMemberDecl[] sm_array = null; 214 String 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 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 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 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 put("field_name", "sh_rid"); 255 map("JDO_FIELD"); 258 259 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 277 public void 278 to_jdo_xml(String base_dir, 279 List storage_types, 280 String user_properties_file) 281 throws GenerationException 282 { 283 java.io.File out_dir = null; 284 StorageTypeDecl[] st_array = null; 285 java.util.Map packages = null; 286 java.util.List childs = null; 287 String parent_id = null; 288 289 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 st_array = (StorageTypeDecl[])storage_types.toArray(new StorageTypeDecl[0]); 299 packages = new java.util.HashMap (); 300 for (int i=0; i<st_array.length; i++) 301 { 302 parent_id = translator_.getPackage(st_array[i]); 303 childs = (ArrayList ) packages.get(parent_id); 304 305 if (childs == null) 306 { 307 childs = new java.util.ArrayList (); 308 } 309 childs.add(st_array[i]); 310 packages.put(parent_id, childs); 311 } 312 313 java.util.Properties user_props = null; 315 316 try { 317 java.io.InputStream propStream = null; 318 319 propStream = new java.io.FileInputStream (user_properties_file); 320 user_props = new java.util.Properties (); 321 user_props.load(propStream); 322 }catch (Exception e) { 323 user_props = null; 325 } 326 327 java.util.Enumeration keys = null; 329 330 if (user_props != null) 331 { 332 String package_name = null; 333 334 keys = user_props.keys(); 335 336 while (keys.hasMoreElements()) 337 { 338 package_name = (String ) 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 java.util.Iterator it = null; 348 String key = null; 349 java.util.Map user_classes = null; 350 351 it = packages.keySet().iterator(); 352 while (it.hasNext()) 353 { 354 key = (String ) it.next(); 355 childs = (ArrayList ) 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 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 |