KickJava   Java API By Example, From Geeks To Geeks.

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


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

28
29 package org.objectweb.openccm.uml.lib;
30
31 import org.objectweb.util.cmdline.lib.DefaultCommandLine;
32
33 /**
34  * Implementation of the xmi2ir command.
35  *
36  * @author <a HREF="mailto:Pierre.Carpentier@lifl.fr">Pierre Carpentier</a>
37  *
38  * @version 1.0
39  */

40
41 public class XMI2IR
42   extends org.objectweb.openccm.command.lib.CompilerBase
43     implements org.objectweb.openccm.uml.api.XMI2IR
44 {
45     // ==================================================================
46
//
47
// Java IDL Script Commands.
48
//
49
// ==================================================================
50

51     /** The Java IDL Script command for checking the UML model. */
52     private final String JavaDoc checkCommand = "import CIDLChecker; checker = CIDLChecker.CIDLChecker(); checker.checkModel();";
53
54     /** The Java IDL Script command for creating the UML model into the OpenCCM Interface Repository. */
55     private final String JavaDoc createCommand = "import CIDLFeeder; feeder = CIDLFeeder.CIDLFeeder(); feeder.createModel();";
56
57     // ==================================================================
58
//
59
// Internal state.
60
//
61
// ==================================================================
62

63     /** The Java IDL Script Interpreter. */
64     fr.lifl.cim.jidlscript.JISInterpreter jis;
65
66     /** The UML XMI file importer. */
67     org.omg.uml.xmi.UmlXMIImport xmiImport;
68
69     // ==================================================================
70
//
71
// Constructor.
72
//
73
// ==================================================================
74

75     /** The default constructor. */
76     public
77     XMI2IR()
78     {
79         // Calls the CompilerBase constructor.
80
super(new DefaultCommandLine("xmi2ir",
81                                      "xmi_uml_document",
82                                      "Feed the OpenCCM Interface Repository with an XMI 1.1 UML 1.4 document",
83                                      true),
84               false);
85     }
86
87     // ==================================================================
88
//
89
// Internal methods.
90
//
91
// ==================================================================
92

93     /**
94      * Initialization of the Java IDL Script Interpreter.
95      *
96      * @return TRUE if the Java IDL Script is initialized.
97      */

98     private boolean
99     initJIS()
100     {
101         org.omg.CORBA.ORB JavaDoc orb = org.objectweb.openccm.corba.TheORB.getORB();
102         try {
103             // Creation of the interpreter.
104
jis = new fr.lifl.cim.jidlscript.JISInterpreter(orb);
105             String JavaDoc csDirectory = System.getProperty("transformation.cs.dir");
106             java.net.URL JavaDoc urlDirectory = Thread.currentThread().getContextClassLoader().getResource(csDirectory);
107             fr.lifl.cim.jidlscript.lang.JISModuleLoader.loader.addURL(urlDirectory.toString());
108             return true;
109         } catch (fr.lifl.cim.jidlscript.lang.JISError jisError) {
110             getConsole().error("Error in Java IDL Script Interpreter initialization: " + jisError.getMessage());
111             return false;
112         }
113     }
114
115     // ==================================================================
116
//
117
// Public methods for org.objectweb.util.cmdline.api.Application
118
//
119
// ==================================================================
120

121     // ==================================================================
122
//
123
// Public methods for org.objectweb.openccm.command.api.Application
124
//
125
// ==================================================================
126

127     /**
128      * Runs the application.
129      *
130      * @param args The command line arguments.
131      *
132      * @return The status.
133      */

134     public int
135     run(java.lang.String JavaDoc[] args)
136     {
137         // Inits the OpenCCM's Abstract Syntax Tree,
138
// the Java IDL Script Interpreter and the UML Repository.
139
if(!initAST() || !initJIS() || !initUMLRepository())
140             return -1;
141
142         try {
143             parseXMIFile(args[0]);
144         } catch (java.lang.Exception JavaDoc exception) {
145             getConsole().error("[ERROR] An error occurs during the reading of the UML model (" + exception.getMessage() + ").");
146             return -1;
147         }
148
149         try {
150
151             org.omg.CORBA.ComponentIR.Repository ir = org.objectweb.openccm.corba.TheInterfaceRepository.getRepository();
152
153             // Checks if the model is correct.
154
getConsole().message("Checking if the UML model is correct...");
155             jis.eval(checkCommand);
156             getConsole().message("The UML model is correct.");
157
158             // Creates the corresponding elements into the OpenCCM Interface Repository.
159
getConsole().message("Feeding the UML model into the OpenCCM Interface Repository...");
160             jis.eval(createCommand);
161             getConsole().message("The UML model is fed into the OpenCCM Interface Repository.");
162         } catch (fr.lifl.cim.jidlscript.lang.JISError e) {
163             if (((fr.lifl.cim.jidlscript.lang.JISException)e.exception).getType().Name().equals("Throw")) {
164                 String JavaDoc message = ((fr.lifl.cim.jidlscript.lang.JISThrow)e.exception).eval().toString();
165                 if (message.indexOf("java.lang.NullPointerException") != -1) {
166                     getConsole().error("[ERROR] There is a blank field in the UML model (e.g. name, visibility, multiplicity, etc.).");
167                 } else {
168                     // ConstraintException or NotCreatedException
169
java.util.StringTokenizer JavaDoc token = new java.util.StringTokenizer JavaDoc(message, "\n");
170                     while (token.hasMoreTokens()) {
171                         getConsole().error("[ERROR] " + token.nextToken());
172                     }
173                 }
174             } else {
175                 getConsole().error("[ERROR] An error occurs during the importation of the UML model. Check if your XMI file is correct (" + e.exception + ").");
176             }
177             return -1;
178         }
179
180         // All is OK.
181
return 0;
182     }
183
184     // ==================================================================
185
//
186
// Public methods for org.objectweb.openccm.command.api.CommandOnIR3
187
//
188
// ==================================================================
189

190     // ==================================================================
191
//
192
// Public methods for org.objectweb.openccm.command.api.CommandOnAST
193
//
194
// ==================================================================
195

196     // ==================================================================
197
//
198
// Public methods for org.objectweb.openccm.command.api.Compiler
199
//
200
// ==================================================================
201

202     // ==================================================================
203
//
204
// Public methods for org.objectweb.openccm.api.XMI2IR
205
//
206
// ==================================================================
207

208     /**
209      * Inits the UML repository.
210      *
211      * @return TRUE if the UML repository is initialized.
212      */

213     public boolean
214     initUMLRepository()
215     {
216         try {
217             org.omg.uml.UmlPackage pck = null;
218             String JavaDoc metaModelFile = System.getProperty("uml.metamodel.file");
219             pck = org.objectweb.modfact.jmi.repository.uml.UmlPackageImpl.create(metaModelFile);
220             xmiImport = new org.omg.uml.xmi.UmlXMIImport();
221             xmiImport.initiate(pck);
222             return true;
223         } catch (java.lang.Exception JavaDoc exception) {
224             getConsole().error("[ERROR] An error occurs during the initialization of the UML repository. Check your environment.");
225             exception.printStackTrace();
226             return false;
227         }
228     }
229
230     /**
231      * Parses the UML XMI file for reading the UML Package.
232      *
233      * @param filename The filename of the UML XMI file.
234      *
235      * @throws org.xml.sax.SAXException If any parse errors occur.
236      * @throws javax.xml.parsers.ParserConfigurationException If a DocumentBuilder cannot be
237      * created which satisfies the configuration requested.
238      * @throws java.io.IOException If any IO errors occur.
239      */

240     public void
241     parseXMIFile(String JavaDoc filename)
242     throws
243         org.xml.sax.SAXException JavaDoc,
244         javax.xml.parsers.ParserConfigurationException JavaDoc,
245         java.io.IOException JavaDoc
246     {
247         getConsole().message("Reading from " + filename + "...");
248         Object JavaDoc o = xmiImport.parse(filename);
249         org.omg.uml.UmlPackage uml_package = (org.omg.uml.UmlPackage) o;
250         TheUMLPackage.setUMLPackage(uml_package);
251     }
252
253     // ==================================================================
254
//
255
// Static public methods.
256
//
257
// ==================================================================
258

259     /**
260      * The main bootstrap method.
261      *
262      * @param args The command line arguments.
263      */

264     public static void
265     main(String JavaDoc[] args)
266     {
267         XMI2IR xmi2ir = new XMI2IR();
268         xmi2ir.runMain(args);
269     }
270
271 }
272
Popular Tags