View Javadoc

1   /*
2    LevelOptionArgumentImpl.java
3    Creation date : 31/10/2010
4    Copyright © Benjamin Croizet (graffity2199@yahoo.fr)
5   
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License
8    or GNU Lesser General Public License as published by the
9    Free Software Foundation; either version 3 of the License,
10   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 Public License for more details.
16  
17   You should have received copies of the GNU General Public License
18   and GNU Lesser General Public License along with this program;
19   if not, write to the Free Software Foundation, Inc.,
20   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21   http://www.fsf.org/licensing/licenses/gpl.html
22   http://www.gnu.org/licenses/lgpl.html
23   */
24  
25  package net.sourceforge.plantumldependency.commoncli.option.argument.impl.log;
26  
27  import static java.util.logging.Level.parse;
28  import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
29  import static net.sourceforge.plantumldependency.common.utils.string.StringUtils.isNotEmpty;
30  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.EMPTY_OPTION_ARGUMENT_ERROR;
31  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.NOT_LOG_LEVEL_ARGUMENT_ERROR;
32  
33  import java.util.logging.Level;
34  
35  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
36  import net.sourceforge.plantumldependency.commoncli.option.argument.AbstractOptionArgument;
37  
38  /**
39   * The log level implementation of the
40   * {@link net.sourceforge.plantumldependency.commoncli.option.argument.OptionArgument} interface,
41   * specifying a log {@link Level} argument.
42   *
43   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
44   * @since 1.3.0
45   * @version 1.3.0
46   */
47  public class LevelOptionArgumentImpl extends AbstractOptionArgument < Level > {
48  
49      /** Serial version UID. */
50      private static final long serialVersionUID = -456175880858276198L;
51  
52      /** The argument main usage constant. */
53      private static final String MAIN_USAGE = "VERBOSE_LEVEL";
54  
55      /** The argument main usage description constant. */
56      private static final String USAGE_DESCRIPTION = MAIN_USAGE
57              + " specifies the verbose level. The argument may consist of either a level name or an integer value. Classical values are : \"SEVERE\":1000, \"WARNING\":900, \"INFO\":800, \"CONFIG\":700, \"FINE\":500, \"FINER\":400, \"FINEST\":300. By default, if the verbose option is specified but the level is not set, the value \"INFO\":800 is taken. If not specified, the default value is \"WARNING\":900.";
58  
59      /**
60       * Default constructor.
61       *
62       * @param optionArgumentIsMandatory
63       *            <code>true</code> if the argument is mandatory, <code>false</code> otherwise.
64       * @since 1.3.0
65       */
66      public LevelOptionArgumentImpl(final boolean optionArgumentIsMandatory) {
67          super(optionArgumentIsMandatory, new StringBuilder(USAGE_DESCRIPTION));
68      }
69  
70      /**
71       * {@inheritDoc}
72       *
73       * @since 1.3.0
74       */
75      @Override
76      protected String getMainUsageDescription() {
77          return MAIN_USAGE;
78      }
79  
80      /**
81       * {@inheritDoc}
82       *
83       * @since 1.3.0
84       */
85      @Override
86      public Level parseArgument(final String argument) throws CommandLineException {
87          Level level = null;
88  
89          if (isNotEmpty(argument)) {
90              try {
91                  level = parse(argument);
92              } catch (final IllegalArgumentException e) {
93                  throw new CommandLineException(buildLogString(NOT_LOG_LEVEL_ARGUMENT_ERROR, argument), e);
94              }
95          } else {
96              throw new CommandLineException(EMPTY_OPTION_ARGUMENT_ERROR);
97          }
98  
99          return level;
100     }
101 }