1 25 26 package net.sourceforge.cobertura.util; 27 28 import java.io.BufferedReader ; 29 import java.io.File ; 30 import java.io.FileReader ; 31 import java.io.FileWriter ; 32 import java.io.IOException ; 33 import java.util.ArrayList ; 34 import java.util.List ; 35 36 import org.apache.log4j.Logger; 37 38 75 public class CommandLineBuilder { 76 private static final Logger logger = Logger 77 .getLogger(CommandLineBuilder.class); 78 79 private static final String LINESEP = System.getProperty("line.separator"); 80 81 private File commandLineFile = null; 83 84 private FileWriter commandLineWriter = null; 86 87 95 public CommandLineBuilder() throws IOException { 96 commandLineFile = File.createTempFile("cobertura.", ".cmdline"); 97 commandLineFile.deleteOnExit(); 98 commandLineWriter = new FileWriter (commandLineFile); 99 } 100 101 112 public void addArg(String arg) throws IOException { 113 if( arg==null) 114 throw new NullPointerException (); 115 commandLineWriter.write(arg + LINESEP); 116 } 117 118 119 130 public void addArg(String arg1, String arg2) throws IOException { 131 addArg(arg1); 132 addArg(arg2); 133 } 134 135 136 143 public void saveArgs() throws IOException { 144 commandLineWriter.flush(); 145 commandLineWriter.close(); 146 } 147 148 156 public String getCommandLineFile() { 157 return commandLineFile.getAbsolutePath(); 158 } 159 160 165 public void dispose() { 166 commandLineFile.delete(); 167 } 168 169 182 public static String [] preprocessCommandLineArguments(String [] args) throws IOException { 183 boolean hasCommandsFile = false; 184 String commandsFileName = null; 185 for (int i = 0; i < args.length; i++) { 186 if ( args[i].equals( "--commandsfile")) { 187 if( i==args.length-1) { 188 throw new IllegalArgumentException ("'--commandsfile' specified as last option."); 189 } 190 hasCommandsFile = true; 191 commandsFileName = args[++i]; 192 } 193 } 194 195 if (hasCommandsFile) { 196 List arglist = new ArrayList (); 197 BufferedReader bufferedReader = null; 198 199 try { 200 bufferedReader = new BufferedReader (new FileReader ( 201 commandsFileName)); 202 String line; 203 204 while ((line = bufferedReader.readLine()) != null) 205 arglist.add(line); 206 207 } catch (IOException e) { 208 logger.info( "I/O error when reading temporary commands file", e); 209 throw new IOException ( "Unable to read temporary commands file " 210 + commandsFileName + "."); 211 } finally { 212 if (bufferedReader != null) { 213 try { 214 bufferedReader.close(); 215 } catch (IOException e) { 216 } 217 } 218 } 219 220 args = (String []) arglist.toArray(new String [arglist.size()]); 221 } 222 return args; 223 } 224 } 225 | Popular Tags |