1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.documentation.options;
20
21 import java.util.function.Function;
22
23 import org.apache.commons.cli.Option;
24 import org.apache.commons.lang3.StringUtils;
25 import org.apache.commons.text.WordUtils;
26 import org.apache.rat.ConfigurationException;
27 import org.apache.rat.ui.UIOption;
28 import org.apache.rat.utils.CasedString;
29
30 import static java.lang.String.format;
31
32
33
34
35 public final class MavenOption extends UIOption<MavenOption> {
36
37
38
39
40 private static final String XML_FMT = "<%s>";
41
42
43
44
45
46
47 private MavenOption(final MavenOptionBuilder builder) {
48 super(builder);
49 }
50
51 @Override
52 public String toString() {
53 return getName();
54 }
55
56
57
58
59
60 public String getMethodName() {
61 return "set" + name.toCase(CasedString.StringCase.PASCAL);
62 }
63
64 @Override
65 protected String cleanupName(final Option option) {
66
67 return format(XML_FMT, option == this.option ? this.name :
68 MavenOptionBuilder.createName(optionCollection.rename(option)));
69 }
70
71 @Override
72 public String getText() {
73 return cleanupName(option);
74 }
75
76 @Override
77 public String getExample() {
78 if (hasArgs()) {
79 return getExample(getArgName() + "1", getArgName() + "2");
80 }
81 if (hasArg()) {
82 return getExample(getArgName());
83 }
84 return getExample("");
85 }
86
87
88
89
90
91
92 public String getExample(final String... args) {
93 StringBuilder sb = new StringBuilder(String.format(XML_FMT, getName()));
94 if (hasArg()) {
95 if (hasArgs()) {
96 sb.append(System.lineSeparator());
97 for (String arg : args) {
98 sb.append(String.format(" <%1$s>%2$s</%1$s>%n", WordUtils.uncapitalize(getArgName()), arg));
99 }
100 } else {
101 sb.append(args[0]);
102 }
103 } else {
104 sb.append(Boolean.TRUE);
105 }
106 sb.append("</").append(getName()).append(">");
107 return sb.toString();
108 }
109
110
111
112
113
114
115
116 public String getMethodSignature(final String indent, final boolean multiple) {
117 StringBuilder sb = new StringBuilder();
118 if (isDeprecated()) {
119 sb.append(format("%s@Deprecated%n", indent));
120 }
121
122 String camelName = name.toCase(CasedString.StringCase.CAMEL);
123 if (camelName == null) {
124 throw new ConfigurationException("Name must not be null");
125 }
126 String args = option.hasArg() ? "String" : "boolean";
127 if (multiple) {
128 if (!(camelName.endsWith("s") || camelName.endsWith("Approved") || camelName.endsWith("Denied"))) {
129 camelName = camelName + "s";
130 }
131 args = args + "[]";
132 }
133
134 return sb.append(format("%1$s%5$s%n%1$spublic void set%3$s(%4$s %2$s)",
135 indent, name, camelName, args, getParameterAnnotation(camelName)))
136 .toString();
137 }
138
139
140
141
142
143
144 public String getParameterAnnotation(final String camelName) {
145 StringBuilder sb = new StringBuilder("@Parameter");
146 String property = option.hasArgs() ? null : format("property = \"rat.%s\"", camelName);
147 String defaultValue = option.isDeprecated() ? null : getDefaultValue();
148 if (property != null || defaultValue != null) {
149 sb.append("(");
150 if (property != null) {
151 sb.append(property).append(defaultValue != null ? ", " : StringUtils.EMPTY);
152 }
153 if (defaultValue != null) {
154 sb.append(format("defaultValue = \"%s\"", defaultValue));
155 }
156 sb.append(")");
157 }
158 return sb.toString();
159 }
160
161
162
163
164 public static class MavenOptionBuilder extends UIOption.Builder<MavenOption, MavenOptionBuilder> {
165
166 @Override
167 protected Function<Option, CasedString> getNameFactory() {
168 return o -> createName(optionCollection().rename(o));
169 }
170
171 @Override
172 protected MavenOption doBuild() {
173 return new MavenOption(this);
174 }
175
176
177
178
179
180
181
182 static CasedString createName(final String key) {
183 return new CasedString(CasedString.StringCase.KEBAB, key).as(CasedString.StringCase.PASCAL);
184 }
185 }
186 }