1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.documentation.velocity;
20
21 import java.lang.reflect.InvocationTargetException;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.Objects;
26 import java.util.Set;
27 import java.util.TreeSet;
28 import java.util.UUID;
29 import java.util.stream.Collectors;
30
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.rat.analysis.IHeaderMatcher;
33 import org.apache.rat.analysis.matchers.AbstractMatcherContainer;
34 import org.apache.rat.config.parameters.ComponentType;
35 import org.apache.rat.config.parameters.Description;
36 import org.apache.rat.config.parameters.DescriptionBuilder;
37 import org.apache.rat.configuration.XMLConfig;
38
39
40
41
42 public class Matcher {
43
44
45
46 private final Description desc;
47
48
49
50
51 private final IHeaderMatcher self;
52
53
54
55
56 private final Enclosed enclosed;
57
58
59
60
61 private final Set<Attribute> attributes;
62
63
64
65
66
67 Matcher(final Matcher matcher) {
68 this.desc = matcher.desc;
69 this.self = matcher.self;
70 this.enclosed = matcher.enclosed;
71 this.attributes = matcher.attributes;
72 }
73
74
75
76
77
78 Matcher(final IHeaderMatcher self) {
79 this(DescriptionBuilder.buildMap(self.getClass()), self);
80 }
81
82
83
84
85
86
87 Matcher(final Description desc, final IHeaderMatcher self) {
88 Objects.requireNonNull(desc);
89 this.desc = desc;
90 this.self = self;
91 Enclosed enclosed = null;
92 attributes = new TreeSet<>(Comparator.comparing(Attribute::getName));
93 for (Description child : desc.childrenOfType(ComponentType.PARAMETER)) {
94 if (XMLConfig.isInlineNode(desc.getCommonName(), child.getCommonName())) {
95 enclosed = new Enclosed(child);
96 } else {
97 attributes.add(new Attribute(child));
98 }
99 }
100 this.enclosed = enclosed;
101 }
102
103
104
105
106
107 public String getName() {
108 return desc.getCommonName();
109 }
110
111
112
113
114
115 public String getDescription() {
116 return StringUtils.defaultIfEmpty(desc.getDescription(), "");
117 }
118
119
120
121
122
123 public Enclosed getEnclosed() {
124 return enclosed;
125 }
126
127
128
129
130
131 public Collection<Attribute> getAttributes() {
132 return attributes;
133 }
134
135
136
137
138
139 Collection<Matcher> getChildren() {
140 if (self != null && enclosed != null && IHeaderMatcher.class.equals(enclosed.desc.getChildType())) {
141 if (self instanceof AbstractMatcherContainer) {
142 AbstractMatcherContainer matcherContainer = (AbstractMatcherContainer) self;
143 return matcherContainer.getEnclosed().stream().map(Matcher::new).collect(Collectors.toList());
144 }
145 try {
146 IHeaderMatcher matcher = (IHeaderMatcher) enclosed.desc.getter(self.getClass()).invoke(self);
147 return Collections.singleton(new Matcher(matcher));
148 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
149 throw new RuntimeException(e);
150 }
151 }
152 return Collections.emptyList();
153 }
154
155
156
157
158 public final class Enclosed {
159
160 private final Description desc;
161
162
163
164
165
166 Enclosed(final Description desc) {
167 this.desc = desc;
168 }
169
170
171
172
173
174 public String getRequired() {
175 return desc.isRequired() ? "required" : "optional";
176 }
177
178
179
180
181
182 public String getCollection() {
183 return desc.isCollection() ? "or more " : "";
184 }
185
186
187
188
189
190 public String getType() {
191 return desc.getChildType().getSimpleName();
192 }
193
194
195
196
197
198
199
200
201
202
203 public Object getValue() {
204 if (self != null) {
205 try {
206 Object value = desc.getter(self.getClass()).invoke(self);
207 return value instanceof String ? StringUtils.normalizeSpace((String) value) : value;
208 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
209 throw new RuntimeException(e);
210 }
211 }
212 return null;
213 }
214 Class<?> getChildType() {
215 return desc.getChildType();
216 }
217 }
218
219
220
221
222 public final class Attribute {
223
224
225
226 private final Description description;
227
228
229
230
231
232 Attribute(final Description description) {
233 this.description = description;
234 }
235
236
237
238
239
240 public String getName() {
241 return description.getCommonName();
242 }
243
244
245
246
247
248 public String getRequired() {
249 return description.isRequired() ? "required" : "optional";
250 }
251
252
253
254
255
256 public String getType() {
257 return description.getChildType().getSimpleName();
258 }
259
260
261
262
263
264 public String getDescription() {
265 return StringUtils.defaultIfEmpty(description.getDescription(), "");
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280 public String getValue() {
281 if (self != null) {
282 try {
283 Object value = description.getter(self.getClass()).invoke(self);
284 if (value != null) {
285 String result = value.toString();
286 if (description.getCommonName().equals("id")) {
287 try {
288 UUID.fromString(result);
289 return null;
290 } catch (IllegalArgumentException e) {
291
292 }
293 }
294 return result;
295 }
296 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
297 throw new RuntimeException(e);
298 }
299 }
300 return null;
301 }
302 }
303 }