KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > uml > transformation > ast > CCMASTModelCreateUtils


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: CCMASTModelCreateUtils.java,v 1.3 2005/04/01 20:04:08 contrera Exp $
27 ====================================================================*/

28
29 package org.objectweb.openccm.uml.transformation.ast;
30
31 import ispuml.mdaTransformation.model.jmi.JmiModelCreateUtils;
32
33 import java.io.InputStream JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.File JavaDoc;
36 import java.util.Collection JavaDoc;
37
38 import javax.jmi.reflect.RefPackage;
39 import org.objectweb.openccm.ast.api.FileScope;
40 import org.objectweb.openccm.ast.api.Scope;
41
42
43
44 /**
45  * Class containing utilities for the CCM Abstract Syntax Tree (IDL repository).
46  * @author Pierre Carpentier
47  */

48
49 public class CCMASTModelCreateUtils extends JmiModelCreateUtils {
50
51     /** The CCM Abstract Syntax Tree. */
52     private static org.objectweb.openccm.ast.lib.Repository ast;
53
54     /**
55      * Gets the CCM Abstract Syntax Tree.
56      * @return The CCM AST.
57      */

58     public static org.objectweb.openccm.ast.lib.Repository getAst() {
59         return ast;
60     }
61     
62     
63     /**
64      * Create a new model to the OpenCCM AST.
65      * @return The new Scope (root of the model).
66      */

67     public static Scope createModel() {
68         org.omg.CORBA.ComponentIR.Repository ir = org.objectweb.openccm.corba.TheInterfaceRepository.getRepository();
69         ast = new org.objectweb.openccm.ast.lib.Repository(org.objectweb.openccm.ir3.api.ComponentRepositoryHelper.narrow(ir));
70
71         FileScope scope = ast.startFileScope("");
72         scope.create();
73         
74         return scope;
75     }
76
77     /**
78      * Get the underlying implementation of the extent.
79      * @param extent The RefPackage.
80      * @return The outermost package.
81      */

82     public static RefPackage getModelImpl(RefPackage extent) {
83         return extent.refOutermostPackage();
84     }
85
86     /**
87      * Read model from XMI.
88      * @param filename The XMI filename which contains the model.
89      * @param extent The outermost package.
90      * @param importer The class which is able to import the XMI file.
91      */

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

106     public static void readModel(InputStream JavaDoc input, RefPackage extent, String JavaDoc importer) {
107         try {
108             // Taylored
109
Class JavaDoc importerClass = Class.forName(importer);
110             Object JavaDoc importerInstance = importerClass.newInstance();
111             java.lang.reflect.Method JavaDoc [] methods = importerClass.getMethods();
112             java.lang.reflect.Method JavaDoc initiateMethod = null;
113             for (int i=0 ; i<methods.length ; i++) {
114                 if (methods[i].getName().equals("initiate")) {
115                     initiateMethod = methods[i];
116                 }
117             }
118             if (initiateMethod == null)
119                 throw new Exception JavaDoc("Method initiate not found in " + importer + " class.");
120
121             Object JavaDoc initParam [] = { extent };
122             initiateMethod.invoke(importerInstance, initParam);
123             Class JavaDoc parseParamType [] = { InputStream JavaDoc.class };
124             java.lang.reflect.Method JavaDoc parseMethod = importerClass.getMethod("parse", parseParamType);
125             Object JavaDoc parseParam [] = { input };
126             parseMethod.invoke(importerInstance, parseParam);
127         } catch (java.lang.Exception JavaDoc ex) {
128             ex.printStackTrace();
129         }
130     }
131
132     /**
133      * Write model to XMI.
134      * @param filename The XMI filename which will contain the model.
135      * @param extent The outermost package.
136      * @param exporter The class which is able to export the XMI file.
137      */

138     public static void writeModel(String JavaDoc filename, RefPackage extent, String JavaDoc exporter) {
139         throw new UnsupportedOperationException JavaDoc("Write model not implemented");
140     }
141
142     /**
143      * Get all roots from model.
144      * @param elementName
145      * @return The list of the roots.
146      */

147     public static Collection JavaDoc getRoots(RefPackage extent) {
148         Collection JavaDoc roots = new java.util.ArrayList JavaDoc();
149         addRoots(roots, extent);
150         return roots;
151     }
152
153 }
Popular Tags