View Javadoc

1   /*
2    ExistingOrNotFileOptionArgumentImpl.java
3    Creation date : 19/11/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.file;
26  
27  import static java.util.logging.Level.FINE;
28  import static java.util.logging.Logger.getLogger;
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  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.NON_DIRECTORY_FILE_ARGUMENT_ERROR;
33  import static net.sourceforge.plantumldependency.commoncli.constants.log.FineConstants.FILE_ARGUMENT_NOT_SPECIFIED_FINE;
34  import static net.sourceforge.plantumldependency.commoncli.constants.log.FineConstants.FILE_ARGUMENT_SPECIFIED_FINE;
35  
36  import java.io.File;
37  import java.util.logging.Logger;
38  
39  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
40  import net.sourceforge.plantumldependency.commoncli.option.argument.AbstractOptionArgument;
41  
42  /**
43   * The file implementation of the
44   * {@link net.sourceforge.plantumldependency.commoncli.option.argument.OptionArgument} interface,
45   * specifying a non directory existing or non existing {@link File} argument.
46   *
47   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
48   * @since 1.3.0
49   * @version 1.3.0
50   */
51  public class ExistingOrNotFileOptionArgumentImpl extends AbstractOptionArgument < File > {
52  
53      /** Serial version UID. */
54      private static final long serialVersionUID = 8823679291565787057L;
55  
56      /** The class logger. */
57      private static final transient Logger LOGGER = getLogger(ExistingOrNotFileOptionArgumentImpl.class.getName());
58  
59      /** The argument main usage constant. */
60      private static final String MAIN_USAGE = "FILE";
61  
62      /** The argument main usage description constant. */
63      private static final String USAGE_DESCRIPTION = MAIN_USAGE
64              + " specifies a valid file path, where the file can exist or not and is not a directory. It can be absolute or relative. If the file already exists, it overrides it.";
65  
66      /**
67       * Default constructor.
68       *
69       * @param optionArgumentIsMandatory
70       *            <code>true</code> if the argument is mandatory, <code>false</code> otherwise.
71       * @since 1.3.0
72       */
73      public ExistingOrNotFileOptionArgumentImpl(final boolean optionArgumentIsMandatory) {
74          super(optionArgumentIsMandatory, new StringBuilder(USAGE_DESCRIPTION));
75      }
76  
77      /**
78       * {@inheritDoc}
79       *
80       * @since 1.3.0
81       */
82      @Override
83      protected String getMainUsageDescription() {
84          return MAIN_USAGE;
85      }
86  
87      /**
88       * {@inheritDoc}
89       *
90       * @since 1.3.0
91       */
92      @Override
93      public File parseArgument(final String argument) throws CommandLineException {
94          File file = null;
95  
96          if (isNotEmpty(argument)) {
97              file = new File(argument);
98              if (file.isDirectory()) {
99                  throw new CommandLineException(buildLogString(NON_DIRECTORY_FILE_ARGUMENT_ERROR, argument));
100             } else if (file.exists()) {
101                 LOGGER.log(FINE, buildLogString(FILE_ARGUMENT_SPECIFIED_FINE, argument));
102             } else {
103                 LOGGER.log(FINE, buildLogString(FILE_ARGUMENT_NOT_SPECIFIED_FINE, argument));
104             }
105         } else {
106             throw new CommandLineException(EMPTY_OPTION_ARGUMENT_ERROR);
107         }
108 
109         return file;
110     }
111 }