net.sourceforge.cobertura.util

Class CommandLineBuilder

public class CommandLineBuilder extends Object

Helper class for storing long command lines inside temporary file.

Typical usage:

  builder = new CommandLineBuilder();
  builder.addArg("--someoption");
  builder.addArg("optionValue");
  ...
  builder.saveArgs();
  doSomething(builder.getCommandLineFile());
  builder.dispose();
 
It will save options in builder.getCommandLineFile(). Options will be stored one in a line. To retrieve options from file helper method can be used (see documentation):
 String[] args = CommandLineBuilder.preprocessCommandLineArguments(args);
 

NOTICE: No protection against line separators in arguments, should be OK for Cobertura needs.

NOTICE: This class depends on local machine settings (line separator, default encoding). If arguments are saved on different machine than they are loaded, results are unspecified. No problem in Cobertura.

Author: Grzegorz Lukasik

Field Summary
FilecommandLineFile
FileWritercommandLineWriter
static Loggerlogger
static StringLINESEP
Constructor Summary
CommandLineBuilder()
Creates a new instance of the builder.
Method Summary
voidaddArg(String arg)
Adds command line argument.
voidaddArg(String arg1, String arg2)
Adds two command line arguments.
voiddispose()
Explicity frees all resources associated with this instance.
StringgetCommandLineFile()
Gets absolute path to the file with saved arguments.
static String[]preprocessCommandLineArguments(String[] args)
Loads arguments from file if --commandsfile option is used.
voidsaveArgs()
Saves options and made file available to use.

Field Detail

commandLineFile

private File commandLineFile

commandLineWriter

private FileWriter commandLineWriter

logger

private static final Logger logger

LINESEP

private static final String LINESEP

Constructor Detail

CommandLineBuilder

public CommandLineBuilder()
Creates a new instance of the builder. Instances of this class should not be reused to create many command lines.

Throws: IOException if problems with creating temporary file for storing command line occur

Method Detail

addArg

public void addArg(String arg)
Adds command line argument. Each argument can be thought as a single cell in array passed to main method. This method should not be used after arguments were saved.

Parameters: arg command line argument to save

Throws: IOException if problems with temporary file occur NullPointerException if arg is null

addArg

public void addArg(String arg1, String arg2)
Adds two command line arguments. Convienience function, calls addArg two times.

Parameters: arg1 first command line argument to save arg2 second command line argument to save

Throws: IOException if problems with temporary file occur NullPointerException if any arg is null

dispose

public void dispose()
Explicity frees all resources associated with this instance. Result of any other method call after disposing an instance of this class is unspecified.

getCommandLineFile

public String getCommandLineFile()
Gets absolute path to the file with saved arguments. Notice, that however this method can be used as soon as an instance of this class is created, arguments should be read from the file after a call to CommandLineBuilder method.

Returns: absolute path to the file with arguments

preprocessCommandLineArguments

public static String[] preprocessCommandLineArguments(String[] args)
Loads arguments from file if --commandsfile option is used. Checks if passed array contains --commandsfile String, and if so arguments from file specified in the very next array cell are read. If there are more then one --commandsfile the result is unspecified.

Returns: The list of arguments read from commandsfile, or args if commandsfile option was not specified or the file cannot be read.

Throws: NullPointerException if args is null, or any argument is null IllegalArgumentException if --commandsfile is specified as last option IOException if I/O related error with temporary command line file occur

saveArgs

public void saveArgs()
Saves options and made file available to use. Use method CommandLineBuilder to get the file the arguments are saved in.

Throws: IOException if problems with temporary file occur