1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.tools;
20
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Optional;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 import org.apache.commons.cli.Option;
28 import org.apache.commons.lang3.StringUtils;
29 import org.apache.rat.commandline.Arg;
30
31 import static java.lang.String.format;
32
33 public abstract class AbstractOption {
34
35 protected static final Pattern PATTERN = Pattern.compile("-(-[a-z0-9]+)+");
36
37 protected final Option option;
38
39 protected final String name;
40
41
42
43
44
45
46 AbstractOption(final Option option, final String name) {
47 this.option = option;
48 this.name = name;
49 }
50
51
52
53
54
55 public String getDefaultValue() {
56 Arg arg = Arg.findArg(option);
57 return arg == null ? null : arg.defaultValue();
58 }
59
60 protected abstract String cleanupName(Option option);
61
62
63
64
65
66
67 protected String cleanup(final String str) {
68 String workingStr = str;
69 if (StringUtils.isNotBlank(workingStr)) {
70 Map<String, String> maps = new HashMap<>();
71 Matcher matcher = PATTERN.matcher(workingStr);
72 while (matcher.find()) {
73 String key = matcher.group();
74 String optKey = key.substring(2);
75 Optional<Option> maybeResult = Arg.getOptions().getOptions().stream()
76 .filter(o -> optKey.equals(o.getOpt()) || optKey.equals(o.getLongOpt())).findFirst();
77 maybeResult.ifPresent(value -> maps.put(key, cleanupName(value)));
78 }
79 for (Map.Entry<String, String> entry : maps.entrySet()) {
80 workingStr = workingStr.replaceAll(Pattern.quote(format("%s", entry.getKey())), entry.getValue());
81 }
82 }
83 return workingStr;
84 }
85
86
87
88
89
90 public final String getName() {
91 return name;
92 }
93
94
95
96
97
98
99 public final String getDescription() {
100 return cleanup(option.getDescription());
101 }
102
103
104
105
106
107
108 public final Class<?> getType() {
109 return option.hasArg() ? ((Class<?>) option.getType()) : boolean.class;
110 }
111
112
113
114
115
116 public final String getArgName() {
117 return option.getArgName();
118 }
119
120
121
122
123
124 public final boolean isDeprecated() {
125 return option.isDeprecated();
126 }
127
128
129
130
131
132 public final boolean isRequired() {
133 return option.isRequired();
134 }
135
136
137
138
139
140 public final boolean hasArg() {
141 return option.hasArg();
142 }
143
144
145
146
147
148 public final boolean hasArgs() {
149 return option.hasArgs();
150 }
151
152
153
154
155
156 public final String keyValue() {
157 return format("\"%s\"", StringUtils.defaultIfEmpty(option.getLongOpt(), option.getOpt()));
158 }
159
160
161
162
163
164 public final String getDeprecated() {
165 return option.isDeprecated() ? cleanup(StringUtils.defaultIfEmpty(option.getDeprecated().toString(), StringUtils.EMPTY)) : StringUtils.EMPTY;
166 }
167 }