Coverage Report - net.sf.statcvs.output.CommandLineParser
 
Classes in this File Line Coverage Branch Coverage Complexity
CommandLineParser
0%
0/166
0%
0/174
10.111
 
 1  
 /*
 2  
     StatCvs - CVS statistics generation 
 3  
     Copyright (C) 2002  Lukasz Pekacki <lukasz@pekacki.de>
 4  
     http://statcvs.sf.net/
 5  
     
 6  
     This library is free software; you can redistribute it and/or
 7  
     modify it under the terms of the GNU Lesser General Public
 8  
     License as published by the Free Software Foundation; either
 9  
     version 2.1 of the License, or (at your option) any later version.
 10  
 
 11  
     This library is distributed in the hope that it will be useful,
 12  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14  
     Lesser General Public License for more details.
 15  
 
 16  
     You should have received a copy of the GNU Lesser General Public
 17  
     License along with this library; if not, write to the Free Software
 18  
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 19  
     
 20  
         $RCSfile: CommandLineParser.java,v $
 21  
         Created on $Date: 2009/08/19 22:11:15 $ 
 22  
 */
 23  
 package net.sf.statcvs.output;
 24  
 
 25  
 import java.util.ArrayList;
 26  
 import java.util.List;
 27  
 
 28  
 /**
 29  
  * Takes a command line, like given to the {@link net.sf.statcvs.Main#main} method,
 30  
  * and turns it into a {@link ConfigurationOptions} object.
 31  
  * 
 32  
  * @author Richard Cyganiak <rcyg@gmx.de>
 33  
  * @version $Id: CommandLineParser.java,v 1.29 2009/08/19 22:11:15 benoitx Exp $
 34  
  */
 35  
 public class CommandLineParser {
 36  
 
 37  
     private final String[] argsArray;
 38  0
     private final List args = new ArrayList();
 39  0
     private int argCount = 0;
 40  0
     private boolean givenCss = false;
 41  
 
 42  
     /**
 43  
      * Constructor for CommandLineParser
 44  
      * 
 45  
      * @param args the command line parameters
 46  
      */
 47  0
     public CommandLineParser(final String[] args) {
 48  0
         argsArray = args;
 49  0
     }
 50  
 
 51  
     /**
 52  
      * Parses the command line and sets the options (as static
 53  
      * fields in {@link ConfigurationOptions}).
 54  
      * 
 55  
      * @throws ConfigurationException if errors are present on the command line
 56  
      */
 57  
     public void parse() throws ConfigurationException {
 58  0
         for (int i = 0; i < argsArray.length; i++) {
 59  0
             args.add(argsArray[i]);
 60  
         }
 61  0
         while (!args.isEmpty()) {
 62  0
             final String currentArg = popNextArg();
 63  0
             if (currentArg.startsWith("-")) {
 64  0
                 parseSwitch(currentArg.substring(1));
 65  
             } else {
 66  0
                 parseArgument(currentArg);
 67  
             }
 68  0
         }
 69  0
         checkForRequiredArgs();
 70  0
     }
 71  
 
 72  
     protected String popNextArg() {
 73  0
         return (String) args.remove(0);
 74  
     }
 75  
 
 76  
     protected void parseSwitch(final String switchName) throws ConfigurationException {
 77  0
         final String s = switchName.toLowerCase();
 78  0
         if (s.equals("css")) {
 79  0
             if (args.isEmpty()) {
 80  0
                 throw new ConfigurationException("Missing argument for -css");
 81  
             }
 82  0
             ConfigurationOptions.setCssFile(popNextArg());
 83  0
             givenCss = true;
 84  0
         } else if (s.equals("output-dir")) {
 85  0
             if (args.isEmpty()) {
 86  0
                 throw new ConfigurationException("Missing argument for -output-dir");
 87  
             }
 88  0
             ConfigurationOptions.setOutputDir(popNextArg());
 89  0
         } else if (s.equals("verbose")) {
 90  0
             ConfigurationOptions.setVerboseLogging();
 91  0
         } else if (s.equals("debug")) {
 92  0
             ConfigurationOptions.setDebugLogging();
 93  0
         } else if (s.equals("notes")) {
 94  0
             if (args.isEmpty()) {
 95  0
                 throw new ConfigurationException("Missing argument for -notes");
 96  
             }
 97  0
             ConfigurationOptions.setNotesFile(popNextArg());
 98  0
         } else if (s.equals("viewcvs")) {
 99  0
             if (args.isEmpty()) {
 100  0
                 throw new ConfigurationException("Missing argument for -viewcvs");
 101  
             }
 102  0
             ConfigurationOptions.setViewCvsURL(popNextArg());
 103  0
         } else if (s.equals("viewvc")) {
 104  0
             if (args.isEmpty()) {
 105  0
                 throw new ConfigurationException("Missing argument for -viewvc");
 106  
             }
 107  0
             ConfigurationOptions.setViewVcURL(popNextArg());
 108  0
         } else if (s.equals("trac")) {
 109  0
             if (args.isEmpty()) {
 110  0
                 throw new ConfigurationException("Missing argument for -trac");
 111  
             }
 112  0
             ConfigurationOptions.setViewTracURL(popNextArg());
 113  0
         } else if (s.equals("cvsweb")) {
 114  0
             if (args.isEmpty()) {
 115  0
                 throw new ConfigurationException("Missing argument for -cvsweb");
 116  
             }
 117  0
             ConfigurationOptions.setCvswebURL(popNextArg());
 118  0
         } else if (s.equals("chora")) {
 119  0
             if (args.isEmpty()) {
 120  0
                 throw new ConfigurationException("Missing argument for -chora");
 121  
             }
 122  0
             ConfigurationOptions.setChoraURL(popNextArg());
 123  0
         } else if (s.equals("jcvsweb")) {
 124  0
             if (args.isEmpty()) {
 125  0
                 throw new ConfigurationException("Missing argument for -jcvsweb");
 126  
             }
 127  0
             ConfigurationOptions.setJCVSWebURL(popNextArg());
 128  0
         } else if (s.equals("include")) {
 129  0
             if (args.isEmpty()) {
 130  0
                 throw new ConfigurationException("Missing argument for -include");
 131  
             }
 132  0
             ConfigurationOptions.setIncludePattern(popNextArg());
 133  0
         } else if (s.equals("exclude")) {
 134  0
             if (args.isEmpty()) {
 135  0
                 throw new ConfigurationException("Missing argument for -exclude");
 136  
             }
 137  0
             ConfigurationOptions.setExcludePattern(popNextArg());
 138  0
         } else if (s.equals("title")) {
 139  0
             if (args.isEmpty()) {
 140  0
                 throw new ConfigurationException("Missing argument for -title");
 141  
             }
 142  0
             ConfigurationOptions.setProjectName(popNextArg());
 143  0
         } else if (s.equals("tags")) {
 144  0
             if (args.isEmpty()) {
 145  0
                 throw new ConfigurationException("Missing argument for -tags");
 146  
             }
 147  0
             ConfigurationOptions.setSymbolicNamesPattern(popNextArg());
 148  0
         } else if (s.equals("bugzilla")) {
 149  0
             if (args.isEmpty()) {
 150  0
                 throw new ConfigurationException("Missing argument for -bugzilla");
 151  
             }
 152  0
             ConfigurationOptions.setBugzillaUrl(popNextArg());
 153  0
         } else if (s.equals("mantis")) {
 154  0
             if (args.isEmpty()) {
 155  0
                 throw new ConfigurationException("Missing argument for -mantis");
 156  
             }
 157  0
             ConfigurationOptions.setMantisUrl(popNextArg());
 158  0
         } else if (s.equals("config-file")) {
 159  0
             if (args.isEmpty()) {
 160  0
                 throw new ConfigurationException("Missing argument for -config-file");
 161  
             }
 162  0
             ConfigurationOptions.setConfigFile(popNextArg());
 163  0
         } else if (s.equals("charset")) {
 164  0
             if (args.isEmpty()) {
 165  0
                 throw new ConfigurationException("Missing argument for -charset");
 166  
             }
 167  0
             ConfigurationOptions.setCharSet(popNextArg());
 168  0
         } else if (s.equals("headerurl")) {
 169  0
             if (args.isEmpty()) {
 170  0
                 throw new ConfigurationException("Missing argument for -headerUrl");
 171  0
             }
 172  0
             ConfigurationOptions.setHeaderUrl(popNextArg());
 173  0
         } else if (s.equals("footerurl")) {
 174  0
             if (args.isEmpty()) {
 175  0
                 throw new ConfigurationException("Missing argument for -footerUrl");
 176  0
             }
 177  0
             ConfigurationOptions.setFooterUrl(popNextArg());
 178  0
         } else if (s.equals("xdoc")) {
 179  0
             ConfigurationOptions.setOutputFormat("xdoc");
 180  0
             if (!this.givenCss) {
 181  0
                 // set the default to the XDOC css.
 182  0
                 ConfigurationOptions.setDefaultCssFile("objectlab-statcvs-xdoc.css");
 183  0
             }
 184  0
         } else if (s.equals("disable-twitter-button")) {
 185  0
             ConfigurationOptions.setEnableTwitterButton(false);
 186  0
         } else if (s.equals("xml")) {
 187  0
             /*
 188  0
              * edited by Nilendra
 189  0
              * test for the input parameter '-xml'
 190  0
              */
 191  0
             ConfigurationOptions.setOutputFormat("xml");
 192  0
         } else if (s.equals("format")) { // This is undocumented in StatCVS, is for StatSVN compatibility
 193  0
             if (args.isEmpty()) {
 194  0
                 throw new ConfigurationException("Missing argument for -format");
 195  0
             }
 196  0
             final String format = popNextArg();
 197  0
             if ("xdoc".equals(format) && !givenCss) {
 198  0
                 // set the default to the XDOC css.
 199  0
                 ConfigurationOptions.setDefaultCssFile("objectlab-statcvs-xdoc.css");
 200  0
             }
 201  0
             ConfigurationOptions.setOutputFormat(format);
 202  0
         } else if (s.equals("no-developer")) {
 203  0
             if (args.isEmpty()) {
 204  0
                 throw new ConfigurationException("Missing argument for -no-developer");
 205  0
             }
 206  0
             ConfigurationOptions.addNonDeveloperLogin(popNextArg());
 207  0
         } else if (!doChildrenSwitch(s)) {
 208  0
             throw new ConfigurationException("Unrecognized option -" + s);
 209  0
         }
 210  0
     }
 211  0
 
 212  0
     /**
 213  0
      * Give the argument to children classes that may have a way to recognise it.
 214  0
      * @param s
 215  0
      * @return
 216  0
      */
 217  0
     protected boolean doChildrenSwitch(final String s) throws ConfigurationException {
 218  0
         return false;
 219  0
     }
 220  0
 
 221  0
     private void parseArgument(final String arg) throws ConfigurationException {
 222  0
         argCount++;
 223  0
         switch (argCount) {
 224  0
         case 1:
 225  0
             ConfigurationOptions.setLogFileName(arg);
 226  0
             break;
 227  0
         case 2:
 228  0
             ConfigurationOptions.setCheckedOutDirectory(arg);
 229  0
             break;
 230  0
         default:
 231  0
             throw new ConfigurationException("Too many arguments");
 232  0
         }
 233  0
     }
 234  
 
 235  0
     protected void checkForRequiredArgs() throws ConfigurationException {
 236  0
         switch (argCount) {
 237  0
         case 0:
 238  0
             throw new ConfigurationException("Not enough arguments - <logfile> is missing");
 239  0
         case 1:
 240  0
             throw new ConfigurationException("Not enough arguments - <directory> is missing");
 241  
         }
 242  0
     }
 243  
 
 244  
     protected int getArgCount() {
 245  0
         return argCount;
 246  
     }
 247  
 
 248  
     protected boolean isArgsEmpty() {
 249  0
         return args.isEmpty();
 250  
     }
 251  
 }