View Javadoc

1   /*
2    PlantUMLDependencyDisplayNameOptionArgument.java
3    Creation date : 31/05/2014
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.cli.main.option.display.name.argument;
26  
27  import static java.util.regex.Pattern.compile;
28  import static net.sourceforge.plantumldependency.cli.constants.log.ErrorConstants.BAD_PATTERN_ARGUMENT_ERROR;
29  import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
30  import static net.sourceforge.plantumldependency.common.utils.string.StringUtils.isNotEmpty;
31  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.EMPTY_OPTION_ARGUMENT_ERROR;
32  
33  import java.util.regex.Pattern;
34  import java.util.regex.PatternSyntaxException;
35  
36  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
37  import net.sourceforge.plantumldependency.commoncli.option.argument.AbstractOptionArgument;
38  
39  /**
40   * The display package name option argument, telling the program what to display in the generated
41   * file. <i>Note : no option should have the same main or secondary names</i>.
42   *
43   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
44   * @since 1.4.0
45   * @version 1.4.0
46   */
47  public class PlantUMLDependencyDisplayNameOptionArgument extends AbstractOptionArgument < Pattern > {
48  
49      /** The argument main usage constant. */
50      private static final String MAIN_USAGE = "DISPLAY_NAME_PATTERN";
51  
52      /** The argument main usage description constant. */
53      private static final String USAGE_DESCRIPTION = MAIN_USAGE
54              + " specifies display name pattern when generating the plantUML output file, it is a regular expression following the Java pattern (see http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html for description).";
55  
56      /** Serial version UID. */
57      private static final long serialVersionUID = -8804026254773622257L;
58  
59      /**
60       * Default constructor.
61       *
62       * @since 1.4.0
63       */
64      public PlantUMLDependencyDisplayNameOptionArgument() {
65          super(true, new StringBuilder(USAGE_DESCRIPTION));
66      }
67  
68      /**
69       * {@inheritDoc}
70       *
71       * @since 1.4.0
72       */
73      @Override
74      protected String getMainUsageDescription() {
75          return MAIN_USAGE;
76      }
77  
78      /**
79       * {@inheritDoc}
80       *
81       * @since 1.4.0
82       */
83      @Override
84      public Pattern parseArgument(final String argument) throws CommandLineException {
85          Pattern displayNameArgument = null;
86  
87          if (isNotEmpty(argument)) {
88              try {
89                  displayNameArgument = compile(argument);
90              } catch (final PatternSyntaxException e) {
91                  throw new CommandLineException(buildLogString(BAD_PATTERN_ARGUMENT_ERROR, argument), e);
92              }
93          } else {
94              throw new CommandLineException(EMPTY_OPTION_ARGUMENT_ERROR);
95          }
96  
97          return displayNameArgument;
98      }
99  }