View Javadoc

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: 2008/06/25 20:46:43 $ 
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.27 2008/06/25 20:46:43 benoitx Exp $
34   */
35  public class CommandLineParser {
36  
37      private final String[] argsArray;
38      private final List args = new ArrayList();
39      private int argCount = 0;
40      private boolean givenCss = false;
41  
42      /**
43       * Constructor for CommandLineParser
44       * 
45       * @param args the command line parameters
46       */
47      public CommandLineParser(final String[] args) {
48          argsArray = args;
49      }
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          for (int i = 0; i < argsArray.length; i++) {
59              args.add(argsArray[i]);
60          }
61          while (!args.isEmpty()) {
62              final String currentArg = popNextArg();
63              if (currentArg.startsWith("-")) {
64                  parseSwitch(currentArg.substring(1));
65              } else {
66                  parseArgument(currentArg);
67              }
68          }
69          checkForRequiredArgs();
70      }
71  
72      protected String popNextArg() {
73          return (String) args.remove(0);
74      }
75  
76      protected void parseSwitch(final String switchName) throws ConfigurationException {
77          final String s = switchName.toLowerCase();
78          if (s.equals("css")) {
79              if (args.isEmpty()) {
80                  throw new ConfigurationException("Missing argument for -css");
81              }
82              ConfigurationOptions.setCssFile(popNextArg());
83              givenCss = true;
84          } else if (s.equals("output-dir")) {
85              if (args.isEmpty()) {
86                  throw new ConfigurationException("Missing argument for -output-dir");
87              }
88              ConfigurationOptions.setOutputDir(popNextArg());
89          } else if (s.equals("verbose")) {
90              ConfigurationOptions.setVerboseLogging();
91          } else if (s.equals("debug")) {
92              ConfigurationOptions.setDebugLogging();
93          } else if (s.equals("notes")) {
94              if (args.isEmpty()) {
95                  throw new ConfigurationException("Missing argument for -notes");
96              }
97              ConfigurationOptions.setNotesFile(popNextArg());
98          } else if (s.equals("viewcvs")) {
99              if (args.isEmpty()) {
100                 throw new ConfigurationException("Missing argument for -viewcvs");
101             }
102             ConfigurationOptions.setViewCvsURL(popNextArg());
103         } else if (s.equals("viewvc")) {
104             if (args.isEmpty()) {
105                 throw new ConfigurationException("Missing argument for -viewvc");
106             }
107             ConfigurationOptions.setViewVcURL(popNextArg());
108         } else if (s.equals("trac")) {
109             if (args.isEmpty()) {
110                 throw new ConfigurationException("Missing argument for -trac");
111             }
112             ConfigurationOptions.setViewTracURL(popNextArg());
113         } else if (s.equals("cvsweb")) {
114             if (args.isEmpty()) {
115                 throw new ConfigurationException("Missing argument for -cvsweb");
116             }
117             ConfigurationOptions.setCvswebURL(popNextArg());
118         } else if (s.equals("chora")) {
119             if (args.isEmpty()) {
120                 throw new ConfigurationException("Missing argument for -chora");
121             }
122             ConfigurationOptions.setChoraURL(popNextArg());
123         } else if (s.equals("jcvsweb")) {
124             if (args.isEmpty()) {
125                 throw new ConfigurationException("Missing argument for -jcvsweb");
126             }
127             ConfigurationOptions.setJCVSWebURL(popNextArg());
128         } else if (s.equals("include")) {
129             if (args.isEmpty()) {
130                 throw new ConfigurationException("Missing argument for -include");
131             }
132             ConfigurationOptions.setIncludePattern(popNextArg());
133         } else if (s.equals("exclude")) {
134             if (args.isEmpty()) {
135                 throw new ConfigurationException("Missing argument for -exclude");
136             }
137             ConfigurationOptions.setExcludePattern(popNextArg());
138         } else if (s.equals("title")) {
139             if (args.isEmpty()) {
140                 throw new ConfigurationException("Missing argument for -title");
141             }
142             ConfigurationOptions.setProjectName(popNextArg());
143         } else if (s.equals("tags")) {
144             if (args.isEmpty()) {
145                 throw new ConfigurationException("Missing argument for -tags");
146             }
147             ConfigurationOptions.setSymbolicNamesPattern(popNextArg());
148         } else if (s.equals("bugzilla")) {
149             if (args.isEmpty()) {
150                 throw new ConfigurationException("Missing argument for -bugzilla");
151             }
152             ConfigurationOptions.setBugzillaUrl(popNextArg());
153         } else if (s.equals("mantis")) {
154             if (args.isEmpty()) {
155                 throw new ConfigurationException("Missing argument for -mantis");
156             }
157             ConfigurationOptions.setMantisUrl(popNextArg());
158         } else if (s.equals("config-file")) {
159             if (args.isEmpty()) {
160                 throw new ConfigurationException("Missing argument for -config-file");
161             }
162             ConfigurationOptions.setConfigFile(popNextArg());
163         } else if (s.equals("charset")) {
164             if (isArgsEmpty()) {
165                 throw new ConfigurationException("Missing argument for -charset");
166             }
167             ConfigurationOptions.setCharSet(popNextArg());
168         } else if (s.equals("xdoc")) {
169             ConfigurationOptions.setOutputFormat("xdoc");
170             if (!this.givenCss) {
171                 // set the default to the XDOC css.
172                 ConfigurationOptions.setDefaultCssFile("objectlab-statcvs-xdoc.css");
173             }
174         } else if (s.equals("xml")) {
175             /*
176              * edited by Nilendra
177              * test for the input parameter '-xml'
178              */
179             ConfigurationOptions.setOutputFormat("xml");
180         } else if (s.equals("format")) { // This is undocumented in StatCVS, is for StatSVN compatibility
181             if (args.isEmpty()) {
182                 throw new ConfigurationException("Missing argument for -format");
183             }
184             final String format = popNextArg();
185             if ("xdoc".equals(format) && !givenCss) {
186                 // set the default to the XDOC css.
187                 ConfigurationOptions.setDefaultCssFile("objectlab-statcvs-xdoc.css");
188             }
189             ConfigurationOptions.setOutputFormat(format);
190         } else if (s.equals("no-developer")) {
191             if (args.isEmpty()) {
192                 throw new ConfigurationException("Missing argument for -no-developer");
193             }
194             ConfigurationOptions.addNonDeveloperLogin(popNextArg());
195         } else if (!doChildrenSwitch(s)) {
196             throw new ConfigurationException("Unrecognized option -" + s);
197         }
198     }
199 
200     /**
201      * Give the argument to children classes that may have a way to recognise it.
202      * @param s
203      * @return
204      */
205     protected boolean doChildrenSwitch(final String s) throws ConfigurationException {
206         return false;
207     }
208 
209     private void parseArgument(final String arg) throws ConfigurationException {
210         argCount++;
211         switch (argCount) {
212         case 1:
213             ConfigurationOptions.setLogFileName(arg);
214             break;
215         case 2:
216             ConfigurationOptions.setCheckedOutDirectory(arg);
217             break;
218         default:
219             throw new ConfigurationException("Too many arguments");
220         }
221     }
222 
223     protected void checkForRequiredArgs() throws ConfigurationException {
224         switch (argCount) {
225         case 0:
226             throw new ConfigurationException("Not enough arguments - <logfile> is missing");
227         case 1:
228             throw new ConfigurationException("Not enough arguments - <directory> is missing");
229         }
230     }
231 
232     protected int getArgCount() {
233         return argCount;
234     }
235 
236     protected boolean isArgsEmpty() {
237         return args.isEmpty();
238     }
239 }