| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CommandLineBuilder |
|
| 2.7142857142857144;2.714 |
| 1 | /* | |
| 2 | * Cobertura - http://cobertura.sourceforge.net/ | |
| 3 | * | |
| 4 | * Copyright (C) 2005 Grzegorz Lukasik | |
| 5 | * | |
| 6 | * Note: This file is dual licensed under the GPL and the Apache | |
| 7 | * Source License (so that it can be used from both the main | |
| 8 | * Cobertura classes and the ant tasks). | |
| 9 | * | |
| 10 | * Cobertura is free software; you can redistribute it and/or modify | |
| 11 | * it under the terms of the GNU General Public License as published | |
| 12 | * by the Free Software Foundation; either version 2 of the License, | |
| 13 | * or (at your option) any later version. | |
| 14 | * | |
| 15 | * Cobertura is distributed in the hope that it will be useful, but | |
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 18 | * General Public License for more details. | |
| 19 | * | |
| 20 | * You should have received a copy of the GNU General Public License | |
| 21 | * along with Cobertura; if not, write to the Free Software | |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
| 23 | * USA | |
| 24 | */ | |
| 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 | /** | |
| 39 | * Helper class for storing long command lines inside temporary file. | |
| 40 | * <p> | |
| 41 | * Typical usage: | |
| 42 | * | |
| 43 | * <pre> | |
| 44 | * builder = new CommandLineBuilder(); | |
| 45 | * builder.addArg("--someoption"); | |
| 46 | * builder.addArg("optionValue"); | |
| 47 | * ... | |
| 48 | * builder.saveArgs(); | |
| 49 | * doSomething(builder.getCommandLineFile()); | |
| 50 | * builder.dispose(); | |
| 51 | * </pre> | |
| 52 | * | |
| 53 | * It will save options in <code>builder.getCommandLineFile()</code>. Options | |
| 54 | * will be stored one in a line. To retrieve options from file helper method can | |
| 55 | * be used (see documentation): | |
| 56 | * | |
| 57 | * <pre> | |
| 58 | * String[] args = CommandLineBuilder.preprocessCommandLineArguments(args); | |
| 59 | * </pre> | |
| 60 | * | |
| 61 | * </p> | |
| 62 | * | |
| 63 | * <p> | |
| 64 | * NOTICE: No protection against line separators in arguments, should be OK for | |
| 65 | * Cobertura needs. | |
| 66 | * </p> | |
| 67 | * <p> | |
| 68 | * NOTICE: This class depends on local machine settings (line separator, default | |
| 69 | * encoding). If arguments are saved on different machine than they are loaded, | |
| 70 | * results are unspecified. No problem in Cobertura. | |
| 71 | * </p> | |
| 72 | * | |
| 73 | * @author Grzegorz Lukasik | |
| 74 | */ | |
| 75 | public class CommandLineBuilder { | |
| 76 | 17 | private static final Logger logger = Logger |
| 77 | .getLogger(CommandLineBuilder.class); | |
| 78 | ||
| 79 | 17 | private static final String LINESEP = System.getProperty("line.separator"); |
| 80 | ||
| 81 | // File that will be used to store arguments | |
| 82 | 18 | private File commandLineFile = null; |
| 83 | ||
| 84 | // Writer that will be used to write arguments to the file | |
| 85 | 18 | private FileWriter commandLineWriter = null; |
| 86 | ||
| 87 | /** | |
| 88 | * Creates a new instance of the builder. Instances of this class should not | |
| 89 | * be reused to create many command lines. | |
| 90 | * | |
| 91 | * @throws IOException | |
| 92 | * if problems with creating temporary file for storing command | |
| 93 | * line occur | |
| 94 | */ | |
| 95 | 18 | public CommandLineBuilder() throws IOException { |
| 96 | 18 | commandLineFile = File.createTempFile("cobertura.", ".cmdline"); |
| 97 | 18 | commandLineFile.deleteOnExit(); |
| 98 | 18 | commandLineWriter = new FileWriter(commandLineFile); |
| 99 | 18 | } |
| 100 | ||
| 101 | /** | |
| 102 | * Adds command line argument. Each argument can be thought as a single cell | |
| 103 | * in array passed to main method. This method should not be used after | |
| 104 | * arguments were saved. | |
| 105 | * | |
| 106 | * @param arg command line argument to save | |
| 107 | * @throws IOException | |
| 108 | * if problems with temporary file occur | |
| 109 | * @throws NullPointerException | |
| 110 | * if <code>arg</code> is <code>null</code> | |
| 111 | */ | |
| 112 | public void addArg(String arg) throws IOException { | |
| 113 | 100154 | if( arg==null) |
| 114 | 3 | throw new NullPointerException(); |
| 115 | 100151 | commandLineWriter.write(arg + LINESEP); |
| 116 | 100151 | } |
| 117 | ||
| 118 | ||
| 119 | /** | |
| 120 | * Adds two command line arguments. Convienience function, calls | |
| 121 | * {@link #addArg(String)} two times. | |
| 122 | * | |
| 123 | * @param arg1 first command line argument to save | |
| 124 | * @param arg2 second command line argument to save | |
| 125 | * @throws IOException | |
| 126 | * if problems with temporary file occur | |
| 127 | * @throws NullPointerException | |
| 128 | * if any <code>arg</code> is <code>null</code> | |
| 129 | */ | |
| 130 | public void addArg(String arg1, String arg2) throws IOException { | |
| 131 | 50 | addArg(arg1); |
| 132 | 49 | addArg(arg2); |
| 133 | 48 | } |
| 134 | ||
| 135 | ||
| 136 | /** | |
| 137 | * Saves options and made file available to use. Use method | |
| 138 | * {@link #getCommandLineFile} to get the file the arguments are saved in. | |
| 139 | * | |
| 140 | * @throws IOException | |
| 141 | * if problems with temporary file occur | |
| 142 | */ | |
| 143 | public void saveArgs() throws IOException { | |
| 144 | 17 | commandLineWriter.flush(); |
| 145 | 17 | commandLineWriter.close(); |
| 146 | 17 | } |
| 147 | ||
| 148 | /** | |
| 149 | * Gets absolute path to the file with saved arguments. Notice, that however | |
| 150 | * this method can be used as soon as an instance of this class is created, | |
| 151 | * arguments should be read from the file after a call to | |
| 152 | * {@link #saveArgs} method. | |
| 153 | * | |
| 154 | * @return absolute path to the file with arguments | |
| 155 | */ | |
| 156 | public String getCommandLineFile() { | |
| 157 | 20 | return commandLineFile.getAbsolutePath(); |
| 158 | } | |
| 159 | ||
| 160 | /** | |
| 161 | * Explicity frees all resources associated with this instance. Result of | |
| 162 | * any other method call after disposing an instance of this class is | |
| 163 | * unspecified. | |
| 164 | */ | |
| 165 | public void dispose() { | |
| 166 | 17 | commandLineFile.delete(); |
| 167 | 17 | } |
| 168 | ||
| 169 | /** | |
| 170 | * Loads arguments from file if <code>--commandsfile</code> option is used. Checks | |
| 171 | * if passed array contains <code>--commandsfile</code> String, and if | |
| 172 | * so arguments from file specified in the very next array cell are read. If | |
| 173 | * there are more then one <code>--commandsfile</code> the result is unspecified. | |
| 174 | * | |
| 175 | * @return The list of arguments read from commandsfile, or | |
| 176 | * <code>args</code> if commandsfile option was not specified | |
| 177 | * or the file cannot be read. | |
| 178 | * @throws NullPointerException if args is null, or any argument is null | |
| 179 | * @throws IllegalArgumentException if --commandsfile is specified as last option | |
| 180 | * @throws IOException if I/O related error with temporary command line file occur | |
| 181 | */ | |
| 182 | public static String[] preprocessCommandLineArguments(String[] args) throws IOException { | |
| 183 | 26 | boolean hasCommandsFile = false; |
| 184 | 26 | String commandsFileName = null; |
| 185 | 64 | for (int i = 0; i < args.length; i++) { |
| 186 | 41 | if ( args[i].equals( "--commandsfile")) { |
| 187 | 20 | if( i==args.length-1) { |
| 188 | 1 | throw new IllegalArgumentException("'--commandsfile' specified as last option."); |
| 189 | } | |
| 190 | 19 | hasCommandsFile = true; |
| 191 | 19 | commandsFileName = args[++i]; |
| 192 | } | |
| 193 | } | |
| 194 | ||
| 195 | 22 | if (hasCommandsFile) { |
| 196 | 18 | List arglist = new ArrayList(); |
| 197 | 18 | BufferedReader bufferedReader = null; |
| 198 | ||
| 199 | try { | |
| 200 | 18 | bufferedReader = new BufferedReader(new FileReader( |
| 201 | commandsFileName)); | |
| 202 | String line; | |
| 203 | ||
| 204 | 100167 | while ((line = bufferedReader.readLine()) != null) |
| 205 | 100150 | arglist.add(line); |
| 206 | ||
| 207 | 1 | } catch (IOException e) { |
| 208 | 1 | logger.info( "I/O error when reading temporary commands file", e); |
| 209 | 1 | throw new IOException( "Unable to read temporary commands file " |
| 210 | + commandsFileName + "."); | |
| 211 | } finally { | |
| 212 | 18 | if (bufferedReader != null) { |
| 213 | try { | |
| 214 | 17 | bufferedReader.close(); |
| 215 | 0 | } catch (IOException e) { |
| 216 | 17 | } |
| 217 | 0 | } |
| 218 | 1 | } |
| 219 | ||
| 220 | 17 | args = (String[]) arglist.toArray(new String[arglist.size()]); |
| 221 | } | |
| 222 | 21 | return args; |
| 223 | } | |
| 224 | } |