net.sourceforge.cobertura.util
Class CommandLineBuilder

java.lang.Object
  extended bynet.sourceforge.cobertura.util.CommandLineBuilder

public class CommandLineBuilder
extends java.lang.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
private  java.io.File commandLineFile
           
private  java.io.FileWriter commandLineWriter
           
private static java.lang.String LINESEP
           
private static org.apache.log4j.Logger logger
           
 
Constructor Summary
CommandLineBuilder()
          Creates a new instance of the builder.
 
Method Summary
 void addArg(java.lang.String arg)
          Adds command line argument.
 void addArg(java.lang.String arg1, java.lang.String arg2)
          Adds two command line arguments.
 void dispose()
          Explicity frees all resources associated with this instance.
 java.lang.String getCommandLineFile()
          Gets absolute path to the file with saved arguments.
static java.lang.String[] preprocessCommandLineArguments(java.lang.String[] args)
          Loads arguments from file if --commandsfile option is used.
 void saveArgs()
          Saves options and made file available to use.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private static final org.apache.log4j.Logger logger

LINESEP

private static final java.lang.String LINESEP

commandLineFile

private java.io.File commandLineFile

commandLineWriter

private java.io.FileWriter commandLineWriter
Constructor Detail

CommandLineBuilder

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

Throws:
java.io.IOException - if problems with creating temporary file for storing command line occur
Method Detail

addArg

public void addArg(java.lang.String arg)
            throws java.io.IOException
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:
java.io.IOException - if problems with temporary file occur
java.lang.NullPointerException - if arg is null

addArg

public void addArg(java.lang.String arg1,
                   java.lang.String arg2)
            throws java.io.IOException
Adds two command line arguments. Convienience function, calls addArg(String) two times.

Parameters:
arg1 - first command line argument to save
arg2 - second command line argument to save
Throws:
java.io.IOException - if problems with temporary file occur
java.lang.NullPointerException - if any arg is null

saveArgs

public void saveArgs()
              throws java.io.IOException
Saves options and made file available to use. Use method getCommandLineFile() to get the file the arguments are saved in.

Throws:
java.io.IOException - if problems with temporary file occur

getCommandLineFile

public java.lang.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 saveArgs() method.

Returns:
absolute path to the file with arguments

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.


preprocessCommandLineArguments

public static java.lang.String[] preprocessCommandLineArguments(java.lang.String[] args)
                                                         throws java.io.IOException
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:
java.lang.NullPointerException - if args is null, or any argument is null
java.lang.IllegalArgumentException - if --commandsfile is specified as last option
java.io.IOException - if I/O related error with temporary command line file occur