KickJava   Java API By Example, From Geeks To Geeks.

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


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: PreprocessorBase.java,v 1.1 2004/02/05 20:29:57 rouvoy Exp $
27 ====================================================================*/

28
29 package org.objectweb.util.cpp.lib;
30
31 import java.io.File JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 import org.objectweb.util.cmdline.lib.DefaultConsoleHolder;
38 import org.objectweb.util.cpp.api.Preprocessor;
39 import org.objectweb.util.misc.api.ExceptionWrapper;
40
41
42 /**
43  * Abstract base class for Preprocessor instances.
44  *
45  * This provides the following properties:
46  *
47  * - GenerateLineInformation: Indicates if #line information should be generated.
48  *
49  * - IncludeDirectory: The list of directories where included files
50  * are searched.
51  *
52  * - Macro: The list of defined macros.
53  *
54  * This provides two preprocess operations.
55  *
56  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
57  * @version 0.1
58  */

59 public abstract class PreprocessorBase
60               extends DefaultConsoleHolder
61            implements Preprocessor
62 {
63     // ==================================================================
64
//
65
// Internal state.
66
//
67
// ==================================================================
68

69     /** The GenerateLineInformation property. */
70     protected boolean generateLineInformation_;
71
72     /** Arguments for the preprocessor. */
73     protected Vector JavaDoc arguments_;
74
75     /** The list of include directories. */
76     protected Vector JavaDoc includeDirectories_ ;
77
78     /** The list of declared macros. */
79     protected Hashtable JavaDoc macros_;
80
81     // ==================================================================
82
//
83
// Constructor.
84
//
85
// ==================================================================
86

87     /** The default constructor. */
88     public PreprocessorBase() {
89         // Inits internal state.
90
generateLineInformation_ = false;
91         arguments_ = new Vector JavaDoc();
92         includeDirectories_ = new Vector JavaDoc();
93         macros_ = new Hashtable JavaDoc();
94     }
95
96     // ==================================================================
97
//
98
// Internal methods.
99
//
100
// ==================================================================
101

102     // ==================================================================
103
//
104
// Public methods for org.objectweb.util.cpp.api.PreprocessorOperations
105
//
106
// ==================================================================
107

108     /**
109      * Preprocesses a file.
110      *
111      * @param fileName The name of the file to preprocess.
112      *
113      * @return The file containing the output file.
114      */

115     public File JavaDoc preprocess(String JavaDoc fileName) {
116         File JavaDoc tmpFile = null;
117         try {
118             // Creates a temp file.
119
tmpFile = File.createTempFile("OW_preprocessor",".tmp");
120         } catch(java.io.IOException JavaDoc exc) {
121             // Wraps any java.io.IOException exception.
122
throw new ExceptionWrapper(exc);
123         }
124
125         boolean status = preprocess(fileName, tmpFile);
126
127         // If error then deletes the tmp file.
128
if (!status) {
129             tmpFile.delete();
130             return null;
131         }
132
133         // Returns the tmp file.
134
return tmpFile;
135     }
136
137     /**
138      * Preprocesses a file.
139      *
140      * @param fileName The name of the file to preprocess.
141      * @param tmpFile The file containing the output file.
142      *
143      * @return True if OK.
144      */

145     public abstract boolean
146     preprocess(String JavaDoc fileName, File JavaDoc tmpFile);
147
148     /**
149      * Preprocesses a file.
150      *
151      * Must be defined in subclasses.
152      *
153      * @param fileName The name of the file to preprocess.
154      * @param output The output stream.
155      */

156     public abstract boolean
157     preprocess(String JavaDoc fileName, OutputStream JavaDoc output);
158
159     // ==================================================================
160
//
161
// Public methods for org.objectweb.util.cpp.api.Preprocessor
162
//
163
// ==================================================================
164

165     /**
166      * Obtains the GenerateLineInformation property.
167      *
168      * @return The GenerateLineInformation property.
169      */

170     public boolean getGenerateLineInformation() {
171         return generateLineInformation_;
172     }
173
174     /**
175      * Sets the GenerateLineInformation property.
176      *
177      * @param b The GenerateLineInformation property.
178      */

179     public void setGenerateLineInformation(boolean b) {
180         generateLineInformation_ = b;
181     }
182
183     /**
184      * Adds a directory to the list of include directories.
185      *
186      * @param directoru The directory to add.
187      */

188     public void addIncludeDirectory(String JavaDoc directory) {
189         arguments_.addElement("-I" + directory);
190         includeDirectories_.addElement(directory);
191     }
192
193     /**
194      * Obtains the list of include directories.
195      *
196      * @return The list of include directories.
197      */

198     public String JavaDoc[] getIncludeDirectory() {
199         return (String JavaDoc[])includeDirectories_.toArray(new String JavaDoc[0]);
200     }
201
202     /**
203      * Sets the list of include directories.
204      *
205      * @param directories The list of include directories.
206      */

207     public void setIncludeDirectory(String JavaDoc[] directories) {
208         // Reinits the list of include directories.
209
includeDirectories_ = new Vector JavaDoc();
210
211         // Adds each directory.
212
for(int i=0; i<directories.length; i++) {
213             arguments_.addElement("-I" + directories[i]);
214             addIncludeDirectory(directories[i]);
215         }
216     }
217
218     /**
219      * Adds a macro (name - 1) to the list of declared macros.
220      *
221      * @param name The name of the macro to add.
222      */

223     public void addMacro(String JavaDoc name) {
224         arguments_.addElement("-D" + name);
225         macros_.put(name, "1");
226     }
227
228     /**
229      * Adds a macro (name - value) to the list of declared macros.
230      *
231      * @param name The name of the macro to add.
232      * @param value The value of the macro to add.
233      */

234     public void addMacro(String JavaDoc name, String JavaDoc value) {
235         arguments_.addElement("-D" + name + '=' + value);
236         macros_.put(name, value);
237     }
238
239     /**
240      * Removes a macro (name - value) from the list of declared macros.
241      *
242      * @param name The name of the macro to remove.
243      */

244     public void removeMacro(String JavaDoc name) {
245         arguments_.addElement("-U" + name);
246         macros_.remove(name);
247     }
248
249     /**
250      * Obtains the list of declared macros.
251      *
252      * @return The list of declared macros.
253      */

254     public Map JavaDoc getMacro() {
255         // TODO: implement setMacro(java.util.Map macros) method
256
throw new RuntimeException JavaDoc("NOT IMPLEMENTED!!!");
257     }
258
259     /**
260      * Sets the list of declared macros.
261      *
262      * @param macros The list of declared macros.
263      */

264     public void setMacro(Map JavaDoc macros) {
265         // TODO: implement setMacro(java.util.Map macros) method
266
throw new RuntimeException JavaDoc("NOT IMPLEMENTED!!!");
267     }
268 }
269
Popular Tags