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