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