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
30 import org.apache.commons.cli.Option;
31 import org.apache.commons.io.filefilter.IOFileFilter;
32 import org.apache.rat.ConfigurationException;
33 import org.apache.rat.DeprecationReporter;
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.commandline.Arg;
39 import org.apache.rat.commandline.StyleSheets;
40 import org.apache.rat.document.DocumentName;
41 import org.apache.rat.documentation.options.AntOptionCollection;
42 import org.apache.rat.license.LicenseSetFactory;
43 import org.apache.rat.utils.DefaultLog;
44 import org.apache.rat.utils.Log;
45 import org.apache.tools.ant.BuildException;
46 import org.apache.tools.ant.Project;
47 import org.apache.tools.ant.taskdefs.LogOutputStream;
48 import org.apache.tools.ant.types.EnumeratedAttribute;
49 import org.apache.tools.ant.types.Resource;
50 import org.apache.tools.ant.types.ResourceCollection;
51 import org.apache.tools.ant.types.resources.Union;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public class Report extends BaseAntTask {
73
74
75
76 @Deprecated
77 private final List<License> licenses = new ArrayList<>();
78
79
80
81 @Deprecated
82 private final List<Family> families = new ArrayList<>();
83
84
85
86 private final DeprecatedConfig deprecatedConfig = new DeprecatedConfig();
87
88
89
90 private Union nestedResources;
91
92
93
94
95 private static final class DeprecatedConfig {
96
97
98
99 private IOFileFilter inputFileFilter;
100
101
102
103 private final Set<String> approvedLicenseCategories = new HashSet<>();
104
105
106
107 private final Set<String> removedLicenseCategories = new HashSet<>();
108 }
109
110
111
112
113 public Report() {
114 super();
115
116 Log oldLog = DefaultLog.getInstance();
117 if (oldLog instanceof DefaultLog) {
118 DefaultLog.setInstance(new Logger());
119 DefaultLog.getInstance().setLevel(oldLog.getLevel());
120 }
121 }
122
123
124
125
126
127
128 public void add(final ResourceCollection rc) {
129 if (nestedResources == null) {
130 nestedResources = new Union();
131 }
132 nestedResources.add(rc);
133 }
134
135
136
137
138
139
140 @Deprecated
141 public void setInputFileFilter(final IOFileFilter inputFileFilter) {
142 DeprecationReporter.logDeprecated("element inputFileFilter", "0.17", true, "outputFile element");
143 deprecatedConfig.inputFileFilter = inputFileFilter;
144 }
145
146
147
148
149
150
151
152 @Deprecated
153 public void setReportFile(final File reportFile) {
154 DeprecationReporter.logDeprecated("element reportFile", "0.17", true, "outputFile element");
155 addArg("output-file", reportFile.getAbsolutePath());
156 }
157
158
159
160
161
162
163
164 @Deprecated
165 public void addLicense(final License license) {
166 licenses.add(license);
167 }
168
169
170
171
172
173
174
175 @Deprecated
176 public void addFamily(final Family family) {
177 families.add(family);
178 }
179
180
181
182
183
184
185
186 @Deprecated
187 public void addStylesheet(final Resource styleSheet) {
188 DeprecationReporter.logDeprecated("element stylesheet", "0.17", true, "<outputStyle> element");
189 setStylesheet(styleSheet.getName());
190 }
191
192
193
194
195
196
197
198 @Deprecated
199 public void addStyleSheet(final Resource styleSheet) {
200 DeprecationReporter.logDeprecated("attribute styleSheet", "0.17", true, "<outputStyle> element");
201 setOutputStyle(styleSheet.getName());
202 }
203
204
205
206
207
208
209
210 @Deprecated
211 public void setStyleReport(final boolean styleReport) {
212 setOutputStyle(styleReport ? "xml" : "plain-rat");
213 }
214
215
216
217
218
219
220
221 @Deprecated
222 public void setFormat(final String style) {
223 DeprecationReporter.logDeprecated("attribute format", "0.17", true, "outputStyle element");
224 if ("styled".equalsIgnoreCase(style)) {
225 setOutputStyle("plain-rat");
226 } else {
227 setOutputStyle(style);
228 }
229 }
230
231
232
233
234
235
236
237 @Deprecated
238 public void setLicenses(final File fileName) {
239 DeprecationReporter.logDeprecated("attribute licenses", "0.17", true, "<licences> element");
240 setArg(Arg.CONFIGURATION.option().getLongOpt(), fileName.getAbsolutePath());
241 }
242
243
244
245
246
247
248
249 @Deprecated
250 public void setUseDefaultLicenses(final boolean useDefaultLicenses) {
251 DeprecationReporter.logDeprecated("attribute useDefaultLicenses", "0.17", true, "noDefaultLicenses attribute");
252 setNoDefaultLicenses(!useDefaultLicenses);
253 }
254
255
256
257
258
259
260
261 @Deprecated
262 public void setAddApprovedLicense(final String familyCategory) {
263 DeprecationReporter.logDeprecated("attribute addApprovedLicense", "0.17", true, "<licensesApproved> element");
264 deprecatedConfig.approvedLicenseCategories.add(familyCategory);
265 }
266
267
268
269
270
271
272
273 @Deprecated
274 public void addAddApprovedLicense(final String familyCategory) {
275 DeprecationReporter.logDeprecated("element <addApprovedLicense>", "0.17", true, "<licenseFamiliesApproved> element");
276 deprecatedConfig.approvedLicenseCategories.add(familyCategory);
277 }
278
279
280
281
282
283
284
285 @Deprecated
286 public void setRemoveApprovedLicense(final String familyCategory) {
287 DeprecationReporter.logDeprecated("attribute addApprovedLicense", "0.17", true, "<licenseFamiliesApproved> element");
288 deprecatedConfig.removedLicenseCategories.add(familyCategory);
289 }
290
291
292
293
294
295
296
297 @Deprecated
298 public void addRemoveApprovedLicense(final String familyCategory) {
299 DeprecationReporter.logDeprecated("element <removeApprovedLicense>", "0.17", true, "<licenseFamiliesDenied> element");
300 deprecatedConfig.removedLicenseCategories.add(familyCategory);
301 }
302
303
304
305
306
307
308
309 @Deprecated
310 public void setRemoveApprovedLicense(final String[] familyCategory) {
311 DeprecationReporter.logDeprecated("attribute removeApprovedLicense", "0.17", true, "<licenseFamiliesDenied> element");
312 deprecatedConfig.removedLicenseCategories.addAll(Arrays.asList(familyCategory));
313 }
314
315
316
317
318
319
320
321 @Deprecated
322 public void addRemoveApprovedLicense(final String[] familyCategory) {
323 DeprecationReporter.logDeprecated("element <removeApprovedLicense>", "0.17", true, "<licenseFamiliesDenied> element");
324 deprecatedConfig.removedLicenseCategories.addAll(Arrays.asList(familyCategory));
325 }
326
327
328
329
330
331
332
333 @Deprecated
334 public void setCopyrightMessage(final String copyrightMessage) {
335 setCopyright(copyrightMessage);
336 }
337
338
339
340
341
342
343
344 @Deprecated
345 public void setAddLicenseHeaders(final AddLicenseHeaders setting) {
346 DeprecationReporter.logDeprecated("attribute addLicenseHeaders", "0.17", true, "editLicense and editOverwrite attributes");
347 switch (setting.getNative()) {
348 case TRUE:
349 setEditLicense(true);
350 break;
351 case FALSE:
352 setEditLicense(false);
353 break;
354 case FORCED:
355 setEditLicense(true);
356 setEditOverwrite(true);
357 break;
358 }
359 }
360
361
362
363
364
365
366
367 @Deprecated
368 public void setAddDefaultDefinitions(final File fileName) {
369 DeprecationReporter.logDeprecated("element <addDefaultDefinitions>", "0.17", true, "<config> element");
370 setArg(Arg.CONFIGURATION.option().getLongOpt(), fileName.getAbsolutePath());
371 }
372
373
374
375
376
377
378
379 protected List<String> getValues(final Arg arg) {
380 List<String> result = new ArrayList<>();
381 for (Option option : arg.group().getOptions()) {
382 if (option.getLongOpt() != null) {
383 List<String> args = getArg(option.getLongOpt());
384 if (args != null) {
385 result.addAll(args);
386 }
387 }
388 }
389 return result;
390 }
391
392
393
394
395
396
397 protected void removeKey(final Arg arg) {
398 for (Option option : arg.group().getOptions()) {
399 if (option.getLongOpt() != null) {
400 removeArg(option.getLongOpt());
401 }
402 }
403 }
404
405
406
407
408
409
410 public ReportConfiguration getConfiguration() {
411 try {
412 boolean helpLicenses = !getValues(Arg.HELP_LICENSES).isEmpty();
413 removeKey(Arg.HELP_LICENSES);
414 File antFileDir = new File(getProject().getProperty("ant.file")).getParentFile();
415 DocumentName name = DocumentName.builder(antFileDir).build();
416 final ReportConfiguration configuration = OptionCollection.parseCommands(antFileDir, args().toArray(new String[0]),
417 o -> DefaultLog.getInstance().warn("Help option not supported"),
418 true);
419 if (getValues(Arg.OUTPUT_FILE).isEmpty()) {
420 configuration.setOutput(new ReportConfiguration.IODescriptor<>("RAT output", () -> new LogOutputStream(this, Project.MSG_INFO)));
421 }
422
423 configuration.addSource(new ResourceCollectionContainer(name, configuration, nestedResources));
424 configuration.addApprovedLicenseCategories(deprecatedConfig.approvedLicenseCategories);
425 configuration.removeApprovedLicenseCategories(deprecatedConfig.removedLicenseCategories);
426 if (deprecatedConfig.inputFileFilter != null) {
427 configuration.addExcludedFilter(deprecatedConfig.inputFileFilter);
428 }
429 families.stream().map(Family::build).forEach(configuration::addFamily);
430 licenses.stream().map(License::asBuilder)
431 .forEach(l -> configuration.addApprovedLicenseCategory(configuration.addLicense(l).getLicenseFamily()));
432 if (helpLicenses) {
433 new org.apache.rat.help.Licenses(new AntOptionCollection(),
434 configuration, new PrintWriter(DefaultLog.getInstance().asWriter())).printHelp();
435 }
436 return configuration;
437 } catch (IOException | ImplementationException e) {
438 throw new BuildException(e.getMessage(), e);
439 }
440 }
441
442
443
444
445 @Override
446 public void execute() {
447 try {
448 ReportConfiguration config = validate(getConfiguration());
449 Reporter r = new Reporter(config);
450 Reporter.Output output = r.execute();
451 output.format(StyleSheets.PLAIN.getStyleSheet().ioSupplier(), ReportConfiguration.SYSTEM_OUT.ioSupplier());
452 output.format(config);
453 } catch (BuildException e) {
454 throw e;
455 } catch (Exception ioex) {
456 throw new BuildException(ioex);
457 }
458 }
459
460
461
462
463 protected ReportConfiguration validate(final ReportConfiguration cfg) {
464 try {
465 cfg.validate(s -> log(s, Project.MSG_WARN));
466 } catch (ConfigurationException e) {
467 throw new BuildException(e.getMessage(), e.getCause());
468 }
469 if (nestedResources == null) {
470 throw new BuildException("You must specify at least one file to create the report for.");
471 }
472 return cfg;
473 }
474
475
476
477
478
479
480 @Deprecated
481 public static final class AddLicenseHeaders extends EnumeratedAttribute {
482
483
484
485 static final String TRUE = "true";
486
487
488
489 static final String FALSE = "false";
490
491
492
493 static final String FORCED = "forced";
494
495 public AddLicenseHeaders() {
496 }
497
498 public AddLicenseHeaders(final String s) {
499 setValue(s);
500 }
501
502 @Override
503 public String[] getValues() {
504 return new String[]{TRUE, FALSE, FORCED};
505 }
506
507 public org.apache.rat.config.AddLicenseHeaders getNative() {
508 return org.apache.rat.config.AddLicenseHeaders.valueOf(getValue().toUpperCase());
509 }
510 }
511
512
513
514
515
516
517 @Deprecated
518 public static class ApprovalFilter extends EnumeratedAttribute {
519
520 public ApprovalFilter() {
521 }
522
523 public ApprovalFilter(final String s) {
524 setValue(s);
525 }
526
527 @Override
528 public String[] getValues() {
529 return Arrays.stream(LicenseSetFactory.LicenseFilter.values()).map(LicenseSetFactory.LicenseFilter::name)
530 .toList().toArray(new String[LicenseSetFactory.LicenseFilter.values().length]);
531 }
532
533 public LicenseSetFactory.LicenseFilter internalFilter() {
534 return LicenseSetFactory.LicenseFilter.valueOf(getValue());
535 }
536 }
537
538 @Override
539 public void log(final String msg, final int msgLevel) {
540 if (getProject() != null) {
541 getProject().log(msg, msgLevel);
542 } else {
543 DefaultLog.createDefault().log(fromProjectLevel(msgLevel), msg);
544 }
545 }
546
547 @Override
548 public void log(final String msg, final Throwable t, final int msgLevel) {
549 if (getProject() == null) {
550 log(Log.formatLogEntry(msg, t), msgLevel);
551 } else {
552 getProject().log(this, msg, t, msgLevel);
553 }
554 }
555
556
557
558
559
560
561 public static Log.Level fromProjectLevel(final int level) {
562 return switch (level) {
563 case Project.MSG_DEBUG, Project.MSG_VERBOSE -> Log.Level.DEBUG;
564 case Project.MSG_INFO -> Log.Level.INFO;
565 case Project.MSG_WARN -> Log.Level.WARN;
566 case Project.MSG_ERR -> Log.Level.ERROR;
567 default -> Log.Level.OFF;
568 };
569 }
570
571
572
573
574
575
576 static int toProjectLevel(final Log.Level level) {
577 return switch (level) {
578 case DEBUG -> Project.MSG_DEBUG;
579 case INFO -> Project.MSG_INFO;
580 case WARN -> Project.MSG_WARN;
581 case ERROR -> Project.MSG_ERR;
582 default -> -1;
583 };
584 }
585
586
587
588
589 private final class Logger implements Log {
590 @Override
591 public Level getLevel() {
592 return Level.DEBUG;
593 }
594
595 @Override
596 public void log(final Log.Level level, final String message, final Throwable throwable) {
597 log(level, Log.formatLogEntry(message, throwable));
598 }
599
600 @Override
601 public void log(final Level level, final String msg) {
602 Report.this.log(msg, toProjectLevel(level));
603 }
604 }
605 }