KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > corba > xmiio > exporter > GenerationXMIExportApplication


1 /**
2  * copyright 2002 2003 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.corba.xmiio.exporter;
21
22 import org.objectweb.modfact.corba.logging.Level;
23 import org.objectweb.modfact.corba.logging.ModFactLogger;
24 import org.objectweb.modfact.corba.xmiio.common.AbstractXMIIOGenerator;
25 import org.omg.mof.Reflective.MofError;
26
27 /**
28  * The XMI Export Application generator.
29  * This class allows the generation of XMI exporter launcher.
30  */

31 public class GenerationXMIExportApplication extends AbstractXMIIOGenerator {
32
33     /** The logger. */
34     protected ModFactLogger _logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiio.exporter.GenerationXMIExportApplication");
35
36     /**
37      * XMIExport generation.
38      * @param package_ The root package.
39      * @param class_name_ The class name.
40      * @param class_name_application The application class name.
41      */

42     public void generateXMIExportApplication(org.omg.mof.Model.Package package_, String JavaDoc class_name_, String JavaDoc class_name_application_)
43         throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind JavaDoc, org.omg.CORBA.TypeCodePackage.Bounds JavaDoc {
44         _logger.log(Level.FINE, "XMIExport beginning of the generation");
45         _logger.log(Level.INFO, class_name_application_ + " generation ...");
46         String JavaDoc package_name = package_.name();
47         head(package_, class_name_application_);
48         constructors(package_name, class_name_application_);
49         startMethod(package_, class_name_);
50         mainMethod(class_name_application_);
51         outputln("} // end class " + class_name_);
52         flushFile();
53     }
54
55     /**
56      * XMIExport generation for the head.
57      * @param package_ The package.
58      * @param class_name_application_ The application class name.
59      */

60     public void head(org.omg.mof.Model.Package package_, String JavaDoc class_name_application_)
61         throws MofError {
62         outputln("package " + javaHelper.javaPackage(package_) + ".xmi;");
63         outputln();
64         outputln("public class " + class_name_application_);
65         outputln(" extends org.objectweb.modfact.corba.xmiio.exporter.XMIExportApplication {");
66         outputln();
67     }
68
69     /**
70      * XMIExport 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_ + "\");");
77         outputln("}");
78         outputln();
79     }
80
81     /**
82      * XMIExport generation for the main method.
83      * @param class_name_application_ The application class name.
84      */

85     public void mainMethod(String JavaDoc class_name_application_) throws org.omg.mof.Reflective.MofError {
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      * XMIExport generation for the start method.
98      * @param package_ The package.
99      * @param class_name_ The class name.
100      */

101     public void startMethod(org.omg.mof.Model.Package package_, String JavaDoc class_name_)
102         throws MofError {
103         String JavaDoc package_name = package_.name();
104         String JavaDoc package_qualified_name = javaHelper.javaPackage(package_);
105         outputln("/**");
106         outputln(" * Starts the main function");
107         outputln(" */");
108         outputln("public int start(java.lang.String[] args) {");
109         outputln("try {");
110         outputln("// Get the root naming context");
111         outputln("org.omg.CORBA.Object objRef = orb_.resolve_initial_references(\"NameService\");");
112         outputln("// Use NamingContextExt instead of NamingContext. This is part of the Interoperable naming Service.");
113         outputln("org.omg.CosNaming.NamingContextExt ncRef = org.omg.CosNaming.NamingContextExtHelper.narrow(objRef);");
114         outputln();
115         outputln("// Resolve the Object Reference in Naming");
116         outputln("java.lang.String name = \"" + idlHelper.format1(package_name) + "FactoryServer\";");
117         outputln(package_qualified_name + "." + idlHelper.format1(package_name) + "PackageFactory _reference = "
118                 + package_qualified_name + "." + idlHelper.format1(package_name)
119                 + "PackageFactoryHelper.narrow(ncRef.resolve_str(name));");
120         outputln();
121         outputln("// Creation of Model ");
122         String JavaDoc model_package_type;
123         if (mappingValid) {
124             model_package_type = javaHelper.javaPackage(package_) + "._" + idlHelper.format1(package_name) + "Package";
125         } else {
126             model_package_type = javaHelper.javaPackage(package_) + "." + idlHelper.format1(package_name) + "Package";
127         }
128         outputln(model_package_type + " _model_package = " + "_reference.create_" + idlHelper.format2(package_name) + "_package();");
129         outputln();
130         outputln("org.objectweb.modfact.corba.logging.ModFactLogger _logger;");
131         outputln(TABULATION
132                 + "_logger = org.objectweb.modfact.corba.logging.ModFactLogger.getLogger(\""
133                 + package_name + "XMIExport\");");
134         outputln(class_name_ + " xmiExport = new " + class_name_ + "(dtdFile_, \"\", _logger);");
135         outputln("xmiExport.setORB(orb_);");
136         outputln("java.io.FileOutputStream fileOutput = new java.io.FileOutputStream(new java.io.File(outputFile_));");
137         outputln("xmiExport.setOutput(fileOutput);");
138         outputln("if (iorFile_.length() > 0)");
139         outputln(" xmiExport.parseIOR(iorFile_, printIOR_);");
140         outputln("else if (nameService_.length() > 0)");
141         outputln(" xmiExport.parseName(nameService_, printIOR_);");
142         output("} ");
143         outputCatchException("org.omg.CORBA.ORBPackage.InvalidName");
144         outputCatchException("org.omg.CosNaming.NamingContextPackage.InvalidName");
145         outputCatchException("org.omg.CosNaming.NamingContextPackage.NotFound");
146         outputCatchException("org.omg.CosNaming.NamingContextPackage.CannotProceed");
147         outputCatchException("org.omg.mof.Reflective.MofError");
148         outputCatchException("java.io.FileNotFoundException");
149         outputCatchException("java.io.IOException");
150         outputln();
151         outputln("return 0;");
152         outputln("}");
153         outputln();
154     }
155
156     /**
157      * XMIExport generation for a 'catch'.
158      * @param exception_ The exception to catch.
159      */

160     public void outputCatchException(String JavaDoc exception_) {
161         String JavaDoc exception_name;
162         if (exception_.indexOf(".") != -1)
163             exception_name = exception_.substring(exception_.lastIndexOf(".") + 1);
164         else
165             exception_name = exception_;
166         outputln("catch (" + exception_ + " " + idlHelper.format1FirstMin(exception_name) + ") {");
167         outputln(
168             "System.err.println(\""
169                 + idlHelper.format1(exception_name)
170                 + " : \" + "
171                 + idlHelper.format1FirstMin(exception_name)
172                 + ".getMessage());");
173         output("} ");
174     }
175
176 }
177
Popular Tags