KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > command > lib > IR3toIDL2


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): Christophe Demarey, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.command.lib;
28
29 // Package dependencies.
30
// import org.objectweb.openccm.command.api.IR3toIDL2;
31
import org.objectweb.util.cmdline.lib.DefaultCommandLine;
32 import java.util.ArrayList JavaDoc;
33
34 /**
35  * Implementation of the ir3_idl2 command.
36  *
37  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
38  * <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
39  *
40  * @version 0.2
41  */

42
43 public class IR3toIDL2
44      extends GeneratorBase
45   implements org.objectweb.openccm.command.api.IR3toIDL2
46 {
47     // ==================================================================
48
//
49
// Internal state.
50
//
51
// ==================================================================
52

53     /** Include Options. */
54     private OptionMultipleArguments include_;
55     private OptionMultipleArguments userInclude_;
56
57     // ==================================================================
58
//
59
// Constructor.
60
//
61
// ==================================================================
62

63     /** The default constructor. */
64     public
65     IR3toIDL2()
66     {
67         // Calls the GeneratorBase constructor.
68
super(new DefaultCommandLine("ir3_idl2",
69                                      "declaration",
70                                      "Generate OMG IDL2 for a declaration from the OpenCCM Interface Repository",
71                                      true),
72               "generated.idl");
73  
74         // Inits internal state.
75
// Adds the include option.
76
include_ = new OptionMultipleArguments(
77                            "-i",
78                            new String JavaDoc[] { "-i" },
79                           "file",
80                           new String JavaDoc[] { "Add a #include <file> statement" },
81                           "",
82                           true );
83         getCommandLine().addOption(include_);
84
85         // Adds the user include option.
86
userInclude_ = new OptionMultipleArguments(
87                            "-ipath",
88                            new String JavaDoc[] { "-ipath" },
89                            "file",
90                            new String JavaDoc[] {
91                                "Add a #include \"file\" statement where",
92                                "file must be the complete file path"
93                            },
94                            "",
95                            true);
96         getCommandLine().addOption(userInclude_);
97     }
98
99     // ==================================================================
100
//
101
// Internal methods.
102
//
103
// ==================================================================
104

105     // ==================================================================
106
//
107
// Public methods for org.objectweb.util.cmdline.api.Application
108
//
109
// ==================================================================
110

111     // ==================================================================
112
//
113
// Public methods for org.objectweb.openccm.command.api.Application
114
//
115
// ==================================================================
116

117     /**
118      * Runs the application.
119      *
120      * @param args The command line arguments.
121      *
122      * @return The status.
123      */

124     public int
125     run(java.lang.String JavaDoc[] args)
126     {
127         // Inits the OpenCCM Abstract Syntax Tree.
128
if(!initAST())
129             return -1;
130
131         // Computes the _local file name.
132
String JavaDoc localfilename = getOutputFileName();
133         int index = localfilename.lastIndexOf('.');
134         if ((index == -1) || (index == localfilename.length()))
135             localfilename += "_local";
136         else
137             localfilename = localfilename.substring(0, index) + "_local"
138                           + localfilename.substring(index);
139
140         // Generates OMG IDL2.
141
if (!generate_idl2(args[0],
142                            getOutputFileName(),
143                            localfilename,
144                            include_.getOptionArguments(),
145                            userInclude_.getOptionArguments()))
146             return -1;
147
148         // All is OK.
149
return 0;
150     }
151
152     // ==================================================================
153
//
154
// Public methods for org.objectweb.openccm.command.api.CommandOnIR3
155
//
156
// ==================================================================
157

158     // ==================================================================
159
//
160
// Public methods for org.objectweb.openccm.command.api.CommandOnAST
161
//
162
// ==================================================================
163

164     // ==================================================================
165
//
166
// Public methods for org.objectweb.openccm.command.api.Generator
167
//
168
// ==================================================================
169

170     // ==================================================================
171
//
172
// Public methods for org.objectweb.openccm.command.api.IR3toIDL2
173
//
174
// ==================================================================
175

176     /**
177      * Generates OMG IDL2 for a scope.
178      *
179      * @param decl_name The scope to be generated.
180      * @param outputfile The file name to write in.
181      * @param outputfile_local The file name to write local declarations in.
182      * @param includes Files to include with <...>.
183      * @param userIncludes User files to include "...".
184      *
185      * @return True if ok, else false.
186      */

187     public boolean
188     generate_idl2(String JavaDoc decl_name,
189                   String JavaDoc outputfile,
190                   String JavaDoc outputfile_local,
191                   String JavaDoc[] includes,
192                   String JavaDoc[] userIncludes)
193     {
194         // Switch the OpenCCM Interface Repository in mode OMG IDL 2.x.
195
getComponentRepository().as_IDL2_repository();
196
197         // Retrieves the scope to generate.
198
org.objectweb.openccm.ast.api.Scope scope = null;
199         try
200         {
201             scope = (org.objectweb.openccm.ast.api.Scope)getAST().lookup(decl_name);
202         }
203         catch(ClassCastException JavaDoc e)
204         {
205             getConsole().error(decl_name + "is not a scope!");
206             return false;
207         }
208
209         if(scope == null)
210         {
211             getConsole().error("Declaration " + decl_name + " not found!");
212             return false;
213         }
214
215         getConsole().message("Generating OMG IDL2 for " + decl_name + "...");
216
217         // Adds include statements.
218
ArrayList JavaDoc includes_list = new ArrayList JavaDoc( java.util.Arrays.asList(includes) );
219         includes_list.add("Components.idl");
220
221         // Launchs the generator.
222
org.objectweb.openccm.generator.idl.api.IDL2Generator gen =
223                          new org.objectweb.openccm.generator.idl.lib.IDL2Generator(getAST());
224         gen.initialize( outputfile,
225                         outputfile_local,
226                         includes_list,
227                         new ArrayList JavaDoc(java.util.Arrays.asList(userIncludes)),
228                         scope,
229                         "IR3toIDL2" );
230         // Generates.
231
try
232         {
233             gen.generate(scope);
234         }
235         catch(org.objectweb.openccm.generator.common.lib.GenerationException ex)
236         {
237             report_exception(ex);
238             return false;
239         }
240
241         // Gets an idl3 repository
242
getComponentRepository().as_IDL3_repository();
243
244         if (outputfile.equals(outputfile_local))
245             getConsole().message("OMG IDL2 generated in " + outputfile + ".");
246         else
247             getConsole().message("OMG IDL2 generated in " + outputfile + " and " + outputfile_local + ".");
248         return true;
249     }
250
251     // ==================================================================
252
//
253
// Static public methods.
254
//
255
// ==================================================================
256

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

262     public static void
263     main(String JavaDoc[] args)
264     {
265         IR3toIDL2 ir3_idl2 = new IR3toIDL2();
266         ir3_idl2.runMain(args);
267     }
268 }
269
Popular Tags