1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.anttasks;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.stream.Collectors;
30
31 import org.apache.commons.io.filefilter.IOFileFilter;
32 import org.apache.commons.io.filefilter.OrFileFilter;
33 import org.apache.rat.ConfigurationException;
34 import org.apache.rat.ImplementationException;
35 import org.apache.rat.OptionCollection;
36 import org.apache.rat.ReportConfiguration;
37 import org.apache.rat.Reporter;
38 import org.apache.rat.license.LicenseSetFactory;
39 import org.apache.rat.utils.DefaultLog;
40 import org.apache.rat.utils.Log;
41 import org.apache.tools.ant.BuildException;
42 import org.apache.tools.ant.Project;
43 import org.apache.tools.ant.taskdefs.LogOutputStream;
44 import org.apache.tools.ant.types.EnumeratedAttribute;
45 import org.apache.tools.ant.types.Resource;
46 import org.apache.tools.ant.types.ResourceCollection;
47 import org.apache.tools.ant.types.resources.Union;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public class Report extends BaseAntTask {
69
70 private final List<License> licenses = new ArrayList<>();
71
72 private final List<Family> families = new ArrayList<>();
73
74 private final DeprecatedConfig deprecatedConfig = new DeprecatedConfig();
75
76
77
78 private Union nestedResources;
79
80
81
82
83 private static class DeprecatedConfig {
84
85 private IOFileFilter inputFileFilter;
86
87 private final Set<String> approvedLicenseCategories = new HashSet<>();
88
89 private final Set<String> removedLicenseCategories = new HashSet<>();
90 }
91
92
93
94
95 public Report() {
96 Logger log = new Logger();
97 DefaultLog.setInstance(log);
98 }
99
100
101
102
103
104 public void add(final ResourceCollection rc) {
105 if (nestedResources == null) {
106 nestedResources = new Union();
107 }
108 nestedResources.add(rc);
109 }
110
111
112
113
114
115 @Deprecated
116 public void setInputFileFilter(final IOFileFilter inputFileFilter) {
117 deprecatedConfig.inputFileFilter = inputFileFilter;
118 }
119
120
121
122
123
124
125 @Deprecated
126 public void setReportFile(final File reportFile) {
127 setOut(reportFile.getAbsolutePath());
128 }
129
130
131
132
133
134 public void addLicense(final License license) {
135 licenses.add(license);
136 }
137
138
139
140
141
142 public void addFamily(final Family family) {
143 families.add(family);
144 }
145
146
147
148
149
150
151 @Deprecated
152 public void addStylesheet(final Resource styleSheet) {
153 setStylesheet(styleSheet.getName());
154 }
155
156
157
158
159
160
161 @Deprecated
162 public void addStyleSheet(final Resource styleSheet) {
163 setStylesheet(styleSheet.getName());
164 }
165
166
167
168
169
170
171 @Deprecated
172 public void setStyleReport(final boolean styleReport) {
173 setXml(!styleReport);
174 }
175
176
177
178
179
180
181 @Deprecated
182 public void setFormat(final String style) {
183 setStyleReport("styled".equalsIgnoreCase(style));
184 }
185
186
187
188
189
190
191 public void setLicenses(final File fileName) {
192 try {
193 createLicenses().addText(fileName.getCanonicalPath());
194 } catch (IOException e) {
195 throw new BuildException("Unable to read license file " + fileName, e);
196 }
197 }
198
199
200
201
202
203
204 @Deprecated
205 public void setUseDefaultLicenses(final boolean useDefaultLicenses) {
206 setNoDefaultLicenses(!useDefaultLicenses);
207 }
208
209
210
211
212
213
214 @Deprecated
215 public void setAddApprovedLicense(final String familyCategory) {
216 deprecatedConfig.approvedLicenseCategories.add(familyCategory);
217 }
218
219
220
221
222
223 public void addAddApprovedLicense(final String familyCategory) {
224 deprecatedConfig.approvedLicenseCategories.add(familyCategory);
225 }
226
227
228
229
230
231
232 @Deprecated
233 public void setRemoveApprovedLicense(final String familyCategory) {
234 deprecatedConfig.removedLicenseCategories.add(familyCategory);
235 }
236
237
238
239
240
241 public void addRemoveApprovedLicense(final String familyCategory) {
242 deprecatedConfig.removedLicenseCategories.add(familyCategory);
243 }
244
245
246
247
248
249
250 @Deprecated
251 public void setRemoveApprovedLicense(final String[] familyCategory) {
252 deprecatedConfig.removedLicenseCategories.addAll(Arrays.asList(familyCategory));
253 }
254
255
256
257
258
259 public void addRemoveApprovedLicense(final String[] familyCategory) {
260 deprecatedConfig.removedLicenseCategories.addAll(Arrays.asList(familyCategory));
261 }
262
263
264
265
266
267 @Deprecated
268 public void setCopyrightMessage(final String copyrightMessage) {
269 setCopyright(copyrightMessage);
270 }
271
272
273
274
275
276
277 @Deprecated
278 public void setAddLicenseHeaders(final AddLicenseHeaders setting) {
279 switch (setting.getNative()) {
280 case TRUE:
281 setAddLicense(true);
282 break;
283 case FALSE:
284 setAddLicense(false);
285 break;
286 case FORCED:
287 setAddLicense(true);
288 setForce(true);
289 break;
290 }
291 }
292
293
294
295
296
297
298 @Deprecated
299 public void setAddDefaultDefinitions(final File fileName) {
300 try {
301 Licenses lic = createLicenses();
302 lic.addText(fileName.getCanonicalPath());
303 } catch (IOException e) {
304 throw new BuildException("Unable to read license file " + fileName, e);
305 }
306 }
307
308
309
310
311
312 public ReportConfiguration getConfiguration() {
313 try {
314 final ReportConfiguration configuration = OptionCollection.parseCommands(args().toArray(new String[0]),
315 o -> DefaultLog.getInstance().warn("Help option not supported"),
316 true);
317 if (!args().contains(asKey(OptionCollection.OUT))) {
318 configuration.setOut(() -> new LogOutputStream(this, Project.MSG_INFO));
319 }
320 configuration.setReportable(new ResourceCollectionContainer(nestedResources));
321 configuration.addApprovedLicenseCategories(deprecatedConfig.approvedLicenseCategories);
322 configuration.removeApprovedLicenseCategories(deprecatedConfig.removedLicenseCategories);
323 if (deprecatedConfig.inputFileFilter != null) {
324 if (configuration.getFilesToIgnore() != null) {
325 configuration.setFilesToIgnore(new OrFileFilter(configuration.getFilesToIgnore(), deprecatedConfig.inputFileFilter));
326 } else {
327 configuration.setFilesToIgnore(deprecatedConfig.inputFileFilter);
328 }
329 }
330 families.stream().map(Family::build).forEach(configuration::addFamily);
331 licenses.stream().map(License::asBuilder)
332 .forEach(l -> configuration.addApprovedLicenseCategory(configuration.addLicense(l).getLicenseFamily()));
333 return configuration;
334 } catch (IOException | ImplementationException e) {
335 throw new BuildException(e.getMessage(), e);
336 }
337 }
338
339
340
341
342 @Override
343 public void execute() {
344 try {
345 Reporter r = new Reporter(validate(getConfiguration()));
346 r.output(null, () -> new ReportConfiguration.NoCloseOutputStream(System.out));
347 r.output();
348 } catch (BuildException e) {
349 throw e;
350 } catch (Exception ioex) {
351 throw new BuildException(ioex);
352 }
353 }
354
355
356
357
358 protected ReportConfiguration validate(final ReportConfiguration cfg) {
359 try {
360 cfg.validate(s -> log(s, Project.MSG_WARN));
361 } catch (ConfigurationException e) {
362 throw new BuildException(e.getMessage(), e.getCause());
363 }
364 if (nestedResources == null) {
365 throw new BuildException("You must specify at least one file to create the report for.");
366 }
367 return cfg;
368 }
369
370
371
372
373
374 @Deprecated
375 public static class AddLicenseHeaders extends EnumeratedAttribute {
376
377 static final String TRUE = "true";
378
379 static final String FALSE = "false";
380
381 static final String FORCED = "forced";
382
383 public AddLicenseHeaders() {
384 }
385
386 public AddLicenseHeaders(final String s) {
387 setValue(s);
388 }
389
390 @Override
391 public String[] getValues() {
392 return new String[] { TRUE, FALSE, FORCED };
393 }
394
395 public org.apache.rat.config.AddLicenseHeaders getNative() {
396 return org.apache.rat.config.AddLicenseHeaders.valueOf(getValue().toUpperCase());
397 }
398 }
399
400
401
402
403
404 @Deprecated
405 public static class ApprovalFilter extends EnumeratedAttribute {
406
407 public ApprovalFilter() {
408 }
409
410 public ApprovalFilter(final String s) {
411 setValue(s);
412 }
413
414 @Override
415 public String[] getValues() {
416 return Arrays.stream(LicenseSetFactory.LicenseFilter.values()).map(LicenseSetFactory.LicenseFilter::name)
417 .collect(Collectors.toList()).toArray(new String[LicenseSetFactory.LicenseFilter.values().length]);
418 }
419
420 public LicenseSetFactory.LicenseFilter internalFilter() {
421 return LicenseSetFactory.LicenseFilter.valueOf(getValue());
422 }
423 }
424
425
426
427
428 private class Logger implements Log {
429
430 private void write(final int level, final String msg) {
431 try (PrintWriter pw = new PrintWriter(new LogOutputStream(Report.this, level))) {
432 pw.write(msg);
433 }
434 }
435
436 @Override
437 public void log(final Level level, final String msg) {
438 switch (level) {
439 case DEBUG:
440 write(Project.MSG_DEBUG, msg);
441 break;
442 case INFO:
443 write(Project.MSG_INFO, msg);
444 break;
445 case WARN:
446 write(Project.MSG_WARN, msg);
447 break;
448 case ERROR:
449 write(Project.MSG_ERR, msg);
450 break;
451 case OFF:
452 default:
453 break;
454 }
455 }
456 }
457 }