KickJava   Java API By Example, From Geeks To Geeks.

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


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.Compiler;
31 import org.objectweb.util.cmdline.api.CommandLine;
32 import org.objectweb.util.cpp.lib.PreprocessorApplication;
33
34 /**
35  * Abstract base class for all compiler commands.
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 1.2
41  */

42
43 public abstract class CompilerBase
44               extends CommandOnASTBase
45            implements Compiler JavaDoc
46 {
47     // ==================================================================
48
//
49
// Internal state.
50
//
51
// ==================================================================
52

53     /** The PreprocessorApplication. */
54     private PreprocessorApplication preprocessor_;
55
56     // ==================================================================
57
//
58
// Constructor.
59
//
60
// ==================================================================
61

62     /**
63      * The constructor with the command line manager.
64      *
65      * @param commandLine The command line manager.
66      */

67     public
68     CompilerBase(CommandLine commandLine)
69     {
70         this(commandLine, true);
71     }
72
73     /**
74      * The constructor with the command line manager
75      * and boolean to manage options.
76      *
77      * @param commandLine The command line manager.
78      * @param withPreprocessorOptions True to add preprocessor options in the command line.
79      */

80     public
81     CompilerBase(CommandLine commandLine, boolean withPreprocessorOptions)
82     {
83         // Calls the CommandOnASTBase constructor.
84
super(commandLine);
85
86         // Inits internal state.
87
preprocessor_ = new PreprocessorApplication(commandLine, false, withPreprocessorOptions);
88         preprocessor_.setConsole(getConsole());
89     }
90
91     // ==================================================================
92
//
93
// Internal methods.
94
//
95
// ==================================================================
96

97     // ==================================================================
98
//
99
// Public methods for org.objectweb.util.cmdline.api.Application
100
//
101
// ==================================================================
102

103     // ==================================================================
104
//
105
// Public methods for org.objectweb.openccm.command.api.Application
106
//
107
// ==================================================================
108

109     // ==================================================================
110
//
111
// Public methods for org.objectweb.openccm.command.api.CommandOnIR3
112
//
113
// ==================================================================
114

115     // ==================================================================
116
//
117
// Public methods for org.objectweb.openccm.command.api.CommandOnAST
118
//
119
// ==================================================================
120

121     // ==================================================================
122
//
123
// Public methods for org.objectweb.openccm.command.api.Compiler
124
//
125
// ==================================================================
126

127     /**
128      * Compiles the OMG IDL file and feeds the OpenCCM Interface Repository.
129      *
130      * @param filename The file name to compile.
131      *
132      * @return The associated AST FileScope if ok, or else null.
133      */

134     public org.objectweb.openccm.ast.api.FileScope
135     compile(String JavaDoc filename)
136     {
137         if (!new java.io.File JavaDoc(filename).exists())
138             getConsole().error("File " + filename + " not found!");
139
140         getConsole().message("Reading from file " + filename + "...");
141
142         // Preprocesses the file.
143
java.io.File JavaDoc file = preprocessor_.preprocess(filename);
144         if(file == null) return null;
145
146         // Creates a new parser.
147
org.objectweb.openccm.parser.lib.ErrorManager errorManager = null;
148         org.objectweb.openccm.parser.api.Parser parser = null;
149         try
150         {
151             errorManager = new org.objectweb.openccm.parser.lib.ErrorManager(filename);
152             parser = new org.objectweb.openccm.parser.lib.Parser(
153                              new java.io.FileInputStream JavaDoc(file),
154                              getAST(),
155                              errorManager);
156         }
157         catch(java.io.FileNotFoundException JavaDoc e)
158         {
159             getConsole().error("File " + file.toString() + " not found!");
160             return null;
161         }
162
163         // Parses the file.
164
getConsole().message("Compiling " + filename + " file...");
165         org.objectweb.openccm.ast.api.FileScope result = parser.parseCIDL();
166
167         int nbErrors = errorManager.getNbErrors();
168         int nbWarnings = errorManager.getNbWarnings();
169
170         if (result == null)
171         {
172             String JavaDoc msg = "Compilation failed: " + nbErrors + " error";
173             if(nbErrors > 1) msg += 's';
174             msg += ", " + nbWarnings + " warning";
175             if(nbWarnings > 1) msg += 's';
176             msg += '.';
177             getConsole().error(msg);
178         } else {
179             String JavaDoc msg = "Compilation completed: " + nbWarnings + " warning";
180             if(nbWarnings > 1) msg += 's';
181             msg += '.';
182             getConsole().message(msg);
183         }
184
185         // Deletes the temporary file generated by the preprocessor
186
// even if there are compilation errors (correction of the bug #553).
187
try
188         {
189             file.delete();
190         }
191         catch(Exception JavaDoc e)
192         {
193             getConsole().error("Problem when deleting temporary file " + file.toString());
194             report_exception(e);
195         }
196
197         return result;
198     }
199
200     /**
201      * Gets the preprocessor of the compiler.
202      *
203      * @return The preprocessor.
204      */

205     public org.objectweb.util.cpp.api.PreprocessorApplication
206     getPreprocessorApplication()
207     {
208         return preprocessor_;
209     }
210
211     // ==================================================================
212
//
213
// Other public methods.
214
//
215
// ==================================================================
216
}
217
Popular Tags