KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > uml > lib > UML2CCM


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): Pierre Carpentier.
23 Contributor(s): Philippe Merle.
24
25 ---------------------------------------------------------------------
26 $Id: UML2CCM.java,v 1.1 2004/05/26 11:25:34 carpentier Exp $
27 ====================================================================*/

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 JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Map JavaDoc;
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 /**
52  * Implementation of the uml2ccm command.
53  *
54  * @author <a HREF="mailto:Pierre.Carpentier@lifl.fr">Pierre Carpentier</a>
55  *
56  * @version 1.0
57  */

58
59 public class UML2CCM
60   extends org.objectweb.openccm.command.lib.CompilerBase
61     implements org.objectweb.openccm.uml.api.UML2CCM
62 {
63
64     // ==================================================================
65
//
66
// Internal state.
67
//
68
// ==================================================================
69

70     /** The UML XMI file importer. */
71     private org.omg.uml.xmi.UmlXMIImport xmiImport;
72     
73     /** Flag to indicate the kind of the Java implementation (monolithic or CIF). */
74     private boolean monolithic = false;
75     
76     /** The output directory for the generated files. */
77     private String JavaDoc outputDirectory = "generated";
78
79     // ==================================================================
80
//
81
// Constructor.
82
//
83
// ==================================================================
84

85     /** The default constructor. */
86     public
87     UML2CCM()
88     {
89         // Calls the CompilerBase constructor.
90
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     // ==================================================================
100
//
101
// Internal methods.
102
//
103
// ==================================================================
104

105     // ==================================================================
106
//
107
// Public methods for org.objectweb.util.cmdline.api.Application
108
//
109
// ==================================================================
110

111     // ==================================================================
112
//
113
// Public methods for org.objectweb.openccm.command.api.Application
114
//
115
// ==================================================================
116

117     /**
118      * Runs the application.
119      *
120      * @param args The command line arguments.
121      *
122      * @return The status.
123      */

124     public int
125     run(java.lang.String JavaDoc[] args)
126     {
127         // Inits the OpenCCM's Abstract Syntax Tree,
128
// the Java IDL Script Interpreter and the UML Repository.
129
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 JavaDoc 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         // All is OK.
145
return 0;
146     }
147
148     // ==================================================================
149
//
150
// Public methods for org.objectweb.openccm.command.api.CommandOnIR3
151
//
152
// ==================================================================
153

154     // ==================================================================
155
//
156
// Public methods for org.objectweb.openccm.command.api.CommandOnAST
157
//
158
// ==================================================================
159

160     // ==================================================================
161
//
162
// Public methods for org.objectweb.openccm.command.api.Compiler
163
//
164
// ==================================================================
165

166     // ==================================================================
167
//
168
// Public methods for org.objectweb.openccm.api.UML2CCM
169
//
170
// ==================================================================
171

172     /**
173      * Inits the UML repository.
174      *
175      * @return TRUE if the UML repository is initialized.
176      */

177     public boolean
178     initUMLRepository()
179     {
180         try {
181             org.omg.uml.UmlPackage pck = null;
182             String JavaDoc 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 JavaDoc 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     /**
195      * Parses the UML XMI file for reading the UML Package.
196      *
197      * @param filename The filename of the UML XMI file.
198      *
199      * @throws org.xml.sax.SAXException If any parse errors occur.
200      * @throws javax.xml.parsers.ParserConfigurationException If a DocumentBuilder cannot be
201      * created which satisfies the configuration requested.
202      * @throws java.io.IOException If any IO errors occur.
203      */

204     public void
205     runTransformation(String JavaDoc filename)
206     throws
207         org.xml.sax.SAXException JavaDoc,
208         javax.xml.parsers.ParserConfigurationException JavaDoc,
209         java.io.IOException JavaDoc
210     {
211         // Create ModelData
212
ModelData srcModel = new ModelData(new File JavaDoc(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         // Get URL to transformation XML files
216
String JavaDoc jarFile = "jar:" + System.getProperty("uml.jar.file") + "!/";
217         URL JavaDoc [] ruleFilename = new URL JavaDoc[2];
218         if (this.monolithic)
219             ruleFilename[0] = new URL JavaDoc(jarFile + "xml/transformation/uml2javaImplMonolithic.xml");
220         else
221             ruleFilename[0] = new URL JavaDoc(jarFile + "xml/transformation/uml2javaImplCIF.xml");
222         ruleFilename[1] = new URL JavaDoc(jarFile + "xml/transformation/javaModel2JavaFiles.xml");
223
224         // Call the UML to Java implementation transformation
225
umlToJavaImplTransformation(srcModel, dstModel, ruleFilename);
226         
227         // Call the UML to OMG IDL 3.0 and OMG CIDL transformation
228
ruleFilename = new URL JavaDoc[1];
229         java.net.URL JavaDoc url = new java.net.URL JavaDoc(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     /**
236      * Starts the transformation from the UML representation
237      * to the Java implementation files (CIF or monolithic).
238      *
239      * @param srcModel The information about the source model {metamodelClass, srcFilename, modelUri}
240      * @param dstModel The information about the destination model (metamodelClass, srcFilename, modelUri).
241      * @param ruleFileUrls The URLs of the transformation XML files.
242      */

243     public void
244     umlToJavaImplTransformation(ModelData srcModel, ModelData dstModel, URL JavaDoc[] ruleFileUrls) {
245         getConsole().message("Generating the Java implementation files...");
246         try {
247             // Create Engine
248
MappingRuleEngineWithCollection engine = new MappingRuleEngineWithCollection();
249             engine.getContext().putAttribute("outputdir", this.outputDirectory);
250
251             // Open the input models
252
PropertyValues engineArgs = new PropertyValues();
253             // Create src extent (UML)
254
RefPackage extent = JmiModFactModelCreateUtils.createModel(srcModel.getMetamodelURL(), srcModel.getMetamodelJmiClasses());
255             JmiModFactModelCreateUtils.readModel(srcModel.getModelFile(), extent, srcModel.getModelIOClass());
256             // Add model contexts to engine
257
engine.getContext().addModel(srcModel.getModelUri(), new JmiModfactUML14ModelContext(extent));
258             engine.getContext().setSrc(new JmiModfactUML14ModelContext(extent));
259             // Add model to engine args
260
engineArgs.add(extent);
261
262             // Open the output models
263
Map JavaDoc dstModels = new HashMap JavaDoc();
264             RefPackage dstExtent = null;
265             ModelContext dstContext = null;
266             // Create dst extent (Java)
267
extent = JmiModFactModelCreateUtils.createModel(dstModel.getMetamodelURL(), dstModel.getMetamodelJmiClasses());
268             dstExtent = extent;
269             // Add model contexts to engine
270
ModelContext modelContext = createModelContext(null, extent);
271             dstContext = modelContext;
272             engine.getContext().addModel(dstModel.getModelUri(), modelContext);
273             // keep trace of output models
274
dstModels.put(dstModel.getModelUri(), extent);
275
276             // Create code generator initializer
277
ispuml.mdaTransformation.rules.codeGenerator.CodeGeneratorXmlInitializer initializer;
278             initializer = new ispuml.mdaTransformation.rules.codeGenerator.CodeGeneratorXmlInitializer();
279
280             // Set the rule files
281
for (int i = 0; i < ruleFileUrls.length; i++) {
282                 initializer.addRuleURL(ruleFileUrls[i]);
283             }
284
285             engine.initialize(initializer);
286
287             // Run transformer
288
// Pass the extent if there is only one src extent.
289
// Pass the list of extent otherwise
290
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 JavaDoc ex) {
300             ex.printStackTrace();
301         } catch (java.io.IOException JavaDoc ex) {
302             ex.printStackTrace();
303         } catch (Exception JavaDoc ex) {
304             ex.printStackTrace();
305         }
306     }
307     
308     /**
309      * Starts the transformation from the UML representation
310      * to the OMG IDL 3.0 and OMG CIDL files.
311      *
312      * @param srcModel The information about the source model {metamodelClass, srcFilename, modelUri}
313      * @param dstModel The information about the destination model (metamodelClass, srcFilename, modelUri).
314      * @param ruleFileUrls The URLs of the transformation XML files.
315      */

316     public void
317     umlToAstTransformation(ModelData srcModel, ModelData dstModel, URL JavaDoc[] ruleFileUrls) {
318         getConsole().message("Generating the OMG IDL 3.0 and OMG CIDL files...");
319         try {
320             // Create Engine
321
MappingRuleEngineWithCollection engine = new MappingRuleEngineWithCollection();
322             engine.getContext().putAttribute("outputdir", this.outputDirectory);
323             
324             // Open the input models
325
PropertyValues engineArgs = new PropertyValues();
326             // Create src extent (UML)
327
RefPackage extent = JmiModFactModelCreateUtils.createModel(srcModel.getMetamodelURL(), srcModel.getMetamodelJmiClasses());
328             JmiModFactModelCreateUtils.readModel(srcModel.getModelFile(), extent, srcModel.getModelIOClass());
329             // Add model contexts to engine
330
engine.getContext().addModel(srcModel.getModelUri(), new JmiModfactUML14ModelContext(extent));
331             engine.getContext().setSrc(new JmiModfactUML14ModelContext(extent));
332             // Add model to engine args
333
engineArgs.add(extent);
334
335             // Open the output models
336
Map JavaDoc dstModels = new HashMap JavaDoc();
337             // Create dst extent (Java)
338
Scope scope = CCMASTModelCreateUtils.createModel();
339             // Add model contexts to engine
340
ModelContext modelContext = new CCMASTModelContext(scope);
341             engine.getContext().addModel(dstModel.getModelUri(), modelContext);
342             // keep trace of output models
343
dstModels.put(dstModel.getModelUri(), scope);
344
345             // Create code generator initializer
346
ispuml.mdaTransformation.rules.codeGenerator.CodeGeneratorXmlInitializer initializer;
347             initializer = new ispuml.mdaTransformation.rules.codeGenerator.CodeGeneratorXmlInitializer();
348
349             // Set the rule files
350
for (int i = 0; i < ruleFileUrls.length; i++) {
351                 initializer.addRuleURL(ruleFileUrls[i]);
352             }
353
354             engine.initialize(initializer);
355
356             // Run transformer
357
// Pass the extent if there is only one src extent.
358
// Pass the list of extent otherwise
359
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 JavaDoc ex) {
367             ex.printStackTrace();
368         }
369     }
370
371     /**
372      * Creates the ModelContext associated to the repository.
373      *
374      * @param modelContextClass The name of the ModelContext class.
375      * @param extent The outermost package of the repository.
376      */

377     protected ModelContext createModelContext(String JavaDoc modelContextClass, RefPackage extent) throws Exception JavaDoc {
378         // Use the default implementation if none is specified.
379
if (modelContextClass == null)
380             return new JmiModfactUML14ModelContext(extent);
381
382         try {
383             Class JavaDoc contextClass = Class.forName(modelContextClass);
384             // Try to get the constructor with an extent parameter.
385
// Use the default constructor if not found.
386
ModelContext context;
387             try {
388                 java.lang.reflect.Constructor JavaDoc constructor = contextClass.getConstructor(new Class JavaDoc[] { RefPackage.class });
389                 context = (ModelContext) constructor.newInstance(new Object JavaDoc[] { extent });
390             } catch (Exception JavaDoc ex) {
391                 ex.printStackTrace();
392                 context = (ModelContext) contextClass.newInstance();
393             }
394
395             return context;
396         } catch (Exception JavaDoc ex) {
397             throw ex;
398         }
399     }
400     
401     /**
402      * Sets the monolithic flag to indicate if the Java implementation
403      * will be the monolithic or CIF version.
404      *
405      * @param monolithic TRUE if the Java implementation should be
406      * the monolithic version.
407      */

408     public void
409     setMonolithic(boolean monolithic) {
410         this.monolithic = monolithic;
411     }
412     
413     /**
414      * Sets the output directory for the generated files.
415      *
416      * @param directory The output directory for the generated files.
417      */

418     public void
419     setOutputDirectory(String JavaDoc directory) {
420         this.outputDirectory = directory;
421     }
422
423     // ==================================================================
424
//
425
// Static public methods.
426
//
427
// ==================================================================
428

429     /**
430      * The main bootstrap method.
431      *
432      * @param args The command line arguments.
433      */

434     public static void
435     main(String JavaDoc[] args)
436     {
437         UML2CCM xmi2ir = new UML2CCM();
438         xmi2ir.runMain(args);
439     }
440
441 }
442
Popular Tags