1 28 29 package org.objectweb.openccm.uml.lib; 30 31 import ispuml.mdaTransformation.TransformationException; 32 import ispuml.mdaTransformation.model.ModelContext; 33 import ispuml.mdaTransformation.rules.PropertyValues; 34 35 import java.io.File ; 36 import java.net.URL ; 37 import java.util.HashMap ; 38 import java.util.Map ; 39 40 import javax.jmi.reflect.RefPackage; 41 42 import org.objectweb.util.cmdline.lib.DefaultCommandLine; 43 import org.objectweb.openccm.ast.api.Scope; 44 import org.objectweb.openccm.uml.transformation.ast.CCMASTModelContext; 45 import org.objectweb.openccm.uml.transformation.ast.CCMASTModelCreateUtils; 46 import org.objectweb.openccm.uml.transformation.engine.MappingRuleEngineWithCollection; 47 import org.objectweb.openccm.uml.transformation.log.ConsoleSingleton; 48 import org.objectweb.openccm.uml.transformation.modfact.JmiModFactModelCreateUtils; 49 import org.objectweb.openccm.uml.transformation.modfact.JmiModfactUML14ModelContext; 50 51 58 59 public class UML2CCM 60 extends org.objectweb.openccm.command.lib.CompilerBase 61 implements org.objectweb.openccm.uml.api.UML2CCM 62 { 63 64 70 71 private org.omg.uml.xmi.UmlXMIImport xmiImport; 72 73 74 private boolean monolithic = false; 75 76 77 private String outputDirectory = "generated"; 78 79 85 86 public 87 UML2CCM() 88 { 89 super(new DefaultCommandLine("uml2ccm", 91 "xmi_uml_document", 92 "Transform the XMI 1.1 UML 1.4 document to the OMG IDL 3.0, OMG CIDL and Java Implementation files", 93 true), 94 false); 95 this.getCommandLine().addOption(new OptionMonolithic(this)); 96 this.getCommandLine().addOption(new OptionOutputDirectory(this)); 97 } 98 99 105 111 117 124 public int 125 run(java.lang.String [] args) 126 { 127 if(!initAST()) 130 return -1; 131 132 ConsoleSingleton.setConsole(this.getConsole()); 133 134 try { 135 runTransformation(args[0]); 136 getConsole().message("The OMG IDL 3.0, OMG CIDL and Java implementation files are generated."); 137 } catch (java.lang.Exception exception) { 138 getConsole().error("[ERROR] An error occurs during the reading of the UML model (" + exception.getMessage() + ")."); 139 return -1; 140 } 141 142 org.omg.CORBA.ComponentIR.Repository ir = org.objectweb.openccm.corba.TheInterfaceRepository.getRepository(); 143 144 return 0; 146 } 147 148 154 160 166 172 177 public boolean 178 initUMLRepository() 179 { 180 try { 181 org.omg.uml.UmlPackage pck = null; 182 String metaModelFile = System.getProperty("uml.metamodel.file"); 183 pck = org.objectweb.modfact.jmi.repository.uml.UmlPackageImpl.create(metaModelFile); 184 xmiImport = new org.omg.uml.xmi.UmlXMIImport(); 185 xmiImport.initiate(pck); 186 return true; 187 } catch (java.lang.Exception exception) { 188 getConsole().error("[ERROR] An error occurs during the initialization of the UML repository. Check your environment."); 189 exception.printStackTrace(); 190 return false; 191 } 192 } 193 194 204 public void 205 runTransformation(String filename) 206 throws 207 org.xml.sax.SAXException , 208 javax.xml.parsers.ParserConfigurationException , 209 java.io.IOException 210 { 211 ModelData srcModel = new ModelData(new File (filename), "uml", System.getProperty("uml.metamodel.file"), "org.objectweb.modfact.jmi.repository.uml.UmlPackageImpl", "org.omg.uml.xmi.UmlXMIImport"); 213 ModelData dstModel = new ModelData(null, "java", System.getProperty("java.metamodel.file"), "org.objectweb.modfact.jmi.repository.javamodel.JavaModelPackageImpl", "javamodel.xmi.JavaModelXMIExport"); 214 215 String jarFile = "jar:" + System.getProperty("uml.jar.file") + "!/"; 217 URL [] ruleFilename = new URL [2]; 218 if (this.monolithic) 219 ruleFilename[0] = new URL (jarFile + "xml/transformation/uml2javaImplMonolithic.xml"); 220 else 221 ruleFilename[0] = new URL (jarFile + "xml/transformation/uml2javaImplCIF.xml"); 222 ruleFilename[1] = new URL (jarFile + "xml/transformation/javaModel2JavaFiles.xml"); 223 224 umlToJavaImplTransformation(srcModel, dstModel, ruleFilename); 226 227 ruleFilename = new URL [1]; 229 java.net.URL url = new java.net.URL (jarFile + "xml/transformation/uml2ast.xml"); 230 ruleFilename[0] = url; 231 dstModel = new ModelData(null, "ccm-ast", null, null, null); 232 umlToAstTransformation(srcModel, dstModel, ruleFilename); 233 } 234 235 243 public void 244 umlToJavaImplTransformation(ModelData srcModel, ModelData dstModel, URL [] ruleFileUrls) { 245 getConsole().message("Generating the Java implementation files..."); 246 try { 247 MappingRuleEngineWithCollection engine = new MappingRuleEngineWithCollection(); 249 engine.getContext().putAttribute("outputdir", this.outputDirectory); 250 251 PropertyValues engineArgs = new PropertyValues(); 253 RefPackage extent = JmiModFactModelCreateUtils.createModel(srcModel.getMetamodelURL(), srcModel.getMetamodelJmiClasses()); 255 JmiModFactModelCreateUtils.readModel(srcModel.getModelFile(), extent, srcModel.getModelIOClass()); 256 engine.getContext().addModel(srcModel.getModelUri(), new JmiModfactUML14ModelContext(extent)); 258 engine.getContext().setSrc(new JmiModfactUML14ModelContext(extent)); 259 engineArgs.add(extent); 261 262 Map dstModels = new HashMap (); 264 RefPackage dstExtent = null; 265 ModelContext dstContext = null; 266 extent = JmiModFactModelCreateUtils.createModel(dstModel.getMetamodelURL(), dstModel.getMetamodelJmiClasses()); 268 dstExtent = extent; 269 ModelContext modelContext = createModelContext(null, extent); 271 dstContext = modelContext; 272 engine.getContext().addModel(dstModel.getModelUri(), modelContext); 273 dstModels.put(dstModel.getModelUri(), extent); 275 276 ispuml.mdaTransformation.rules.codeGenerator.CodeGeneratorXmlInitializer initializer; 278 initializer = new ispuml.mdaTransformation.rules.codeGenerator.CodeGeneratorXmlInitializer(); 279 280 for (int i = 0; i < ruleFileUrls.length; i++) { 282 initializer.addRuleURL(ruleFileUrls[i]); 283 } 284 285 engine.initialize(initializer); 286 287 if (engineArgs.size() == 1) 291 engine.runTransform(engineArgs.get(0)); 292 else 293 engine.runTransform(engineArgs); 294 295 extent = (RefPackage) dstModels.get(dstModel.getModelUri()); 296 engine.runTransform(dstExtent); 297 } catch (TransformationException ex) { 298 ex.printStackTrace(); 299 } catch (java.io.FileNotFoundException ex) { 300 ex.printStackTrace(); 301 } catch (java.io.IOException ex) { 302 ex.printStackTrace(); 303 } catch (Exception ex) { 304 ex.printStackTrace(); 305 } 306 } 307 308 316 public void 317 umlToAstTransformation(ModelData srcModel, ModelData dstModel, URL [] ruleFileUrls) { 318 getConsole().message("Generating the OMG IDL 3.0 and OMG CIDL files..."); 319 try { 320 MappingRuleEngineWithCollection engine = new MappingRuleEngineWithCollection(); 322 engine.getContext().putAttribute("outputdir", this.outputDirectory); 323 324 PropertyValues engineArgs = new PropertyValues(); 326 RefPackage extent = JmiModFactModelCreateUtils.createModel(srcModel.getMetamodelURL(), srcModel.getMetamodelJmiClasses()); 328 JmiModFactModelCreateUtils.readModel(srcModel.getModelFile(), extent, srcModel.getModelIOClass()); 329 engine.getContext().addModel(srcModel.getModelUri(), new JmiModfactUML14ModelContext(extent)); 331 engine.getContext().setSrc(new JmiModfactUML14ModelContext(extent)); 332 engineArgs.add(extent); 334 335 Map dstModels = new HashMap (); 337 Scope scope = CCMASTModelCreateUtils.createModel(); 339 ModelContext modelContext = new CCMASTModelContext(scope); 341 engine.getContext().addModel(dstModel.getModelUri(), modelContext); 342 dstModels.put(dstModel.getModelUri(), scope); 344 345 ispuml.mdaTransformation.rules.codeGenerator.CodeGeneratorXmlInitializer initializer; 347 initializer = new ispuml.mdaTransformation.rules.codeGenerator.CodeGeneratorXmlInitializer(); 348 349 for (int i = 0; i < ruleFileUrls.length; i++) { 351 initializer.addRuleURL(ruleFileUrls[i]); 352 } 353 354 engine.initialize(initializer); 355 356 if (engineArgs.size() == 1) 360 engine.runTransform(engineArgs.get(0)); 361 else 362 engine.runTransform(engineArgs); 363 364 } catch (TransformationException ex) { 365 ex.printStackTrace(); 366 } catch (Exception ex) { 367 ex.printStackTrace(); 368 } 369 } 370 371 377 protected ModelContext createModelContext(String modelContextClass, RefPackage extent) throws Exception { 378 if (modelContextClass == null) 380 return new JmiModfactUML14ModelContext(extent); 381 382 try { 383 Class contextClass = Class.forName(modelContextClass); 384 ModelContext context; 387 try { 388 java.lang.reflect.Constructor constructor = contextClass.getConstructor(new Class [] { RefPackage.class }); 389 context = (ModelContext) constructor.newInstance(new Object [] { extent }); 390 } catch (Exception ex) { 391 ex.printStackTrace(); 392 context = (ModelContext) contextClass.newInstance(); 393 } 394 395 return context; 396 } catch (Exception ex) { 397 throw ex; 398 } 399 } 400 401 408 public void 409 setMonolithic(boolean monolithic) { 410 this.monolithic = monolithic; 411 } 412 413 418 public void 419 setOutputDirectory(String directory) { 420 this.outputDirectory = directory; 421 } 422 423 429 434 public static void 435 main(String [] args) 436 { 437 UML2CCM xmi2ir = new UML2CCM(); 438 xmi2ir.runMain(args); 439 } 440 441 } 442 | Popular Tags |