KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > cpp > lib > PreprocessorApplication


1 /*====================================================================
2
3 ObjectWeb Util Preprocessor Package.
4 Copyright (C) 2004 INRIA & USTL - LIFL - GOAL
5 Contact: architecture@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): Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 --------------------------------------------------------------------
26 $Id: PreprocessorApplication.java,v 1.3 2004/04/15 13:47:12 carpentier Exp $
27 ====================================================================*/

28
29 package org.objectweb.util.cpp.lib;
30
31 import java.io.File JavaDoc;
32 import java.io.OutputStream JavaDoc;
33
34 import org.objectweb.util.cmdline.api.CommandLine;
35 import org.objectweb.util.cmdline.lib.ApplicationBase;
36 import org.objectweb.util.cmdline.lib.DefaultCommandLine;
37 import org.objectweb.util.cpp.api.Preprocessor;
38
39 /**
40  * This provides a preprocessor written in Java.
41  *
42  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
43  * @version 0.1
44  */

45 public class PreprocessorApplication
46      extends ApplicationBase
47   implements org.objectweb.util.cpp.api.PreprocessorApplication
48 {
49     // ==================================================================
50
//
51
// Internal state.
52
//
53
// ==================================================================
54

55     /** The preprocessor. */
56     private Preprocessor preprocessor_;
57
58     /** Option -P */
59     private OptionP optionP_;
60
61     /** Option -DNAME */
62     private OptionDNAME optionDNAME_;
63
64     /** Option -UNAME */
65     private OptionUNAME optionUNAME_;
66
67     /** Option -IDIR */
68     private OptionIDIR optionIDIR_;
69
70     // ==================================================================
71
//
72
// Constructor.
73
//
74
// ==================================================================
75

76     /** The default constructor. */
77     public PreprocessorApplication() {
78         this(new DefaultCommandLine("preprocessor",
79                                     "file",
80                                     "Preprocess a file.",
81                                     true),
82              true);
83     }
84
85     /**
86      * The constructor with a provided command line manager.
87      *
88      * @param commandLine The provided command line manager.
89      * @param withDefaultOptions True to add default options in the command line.
90      */

91     public PreprocessorApplication(CommandLine commandLine, boolean withDefaultOptions) {
92         this(commandLine,withDefaultOptions,true);
93     }
94
95     /**
96      * The constructor with a provided command line manager.
97      *
98      * @param commandLine The provided command line manager.
99      * @param withDefaultOptions True to add default options in the command line.
100      * @param withPreprocessorOptions True to add preprocessor options in the command line.
101      */

102     public PreprocessorApplication(CommandLine commandLine,
103                                    boolean withDefaultOptions,
104                                    boolean withPreprocessorOptions) {
105         // Calls the default ApplicationBase constructor.
106
super(commandLine, withDefaultOptions);
107
108         // Inits internal state.
109
setPreprocessor(new PreprocessorJPP());
110
111         if (withPreprocessorOptions) {
112             // Creates the preprocessor options.
113
commandLine.addOption(new OptionCPP(this));
114
115             optionP_ = new OptionP(getPreprocessor());
116             commandLine.addOption(optionP_);
117             optionDNAME_ = new OptionDNAME(getPreprocessor());
118             commandLine.addOption(optionDNAME_);
119             optionUNAME_ = new OptionUNAME(getPreprocessor());
120             commandLine.addOption(optionUNAME_);
121             optionIDIR_ = new OptionIDIR(getPreprocessor());
122             commandLine.addOption(optionIDIR_);
123         }
124     }
125
126     // ==================================================================
127
//
128
// Public methods for org.objectweb.util.api.Identifiable
129
//
130
// ==================================================================
131

132     // ==================================================================
133
//
134
// Public methods for org.objectweb.util.cmdline.api.Application
135
//
136
// ==================================================================
137

138     /**
139      * Starts the main function.
140      *
141      * Must be defined into subclasses.
142      *
143      * @param args The command line arguments.
144      *
145      * @return The status of the application.
146      */

147     public int start(String JavaDoc[] args) {
148        boolean result = preprocess(args[0], getConsole().getOutputStream());
149        return result?0:-1;
150     }
151
152     // ==================================================================
153
//
154
// Public methods for org.objectweb.util.cpp.api.PreprocessorHolder
155
//
156
// ==================================================================
157

158     /**
159      * Obtains the associated preprocessor.
160      *
161      * @return The associated preprocessor.
162      */

163     public Preprocessor getPreprocessor() {
164         return preprocessor_;
165     }
166
167     /**
168      * Sets the associated preprocessor.
169      *
170      * @param preprocessor The associated preprocessor.
171      */

172     public void setPreprocessor(Preprocessor preprocessor) {
173         preprocessor_ = preprocessor;
174         preprocessor_.setConsole(getConsole());
175         if(optionP_ != null)
176             optionP_.setPreprocessor(preprocessor);
177         if(optionDNAME_ != null)
178             optionDNAME_.setPreprocessor(preprocessor);
179         if(optionUNAME_ != null)
180             optionUNAME_.setPreprocessor(preprocessor);
181         if(optionIDIR_ != null)
182             optionIDIR_.setPreprocessor(preprocessor);
183     }
184
185     // ==================================================================
186
//
187
// Public methods for org.objectweb.util.cpp.api.PreprocessorOperations
188
//
189
// ==================================================================
190

191     /**
192      * Preprocesses a file.
193      *
194      * @param fileName The name of the file to preprocess.
195      *
196      * @return The file containing the output file.
197      */

198     public File JavaDoc preprocess(String JavaDoc fileName) {
199          getConsole().message("Preprocessing file " + fileName + "...");
200
201          File JavaDoc result = getPreprocessor().preprocess(fileName);
202
203          // If error
204
if (result == null) {
205              getConsole().error("Preprocessing file " + fileName + " failed!");
206              return null;
207          }
208
209          getConsole().message("File " + fileName + " preprocessed.");
210          return result;
211     }
212
213     /**
214      * Preprocesses a file.
215      *
216      * @param fileName The name of the file to preprocess.
217      * @param tmpFile The file containing the output file.
218      *
219      * @return True if OK.
220      */

221     public boolean preprocess(String JavaDoc fileName, File JavaDoc tmpFile) {
222          getConsole().message("Preprocessing file " + fileName + "...");
223
224          boolean result = getPreprocessor().preprocess(fileName, tmpFile);
225
226          // If error
227
if (!result) {
228              getConsole().error("Preprocessing file " + fileName + " failed!");
229              return false;
230          }
231
232          getConsole().message("File " + fileName + " preprocessed.");
233          return true;
234     }
235
236     /**
237      * Preprocesses a file.
238      *
239      * @param fileName The name of the file to preprocess.
240      * @param output The output stream.
241      */

242     public boolean preprocess(String JavaDoc fileName, OutputStream JavaDoc output) {
243          getConsole().message("Preprocessing file " + fileName + "...");
244
245          boolean result = getPreprocessor().preprocess(fileName, output);
246
247          // If error
248
if (!result) {
249              getConsole().error("Preprocessing file " + fileName + " failed!");
250              return false;
251          }
252
253          getConsole().message("File " + fileName + " preprocessed.");
254          return true;
255     }
256
257     // ==================================================================
258
//
259
// Public methods for org.objectweb.util.cpp.api.PreprocessorApplication
260
//
261
// ==================================================================
262

263     // ==================================================================
264
//
265
// Public static methods.
266
//
267
// ==================================================================
268

269     /**
270      * The main bootstrap method.
271      *
272      * @param args The command line arguments.
273      */

274     public static void main(String JavaDoc[] args) {
275         PreprocessorApplication application = new PreprocessorApplication();
276         application.runMain(args);
277     }
278 }
279
Popular Tags