KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > xmiio > importer > GenerationXMIImportApplication


1 /**
2  * copyright 2002 2004 Laboratoire d'Informatique Paris 6 (LIP6)
3  *
4  * This file is part of ModFact.
5  *
6  * ModFact is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * at your option) any later version.
10  *
11  * ModFact is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with ModFact; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package org.objectweb.modfact.jmi.xmiio.importer;
21
22 import javax.jmi.model.MofPackage;
23
24 import org.objectweb.modfact.jmi.helper.JMIProvider;
25 import org.objectweb.modfact.jmi.helper.XMIIOHelper;
26 import org.objectweb.modfact.jmi.xmiio.common.AbstractXMIIOGenerator;
27
28
29 /**
30  * The XMI Import Application generator.
31  * This class allows the generation of XMI importer launcher.
32  */

33 public class GenerationXMIImportApplication extends AbstractXMIIOGenerator {
34
35
36     /** The IOR output file name. */
37     private String JavaDoc _ior_filename = new String JavaDoc("");
38
39     /**
40      * XMIImport generation.
41      * @param package_ The root package.
42      * @param class_name_ The class name of the generated class.
43      * @param class_name_application The application class name.
44      */

45     public void generateXMIImportApplication(MofPackage package_, String JavaDoc class_name_, String JavaDoc class_name_application_) {
46         String JavaDoc package_name_ = JMIProvider.jmiPackageExtentName(package_);
47         head(package_, class_name_application_);
48         constructors(package_name_, class_name_application_);
49         startMethod(package_name_, package_, class_name_);
50         mainMethod(class_name_application_);
51         outputln("} // end class " + class_name_);
52         out.flush();
53     }
54
55     /**
56      * XMIImport generation for the head.
57      * @param package_name_ The package name.
58      * @param class_name_application_ The application class name.
59      */

60     public void head(MofPackage package_, String JavaDoc class_name_application_) {
61         String JavaDoc packageQualifiedName = JMIProvider.qualifierOf(package_);
62         outputln("package " + packageQualifiedName + ".xmi;");
63         outputln();
64         outputln("public class " + class_name_application_);
65         outputln(" extends org.objectweb.modfact.jmi.service.cmdline.xmiio.XMIImportApplication {");
66         outputln();
67     }
68
69     /**
70      * XMIImport generation for the constructors.
71      * @param package_name_ The package name.
72      * @param class_name_application_ The application class name.
73      */

74     public void constructors(String JavaDoc package_name_, String JavaDoc class_name_application_) {
75         outputln("public " + class_name_application_ + "() {");
76         outputln("super(\"" + package_name_ + "\", \"" + class_name_application_ + "\", \"" + JMIProvider.jmiFormat1(package_name_) + "Package.ior\");");
77         outputln("}");
78         outputln();
79     }
80
81     /**
82      * XMIImport generation for the main method.
83      * @param class_name_application_ The application class name.
84      */

85     public void mainMethod(String JavaDoc class_name_application_) {
86         outputln("/**");
87         outputln(" * The main method");
88         outputln(" */");
89         outputln("public static void main (java.lang.String [] args) {");
90         outputln(class_name_application_ + " application = new " + class_name_application_ + "();");
91         outputln("application.runMain(args);");
92         outputln("}");
93         outputln();
94     }
95
96     /**
97      * XMIImport generation for the start method.
98      * @param package_name_ The package name.
99      * @param class_name_ The class name.
100      */

101     public void startMethod(String JavaDoc package_name_, MofPackage package_, String JavaDoc class_name_) {
102         outputln("/**");
103         outputln(" * Starts the main function");
104         outputln(" */");
105         outputln("public int start(java.lang.String[] args) {");
106         outputln("try {");
107         outputln();
108         outputln("// Creation of Model ");
109         
110         String JavaDoc model_package_type;
111         model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package";
112         
113         outputln(model_package_type + " pck = null;");
114         outputln("try {");
115         outputln("java.io.File metaModel = new java.io.File(metaModelFile);");
116         String JavaDoc shortQualif = JMIProvider.shortQualifierOf(package_);
117         outputln("pck = org.objectweb.modfact.jmi.repository." + shortQualif + "." + JMIProvider.jmiFormat1(package_name_) + "PackageImpl.create(metaModel.toURL().toString());");
118         outputln("} catch (Exception e) {");
119         outputln("System.err.println(\"exception : \" + e.getMessage());");
120         outputln("e.printStackTrace();");
121         outputln("}");
122         
123         outputln(class_name_ + " xmiImport = new " + class_name_ + "();");
124         outputln("xmiImport.initiate(pck);");
125         outputln(model_package_type + " _package = " + "(" + model_package_type + ") " + "xmiImport.parse(modelFile_);");
126         outputln("System.out.println(\"Package is in the repository\");");
127         output("} ");
128         outputCatchException("org.xml.sax.SAXException");
129         outputCatchException("javax.xml.parsers.ParserConfigurationException");
130         outputCatchException("java.io.FileNotFoundException");
131         outputCatchException("java.io.IOException");
132         outputln();
133         outputln("return 0;");
134         outputln("}");
135         outputln();
136     }
137
138     /**
139      * XMIImport generation for a 'catch'.
140      * @param exception_ The exception to catch.
141      */

142     public void outputCatchException(String JavaDoc exception_) {
143         String JavaDoc exception_name;
144         if (exception_.indexOf(".") != -1)
145             exception_name = exception_.substring(exception_.lastIndexOf(".") + 1);
146         else
147             exception_name = exception_;
148         outputln("catch (" + exception_ + " " + XMIIOHelper.format1FirstMin(exception_name) + ") {");
149         outputln("System.err.println(\"" + JMIProvider.jmiFormat1(exception_name) + " : \" + " + XMIIOHelper.format1FirstMin(exception_name) + ".getMessage());");
150         output("} ");
151     }
152     
153
154 }
155
Popular Tags