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