KickJava   Java API By Example, From Geeks To Geeks.

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


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: PreprocessorCPP.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.FileReader JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.io.PrintStream JavaDoc;
35
36 import org.objectweb.util.misc.api.ExceptionWrapper;
37
38
39 /**
40  * This provides a wrapper to external cpp preprocessors.
41  *
42  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
43  * @version 0.2
44  */

45 public class PreprocessorCPP
46      extends PreprocessorBase
47 {
48     // ==================================================================
49
//
50
// Internal state.
51
//
52
// ==================================================================
53

54     /** The path to the external C preprocessor used. */
55     private String JavaDoc cppPath_;
56  
57     // ==================================================================
58
//
59
// Constructors.
60
//
61
// ==================================================================
62

63     /** The default constructor. */
64     public PreprocessorCPP() {
65         // The default cppPath is cpp.
66
this("cpp");
67     }
68
69     /**
70      * The constructor with the path to the external C preprocessor used.
71      *
72      * @param cppPath The path to the external C preprocessor used.
73      */

74     public PreprocessorCPP(String JavaDoc cppPath) {
75         cppPath_ = cppPath;
76     }
77
78     // ==================================================================
79
//
80
// Internal methods.
81
//
82
// ==================================================================
83

84     // ==================================================================
85
//
86
// Public methods for org.objectweb.util.cpp.api.PreprocessorOperations
87
//
88
// ==================================================================
89

90     /**
91      * Preprocesses a file.
92      *
93      * @param fileName The name of the file to preprocess.
94      * @param tmpFile The file containing the output file.
95      *
96      * @return True if OK.
97      */

98     public boolean preprocess(String JavaDoc fileName, File JavaDoc tmpFile) {
99         // Prepares the command line.
100

101         // Allocates the string command array passed as argument
102
// to the exec() method.
103

104         int nb_arguments = arguments_.size();
105         String JavaDoc[] cmd = null;
106
107         // This array length is equals to the number of arguments
108
// plus 3 (i.e. cpp path, input and output file names)
109
// plus 1 if the -P option is required.
110

111         if(!getGenerateLineInformation()) {
112             cmd = new String JavaDoc[3 + nb_arguments];
113         } else {
114             cmd = new String JavaDoc[4 + nb_arguments];
115         }
116
117         // Adds the cpp path to the command array.
118
int i = 0;
119         cmd[i] = cppPath_; i++;
120
121         // Adds the -P option if required.
122
if(getGenerateLineInformation()) {
123             cmd[i] = "-P"; i++;
124         }
125
126         // Adds all arguments.
127
for(int j=0; j<nb_arguments; j++) {
128             cmd[i] = (String JavaDoc)arguments_.elementAt(j); i++;
129         }
130
131         // Adds the input file name.
132
cmd[i] = fileName; i++;
133
134         // Adds the output file name.
135
cmd[i] = tmpFile.toString(); i++;
136
137         // Executes the command.
138
Process JavaDoc cpp = null;
139         try {
140             cpp = Runtime.getRuntime().exec(cmd);
141         } catch(java.io.IOException JavaDoc exc) {
142             // Wraps any java.io.IOException exception.
143
throw new ExceptionWrapper(exc);
144         }
145
146         int status = 0;
147         try {
148             // Waits for the process completion.
149
status = cpp.waitFor();
150         } catch(java.lang.InterruptedException JavaDoc exc) {
151             // Wraps any java.lang.InterruptedException exception.
152
throw new ExceptionWrapper(exc);
153         }
154
155         // Dumps its standard input and error streams.
156
IOHelper.dump(cpp.getInputStream(), getConsole().getOutputStream());
157         IOHelper.dump(cpp.getErrorStream(), getConsole().getErrorStream());
158
159         // Returns true if the status == 0.
160
return status == 0;
161     }
162
163     /**
164      * Preprocesses a file.
165      *
166      * @param fileName The name of the file to preprocess.
167      * @param output The output stream.
168      */

169     public boolean preprocess(String JavaDoc fileName, OutputStream JavaDoc output) {
170         // Preprocesses the file.
171
File JavaDoc tmpFile = preprocess(fileName);
172         if(tmpFile == null)
173             return false;
174
175         try {
176             // Dumps the content of the temp file.
177
IOHelper.dump(new FileReader JavaDoc(tmpFile), new PrintStream JavaDoc(output));
178         } catch(java.io.IOException JavaDoc exc) {
179             // Wraps any java.io.IOException exception.
180
throw new ExceptionWrapper(exc);
181         } finally {
182             // Always destroys the temp file.
183
tmpFile.delete();
184         }
185
186         return true;
187     }
188
189     // ==================================================================
190
//
191
// Public methods for org.objectweb.util.cpp.api.Preprocessor
192
//
193
// ==================================================================
194
}
195
Popular Tags