001    package org.hackystat.sensor.xmldata.option;
002    
003    import java.io.BufferedReader;
004    import java.io.File;
005    import java.io.FileNotFoundException;
006    import java.io.FileReader;
007    import java.io.IOException;
008    import java.io.Reader;
009    import java.util.ArrayList;
010    import java.util.List;
011    import java.util.StringTokenizer;
012    
013    import org.hackystat.sensor.xmldata.XmlDataController;
014    
015    /**
016     * The option used when specifying the command-line arguments via a text file.
017     * @author aito
018     * 
019     */
020    public class ArgListOption extends AbstractOption {
021      /** The name of this option, which is "-argList". */
022      public static final String OPTION_NAME = "-argList";
023    
024      /**
025       * Creates this option with the specified controller and parameters.
026       * @param controller the specified controller.
027       * @param parameters the specified parameters.
028       */
029      public ArgListOption(XmlDataController controller, List<String> parameters) {
030        super(controller, OPTION_NAME, parameters);
031      }
032    
033      /**
034       * Returns true if the specified parameters contains only one element, which
035       * is a valid text file containing the list of command-line arguments.
036       * @return true if the parameters are valid, false if not.
037       */
038      @Override
039      public boolean isValid() {
040        if (this.getParameters().isEmpty() || this.getParameters().size() > 1) {
041          String msg = "The -argList option only accepts one parameter, which "
042              + "is the file containing the command-line arguments.";
043          this.getController().fireMessage(msg);
044          return false;
045        }
046        else {
047          File paramFile = new File(this.getParameters().get(0));
048          if (!paramFile.exists()) {
049            String msg = "The specified file, " + this.getParameters().get(0) + ", does not exist.";
050            this.getController().fireMessage(msg);
051            return false;
052          }
053        }
054        return true;
055      }
056    
057      /** Executes this option using the specified argument list file. */
058      @Override
059      public void execute() {
060        List<String> arguments = new ArrayList<String>();
061        Reader fileReader = null;
062        try {
063          fileReader = new FileReader(this.getParameters().get(0));
064          BufferedReader bufferedReader = new BufferedReader(fileReader);
065          String argumentLine = bufferedReader.readLine();
066          String argumentString = "";
067          // Compiles a string of the entire file.
068          while (argumentLine != null) {
069            argumentString = argumentString.concat(argumentLine + " ");
070            argumentLine = bufferedReader.readLine();
071          }
072    
073          StringTokenizer tokenizer = new StringTokenizer(argumentString);
074          while (tokenizer.hasMoreTokens()) {
075            arguments.add(tokenizer.nextToken());
076          }
077          bufferedReader.close();
078    
079          // Then, process the args string and execute the new list of arguments.
080          this.getController().processArguments(arguments);
081          this.getController().execute();
082        }
083        catch (FileNotFoundException e) {
084          String msg = "The file, " + this.getParameters().get(0) + ", could not be found.";
085          this.getController().fireMessage(msg, e.toString());
086        }
087        catch (IOException e) {
088          String msg = "The file, " + this.getParameters().get(0) + ", could not be accessed.";
089          this.getController().fireMessage(msg, e.toString());
090        }
091        finally {
092          try {
093            fileReader.close();
094          }
095          catch (Exception e) {
096            throw new RuntimeException("Failed to clean up fileReader.", e);
097          }
098        }
099      }
100    }