KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

59     public void head(org.omg.mof.Model.Package package_, String JavaDoc class_name_application_) throws MofError {
60         outputln("package " + javaHelper.javaPackage(package_) + ".xmi;");
61         outputln();
62         outputln("public class " + class_name_application_);
63         outputln(" extends org.objectweb.modfact.corba.xmiio.importer.AbstractXMIImportApplication {");
64         outputln();
65         outputln("/** The CORBA Helper. */");
66         outputln("org.objectweb.modfact.corba.helper.CORBACommon corbaHelper;");
67         outputln();
68     }
69
70     /**
71      * XMIImport generation for the constructors.
72      * @param package_name_ The package name.
73      * @param class_name_application_ The application class name.
74      */

75     public void constructors(String JavaDoc package_name_, String JavaDoc class_name_application_) {
76         outputln("public " + class_name_application_ + "() {");
77         outputln("super(\"" + package_name_ + "\", \"" + class_name_application_ + "\", \"" + idlHelper.format1(package_name_) + "Package.ior\");");
78         outputln("corbaHelper = new org.objectweb.modfact.corba.provider.CORBAProvider();");
79         outputln("}");
80         outputln();
81     }
82
83     /**
84      * XMIImport generation for the main method.
85      * @param class_name_application_ The application class name.
86      */

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

104     public void startMethod(org.omg.mof.Model.Package package_, String JavaDoc package_name_, String JavaDoc class_name_)
105         throws MofError {
106         outputln("/**");
107         outputln(" * Starts the main function");
108         outputln(" */");
109         outputln("public int start(java.lang.String[] args) {");
110         outputln("try {");
111         outputln("// Get the root naming context");
112         outputln("org.omg.CORBA.Object objRef = orb_.resolve_initial_references(\"NameService\");");
113         outputln("// Use NamingContextExt instead of NamingContext. This is part of the Interoperable naming Service.");
114         outputln("org.omg.CosNaming.NamingContextExt ncRef = org.omg.CosNaming.NamingContextExtHelper.narrow(objRef);");
115         outputln();
116         outputln("// Resolve the Object Reference in Naming");
117         outputln("java.lang.String name = \"" + idlHelper.format1(package_name_) + "FactoryServer\";");
118         String JavaDoc package_type = javaHelper.javaPackage(package_) + "." + idlHelper.format1(package_name_);
119         outputln(package_type + "PackageFactory _reference = " + package_type + "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("_logger = org.objectweb.modfact.corba.logging.ModFactLogger.getLogger(\""+ package_name_ + "XMIImport\");");
132         outputln(class_name_ + " xmiImport = new " + class_name_ + "(_logger);");
133         outputln("xmiImport.initiate(_model_package, orb_);");
134         outputln(model_package_type + " _package = " + "(" + model_package_type + ") " + "xmiImport.parse(inputFile_);");
135         // NameService
136
outputln("if (nameService_.length() > 0) {");
137         outputln("org.omg.CosNaming.NameComponent path[] = ncRef.to_name(nameService_);");
138         outputln("ncRef.rebind(path, _package);");
139         outputln("}");
140         // Print IOR
141
outputln("if (printIOR_)");
142         outputln(" corbaHelper.printIOR(orb_, _package, System.out);");
143         // Write IOR to a file
144
outputln("corbaHelper.writeIOR(orb_, _package, iorFile_);");
145         outputln("_logger.log(org.objectweb.modfact.corba.logging.Level.INFO, \"Package is in the repository\");");
146         output("} ");
147         outputCatchException("org.omg.CORBA.ORBPackage.InvalidName");
148         outputCatchException("org.omg.CosNaming.NamingContextPackage.InvalidName");
149         outputCatchException("org.omg.CosNaming.NamingContextPackage.NotFound");
150         outputCatchException("org.omg.CosNaming.NamingContextPackage.CannotProceed");
151         outputCatchException("org.omg.mof.Reflective.MofError");
152         outputCatchException("org.xml.sax.SAXException");
153         outputCatchException("javax.xml.parsers.ParserConfigurationException");
154         outputCatchException("java.io.FileNotFoundException");
155         outputCatchException("java.io.IOException");
156         outputln();
157         outputln("return 0;");
158         outputln("}");
159         outputln();
160     }
161
162     /**
163      * XMIImport generation for a 'catch'.
164      * @param exception_ The exception to catch.
165      */

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