KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > corba > command > lib > CIDLtoCIF


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.corba.command.lib;
28
29 // Package dependencies.
30
import org.objectweb.util.cmdline.lib.DefaultCommandLine;
31 import org.objectweb.util.cmdline.lib.DefaultOptionArgument;
32 import org.objectweb.util.cmdline.lib.DefaultOptionArguments;
33 import org.objectweb.openccm.ast.api.FileScope;
34 import org.objectweb.openccm.command.lib.CompilerGeneratorBase;
35 import org.objectweb.openccm.command.lib.OptionMultipleArguments;
36
37 import java.util.List JavaDoc;
38
39 /**
40  * Implementation of the cidl_cif command.
41  *
42  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
43  * <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
44  *
45  * @version 0.2
46  */

47
48 public class CIDLtoCIF
49      extends CompilerGeneratorBase
50   implements org.objectweb.corba.command.api.CIDLtoCIF
51 {
52     // ==================================================================
53
//
54
// Internal state.
55
//
56
// ==================================================================
57

58     /** Include Options. */
59     private OptionMultipleArguments userInclude_;
60
61     /** Output directory Option for CIF skeletons. */
62     org.objectweb.util.cmdline.api.OptionArgument odir_; // -d <directory>
63

64     /** Output directory Option for CIDL dependencies. */
65     org.objectweb.util.cmdline.api.OptionArgument depdir_; // -dep <directory>
66

67     /** Unwanted prefix for CIDL dependencies. */
68     org.objectweb.util.cmdline.api.OptionArguments noprefix_; // --noprefix <prefix>
69

70     // ==================================================================
71
//
72
// Constructor.
73
//
74
// ==================================================================
75

76     /** The default constructor. */
77     public
78     CIDLtoCIF()
79     {
80         // Calls the CompilerGeneratorBase constructor.
81
super(new DefaultCommandLine("cidl_cif",
82                                      "file",
83                                      new String JavaDoc[] {
84                                         "Compile an OMG CIDL file and generate its associated:",
85                                         "- local CIF interfaces",
86                                         "- Java component and home executor skeletons",
87                                         "- class dependencies files"
88                                      },
89                                      true),
90               "generated_cif.idl");
91
92         // Inits internal state.
93
userInclude_ = new OptionMultipleArguments(
94                                "-ipath",
95                                new String JavaDoc[] { "-ipath" },
96                                "file",
97                                new String JavaDoc[] {
98                                    "Add a #include \"file\" statement where",
99                                    "file must be the complete file path"
100                                },
101                                "",
102                                true);
103         getCommandLine().addOption(userInclude_);
104
105         // Specify CIF skeletons output base directory
106
odir_ = new DefaultOptionArgument(
107                         new String JavaDoc[] { "-d" },
108                         "outputdir",
109                         new String JavaDoc[] {
110                           "Generate Java implementations into directory <outputdir>,",
111                           "default is generated/"
112                         },
113                         "generated" );
114         getCommandLine().addOption(odir_);
115
116         // Specify CIDL dependencies output base directory
117
depdir_ = new DefaultOptionArgument(
118                         new String JavaDoc[] { "-dep" },
119                         "dependencies_dir",
120                         new String JavaDoc[] {
121                           "Generate CIDL class dependencies into directory <dependencies_dir>,",
122                           "default is dependencies/"
123                         },
124                         "dependencies" );
125         getCommandLine().addOption(depdir_);
126
127         // Specify unwanted prefix
128
noprefix_ = new DefaultOptionArguments(
129                         new String JavaDoc[] { "--noprefix" },
130                         "aprefix",
131                         new String JavaDoc[] {
132                           "CIDL class dependencies starting with <aprefix>",
133                           "will not be included."
134                         } );
135         getCommandLine().addOption(noprefix_);
136    }
137
138     // ==================================================================
139
//
140
// Internal methods.
141
//
142
// ==================================================================
143

144     // ==================================================================
145
//
146
// Public methods for org.objectweb.util.cmdline.api.Application
147
//
148
// ==================================================================
149

150     // ==================================================================
151
//
152
// Public methods for org.objectweb.openccm.command.api.Application
153
//
154
// ==================================================================
155

156     /**
157      * Runs the application.
158      *
159      * @param args The command line arguments.
160      *
161      * @return The status.
162      */

163     public int
164     run(java.lang.String JavaDoc[] args)
165     {
166         String JavaDoc filename = args[0];
167
168         // Inits the OpenCCM Abstract Syntax Tree.
169
if(!initAST())
170             return -1;
171
172         // Compiles the file.
173
FileScope fileScope = compile(filename);
174         if(fileScope == null)
175             return -1;
176
177         // Generates CIF
178
if (!generate_cif(fileScope,
179                           filename, // Input File
180
getOutputFileName(), // IDL File
181
getCIFOutputDirectory(), // Base directory for Java Mapping
182
userInclude_.getOptionArguments()))
183             return -1;
184
185         // Generates CIDL dependencies
186
if ( !generate_cif_dependencies(fileScope,
187                                         getDependenciesOutputDirectory(),
188                                         getNoPrefix()) )
189             return -1;
190
191         // All is OK.
192
return 0;
193     }
194
195     // ==================================================================
196
//
197
// Public methods for org.objectweb.openccm.command.api.CommandOnIR3
198
//
199
// ==================================================================
200

201     // ==================================================================
202
//
203
// Public methods for org.objectweb.openccm.command.api.CommandOnAST
204
//
205
// ==================================================================
206

207     // ==================================================================
208
//
209
// Public methods for org.objectweb.openccm.command.api.Compiler
210
//
211
// ==================================================================
212

213     // ==================================================================
214
//
215
// Public methods for org.objectweb.openccm.command.api.Generator
216
//
217
// ==================================================================
218

219     // ==================================================================
220
//
221
// Public methods for org.objectweb.openccm.command.api.CIDLtoCIF
222
//
223
// ==================================================================
224

225     /**
226      * Obtains CIF skeletons output directory.
227      *
228      * @result The CIF skeletons output directory.
229      */

230     public String JavaDoc
231     getCIFOutputDirectory()
232     {
233         return odir_.getArgument();
234     }
235
236     /**
237      * Obtains CIDL dependencies output directory.
238      *
239      * @result The output directory.
240      */

241     public String JavaDoc
242     getDependenciesOutputDirectory()
243     {
244         return depdir_.getArgument();
245     }
246
247     /**
248      * Obtains unwanted prefixs for CIDL dependencies.
249      *
250      * @result The list of prefixs.
251      */

252     public String JavaDoc[]
253     getNoPrefix()
254     {
255         return noprefix_.getOptionValues();
256     }
257
258     /**
259      * Generates CIF interfaces and skeletons
260      * from an AST FileScope instance.
261      *
262      * Note that checkComponentRepository() and initAST() methods
263      * must be called before.
264      *
265      * @param filescope The source AST FileScope returned by the compile() method.
266      * @param inputfile - The source file.
267      * @param outputfile - The file name for the IDL mapping.
268      * @param base_dir - The base directory for java mapping.
269      * @param userIncludes - User files to include "...".
270      *
271      * @return True if ok, else false.
272      */

273     public boolean
274     generate_cif(FileScope filescope,
275                  String JavaDoc inputfile,
276                  String JavaDoc outputfile,
277                  String JavaDoc base_dir,
278                  String JavaDoc[] userIncludes)
279     {
280         List JavaDoc list = null;
281
282          // Generates CIF interfaces in the AST.
283
org.objectweb.corba.generator.cif_idl.api.CIF_IDLGenerator cif_gen =
284             new org.objectweb.corba.generator.cif_idl.lib.CIF_IDLGenerator( getAST(),
285                                                                             getComponentRepository() );
286         try
287         {
288             list = cif_gen.declare_cif(filescope);
289         }
290         catch(org.objectweb.openccm.generator.common.lib.GenerationException ex)
291         {
292             report_exception(ex);
293             return false;
294         }
295
296         // Switch the OpenCCM Interface Repository in mode OMG IDL 2.x.
297
getComponentRepository().as_IDL2_repository();
298
299         // Launchs the IDL2 generator.
300
org.objectweb.openccm.generator.idl.api.IDL2Generator idl2_gen =
301             new org.objectweb.openccm.generator.idl.lib.IDL2Generator( getAST() );
302         idl2_gen.initialize(outputfile,
303                             outputfile,
304                             new java.util.ArrayList JavaDoc(), //includes
305
new java.util.ArrayList JavaDoc(java.util.Arrays.asList(userIncludes)), //user includes
306
null,
307                             "CIDLtoCIF" );
308
309         // Genarates CIF interfaces java mapping.
310
getConsole().message("Generating CIF mapping from " + inputfile + "...");
311         try
312         {
313             idl2_gen.generate(list);
314         }
315         catch(org.objectweb.openccm.generator.common.lib.GenerationException ex)
316         {
317             report_exception(ex);
318             return false;
319         }
320         getConsole().message("OMG IDL mapping generated in " + outputfile + ".");
321
322         // Generates CIF skeletons
323
org.objectweb.corba.generator.cif.lib.CIF_JavaGenerator cif_impl_gen =
324             new org.objectweb.corba.generator.cif.lib.CIF_JavaGenerator( getAST() );
325         getConsole().message("Generating CIF implementations...");
326         try
327         {
328             cif_impl_gen.cif_to_java(base_dir, filescope);
329         }
330         catch(org.objectweb.openccm.generator.common.lib.GenerationException ex)
331         {
332             report_exception(ex);
333             return false;
334         }
335         getConsole().message("CIF implementations generated.");
336
337         // Deletes CIF modules in I.R.
338
cif_gen.remove_decls();
339
340         // Switch the OpenCCM Interface Repository in mode OMG IDL 3.x.
341
getComponentRepository().as_IDL3_repository();
342
343         return true;
344     }
345
346     /**
347      * Generates CIF dependencies from an AST FileScope instance.
348      *
349      * Note that checkComponentRepository() and initAST() methods
350      * must be called before.
351      *
352      * @param filescope - The source AST FileScope returned by the compile() method.
353      * @param base_dir - The base directory for dependencies output.
354      * @param unwanted_prefixs - Unwanted prefixs for CIDL dependencies.
355      *
356      * @return True if ok, else false.
357      */

358     public boolean
359     generate_cif_dependencies(FileScope filescope,
360                               String JavaDoc base_dir,
361                               String JavaDoc[] unwanted_prefixs)
362     {
363         org.objectweb.corba.generator.dependencies.api.CIDLFileDependenciesResolver dep_resolver = null;
364
365         dep_resolver =
366             new org.objectweb.corba.generator.dependencies.lib.CIDLFileDependenciesResolver( getAST() );
367         getConsole().message("Generating CIDL dependencies...");
368         try
369         {
370             dep_resolver.set_filters(unwanted_prefixs);
371             dep_resolver.generate_dependencies( filescope, base_dir );
372         }
373         catch(org.objectweb.openccm.generator.common.lib.GenerationException ex)
374         {
375             report_exception(ex);
376             return false;
377         }
378         getConsole().message("CIDL dependencies generated.");
379
380         return true;
381     }
382
383     // ==================================================================
384
//
385
// Static public methods.
386
//
387
// ==================================================================
388

389     /**
390      * The main bootstrap method.
391      *
392      * @param args The command line arguments.
393      */

394     public static void
395     main(String JavaDoc[] args)
396     {
397         CIDLtoCIF cidl2cif = new CIDLtoCIF();
398         cidl2cif.runMain(args);
399     }
400 }
401
Popular Tags