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.get(key);
201 if (values == null) {
202 values = new ArrayList<>();
203 args.put(key, values);
204 }
205 values.addAll(newValues);
206 }
207 }
208 }
209
210
211
212
213
214
215
216 protected void addArg(String key, String value) {
217 if (StringUtils.isNotBlank(value)) {
218 if (validateSet(key)) {
219 List<String> values = args.get(key);
220 if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
221 DefaultLog.getInstance().debug(String.format("Adding [%s] to %s", String.join(", ", Arrays.asList(value)), key));
222 }
223 if (values == null) {
224 values = new ArrayList<>();
225 args.put(key, values);
226 }
227 values.add(value);
228 }
229 }
230 }
231
232
233
234
235
236 protected void removeArg(String key) {
237 args.remove(key);
238 }
239
240
241
242 protected BaseAntTask() {
243 setDeprecationReporter();
244 }
245
246
247
248
249
250
251
252
253 public void setCopyright(String copyright) {
254 setArg("copyright", copyright);
255
256 }
257
258
259
260
261
262 public void setEditCopyright(String editCopyright) {
263 setArg("edit-copyright", editCopyright);
264
265 }
266
267
268
269
270
271
272 public void setForce(boolean force) {
273 if (force) { setArg("force", null); } else { removeArg("force"); }
274 }
275
276
277
278
279
280 public void setEditOverwrite(boolean editOverwrite) {
281 if (editOverwrite) { setArg("edit-overwrite", null); } else { removeArg("edit-overwrite"); }
282 }
283
284
285
286
287
288
289 public void setAddLicense(boolean addLicense) {
290 if (addLicense) { setArg("addLicense", null); } else { removeArg("addLicense"); }
291 }
292
293
294
295
296
297 public void setEditLicense(boolean editLicense) {
298 if (editLicense) { setArg("edit-license", null); } else { removeArg("edit-license"); }
299 }
300
301
302
303
304
305 public void setConfigurationNoDefaults(boolean configurationNoDefaults) {
306 if (configurationNoDefaults) { setArg("configuration-no-defaults", null); } else { removeArg("configuration-no-defaults"); }
307 }
308
309
310
311
312
313
314 public void setNoDefaultLicenses(boolean noDefaultLicenses) {
315 if (noDefaultLicenses) { setArg("no-default-licenses", null); } else { removeArg("no-default-licenses"); }
316 }
317
318
319
320
321
322
323 public void setExcludeFile(String excludeFile) {
324 setArg("exclude-file", excludeFile);
325
326 }
327
328
329
330
331
332 public void setInputExcludeSize(String inputExcludeSize) {
333 setArg("input-exclude-size", inputExcludeSize);
334
335 }
336
337
338
339
340
341
342 public void setIncludesFile(String includesFile) {
343 setArg("includes-file", includesFile);
344
345 }
346
347
348
349
350
351
352 public void setScanHiddenDirectories(boolean scanHiddenDirectories) {
353 if (scanHiddenDirectories) { setArg("scan-hidden-directories", null); } else { removeArg("scan-hidden-directories"); }
354 }
355
356
357
358
359
360 public void setOutputStyle(String outputStyle) {
361 setArg("output-style", outputStyle);
362
363 }
364
365
366
367
368
369
370 public void setStylesheet(String stylesheet) {
371 setArg("stylesheet", stylesheet);
372
373 }
374
375
376
377
378
379
380 public void setXml(boolean xml) {
381 if (xml) { setArg("xml", null); } else { removeArg("xml"); }
382 }
383
384
385
386
387
388 public void setOutputLicenses(String outputLicenses) {
389 setArg("output-licenses", outputLicenses);
390
391 }
392
393
394
395
396
397
398 public void setListLicenses(String listLicenses) {
399 setArg("list-licenses", listLicenses);
400
401 }
402
403
404
405
406
407 public void setOutputFamilies(String outputFamilies) {
408 setArg("output-families", outputFamilies);
409
410 }
411
412
413
414
415
416
417 public void setListFamilies(String listFamilies) {
418 setArg("list-families", listFamilies);
419
420 }
421
422
423
424
425
426 public void setDryRun(boolean dryRun) {
427 if (dryRun) { setArg("dry-run", null); } else { removeArg("dry-run"); }
428 }
429
430
431
432
433
434
435 public void setOut(String out) {
436 setArg("out", out);
437
438 }
439
440
441
442
443
444 public void setOutputFile(String outputFile) {
445 setArg("output-file", outputFile);
446
447 }
448
449
450
451
452
453 public void setOutputArchive(String outputArchive) {
454 setArg("output-archive", outputArchive);
455
456 }
457
458
459
460
461
462 public void setOutputStandard(String outputStandard) {
463 setArg("output-standard", outputStandard);
464
465 }
466
467
468
469
470
471 public void setHelpLicenses(boolean helpLicenses) {
472 if (helpLicenses) { setArg("help-licenses", null); } else { removeArg("help-licenses"); }
473 }
474
475
476
477
478
479
480
481
482 public Config createConfig() {
483 return new Config();
484 }
485
486 public class Config {
487 Config() { }
488
489 public void addConfiguredFileset(FileSet fileSet) {
490 for (Resource resource : fileSet) {
491 if (resource.isFilesystemOnly()) {
492 addArg("config", ((FileResource) resource).getFile().getAbsolutePath());
493 }
494 }
495 }
496
497 }
498
499
500
501
502 public Licenses createLicenses() {
503 return new Licenses();
504 }
505
506 public class Licenses {
507 Licenses() { }
508
509 public void addConfiguredFileset(FileSet fileSet) {
510 for (Resource resource : fileSet) {
511 if (resource.isFilesystemOnly()) {
512 addArg("licenses", ((FileResource) resource).getFile().getAbsolutePath());
513 }
514 }
515 }
516
517 }
518
519
520
521 public LicensesApproved createLicensesApproved() {
522 return new LicensesApproved();
523 }
524
525 public class LicensesApproved {
526 LicensesApproved() { }
527
528 public void addConfiguredFileset(FileSet fileSet) {
529 for (Resource resource : fileSet) {
530 if (resource.isFilesystemOnly()) {
531 addArg("licenses-approved-file", ((FileResource) resource).getFile().getAbsolutePath());
532 }
533 }
534 }
535
536 public void addConfiguredLst(Lst licenseID) {
537 addArg("licenses-approved", licenseID.value);
538 }
539
540 }
541
542
543
544 public LicenseFamiliesApproved createLicenseFamiliesApproved() {
545 return new LicenseFamiliesApproved();
546 }
547
548 public class LicenseFamiliesApproved {
549 LicenseFamiliesApproved() { }
550
551 public void addConfiguredFileset(FileSet fileSet) {
552 for (Resource resource : fileSet) {
553 if (resource.isFilesystemOnly()) {
554 addArg("license-families-approved-file", ((FileResource) resource).getFile().getAbsolutePath());
555 }
556 }
557 }
558
559 public void addConfiguredLst(Lst familyID) {
560 addArg("license-families-approved", familyID.value);
561 }
562
563 }
564
565
566
567 public LicensesDenied createLicensesDenied() {
568 return new LicensesDenied();
569 }
570
571 public class LicensesDenied {
572 LicensesDenied() { }
573
574 public void addConfiguredFileset(FileSet fileSet) {
575 for (Resource resource : fileSet) {
576 if (resource.isFilesystemOnly()) {
577 addArg("licenses-denied-file", ((FileResource) resource).getFile().getAbsolutePath());
578 }
579 }
580 }
581
582 public void addConfiguredLst(Lst licenseID) {
583 addArg("licenses-denied", licenseID.value);
584 }
585
586 }
587
588
589
590 public LicenseFamiliesDenied createLicenseFamiliesDenied() {
591 return new LicenseFamiliesDenied();
592 }
593
594 public class LicenseFamiliesDenied {
595 LicenseFamiliesDenied() { }
596
597 public void addConfiguredFileset(FileSet fileSet) {
598 for (Resource resource : fileSet) {
599 if (resource.isFilesystemOnly()) {
600 addArg("license-families-denied-file", ((FileResource) resource).getFile().getAbsolutePath());
601 }
602 }
603 }
604
605 public void addConfiguredLst(Lst familyID) {
606 addArg("license-families-denied", familyID.value);
607 }
608
609 }
610
611
612
613 public CounterMax createCounterMax() {
614 return new CounterMax();
615 }
616
617 public class CounterMax {
618 CounterMax() { }
619
620 public void addConfiguredCntr(Cntr counterPattern) {
621 addArg("counter-max", counterPattern.value);
622 }
623
624 }
625
626
627
628 public CounterMin createCounterMin() {
629 return new CounterMin();
630 }
631
632 public class CounterMin {
633 CounterMin() { }
634
635 public void addConfiguredCntr(Cntr counterPattern) {
636 addArg("counter-min", counterPattern.value);
637 }
638
639 }
640
641
642
643
644 public Exclude createExclude() {
645 return new Exclude();
646 }
647
648 public class Exclude {
649 Exclude() { }
650
651 public void addConfiguredExpr(Expr expression) {
652 addArg("exclude", expression.value);
653 }
654
655 }
656
657
658
659 public InputExclude createInputExclude() {
660 return new InputExclude();
661 }
662
663 public class InputExclude {
664 InputExclude() { }
665
666 public void addConfiguredFileset(FileSet fileSet) {
667 for (Resource resource : fileSet) {
668 if (resource.isFilesystemOnly()) {
669 addArg("input-exclude-file", ((FileResource) resource).getFile().getAbsolutePath());
670 }
671 }
672 }
673
674 public void addConfiguredStd(Std standardCollection) {
675 addArg("input-exclude-std", standardCollection.value);
676 }
677
678 public void addConfiguredExpr(Expr expression) {
679 addArg("input-exclude", expression.value);
680 }
681
682 }
683
684
685
686 public InputInclude createInputInclude() {
687 return new InputInclude();
688 }
689
690 public class InputInclude {
691 InputInclude() { }
692
693 public void addConfiguredFileset(FileSet fileSet) {
694 for (Resource resource : fileSet) {
695 if (resource.isFilesystemOnly()) {
696 addArg("input-include-file", ((FileResource) resource).getFile().getAbsolutePath());
697 }
698 }
699 }
700
701 public void addConfiguredStd(Std standardCollection) {
702 addArg("input-include-std", standardCollection.value);
703 }
704
705 public void addConfiguredExpr(Expr expression) {
706 addArg("input-include", expression.value);
707 }
708
709 }
710
711
712
713
714 public Include createInclude() {
715 return new Include();
716 }
717
718 public class Include {
719 Include() { }
720
721 public void addConfiguredExpr(Expr expression) {
722 addArg("include", expression.value);
723 }
724
725 }
726
727
728
729 public InputExcludeParsedScm createInputExcludeParsedScm() {
730 return new InputExcludeParsedScm();
731 }
732
733 public class InputExcludeParsedScm {
734 InputExcludeParsedScm() { }
735
736 public void addConfiguredStd(Std standardCollection) {
737 addArg("input-exclude-parsed-scm", standardCollection.value);
738 }
739
740 }
741
742
743
744 protected static class TxtValue {
745 protected TxtValue() { }
746
747 public String value;
748
749 public void addText(String text) {
750 value = text.trim();
751 }
752 }
753
754 public static class Std extends TxtValue {
755 public Std() { }
756 }
757
758 public static class Expr extends TxtValue {
759 public Expr() { }
760 }
761
762 public static class Cntr extends TxtValue {
763 public Cntr() { }
764 }
765
766 public static class Filename extends TxtValue {
767 public Filename() { }
768 }
769
770 public static class Lst extends TxtValue {
771 public Lst() { }
772 }
773 }