1 28 29 package org.objectweb.util.cpp.lib; 30 31 import java.io.File ; 32 import java.io.OutputStream ; 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 45 public class PreprocessorApplication 46 extends ApplicationBase 47 implements org.objectweb.util.cpp.api.PreprocessorApplication 48 { 49 55 56 private Preprocessor preprocessor_; 57 58 59 private OptionP optionP_; 60 61 62 private OptionDNAME optionDNAME_; 63 64 65 private OptionUNAME optionUNAME_; 66 67 68 private OptionIDIR optionIDIR_; 69 70 76 77 public PreprocessorApplication() { 78 this(new DefaultCommandLine("preprocessor", 79 "file", 80 "Preprocess a file.", 81 true), 82 true); 83 } 84 85 91 public PreprocessorApplication(CommandLine commandLine, boolean withDefaultOptions) { 92 this(commandLine,withDefaultOptions,true); 93 } 94 95 102 public PreprocessorApplication(CommandLine commandLine, 103 boolean withDefaultOptions, 104 boolean withPreprocessorOptions) { 105 super(commandLine, withDefaultOptions); 107 108 setPreprocessor(new PreprocessorJPP()); 110 111 if (withPreprocessorOptions) { 112 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 132 138 147 public int start(String [] args) { 148 boolean result = preprocess(args[0], getConsole().getOutputStream()); 149 return result?0:-1; 150 } 151 152 158 163 public Preprocessor getPreprocessor() { 164 return preprocessor_; 165 } 166 167 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 191 198 public File preprocess(String fileName) { 199 getConsole().message("Preprocessing file " + fileName + "..."); 200 201 File result = getPreprocessor().preprocess(fileName); 202 203 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 221 public boolean preprocess(String fileName, File tmpFile) { 222 getConsole().message("Preprocessing file " + fileName + "..."); 223 224 boolean result = getPreprocessor().preprocess(fileName, tmpFile); 225 226 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 242 public boolean preprocess(String fileName, OutputStream output) { 243 getConsole().message("Preprocessing file " + fileName + "..."); 244 245 boolean result = getPreprocessor().preprocess(fileName, output); 246 247 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 263 269 274 public static void main(String [] args) { 275 PreprocessorApplication application = new PreprocessorApplication(); 276 application.runMain(args); 277 } 278 } 279 | Popular Tags |