KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > uml > transformation > modfact > JmiModFactModelCreateUtils


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: JmiModFactModelCreateUtils.java,v 1.1 2004/05/26 11:25:35 carpentier Exp $
27 ====================================================================*/

28
29 package org.objectweb.openccm.uml.transformation.modfact;
30
31 import ispuml.mdaTransformation.model.jmi.JmiModelCreateUtils;
32
33 import java.io.File JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.FileOutputStream JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.util.Collection JavaDoc;
38
39 import javax.jmi.reflect.RefPackage;
40
41
42 /**
43  * Class containing utilities for a (ModFact) JMI Repository.
44  * @author Cedric Dumoulin, Pierre Carpentier
45  */

46
47 public class JmiModFactModelCreateUtils extends JmiModelCreateUtils {
48
49     /**
50      * Create a new extent with the specified implementation.
51      * @param modelImplClassName The nsuml outermost package implementation.
52      * @return The new RefPackage.
53      */

54     public static RefPackage createModel(String JavaDoc metaModelURL, String JavaDoc metamodelJmiClasses) {
55         try {
56             // Taylored
57
Class JavaDoc metaModelClass = Class.forName(metamodelJmiClasses);
58             Class JavaDoc parameterType [] = { String JavaDoc.class };
59             java.lang.reflect.Method JavaDoc createMethod = metaModelClass.getMethod("create", parameterType);
60             Object JavaDoc parameter [] = { metaModelURL };
61             return (RefPackage)createMethod.invoke(null, parameter);
62             // Reflexive
63
//RefObject metaObject = RefPackageImpl.createMetaObject(metaModelName, metaModelURL);
64
//return RefPackageImpl.getM1Repository(metaObject);
65
} catch (Exception JavaDoc ex) {
66             ex.printStackTrace();
67         }
68         return null;
69     }
70
71     /**
72      * Get the underlying implementation of the extent.
73      * @param extent The RefPackage
74      * @return The outermost package.
75      */

76     public static RefPackage getModelImpl(RefPackage extent) {
77         return extent.refOutermostPackage();
78     }
79
80     /**
81      * Read model from XMI.
82      * @param file The XMI file which contains the model.
83      * @param extent The outermost package.
84      * @param importer The class which is able to import the XMI file.
85      */

86     public static void readModel(File JavaDoc file, RefPackage extent, String JavaDoc importer) {
87         try {
88             readModel(new FileInputStream JavaDoc(file), extent, importer);
89         } catch (java.lang.Exception JavaDoc ex) {
90             ex.printStackTrace();
91         }
92     }
93
94     /**
95      * Read model from XMI.
96      * @param input The InputStream of the XMI file.
97      * @param extent The outermost package.
98      * @param importer The class which is able to import the XMI file.
99      */

100     public static void readModel(InputStream JavaDoc input, RefPackage extent, String JavaDoc importer) {
101         try {
102             // Taylored
103
Class JavaDoc importerClass = Class.forName(importer);
104             Object JavaDoc importerInstance = importerClass.newInstance();
105             java.lang.reflect.Method JavaDoc [] methods = importerClass.getMethods();
106             java.lang.reflect.Method JavaDoc initiateMethod = null;
107             for (int i=0 ; i<methods.length ; i++) {
108                 if (methods[i].getName().equals("initiate")) {
109                     initiateMethod = methods[i];
110                 }
111             }
112             if (initiateMethod == null)
113                 throw new Exception JavaDoc("Method initiate not found in " + importer + " class.");
114
115             Object JavaDoc initParam [] = { extent };
116             initiateMethod.invoke(importerInstance, initParam);
117             Class JavaDoc parseParamType [] = { InputStream JavaDoc.class };
118             java.lang.reflect.Method JavaDoc parseMethod = importerClass.getMethod("parse", parseParamType);
119             Object JavaDoc parseParam [] = { input };
120             parseMethod.invoke(importerInstance, parseParam);
121
122             // Taylored - Only UML
123
//org.omg.uml.xmi.UmlXMIImport xmiImport = new org.omg.uml.xmi.UmlXMIImport();
124
//xmiImport.initiate((org.omg.uml.UmlPackage)extent);
125
//org.omg.uml.UmlPackage _package = (org.omg.uml.UmlPackage) xmiImport.parse(input);
126

127             // Reflexive
128
//XmiReaderImpl reader = new XmiReaderImpl();
129
//System.out.println("readModel(input, " + extent + ")");
130
//reader.read(input, "", extent);
131

132             //System.out.println("readModel(input, " + getModelImpl(extent) + ")");
133
//reader.read(input, "", getModelImpl(extent));
134
//MDFOutermostPackageImpl extentImpl = getModelImpl(extent);
135
//extentImpl.getXMIReader( xmiVersion ).read( extentImpl, input );
136
} catch (java.lang.Exception JavaDoc ex) {
137             ex.printStackTrace();
138         }
139     }
140
141     /**
142      * Write model to XMI.
143      * @param file The XMI file which will contain the model.
144      * @param extent The outermost package.
145      * @param exporter The class which is able to export the XMI file.
146      */

147     public static void writeModel(File JavaDoc file, RefPackage extent, String JavaDoc exporter) {
148         try {
149             // Taylored - Only UML
150
//org.omg.uml.xmi.UmlXMIExport xmiExport = new org.omg.uml.xmi.UmlXMIExport();
151
//xmiExport.setOutput(new FileOutputStream(new File("output.xmi")));
152
//xmiExport.parse((org.omg.uml.UmlPackage)extent);
153
//org.omg.uml.UmlPackage _package = (org.omg.uml.UmlPackage) xmiImport.parse(input);
154
org.objectweb.modfact.jmi.logging.ModFactLogger _logger;
155             _logger = org.objectweb.modfact.jmi.logging.ModFactLogger.getLogger("ModelXMIExport");
156
157             Class JavaDoc exporterClass = Class.forName(exporter);
158             Class JavaDoc [] constructorParamType = { String JavaDoc.class, org.objectweb.modfact.jmi.logging.ModFactLogger.class };
159             java.lang.reflect.Constructor JavaDoc constructor = exporterClass.getConstructor(constructorParamType);
160             Object JavaDoc [] constructorParam = { new String JavaDoc(), _logger };
161             Object JavaDoc exporterInstance = constructor.newInstance(constructorParam);
162             //exporterClass.newInstance();
163

164             FileOutputStream JavaDoc fileOutput = new FileOutputStream JavaDoc(file);
165             Class JavaDoc outputParamType [] = { java.io.OutputStream JavaDoc.class };
166             Object JavaDoc outputParam [] = { fileOutput };
167             exporterClass.getMethod("setOutput", outputParamType).invoke(exporterInstance, outputParam);
168             Class JavaDoc parseParamType [] = { Object JavaDoc.class };
169             Object JavaDoc parseParam [] = { extent };
170             exporterClass.getMethod("parse", parseParamType).invoke(exporterInstance, parseParam);
171             
172             // Reflexive
173
//XmiWriterImpl writer = new XmiWriterImpl();
174
//FileOutputStream fos = new FileOutputStream(new File(filename));
175
//writer.write(fos, extent, "");
176
} catch (java.lang.Exception JavaDoc ex) {
177             ex.printStackTrace();
178         }
179     }
180
181     /**
182      * Get all roots from model.
183      * @param elementName
184      * @return The list of the roots.
185      */

186     public static Collection JavaDoc getRoots(RefPackage extent) {
187         Collection JavaDoc roots = new java.util.ArrayList JavaDoc();
188         addRoots(roots, extent);
189         return roots;
190     }
191
192 }
Popular Tags