KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > jmi > uml2mof > Main


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.lib.jmi.uml2mof;
20
21 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import javax.jmi.model.ModelPackage;
25 import javax.jmi.model.MofPackage;
26 import javax.jmi.xmi.XmiReader;
27 import javax.jmi.xmi.XmiWriter;
28 import org.netbeans.api.mdr.MDRManager;
29 import org.netbeans.api.mdr.MDRepository;
30 import org.omg.uml.UmlPackage;
31 import org.openide.ErrorManager;
32 import org.openide.util.Lookup;
33
34 /**
35  *
36  * @author Martin Matula
37  */

38 public class Main {
39     // name of a MOF extent that will serve as a target extent for the UML2MOF transformation
40
private static final String JavaDoc MOF_INSTANCE = "MOFInstance";
41     // name of a UML extent (instance of UML metamodel) that the UML models will be loaded into
42
private static final String JavaDoc UML_INSTANCE = "UMLInstance";
43     // name of a MOF extent that will contain definition of UML metamodel
44
private static final String JavaDoc UML_MM = "UML";
45
46     // repository
47
private static MDRepository rep;
48     // UML extent
49
private static UmlPackage uml;
50     // MOF extent
51
private static ModelPackage mof;
52     // XMI reader
53
private static XmiReader reader;
54     
55     public static void main(String JavaDoc args[]) {
56         try {
57             // get the default repository from the MDR manager
58
rep = MDRManager.getDefault().getDefaultRepository();
59             // get's the URL of the file passed as the first commandline parameter
60
// (file containing the UML model to be transformed to MOF)
61
String JavaDoc uri = new File JavaDoc(args[0]).toURL().toString();
62             // opens an output stream for the file name passed as the second commandline parameter
63
// (name of file to be used to save the resulting MOF metamodel)
64
FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(args[1]);
65             // look up an implementation of XmiReader interface
66
reader = (XmiReader) Lookup.getDefault().lookup(XmiReader.class);
67             // look up an implementation of XmiWriter interface
68
XmiWriter writer = (XmiWriter) Lookup.getDefault().lookup(XmiWriter.class);
69             
70             // initialize the repository (make sure the UML metamodel is loaded and both
71
// UML and MOF metamodels are instantiated)
72
init();
73             
74             // start a write transaction
75
rep.beginTrans(true);
76             try {
77                 // read the UML model into the UML extent
78
reader.read(uri, uml);
79                 // transform the UML model in UML extent into a MOF metamodel (which will reside in MOF extent)
80
Transformer.execute(uml, mof);
81                 // write the content of the MOF extent to the XMI (i.e. write the resulting MOF metamodel)
82
writer.write(out, mof, null);
83             } finally {
84                 // rollback the write transaction
85
// (this is to make sure the transformed models are not kept in the storage - they
86
// will probably not be needed anymore - another alternative to this would be to
87
// remove both UML model and the resulting MOF metamodel and do commit, but in this
88
// case doing rollback is simpler)
89
rep.endTrans(true);
90                 // shutdown the repository to make sure all caches are flushed to disk
91
MDRManager.getDefault().shutdownAll();
92                 out.close();
93             }
94         } catch (Exception JavaDoc e) {
95             ErrorManager.getDefault().notify(ErrorManager.ERROR, e);
96         }
97     }
98     
99     /** Makes sure UML and MOF extents are created. */
100     private static void init() throws Exception JavaDoc {
101         // try to retrieve MOF and UML extents
102
mof = (ModelPackage) rep.getExtent(MOF_INSTANCE);
103         uml = (UmlPackage) rep.getExtent(UML_INSTANCE);
104         // check whether both extents exist (they do not exist if this is the first time
105
// the UML2MOF tool is run or the storage files created by previous runs
106
// were deleted)
107
if (mof == null) {
108             // MOF extent does not exist -> create it
109
mof = (ModelPackage) rep.createExtent(MOF_INSTANCE);
110         }
111         if (uml == null) {
112             // UML extent does not exist -> create it (note that in case one want's to instantiate
113
// a metamodel other than MOF, they need to provide the second parameter of the createExtent
114
// method which indicates the metamodel package that should be instantiated)
115
uml = (UmlPackage) rep.createExtent(UML_INSTANCE, getUmlPackage());
116         }
117     }
118     
119     /** Finds "UML" package -> this is the topmost package of UML metamodel - that's the
120      * package that needs to be instantiated in order to create a UML extent
121      */

122     private static MofPackage getUmlPackage() throws Exception JavaDoc {
123         // get the MOF extent containing definition of UML metamodel
124
ModelPackage umlMM = (ModelPackage) rep.getExtent(UML_MM);
125         if (umlMM == null) {
126             // it is not present -> create it
127
umlMM = (ModelPackage) rep.createExtent(UML_MM);
128         }
129         // find package named "UML" in this extent
130
MofPackage result = getUmlPackage(umlMM);
131         if (result == null) {
132             // it cannot be found -> UML metamodel is not loaded -> load it from XMI
133
reader.read(UmlPackage.class.getResource("resources/01-02-15_Diff.xml").toString(), umlMM);
134             // try to find the "UML" package again
135
result = getUmlPackage(umlMM);
136         }
137         return result;
138     }
139     
140     /** Finds "UML" package in a given extent
141      * @param umlMM MOF extent that should be searched for "UML" package.
142      */

143     private static MofPackage getUmlPackage(ModelPackage umlMM) {
144         // iterate through all instances of package
145
for (Iterator JavaDoc it = umlMM.getMofPackage().refAllOfClass().iterator(); it.hasNext();) {
146             MofPackage pkg = (MofPackage) it.next();
147             // is the package topmost and is it named "UML"?
148
if (pkg.getContainer() == null && "UML".equals(pkg.getName())) {
149                 // yes -> return it
150
return pkg;
151             }
152         }
153         // a topmost package named "UML" could not be found
154
return null;
155     }
156 }
157
Popular Tags