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: 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      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 (args.isEmpty()) {
165                 throw new ConfigurationException("Missing argument for -charset");
166             }
167             ConfigurationOptions.setCharSet(popNextArg());
168         } else if (s.equals("headerurl")) {
169             if (args.isEmpty()) {
170                 throw new ConfigurationException("Missing argument for -headerUrl");
171             }
172             ConfigurationOptions.setHeaderUrl(popNextArg());
173         } else if (s.equals("footerurl")) {
174             if (args.isEmpty()) {
175                 throw new ConfigurationException("Missing argument for -footerUrl");
176             }
177             ConfigurationOptions.setFooterUrl(popNextArg());
178         } else if (s.equals("xdoc")) {
179             ConfigurationOptions.setOutputFormat("xdoc");
180             if (!this.givenCss) {
181                 // set the default to the XDOC css.
182                 ConfigurationOptions.setDefaultCssFile("objectlab-statcvs-xdoc.css");
183             }
184         } else if (s.equals("disable-twitter-button")) {
185             ConfigurationOptions.setEnableTwitterButton(false);
186         } else if (s.equals("xml")) {
187             /*
188              * edited by Nilendra
189              * test for the input parameter '-xml'
190              */
191             ConfigurationOptions.setOutputFormat("xml");
192         } else if (s.equals("format")) { // This is undocumented in StatCVS, is for StatSVN compatibility
193             if (args.isEmpty()) {
194                 throw new ConfigurationException("Missing argument for -format");
195             }
196             final String format = popNextArg();
197             if ("xdoc".equals(format) && !givenCss) {
198                 // set the default to the XDOC css.
199                 ConfigurationOptions.setDefaultCssFile("objectlab-statcvs-xdoc.css");
200             }
201             ConfigurationOptions.setOutputFormat(format);
202         } else if (s.equals("no-developer")) {
203             if (args.isEmpty()) {
204                 throw new ConfigurationException("Missing argument for -no-developer");
205             }
206             ConfigurationOptions.addNonDeveloperLogin(popNextArg());
207         } else if (!doChildrenSwitch(s)) {
208             throw new ConfigurationException("Unrecognized option -" + s);
209         }
210     }
211 
212     /**
213      * Give the argument to children classes that may have a way to recognise it.
214      * @param s
215      * @return
216      */
217     protected boolean doChildrenSwitch(final String s) throws ConfigurationException {
218         return false;
219     }
220 
221     private void parseArgument(final String arg) throws ConfigurationException {
222         argCount++;
223         switch (argCount) {
224         case 1:
225             ConfigurationOptions.setLogFileName(arg);
226             break;
227         case 2:
228             ConfigurationOptions.setCheckedOutDirectory(arg);
229             break;
230         default:
231             throw new ConfigurationException("Too many arguments");
232         }
233     }
234 
235     protected void checkForRequiredArgs() throws ConfigurationException {
236         switch (argCount) {
237         case 0:
238             throw new ConfigurationException("Not enough arguments - <logfile> is missing");
239         case 1:
240             throw new ConfigurationException("Not enough arguments - <directory> is missing");
241         }
242     }
243 
244     protected int getArgCount() {
245         return argCount;
246     }
247 
248     protected boolean isArgsEmpty() {
249         return args.isEmpty();
250     }
251 }