View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.utils.CasedString;
26  import org.apache.rat.utils.DefaultLog;
27  import org.apache.rat.utils.Log;
28  import org.apache.tools.ant.Task;
29  
30  import java.util.ArrayList;
31  import java.util.Arrays;
32  import java.util.Collections;
33  import java.util.HashMap;
34  import java.util.List;
35  import java.util.Locale;
36  import java.util.Map;
37  import java.util.Objects;
38  import java.util.stream.Collectors;
39  
40  /**
41   * Generated class to provide Ant support for standard RAT command line options.
42   *
43   * DO NOT EDIT - GENERATED FILE
44   */
45  public abstract class BaseAntTask extends Task {
46  
47      private final static Map<String,String> xlateName = new HashMap<>();
48  
49      private final static List<String> unsupportedArgs = new ArrayList<>();
50  
51      static {
52          xlateName.put("addLicense", "add-license");
53          unsupportedArgs.add("log-level");
54          unsupportedArgs.add("dir");
55          unsupportedArgs.add("help");
56      }
57  
58      public static String createName(String longOpt) {
59          String name = StringUtils.defaultIfEmpty(xlateName.get(longOpt), longOpt).toLowerCase(Locale.ROOT);
60          return new CasedString(CasedString.StringCase.KEBAB, name).toCase(CasedString.StringCase.CAMEL);
61      }
62  
63      public static List<String> unsupportedArgs() {
64          return Collections.unmodifiableList(unsupportedArgs);
65      }
66  
67      ///////////////////////// Start common Arg manipulation code
68  
69      /**
70       * A map of CLI based arguments to values.
71       */
72      protected final Map<String, List<String>> args = new HashMap<>();
73  
74      /**
75       * Gets the list of arguments prepared for the CLI code to parse.
76       * @return the List of arguments for the CLI command line.
77       */
78      protected List<String> args() {
79          List<String> result = new ArrayList<>();
80          for (Map.Entry<String, List<String>> entry : args.entrySet()) {
81              result.add("--" + entry.getKey());
82              result.addAll(entry.getValue().stream().filter(Objects::nonNull).collect(Collectors.toList()));
83          }
84          return result;
85      }
86  
87      private String argsKey(Option opt) {
88          return StringUtils.defaultIfEmpty(opt.getLongOpt(), opt.getKey());
89      }
90  
91      private boolean validateSet(String key) {
92          Arg arg = Arg.findArg(key);
93          if (arg != null) {
94              Option opt = arg.find(key);
95              Option main = arg.option();
96              if (opt.isDeprecated()) {
97                  args.remove(argsKey(main));
98                  // deprecated options must be explicitly set so let it go.
99                  return true;
100             }
101             // non-deprecated options may have default so ignore it if another option has already been set.
102             for (Option o : arg.group().getOptions()) {
103                 if (!o.equals(main)) {
104                     if (args.containsKey(argsKey(o))) {
105                         return false;
106                     }
107                 }
108             }
109             return true;
110         }
111         return false;
112     }
113 
114     /**
115      * Set a key and value into the argument list.
116      * Replaces any existing value.
117      * @param key the key for the map.
118      * @param value the value to set.
119      */
120     protected void setArg(String key, String value) {
121         if (validateSet(key)) {
122             List<String> values = new ArrayList<>();
123             if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
124                 DefaultLog.getInstance().debug(String.format("Adding [%s] to %s", String.join(", ", values), key));
125             }
126             values.add(value);
127             args.put(key, values);
128         }
129     }
130 
131     /**
132      * Get the list of values for a key.
133      * @param key the key for the map.
134      * @return the list of values for the key or {@code null} if not set.
135      */
136     public List<String> getArg(String key) {
137         return args.get(key);
138     }
139 
140     /**
141      * Add values to the key in the argument list.
142      * If the key does not exist, adds it.
143      * @param key the key for the map.
144      * @param value the value to set.
145      */
146     protected void addArg(String key, String[] value) {
147         if (validateSet(key)) {
148             if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
149                 DefaultLog.getInstance().debug(String.format("Adding [%s] to %s", String.join(", ", Arrays.asList(value)), key));
150             }
151             List<String> values = args.get(key);
152             if (values == null) {
153                 values = new ArrayList<>();
154                 args.put(key, values);
155             }
156             values.addAll(Arrays.asList(value));
157         }
158     }
159 
160     /**
161      * Add a value to the key in the argument list.
162      * If the key does not exist, adds it.
163      * @param key the key for the map.
164      * @param value the value to set.
165      */
166     protected void addArg(String key, String value) {
167         if (validateSet(key)) {
168             List<String> values = args.get(key);
169             if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
170                 DefaultLog.getInstance().debug(String.format("Adding [%s] to %s", String.join(", ", Arrays.asList(value)), key));
171             }
172             if (values == null) {
173                 values = new ArrayList<>();
174                 args.put(key, values);
175             }
176             values.add(value);
177         }
178     }
179 
180     /**
181      * Remove a key from the argument list.
182      * @param key the key to remove from the map.
183      */
184     protected void removeArg(String key) {
185         args.remove(key);
186     }
187 
188  ///////////////////////// End common Arg manipulation code
189 
190     protected BaseAntTask() {}
191 
192     /**
193      * A child element.
194      */
195     protected class Child {
196         final String key;
197 
198         /**
199          * Constructor.
200          * @param key The key for this child in the CLI map.
201          */
202         protected Child(String key) {
203             this.key = key;
204         }
205 
206         /**
207          * Sets the text enclosed in the child element.
208          * @param arg The text enclosed in the child element.
209          */
210         public void addText(String arg) {
211             addArg(key, arg);
212         }
213     }
214 
215     /*  GENERATED METHODS */
216 
217     /**
218      * The copyright message to use in the license headers.
219      * Deprecated for removal since 0.17: Use --edit-copyright instead.
220      * @deprecated
221      * @param copyright The value to set
222      */
223     public void setCopyright(String copyright) {
224         setArg("copyright", copyright);
225 
226     }
227 
228     /**
229      * The copyright message to use in the license headers. Usually in the form of &quot;Copyright 2008 Foo&quot;.  Only valid with editLicense attribute
230      * @param editCopyright The value to set
231      */
232     public void setEditCopyright(String editCopyright) {
233         setArg("edit-copyright", editCopyright);
234 
235     }
236 
237     /**
238      * Forces any changes in files to be written directly to the source files (i.e. new files are not created).
239      * Deprecated for removal since 0.17: Use --edit-overwrite instead.
240      * @deprecated
241      */
242     public void setForce(boolean force) {
243         if (force) { setArg("force", null); } else { removeArg("force"); }
244     }
245 
246     /**
247      * Forces any changes in files to be written directly to the source files (i.e. new files are not created). Only valid with editLicense attribute
248      */
249     public void setEditOverwrite(boolean editOverwrite) {
250         if (editOverwrite) { setArg("edit-overwrite", null); } else { removeArg("edit-overwrite"); }
251     }
252 
253     /**
254      * Add the default license header to any file with an unknown license that is not in the exclusion list.
255      * Deprecated for removal since 0.17: Use --edit-license instead.
256      * @deprecated
257      */
258     public void setAddLicense(boolean addLicense) {
259         if (addLicense) { setArg("addLicense", null); } else { removeArg("addLicense"); }
260     }
261 
262     /**
263      * Add the default license header to any file with an unknown license that is not in the exclusion list. By default new files will be created with the license header, to force the modification of existing files use the editOverwrite attribute option.
264      */
265     public void setEditLicense(boolean editLicense) {
266         if (editLicense) { setArg("edit-license", null); } else { removeArg("edit-license"); }
267     }
268 
269     /**
270      * Ignore default configuration.
271      */
272     public void setConfigurationNoDefaults(boolean configurationNoDefaults) {
273         if (configurationNoDefaults) { setArg("configuration-no-defaults", null); } else { removeArg("configuration-no-defaults"); }
274     }
275 
276     /**
277      * Ignore default configuration.
278      * Deprecated for removal since 0.17: Use --configuration-no-defaults instead.
279      * @deprecated
280      */
281     public void setNoDefaultLicenses(boolean noDefaultLicenses) {
282         if (noDefaultLicenses) { setArg("no-default-licenses", null); } else { removeArg("no-default-licenses"); }
283     }
284 
285     /**
286      * Name of file containing the approved License IDs.
287      * @param licensesApprovedFile The value to set
288      */
289     public void setLicensesApprovedFile(String licensesApprovedFile) {
290         setArg("licenses-approved-file", licensesApprovedFile);
291 
292     }
293 
294     /**
295      * Name of file containing the approved family IDs.
296      * @param licenseFamiliesApprovedFile The value to set
297      */
298     public void setLicenseFamiliesApprovedFile(String licenseFamiliesApprovedFile) {
299         setArg("license-families-approved-file", licenseFamiliesApprovedFile);
300 
301     }
302 
303     /**
304      * Name of file containing the denied license IDs.
305      * @param licensesDeniedFile The value to set
306      */
307     public void setLicensesDeniedFile(String licensesDeniedFile) {
308         setArg("licenses-denied-file", licensesDeniedFile);
309 
310     }
311 
312     /**
313      * Name of file containing the denied license IDs.
314      * @param licenseFamiliesDeniedFile The value to set
315      */
316     public void setLicenseFamiliesDeniedFile(String licenseFamiliesDeniedFile) {
317         setArg("license-families-denied-file", licenseFamiliesDeniedFile);
318 
319     }
320 
321     /**
322      * Reads &lt;Expression&gt; entries from a file. Entries will be excluded from processing.
323      * Deprecated for removal since 0.17: Use --input-exclude-file instead.
324      * @deprecated
325      * @param excludeFile The value to set
326      */
327     public void setExcludeFile(String excludeFile) {
328         setArg("exclude-file", excludeFile);
329 
330     }
331 
332     /**
333      * Reads &lt;Expression&gt; entries from a file. Entries will be excluded from processing.
334      * @param inputExcludeFile The value to set
335      */
336     public void setInputExcludeFile(String inputExcludeFile) {
337         setArg("input-exclude-file", inputExcludeFile);
338 
339     }
340 
341     /**
342      * Excludes files with sizes less than the given argument.
343      * @param inputExcludeSize The value to set
344      */
345     public void setInputExcludeSize(String inputExcludeSize) {
346         setArg("input-exclude-size", inputExcludeSize);
347 
348     }
349 
350     /**
351      * Reads &lt;Expression&gt; entries from a file. Entries will override excluded files.
352      * @param inputIncludeFile The value to set
353      */
354     public void setInputIncludeFile(String inputIncludeFile) {
355         setArg("input-include-file", inputIncludeFile);
356 
357     }
358 
359     /**
360      * Reads &lt;Expression&gt; entries from a file. Entries will be excluded from processing.
361      * Deprecated for removal since 0.17: Use --input-include-file instead.
362      * @deprecated
363      * @param includesFile The value to set
364      */
365     public void setIncludesFile(String includesFile) {
366         setArg("includes-file", includesFile);
367 
368     }
369 
370     /**
371      * Scans hidden directories.
372      * Deprecated for removal since 0.17: Use --input-include-std with 'HIDDEN_DIR' argument instead.
373      * @deprecated
374      */
375     public void setScanHiddenDirectories(boolean scanHiddenDirectories) {
376         if (scanHiddenDirectories) { setArg("scan-hidden-directories", null); } else { removeArg("scan-hidden-directories"); }
377     }
378 
379     /**
380      * XSLT stylesheet to use when creating the report. Either an external xsl file may be specified or one of the internal named sheets.
381      * @param outputStyle The value to set
382      */
383     public void setOutputStyle(String outputStyle) {
384         setArg("output-style", outputStyle);
385 
386     }
387 
388     /**
389      * XSLT stylesheet to use when creating the report.
390      * Deprecated for removal since 0.17: Use --output-style instead.
391      * @deprecated
392      * @param stylesheet The value to set
393      */
394     public void setStylesheet(String stylesheet) {
395         setArg("stylesheet", stylesheet);
396 
397     }
398 
399     /**
400      * forces XML output rather than the textual report.
401      * Deprecated for removal since 0.17: Use --output-style with the 'xml' argument instead.
402      * @deprecated
403      */
404     public void setXml(boolean xml) {
405         if (xml) { setArg("xml", null); } else { removeArg("xml"); }
406     }
407 
408     /**
409      * List the defined licenses.
410      * @param outputLicenses The value to set
411      */
412     public void setOutputLicenses(String outputLicenses) {
413         setArg("output-licenses", outputLicenses);
414 
415     }
416 
417     /**
418      * List the defined licenses.
419      * Deprecated for removal since 0.17: Use --output-licenses instead.
420      * @deprecated
421      * @param listLicenses The value to set
422      */
423     public void setListLicenses(String listLicenses) {
424         setArg("list-licenses", listLicenses);
425 
426     }
427 
428     /**
429      * List the defined license families.
430      * @param outputFamilies The value to set
431      */
432     public void setOutputFamilies(String outputFamilies) {
433         setArg("output-families", outputFamilies);
434 
435     }
436 
437     /**
438      * List the defined license families.
439      * Deprecated for removal since 0.17: Use --output-families instead.
440      * @deprecated
441      * @param listFamilies The value to set
442      */
443     public void setListFamilies(String listFamilies) {
444         setArg("list-families", listFamilies);
445 
446     }
447 
448     /**
449      * If set do not update the files but generate the reports.
450      */
451     public void setDryRun(boolean dryRun) {
452         if (dryRun) { setArg("dry-run", null); } else { removeArg("dry-run"); }
453     }
454 
455     /**
456      * Define the output file where to write a report to.
457      * Deprecated for removal since 0.17: Use --output-file instead.
458      * @deprecated
459      * @param out The value to set
460      */
461     public void setOut(String out) {
462         setArg("out", out);
463 
464     }
465 
466     /**
467      * Define the output file where to write a report to.
468      * @param outputFile The value to set
469      */
470     public void setOutputFile(String outputFile) {
471         setArg("output-file", outputFile);
472 
473     }
474 
475     /**
476      * Specifies the level of detail in ARCHIVE file reporting.
477      * @param outputArchive The value to set
478      */
479     public void setOutputArchive(String outputArchive) {
480         setArg("output-archive", outputArchive);
481 
482     }
483 
484     /**
485      * Specifies the level of detail in STANDARD file reporting.
486      * @param outputStandard The value to set
487      */
488     public void setOutputStandard(String outputStandard) {
489         setArg("output-standard", outputStandard);
490 
491     }
492 
493     /**
494      * Print information about registered licenses.
495      */
496     public void setHelpLicenses(boolean helpLicenses) {
497         if (helpLicenses) { setArg("help-licenses", null); } else { removeArg("help-licenses"); }
498     }
499 
500 
501 
502     /*  GENERATED CLASSES */
503 
504     /**
505      * File names for system configuration.
506      */
507     public Config createConfig() {
508         return new Config();
509     }
510 
511     public class Config extends Child { Config() {super("config");}}
512 
513     /**
514      * File names for system configuration.
515      * Deprecated for removal since 0.17: Use --config instead.
516      * @deprecated
517      */
518     public Licenses createLicenses() {
519         return new Licenses();
520     }
521 
522     public class Licenses extends Child { Licenses() {super("licenses");}}
523 
524     /**
525      * The approved License IDs. These licenses will be added to the list of approved licenses.
526      */
527     public LicensesApproved createLicensesApproved() {
528         return new LicensesApproved();
529     }
530 
531     public class LicensesApproved extends Child { LicensesApproved() {super("licenses-approved");}}
532 
533     /**
534      * Name of file containing the approved License IDs.
535      */
536     public LicensesApprovedFile createLicensesApprovedFile() {
537         return new LicensesApprovedFile();
538     }
539 
540     public class LicensesApprovedFile extends Child { LicensesApprovedFile() {super("licenses-approved-file");}}
541 
542     /**
543      * The approved License Family IDs. These licenses families will be added to the list of approved licenses families.
544      */
545     public LicenseFamiliesApproved createLicenseFamiliesApproved() {
546         return new LicenseFamiliesApproved();
547     }
548 
549     public class LicenseFamiliesApproved extends Child { LicenseFamiliesApproved() {super("license-families-approved");}}
550 
551     /**
552      * Name of file containing the approved family IDs.
553      */
554     public LicenseFamiliesApprovedFile createLicenseFamiliesApprovedFile() {
555         return new LicenseFamiliesApprovedFile();
556     }
557 
558     public class LicenseFamiliesApprovedFile extends Child { LicenseFamiliesApprovedFile() {super("license-families-approved-file");}}
559 
560     /**
561      * The denied License IDs. These licenses will be removed from the list of approved licenses. Once licenses are removed they can not be added back.
562      */
563     public LicensesDenied createLicensesDenied() {
564         return new LicensesDenied();
565     }
566 
567     public class LicensesDenied extends Child { LicensesDenied() {super("licenses-denied");}}
568 
569     /**
570      * Name of file containing the denied license IDs.
571      */
572     public LicensesDeniedFile createLicensesDeniedFile() {
573         return new LicensesDeniedFile();
574     }
575 
576     public class LicensesDeniedFile extends Child { LicensesDeniedFile() {super("licenses-denied-file");}}
577 
578     /**
579      * The denied License family IDs. These license families will be removed from the list of approved licenses.
580      */
581     public LicenseFamiliesDenied createLicenseFamiliesDenied() {
582         return new LicenseFamiliesDenied();
583     }
584 
585     public class LicenseFamiliesDenied extends Child { LicenseFamiliesDenied() {super("license-families-denied");}}
586 
587     /**
588      * Name of file containing the denied license IDs.
589      */
590     public LicenseFamiliesDeniedFile createLicenseFamiliesDeniedFile() {
591         return new LicenseFamiliesDeniedFile();
592     }
593 
594     public class LicenseFamiliesDeniedFile extends Child { LicenseFamiliesDeniedFile() {super("license-families-denied-file");}}
595 
596     /**
597      * The acceptable maximum number for the specified counter. A value of '-1' specifies an unlimited number.
598      */
599     public CounterMax createCounterMax() {
600         return new CounterMax();
601     }
602 
603     public class CounterMax extends Child { CounterMax() {super("counter-max");}}
604 
605     /**
606      * The minimum number for the specified counter.
607      */
608     public CounterMin createCounterMin() {
609         return new CounterMin();
610     }
611 
612     public class CounterMin extends Child { CounterMin() {super("counter-min");}}
613 
614     /**
615      * A file containing file names to process. File names must use linux directory separator ('/') or none at all. File names that do not start with '/' are relative to the directory where the argument is located.
616      */
617     public InputSource createInputSource() {
618         return new InputSource();
619     }
620 
621     public class InputSource extends Child { InputSource() {super("input-source");}}
622 
623     /**
624      * Excludes files matching &lt;Expression&gt;.
625      * Deprecated for removal since 0.17: Use --input-exclude instead.
626      * @deprecated
627      */
628     public Exclude createExclude() {
629         return new Exclude();
630     }
631 
632     public class Exclude extends Child { Exclude() {super("exclude");}}
633 
634     /**
635      * Excludes files matching &lt;Expression&gt;.
636      */
637     public InputExclude createInputExclude() {
638         return new InputExclude();
639     }
640 
641     public class InputExclude extends Child { InputExclude() {super("input-exclude");}}
642 
643     /**
644      * Reads &lt;Expression&gt; entries from a file. Entries will be excluded from processing.
645      * Deprecated for removal since 0.17: Use --input-exclude-file instead.
646      * @deprecated
647      */
648     public ExcludeFile createExcludeFile() {
649         return new ExcludeFile();
650     }
651 
652     public class ExcludeFile extends Child { ExcludeFile() {super("exclude-file");}}
653 
654     /**
655      * Reads &lt;Expression&gt; entries from a file. Entries will be excluded from processing.
656      */
657     public InputExcludeFile createInputExcludeFile() {
658         return new InputExcludeFile();
659     }
660 
661     public class InputExcludeFile extends Child { InputExcludeFile() {super("input-exclude-file");}}
662 
663     /**
664      * Excludes files defined in standard collections based on commonly occurring groups.
665      */
666     public InputExcludeStd createInputExcludeStd() {
667         return new InputExcludeStd();
668     }
669 
670     public class InputExcludeStd extends Child { InputExcludeStd() {super("input-exclude-std");}}
671 
672     /**
673      * Excludes files with sizes less than the given argument.
674      */
675     public InputExcludeSize createInputExcludeSize() {
676         return new InputExcludeSize();
677     }
678 
679     public class InputExcludeSize extends Child { InputExcludeSize() {super("input-exclude-size");}}
680 
681     /**
682      * Includes files matching &lt;Expression&gt;. Will override excluded files.
683      */
684     public InputInclude createInputInclude() {
685         return new InputInclude();
686     }
687 
688     public class InputInclude extends Child { InputInclude() {super("input-include");}}
689 
690     /**
691      * Includes files matching &lt;Expression&gt;. Will override excluded files.
692      * Deprecated for removal since 0.17: Use --input-include instead.
693      * @deprecated
694      */
695     public Include createInclude() {
696         return new Include();
697     }
698 
699     public class Include extends Child { Include() {super("include");}}
700 
701     /**
702      * Reads &lt;Expression&gt; entries from a file. Entries will override excluded files.
703      */
704     public InputIncludeFile createInputIncludeFile() {
705         return new InputIncludeFile();
706     }
707 
708     public class InputIncludeFile extends Child { InputIncludeFile() {super("input-include-file");}}
709 
710     /**
711      * Reads &lt;Expression&gt; entries from a file. Entries will be excluded from processing.
712      * Deprecated for removal since 0.17: Use --input-include-file instead.
713      * @deprecated
714      */
715     public IncludesFile createIncludesFile() {
716         return new IncludesFile();
717     }
718 
719     public class IncludesFile extends Child { IncludesFile() {super("includes-file");}}
720 
721     /**
722      * Includes files defined in standard collections based on commonly occurring groups. Will override excluded files.
723      */
724     public InputIncludeStd createInputIncludeStd() {
725         return new InputIncludeStd();
726     }
727 
728     public class InputIncludeStd extends Child { InputIncludeStd() {super("input-include-std");}}
729 
730     /**
731      * Parse SCM based exclusion files to exclude specified files and directories.
732      */
733     public InputExcludeParsedScm createInputExcludeParsedScm() {
734         return new InputExcludeParsedScm();
735     }
736 
737     public class InputExcludeParsedScm extends Child { InputExcludeParsedScm() {super("input-exclude-parsed-scm");}}
738 
739     /**
740      * Define the output file where to write a report to.
741      * Deprecated for removal since 0.17: Use --output-file instead.
742      * @deprecated
743      */
744     public Out createOut() {
745         return new Out();
746     }
747 
748     public class Out extends Child { Out() {super("out");}}
749 
750     /**
751      * Define the output file where to write a report to.
752      */
753     public OutputFile createOutputFile() {
754         return new OutputFile();
755     }
756 
757     public class OutputFile extends Child { OutputFile() {super("output-file");}}
758 
759 
760 }