1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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.EXISTING_FILE_ARGUMENT_ERROR;
33 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.NON_DIRECTORY_FILE_ARGUMENT_ERROR;
34 import static net.sourceforge.plantumldependency.commoncli.constants.log.FineConstants.FILE_ARGUMENT_NOT_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
44
45
46
47
48
49
50
51 public class NotExistingFileOptionArgumentImpl extends AbstractOptionArgument < File > {
52
53
54 private static final long serialVersionUID = 982630705812913705L;
55
56
57 private static final transient Logger LOGGER = getLogger(NotExistingFileOptionArgumentImpl.class.getName());
58
59
60 private static final String MAIN_USAGE = "FILE";
61
62
63 private static final String USAGE_DESCRIPTION = MAIN_USAGE
64 + " specifies a valid file path, where the file doesn't already exist and is not a directory. It can be absolute or relative.";
65
66
67
68
69
70
71
72
73 public NotExistingFileOptionArgumentImpl(final boolean optionArgumentIsMandatory) {
74 super(optionArgumentIsMandatory, new StringBuilder(USAGE_DESCRIPTION));
75 }
76
77
78
79
80
81
82 @Override
83 protected String getMainUsageDescription() {
84 return MAIN_USAGE;
85 }
86
87
88
89
90
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 throw new CommandLineException(buildLogString(EXISTING_FILE_ARGUMENT_ERROR, 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 }