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: ConfigurationOptions.java,v $
21  	$Date: 2008/04/17 15:02:01 $ 
22  */
23  package net.sf.statcvs.output;
24  
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.FileInputStream;
28  import java.io.FileNotFoundException;
29  import java.io.FileReader;
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.net.MalformedURLException;
33  import java.net.URL;
34  import java.util.ArrayList;
35  import java.util.Collection;
36  import java.util.Properties;
37  import java.util.regex.Pattern;
38  import java.util.regex.PatternSyntaxException;
39  
40  import net.sf.statcvs.pages.MarkupSyntax;
41  import net.sf.statcvs.util.FilePatternMatcher;
42  import net.sf.statcvs.util.FileUtils;
43  import net.sf.statcvs.weblinks.bugs.BugTracker;
44  import net.sf.statcvs.weblinks.bugs.Bugzilla;
45  import net.sf.statcvs.weblinks.bugs.Mantis;
46  
47  /**
48   * Class for storing all command line parameters. The parameters
49   * are set by the {@link net.sf.statcvs.Main#main} method. Interested classes
50   * can read all parameter values from here.
51   * 
52   * TODO: Should be moved to more appropriate package and made non-public
53   * 
54   * @author jentzsch
55   * @version $Id: ConfigurationOptions.java,v 1.33 2008/04/17 15:02:01 benoitx Exp $
56   */
57  public class ConfigurationOptions {
58  
59      private static final String LOGGING_CONFIG_DEFAULT = "logging.properties";
60      private static final String LOGGING_CONFIG_VERBOSE = "logging-verbose.properties";
61      private static final String LOGGING_CONFIG_DEBUG = "logging-debug.properties";
62  
63      private static String logFileName = null;
64      private static String checkedOutDirectory = null;
65      private static String projectName = null;
66      private static String outputDir = "";
67      private static String loggingProperties = LOGGING_CONFIG_DEFAULT;
68      private static String notesFile = null;
69      private static String notes = null;
70  
71      private static FilePatternMatcher includePattern = null;
72      private static FilePatternMatcher excludePattern = null;
73  
74      private static Collection nonDeveloperLogins = new ArrayList();
75  
76      private static CssHandler cssHandler = new DefaultCssHandler("statcvs.css");
77      private static WebRepositoryIntegration webRepository = null;
78      private static Pattern symbolicNamesPattern;
79  
80      private static BugTracker webBugTracker = BugTracker.NO_BUG_TRACKER;
81      private static String outputFormat = "html";
82      private static Properties properties = new Properties();
83  
84      /**
85       * returns the {@link CssHandler}
86       * @return the CssHandler
87       */
88      public static CssHandler getCssHandler() {
89          return cssHandler;
90      }
91  
92      /**
93       * Method getProjectName.
94       * @return String name of the project
95       */
96      public static String getProjectName() {
97          return projectName;
98      }
99  
100     /**
101      * Method getCheckedOutDirectory.
102      * @return String name of the checked out directory
103      */
104     public static String getCheckedOutDirectory() {
105         return checkedOutDirectory;
106     }
107 
108     /**
109      * Method getLogfilename.
110      * @return String name of the logfile to be parsed
111      */
112     public static String getLogFileName() {
113         return logFileName;
114     }
115 
116     /**
117      * Returns the outputDir.
118      * @return String output Directory
119      */
120     public static String getOutputDir() {
121         return outputDir;
122     }
123 
124     /**
125      * Returns the report notes (from "-notes filename" switch) or <tt>null</tt>
126      * if not specified
127      * @return the report notes
128      */
129     public static String getNotes() {
130         return notes;
131     }
132 
133     /**
134      * Returns a {@link WebRepositoryIntegration} object if the user
135      * has specified a URL to one. <tt>null</tt> otherwise.
136      * @return the web repository
137      */
138     public static WebRepositoryIntegration getWebRepository() {
139         return webRepository;
140     }
141 
142     /**
143      * Sets the checkedOutDirectory.
144      * @param checkedOutDirectory The checkedOutDirectory to set
145      * @throws ConfigurationException if directory does not exist
146      */
147     public static void setCheckedOutDirectory(final String checkedOutDirectory) throws ConfigurationException {
148         final File directory = new File(checkedOutDirectory);
149         if (!directory.exists() || !directory.isDirectory()) {
150             throw new ConfigurationException("directory does not exist: " + checkedOutDirectory);
151         }
152         ConfigurationOptions.checkedOutDirectory = checkedOutDirectory;
153     }
154 
155     /**
156      * Sets the cssFile. Currently, the css file can be any local file or
157      * a HTTP URL. If it is a local file, a copy will be included in the
158      * output directory. If this method is never called, a default CSS file
159      * will be generated in the output directory.
160      *
161      * @param cssFile The cssFile to set
162      * @throws ConfigurationException if the specified CSS file can not be
163      * accessed from local file system or from URL source, or if the specified
164      * CSS file is local and does not exist
165      */
166     public static void setCssFile(final String cssFile) throws ConfigurationException {
167         try {
168             final URL url = new URL(cssFile);
169             if (!url.getProtocol().equals("http")) {
170                 throw new ConfigurationException("Only HTTP URLs or local files allowed for -css");
171             }
172             cssHandler = new UrlCssHandler(url);
173         } catch (final MalformedURLException isLocalFile) {
174             cssHandler = new LocalFileCssHandler(cssFile);
175         }
176         cssHandler.checkForMissingResources();
177     }
178 
179     /**
180      * Sets the logFileName.
181      * @param logFileName The logFileName to set
182      * @throws ConfigurationException if the file does not exist
183      */
184     public static void setLogFileName(final String logFileName) throws ConfigurationException {
185         final File inputFile = new File(logFileName);
186         if (!inputFile.exists()) {
187             throw new ConfigurationException("Specified logfile not found: " + logFileName);
188         }
189         ConfigurationOptions.logFileName = logFileName;
190     }
191 
192     /**
193      * Sets the outputDir.
194      * @param outputDir The outputDir to set
195      * @throws ConfigurationException if the output directory cannot be created
196      */
197     public static void setOutputDir(String outputDir) throws ConfigurationException {
198         if (!outputDir.endsWith(FileUtils.getDirSeparator())) {
199             outputDir += FileUtils.getDefaultDirSeparator();
200         }
201         final File outDir = new File(outputDir);
202         if (!outDir.exists() && !outDir.mkdirs()) {
203             throw new ConfigurationException("Can't create output directory: " + outputDir);
204         }
205         ConfigurationOptions.outputDir = outputDir;
206     }
207 
208     /**
209      * Sets the name of the notes file. The notes file will be included
210      * on the index page of the output. It must contain a valid
211      * block-level HTML fragment (for example
212      * <tt>"&lt;p&gt;Some notes&lt;/p&gt;"</tt>) 
213      * @param notesFile a local filename
214      * @throws ConfigurationException if the file is not found or can't be read
215      */
216     public static void setNotesFile(final String notesFile) throws ConfigurationException {
217         final File f = new File(notesFile);
218         if (!f.exists()) {
219             throw new ConfigurationException("Notes file not found: " + notesFile);
220         }
221         if (!f.canRead()) {
222             throw new ConfigurationException("Can't read notes file: " + notesFile);
223         }
224         ConfigurationOptions.notesFile = notesFile;
225         try {
226             notes = readNotesFile();
227         } catch (final IOException e) {
228             throw new ConfigurationException(e.getMessage());
229         }
230     }
231 
232     /**
233      * Sets the URL to a <a href="http://viewcvs.sourceforge.net/">ViewCVS</a>
234      * web-based CVS browser. This must be the URL at which the checked-out
235      * module's root can be viewed in ViewCVS.
236      * @param url URL to a ViewCVS repository
237      */
238     public static void setViewCvsURL(final String url) {
239         ConfigurationOptions.webRepository = new ViewCvsIntegration(url);
240     }
241 
242     /**
243      * Sets the URL to a 
244      * <a href="http://www.freebsd.org/projects/cvsweb.html">cvsweb</a>
245      * web-based CVS browser. This must be the URL at which the checked-out
246      * module's root can be viewed in cvsweb.
247      * @param url URL to a cvsweb repository
248      */
249     public static void setCvswebURL(final String url) {
250         ConfigurationOptions.webRepository = new CvswebIntegration(url);
251     }
252 
253     /**
254      * Sets the URL to a <a href="http://www.horde.org/chora/">Chora</a>
255      * web-based CVS browser. This must be the URL at which the checked-out
256      * module's root can be viewed in Chora.
257      * @param url URL to a cvsweb repository
258      */
259     public static void setChoraURL(final String url) {
260         ConfigurationOptions.webRepository = new ChoraIntegration(url);
261     }
262 
263     /**
264      * Sets the URL to a <a href="http://www.jcvs.org/jcvsweb/">JCVSWeb</a>
265      * web-based CVS browser. This must be the URL at which the checked-out
266      * module's root can be viewed in JCVSWeb.
267      * @param url URL to a JCVSWeb repository
268      */
269     public static void setJCVSWebURL(final String url) {
270         ConfigurationOptions.webRepository = new JCVSWebIntegration(url);
271     }
272 
273     /**
274      * Sets the URL to a <a href="http://www.viewvc.org/">ViewVC</a> web-based CVS/SVN browser. 
275      * This must be the URL at which the checked-out module's
276      * root can be viewed in ViewVC.
277      * 
278      * @param url
279      *            URL to a ViewVC repository
280      */
281     public static void setViewVcURL(final String url) {
282         ConfigurationOptions.webRepository = new ViewVcIntegration(url);
283     }
284 
285     /**
286      * Sets the URL to a <a href="http://trac.edgewall.org/wiki/TracBrowser">Trac</a> web-based SVN browser and issue tracking. 
287      * This must be the URL at which the checked-out module's
288      * root can be viewed in Trac 
289      * 
290      * @param url
291      *            URL to a Trac website
292      */
293     public static void setViewTracURL(final String url) {
294         ConfigurationOptions.webRepository = new TracIntegration(url);
295     }
296 
297     /**
298      * Sets a project title to be used in the reports
299      * @param projectName The project title to be used in the reports
300      */
301     public static void setProjectName(final String projectName) {
302         ConfigurationOptions.projectName = projectName;
303     }
304 
305     /**
306      * Gets the name of the logging properties file
307      * @return the name of the logging properties file
308      */
309     public static String getLoggingProperties() {
310         return loggingProperties;
311     }
312 
313     /**
314      * Sets the logging level to verbose
315      */
316     public static void setVerboseLogging() {
317         ConfigurationOptions.loggingProperties = LOGGING_CONFIG_VERBOSE;
318     }
319 
320     /**
321      * Sets the logging level to debug
322      */
323     public static void setDebugLogging() {
324         ConfigurationOptions.loggingProperties = LOGGING_CONFIG_DEBUG;
325     }
326 
327     private static String readNotesFile() throws IOException {
328         BufferedReader r = null;
329         final StringBuffer result = new StringBuffer();
330         try {
331             r = new BufferedReader(new FileReader(notesFile));
332             String line = r.readLine();
333             while (line != null) {
334                 result.append(line);
335                 line = r.readLine();
336             }
337         } finally {
338             if (r != null) {
339                 r.close();
340             }
341         }
342         return result.toString();
343     }
344 
345     /**
346      * Sets a file include pattern list. Only files matching one of the
347      * patterns will be included in the analysis.
348      * @param patternList a list of Ant-style wildcard patterns, seperated
349      *                    by : or ;
350      * @see net.sf.statcvs.util.FilePatternMatcher
351      */
352     public static void setIncludePattern(final String patternList) {
353         includePattern = new FilePatternMatcher(patternList);
354     }
355 
356     /**
357      * Sets a file exclude pattern list. Files matching any of the
358      * patterns will be excluded from the analysis.
359      * @param patternList a list of Ant-style wildcard patterns, seperated
360      *                    by : or ;
361      * @see net.sf.statcvs.util.FilePatternMatcher
362      */
363     public static void setExcludePattern(final String patternList) {
364         excludePattern = new FilePatternMatcher(patternList);
365     }
366 
367     /**
368      * @return Returns the excludePattern.
369      */
370     public static FilePatternMatcher getExcludePattern() {
371         return excludePattern;
372     }
373 
374     /**
375      * @return Returns the includePattern.
376      */
377     public static FilePatternMatcher getIncludePattern() {
378         return includePattern;
379     }
380 
381     public static void setSymbolicNamesPattern(final String symbolicNamesPattern) throws ConfigurationException {
382         try {
383             ConfigurationOptions.symbolicNamesPattern = Pattern.compile(symbolicNamesPattern);
384         } catch (final PatternSyntaxException e) {
385             throw new ConfigurationException("Invalid regular expression for tags: " + e.getLocalizedMessage());
386         }
387     }
388 
389     public static Pattern getSymbolicNamesPattern() {
390         return symbolicNamesPattern;
391     }
392 
393     public static void setBugzillaUrl(final String bugzillaUrl) {
394         ConfigurationOptions.webBugTracker = new Bugzilla(bugzillaUrl);
395     }
396 
397     public static void setMantisUrl(final String mantisUrl) {
398         ConfigurationOptions.webBugTracker = new Mantis(mantisUrl);
399     }
400 
401     public static BugTracker getWebBugtracker() {
402         return ConfigurationOptions.webBugTracker;
403     }
404 
405     public static void setOutputFormat(final String outputFormat) throws ConfigurationException {
406         if (!"html".equals(outputFormat) && !"xdoc".equals(outputFormat) && !"xml".equals(outputFormat)) {
407             throw new ConfigurationException("Invalid output format, use only 'html', 'xdoc' or 'xml'");
408         }
409         ConfigurationOptions.outputFormat = outputFormat;
410     }
411 
412     public static String getOutputFormat() {
413         return outputFormat;
414     }
415 
416     public static MarkupSyntax getMarkupSyntax() {
417         if ("xdoc".equals(outputFormat)) {
418             return ReportConfig.XDOC;
419         } else if ("xml".equals(outputFormat)) {
420             return ReportConfig.XML;
421         }
422         return ReportConfig.HTML;
423     }
424 
425     public static void setWebRepositoryIntegration(final WebRepositoryIntegration webRepo) {
426         webRepository = webRepo;
427     }
428 
429     /**
430     * Allow change between css that are shipped with the tool.
431     * @param cssName statcvs.css or objectlab-statcvs-xdoc.css
432     */
433     public static void setDefaultCssFile(final String cssName) {
434         cssHandler = new DefaultCssHandler(cssName);
435     }
436 
437     /**
438      * Excludes a login name from charts and reports that compare
439      * several developers. Useful to reduce the noise from admin
440      * accounts.
441      * @param loginName A login name
442      */
443     public static void addNonDeveloperLogin(final String loginName) {
444         nonDeveloperLogins.add(loginName);
445     }
446 
447     /**
448      * Gets login names that should be excluded from charts and
449      * reports that compare several developers.
450      */
451     public static Collection getNonDeveloperLogins() {
452         return nonDeveloperLogins;
453     }
454 
455     /**
456      * Set the config file that may contain user details.
457      * @param propertiesFilename
458      */
459     public static void setConfigFile(final String propertiesFilename) throws ConfigurationException {
460         if (propertiesFilename != null) {
461             InputStream is = null;
462             try {
463                 is = new FileInputStream(propertiesFilename);
464                 properties.load(is);
465             } catch (final FileNotFoundException e) {
466                 throw new ConfigurationException("Unable to find the configuration file " + propertiesFilename);
467             } catch (final IOException e) {
468                 throw new ConfigurationException("Problem reading the configuration file " + propertiesFilename);
469             } finally {
470                 if (is != null) {
471                     try {
472                         is.close();
473                     } catch (final IOException e) {
474                         throw new ConfigurationException("Problem closing stream to the configuration file " + propertiesFilename);
475                     }
476                 }
477             }
478         }
479     }
480 
481     /**
482      * The config properties.
483      * @return
484      */
485     public static Properties getConfigProperties() {
486         return properties;
487     }
488 }