View Javadoc
1   /*
2    * #%L
3    * JRst :: Api
4    * %%
5    * Copyright (C) 2011 CodeLutin, Chatellier Eric
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.jrst;
24  
25  import java.util.Properties;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.nuiton.config.ApplicationConfig;
30  import org.nuiton.config.ArgumentsParserException;
31  
32  /**
33   * Permet la configuration de JRST pour son execution (parsing de la ligne
34   * de commande).
35   *
36   * @author poussin
37   * @version $Revision$
38   *
39   * Last update: $Date$
40   * by : $Author$
41   */
42  public class JRSTConfig {
43  
44      /** to use log facility, just put in your code: log.info(\"...\"); */
45      static private Log log = LogFactory.getLog(JRSTConfig.class);
46  
47      private JRSTConfig() {
48      }
49  
50  
51      static public ApplicationConfig getConfig() {
52          return getConfig(null, null);
53      }
54  
55      static public ApplicationConfig getConfig(String[] args) {
56          return getConfig(null, null, args);
57      }
58  
59      /**
60       * Create WikittyConfig and load particular configuration filename.
61       * 
62       * @param configFilename name of wikitty config file
63       */
64      static public ApplicationConfig getConfig(String configFilename) {
65          return getConfig(null, configFilename);
66      }
67  
68      /**
69       * Create WikittyConfig and use props as default value
70       *
71       * @param props as default value
72       */
73      static public ApplicationConfig getConfig(Properties props) {
74          return getConfig(props, null);
75      }
76  
77      static public ApplicationConfig getConfig(
78              Properties props, String configFilename, String ... args) {
79          ApplicationConfig conf = new ApplicationConfig(props, configFilename);
80          conf.loadDefaultOptions(JRSTConfigOption.values());
81          conf.loadActions(JRSTConfigAction.values());
82          
83          // add options alias
84          conf.addAlias("--console", "--option", "console", "true");
85          conf.addAlias("-c", "--option console true");
86          conf.addAlias("--force", "--option", "force", "true");
87          conf.addAlias("--outFile", "--option", "outFile");
88          conf.addAlias("-o", "--option", "outFile");
89          conf.addAlias("--outType", "--option", "outType");
90          conf.addAlias("-t", "--option", "outType");
91          conf.addAlias("--xslFile", "--option", "xslFile");
92          conf.addAlias("-x", "--option", "xslFile");
93          conf.addAlias("-i", "--intermediate", "intermediateFile");
94          conf.addAlias("--simple", "--option", "simple", "true");
95          conf.addAlias("-s", "--option", "simple", "true");
96  
97          try {
98              conf.parse(args);
99          } catch (ArgumentsParserException eee) {
100             if (log.isErrorEnabled()) {
101                 log.error("Can't load wikitty configuration", eee);
102             }
103         }
104         return conf;
105     }    
106 
107     static public void help() {
108         System.out.println("JRST configuration and action");
109         System.out.println("Options (set with --option <key> <value>:");
110         for (JRSTConfigOption o : JRSTConfigOption.values()) {
111             System.out.println("\t" + o.key + "(" + o.getDefaultValue() + ")\t:" + o.getDescription());
112         }
113 
114         System.out.println("Actions:");
115         for (JRSTConfigAction a : JRSTConfigAction.values()) {
116             System.out.println("\t" + java.util.Arrays.toString(a.aliases) + "(" + a.getAction() + ")\t:" + a.getDescription());
117         }
118         System.exit(0);
119     }
120 
121 }