1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.rat.anttasks;
21
22 import org.apache.commons.cli.Option;
23 import org.apache.commons.lang3.StringUtils;
24 import org.apache.rat.commandline.Arg;
25 import org.apache.rat.DeprecationReporter;
26 import org.apache.rat.utils.CasedString;
27 import org.apache.rat.utils.DefaultLog;
28 import org.apache.rat.utils.Log;
29 import org.apache.tools.ant.Task;
30 import org.apache.tools.ant.types.FileSet;
31 import org.apache.tools.ant.types.Resource;
32 import org.apache.tools.ant.types.resources.FileResource;
33
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Locale;
40 import java.util.Map;
41 import java.util.Objects;
42
43
44
45
46
47 public abstract class BaseAntTask extends Task {
48
49 private static final Map<String, String> xlateName = new HashMap<>();
50
51 private static final List<String> unsupportedArgs = new ArrayList<>();
52
53 private static final Map<String, String> deprecatedArgs = new HashMap<>();
54
55 static {
56 xlateName.put("addLicense", "add-license");
57 unsupportedArgs.add("a");
58 unsupportedArgs.add("input-source");
59 unsupportedArgs.add("dir");
60 unsupportedArgs.add("log-level");
61 unsupportedArgs.add("help-licenses");
62 deprecatedArgs.put("force", "Use of deprecated option 'force'. Deprecated for removal since 0.17: Use editOverwrite attribute instead.");
63 deprecatedArgs.put("list-families", "Use of deprecated option 'listFamilies'. Deprecated for removal since 0.17: Use outputFamilies attribute instead.");
64 deprecatedArgs.put("list-licenses", "Use of deprecated option 'listLicenses'. Deprecated for removal since 0.17: Use outputLicenses attribute instead.");
65 deprecatedArgs.put("licenses", "Use of deprecated option 'licenses'. Deprecated for removal since 0.17: Use <configs> instead.");
66 deprecatedArgs.put("copyright", "Use of deprecated option 'copyright'. Deprecated for removal since 0.17: Use editCopyright attribute instead.");
67 deprecatedArgs.put("exclude", "Use of deprecated option 'excludes'. Deprecated for removal since 0.17: Use <inputExcludes> instead.");
68 deprecatedArgs.put("out", "Use of deprecated option 'out'. Deprecated for removal since 0.17: Use outputFile attribute instead.");
69 deprecatedArgs.put("includes-file", "Use of deprecated option 'includesFile'. Deprecated for removal since 0.17: Use inputIncludeFile attribute instead.");
70 deprecatedArgs.put("exclude-file", "Use of deprecated option 'excludeFile'. Deprecated for removal since 0.17: Use inputExcludeFile attribute instead.");
71 deprecatedArgs.put("addLicense", "Use of deprecated option 'addLicense'. Deprecated for removal since 0.17: Use editLicense attribute instead.");
72 deprecatedArgs.put("stylesheet", "Use of deprecated option 'stylesheet'. Deprecated for removal since 0.17: Use outputStyle attribute instead.");
73 deprecatedArgs.put("include", "Use of deprecated option 'includes'. Deprecated for removal since 0.17: Use <inputIncludes> instead.");
74 deprecatedArgs.put("scan-hidden-directories", "Use of deprecated option 'scanHiddenDirectories'. Deprecated for removal since 0.17: Use <inputIncludeStds> with 'HIDDEN_DIR' argument instead.");
75 deprecatedArgs.put("xml", "Use of deprecated option 'xml'. Deprecated for removal since 0.17: Use outputStyle attribute with the 'xml' argument instead.");
76 deprecatedArgs.put("no-default-licenses", "Use of deprecated option 'noDefaultLicenses'. Deprecated for removal since 0.17: Use configurationNoDefaults attribute instead.");
77 }
78
79 public static String createName(String longOpt) {
80 String name = StringUtils.defaultIfEmpty(xlateName.get(longOpt), longOpt).toLowerCase(Locale.ROOT);
81 return new CasedString(CasedString.StringCase.KEBAB, name).toCase(CasedString.StringCase.PASCAL);
82 }
83
84 public static List<String> unsupportedArgs() {
85 return Collections.unmodifiableList(unsupportedArgs);
86 }
87
88
89
90
91
92
93 private static void setDeprecationReporter() {
94 DeprecationReporter.setLogReporter(opt -> {
95 String msg = deprecatedArgs.get(argsKey(opt));
96 if (msg == null) {
97 DeprecationReporter.getDefault().accept(opt);
98 } else {
99 DefaultLog.getInstance().warn(msg);
100 }
101 });
102 }
103
104 private static String argsKey(Option opt) {
105 return StringUtils.defaultIfEmpty(opt.getLongOpt(), opt.getKey());
106 }
107
108
109
110
111 protected final Map<String, List<String>> args = new HashMap<>();
112
113
114
115
116
117 protected List<String> args() {
118 List<String> result = new ArrayList<>();
119 for (Map.Entry<String, List<String>> entry : args.entrySet()) {
120 result.add("--" + entry.getKey());
121 result.addAll(entry.getValue().stream().filter(Objects::nonNull).toList());
122 }
123 return result;
124 }
125
126 private boolean validateSet(String key) {
127 Arg arg = Arg.findArg(key);
128 if (arg != null) {
129 Option opt = arg.find(key);
130 Option main = arg.option();
131 if (opt.isDeprecated()) {
132 args.remove(argsKey(main));
133
134 return true;
135 }
136
137 for (Option o : arg.group().getOptions()) {
138 if (!o.equals(main)) {
139 if (args.containsKey(argsKey(o))) {
140 return false;
141 }
142 }
143 }
144 return true;
145 }
146 return false;
147 }
148
149
150
151
152
153
154
155 protected void setArg(String key, String value) {
156 if (value == null || StringUtils.isNotBlank(value)) {
157 if (validateSet(key)) {
158 List<String> values = new ArrayList<>();
159 if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
160 DefaultLog.getInstance().debug(String.format("Setting %s to '%s'", key, value));
161 }
162 values.add(value);
163 args.put(key, values);
164 }
165 }
166 }
167
168
169
170
171
172
173 public List<String> getArg(String key) {
174 return args.get(key);
175 }
176
177
178
179
180
181
182
183
184 protected void addArg(String key, String[] value) {
185 List<String> newValues = Arrays.stream(value).filter(StringUtils::isNotBlank).toList();
186 if (!newValues.isEmpty()) {
187 if (validateSet(key)) {
188 if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
189 DefaultLog.getInstance().debug(String.format("Adding [%s] to %s", String.join(", ", Arrays.asList(value)), key));
190 }
191 List<String> values = args.computeIfAbsent(key, k -> new ArrayList<>());
192 values.addAll(newValues);
193 }
194 }
195 }
196
197
198
199
200
201
202
203 protected void addArg(String key, String value) {
204 if (StringUtils.isNotBlank(value)) {
205 if (validateSet(key)) {
206 List<String> values = args.get(key);
207 if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
208 DefaultLog.getInstance().debug(String.format("Adding [%s] to %s", String.join(", ", Arrays.asList(value)), key));
209 }
210 if (values == null) {
211 values = new ArrayList<>();
212 args.put(key, values);
213 }
214 values.add(value);
215 }
216 }
217 }
218
219
220
221
222
223 protected void removeArg(String key) {
224 args.remove(key);
225 }
226
227
228
229 protected BaseAntTask() {
230 setDeprecationReporter();
231 }
232
233
234
235
236
237
238
239 public void setInputIncludeFile(String inputIncludeFile) {
240 setArg("input-include-file", inputIncludeFile);
241
242 }
243
244
245
246
247
248
249 @Deprecated
250 public void setForce(boolean force) {
251 if (force) { setArg("force", null); } else { removeArg("force"); }
252 }
253
254
255
256
257
258
259 @Deprecated
260 public void setListFamilies(String listFamilies) {
261 setArg("list-families", listFamilies);
262
263 }
264
265
266
267
268
269
270 @Deprecated
271 public void setListLicenses(String listLicenses) {
272 setArg("list-licenses", listLicenses);
273
274 }
275
276
277
278
279
280 public void setInputExcludeFile(String inputExcludeFile) {
281 setArg("input-exclude-file", inputExcludeFile);
282
283 }
284
285
286
287
288
289 public void setOutputLicenses(String outputLicenses) {
290 setArg("output-licenses", outputLicenses);
291
292 }
293
294
295
296
297
298 public void setEditLicense(boolean editLicense) {
299 if (editLicense) { setArg("edit-license", null); } else { removeArg("edit-license"); }
300 }
301
302
303
304
305
306 public void setInputExcludeSize(String inputExcludeSize) {
307 setArg("input-exclude-size", inputExcludeSize);
308
309 }
310
311
312
313
314
315
316 @Deprecated
317 public void setCopyright(String copyright) {
318 setArg("copyright", copyright);
319
320 }
321
322
323
324
325
326 public void setOutputArchive(String outputArchive) {
327 setArg("output-archive", outputArchive);
328
329 }
330
331
332
333
334
335 public void setEditOverwrite(boolean editOverwrite) {
336 if (editOverwrite) { setArg("edit-overwrite", null); } else { removeArg("edit-overwrite"); }
337 }
338
339
340
341
342
343 public void setConfigurationNoDefaults(boolean configurationNoDefaults) {
344 if (configurationNoDefaults) { setArg("configuration-no-defaults", null); } else { removeArg("configuration-no-defaults"); }
345 }
346
347
348
349
350
351
352 @Deprecated
353 public void setOut(String out) {
354 setArg("out", out);
355
356 }
357
358
359
360
361
362
363 @Deprecated
364 public void setIncludesFile(String includesFile) {
365 setArg("includes-file", includesFile);
366
367 }
368
369
370
371
372
373 public void setLicensesDeniedFile(String licensesDeniedFile) {
374 setArg("licenses-denied-file", licensesDeniedFile);
375
376 }
377
378
379
380
381
382
383 @Deprecated
384 public void setExcludeFile(String excludeFile) {
385 setArg("exclude-file", excludeFile);
386
387 }
388
389
390
391
392
393 public void setLicensesApprovedFile(String licensesApprovedFile) {
394 setArg("licenses-approved-file", licensesApprovedFile);
395
396 }
397
398
399
400
401
402 public void setOutputStyle(String outputStyle) {
403 setArg("output-style", outputStyle);
404
405 }
406
407
408
409
410
411 public void setDryRun(boolean dryRun) {
412 if (dryRun) { setArg("dry-run", null); } else { removeArg("dry-run"); }
413 }
414
415
416
417
418
419 public void setOutputFile(String outputFile) {
420 setArg("output-file", outputFile);
421
422 }
423
424
425
426
427
428 public void setOutputStandard(String outputStandard) {
429 setArg("output-standard", outputStandard);
430
431 }
432
433
434
435
436
437 public void setLicenseFamiliesApprovedFile(String licenseFamiliesApprovedFile) {
438 setArg("license-families-approved-file", licenseFamiliesApprovedFile);
439
440 }
441
442
443
444
445
446
447 @Deprecated
448 public void setAddLicense(boolean addLicense) {
449 if (addLicense) { setArg("addLicense", null); } else { removeArg("addLicense"); }
450 }
451
452
453
454
455
456
457 @Deprecated
458 public void setStylesheet(String stylesheet) {
459 setArg("stylesheet", stylesheet);
460
461 }
462
463
464
465
466
467 public void setEditCopyright(String editCopyright) {
468 setArg("edit-copyright", editCopyright);
469
470 }
471
472
473
474
475
476 public void setOutputFamilies(String outputFamilies) {
477 setArg("output-families", outputFamilies);
478
479 }
480
481
482
483
484
485 public void setLicenseFamiliesDeniedFile(String licenseFamiliesDeniedFile) {
486 setArg("license-families-denied-file", licenseFamiliesDeniedFile);
487
488 }
489
490
491
492
493
494
495 @Deprecated
496 public void setScanHiddenDirectories(boolean scanHiddenDirectories) {
497 if (scanHiddenDirectories) { setArg("scan-hidden-directories", null); } else { removeArg("scan-hidden-directories"); }
498 }
499
500
501
502
503
504
505 @Deprecated
506 public void setXml(boolean xml) {
507 if (xml) { setArg("xml", null); } else { removeArg("xml"); }
508 }
509
510
511
512
513
514
515 @Deprecated
516 public void setNoDefaultLicenses(boolean noDefaultLicenses) {
517 if (noDefaultLicenses) { setArg("no-default-licenses", null); } else { removeArg("no-default-licenses"); }
518 }
519
520
521
522
523
524
525
526
527 public CounterMins createCounterMins() {
528 return new CounterMins();
529 }
530
531 public class CounterMins {
532 CounterMins() { }
533 public void addConfiguredCntr(Cntr counterPattern) {
534 addArg("counter-min", counterPattern.value);
535 }
536 }
537
538
539
540 public InputIncludes createInputIncludes() {
541 return new InputIncludes();
542 }
543
544 public class InputIncludes {
545 InputIncludes() { }
546 public void addConfiguredExpr(Expr expression) {
547 addArg("input-include", expression.value);
548 }
549 public void addConfiguredStd(Std standardCollection) {
550 addArg("input-include-std", standardCollection.value);
551 }
552 public void addConfiguredFileset(FileSet fileSet) {
553 for (Resource resource : fileSet) {
554 if (resource.isFilesystemOnly()) {
555 addArg("input-include-file", ((FileResource) resource).getFile().getAbsolutePath());
556 }
557 }
558 }
559 }
560
561
562
563 public Configs createConfigs() {
564 return new Configs();
565 }
566
567 public class Configs {
568 Configs() { }
569 public void addConfiguredFileset(FileSet fileSet) {
570 for (Resource resource : fileSet) {
571 if (resource.isFilesystemOnly()) {
572 addArg("config", ((FileResource) resource).getFile().getAbsolutePath());
573 }
574 }
575 }
576 }
577
578
579
580
581 public Licenses createLicenses() {
582 return new Licenses();
583 }
584
585 public class Licenses {
586 Licenses() { }
587 public void addConfiguredFileset(FileSet fileSet) {
588 for (Resource resource : fileSet) {
589 if (resource.isFilesystemOnly()) {
590 addArg("licenses", ((FileResource) resource).getFile().getAbsolutePath());
591 }
592 }
593 }
594 }
595
596
597
598 public InputExcludeStds createInputExcludeStds() {
599 return new InputExcludeStds();
600 }
601
602 public class InputExcludeStds {
603 InputExcludeStds() { }
604 public void addConfiguredStd(Std standardCollection) {
605 addArg("input-exclude-std", standardCollection.value);
606 }
607 }
608
609
610
611
612 public Excludes createExcludes() {
613 return new Excludes();
614 }
615
616 public class Excludes {
617 Excludes() { }
618 public void addConfiguredExpr(Expr expression) {
619 addArg("exclude", expression.value);
620 }
621 }
622
623
624
625 public LicenseFamiliesDenied createLicenseFamiliesDenied() {
626 return new LicenseFamiliesDenied();
627 }
628
629 public class LicenseFamiliesDenied {
630 LicenseFamiliesDenied() { }
631 public void addConfiguredLst(Lst familyID) {
632 addArg("license-families-denied", familyID.value);
633 }
634 public void addConfiguredFileset(FileSet fileSet) {
635 for (Resource resource : fileSet) {
636 if (resource.isFilesystemOnly()) {
637 addArg("license-families-denied-file", ((FileResource) resource).getFile().getAbsolutePath());
638 }
639 }
640 }
641 }
642
643
644
645 public InputIncludeStds createInputIncludeStds() {
646 return new InputIncludeStds();
647 }
648
649 public class InputIncludeStds {
650 InputIncludeStds() { }
651 public void addConfiguredStd(Std standardCollection) {
652 addArg("input-include-std", standardCollection.value);
653 }
654 }
655
656
657
658 public CounterMaxs createCounterMaxs() {
659 return new CounterMaxs();
660 }
661
662 public class CounterMaxs {
663 CounterMaxs() { }
664 public void addConfiguredCntr(Cntr counterPattern) {
665 addArg("counter-max", counterPattern.value);
666 }
667 }
668
669
670
671 public InputExcludes createInputExcludes() {
672 return new InputExcludes();
673 }
674
675 public class InputExcludes {
676 InputExcludes() { }
677 public void addConfiguredExpr(Expr expression) {
678 addArg("input-exclude", expression.value);
679 }
680 public void addConfiguredStd(Std standardCollection) {
681 addArg("input-exclude-std", standardCollection.value);
682 }
683 public void addConfiguredFileset(FileSet fileSet) {
684 for (Resource resource : fileSet) {
685 if (resource.isFilesystemOnly()) {
686 addArg("input-exclude-file", ((FileResource) resource).getFile().getAbsolutePath());
687 }
688 }
689 }
690 }
691
692
693
694 public LicenseFamiliesApproved createLicenseFamiliesApproved() {
695 return new LicenseFamiliesApproved();
696 }
697
698 public class LicenseFamiliesApproved {
699 LicenseFamiliesApproved() { }
700 public void addConfiguredLst(Lst familyID) {
701 addArg("license-families-approved", familyID.value);
702 }
703 public void addConfiguredFileset(FileSet fileSet) {
704 for (Resource resource : fileSet) {
705 if (resource.isFilesystemOnly()) {
706 addArg("license-families-approved-file", ((FileResource) resource).getFile().getAbsolutePath());
707 }
708 }
709 }
710 }
711
712
713
714 public LicensesDenied createLicensesDenied() {
715 return new LicensesDenied();
716 }
717
718 public class LicensesDenied {
719 LicensesDenied() { }
720 public void addConfiguredLst(Lst licenseID) {
721 addArg("licenses-denied", licenseID.value);
722 }
723 public void addConfiguredFileset(FileSet fileSet) {
724 for (Resource resource : fileSet) {
725 if (resource.isFilesystemOnly()) {
726 addArg("licenses-denied-file", ((FileResource) resource).getFile().getAbsolutePath());
727 }
728 }
729 }
730 }
731
732
733
734 public InputExcludeParsedScms createInputExcludeParsedScms() {
735 return new InputExcludeParsedScms();
736 }
737
738 public class InputExcludeParsedScms {
739 InputExcludeParsedScms() { }
740 public void addConfiguredStd(Std standardCollection) {
741 addArg("input-exclude-parsed-scm", standardCollection.value);
742 }
743 }
744
745
746
747 public LicensesApproved createLicensesApproved() {
748 return new LicensesApproved();
749 }
750
751 public class LicensesApproved {
752 LicensesApproved() { }
753 public void addConfiguredLst(Lst licenseID) {
754 addArg("licenses-approved", licenseID.value);
755 }
756 public void addConfiguredFileset(FileSet fileSet) {
757 for (Resource resource : fileSet) {
758 if (resource.isFilesystemOnly()) {
759 addArg("licenses-approved-file", ((FileResource) resource).getFile().getAbsolutePath());
760 }
761 }
762 }
763 }
764
765
766
767
768 public Includes createIncludes() {
769 return new Includes();
770 }
771
772 public class Includes {
773 Includes() { }
774 public void addConfiguredExpr(Expr expression) {
775 addArg("include", expression.value);
776 }
777 }
778
779
780
781 protected static class TxtValue {
782 protected TxtValue() { }
783
784 public String value;
785
786 public void addText(String text) {
787 value = text.trim();
788 }
789 }
790
791 public static class Std extends TxtValue {
792 public Std() { }
793 }
794
795 public static class Expr extends TxtValue {
796 public Expr() { }
797 }
798
799 public static class Cntr extends TxtValue {
800 public Cntr() { }
801 }
802
803 public static class Filename extends TxtValue {
804 public Filename() { }
805 }
806
807 public static class Lst extends TxtValue {
808 public Lst() { }
809 }
810 }