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   *   https://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  package org.apache.rat.documentation.options;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Objects;
27  import java.util.Optional;
28  import java.util.Set;
29  import java.util.TreeMap;
30  import java.util.stream.Collectors;
31  
32  import org.apache.commons.cli.Option;
33  import org.apache.commons.lang3.StringUtils;
34  import org.apache.commons.text.WordUtils;
35  import org.apache.rat.OptionCollection;
36  import org.apache.rat.commandline.Arg;
37  import org.apache.rat.ui.ArgumentTracker;
38  import org.apache.rat.ui.UIOptionCollection;
39  import org.apache.rat.utils.CasedString;
40  
41  import static java.lang.String.format;
42  
43  /**
44   * The collection of AntOptions equivalent to the CLI options
45   * with any unsupported options removed.
46   */
47  public final class AntOptionCollection extends UIOptionCollection<AntOption> {
48      /** mapping of standard name to non-conflicting name. */
49      private static final Map<String, String> RENAME_MAP;
50  
51      /**
52       * The format for an XML element
53       */
54      private static final String DEFAULT_XML = "<%1$s>%%s</%1$s>%n";
55  
56      /** Attributes that are required for example data. */
57      private static final Map<String, Map<String, String>> REQUIRED_ATTRIBUTES = new HashMap<>();
58      /** The list of data types that are specified as XML attributes in Ant build.xml documents */
59      private static final List<Class<?>> ATTRIBUTE_TYPES = new ArrayList<>();
60  
61      /** The map of option name conversions. */
62      private final Map<Option, Option> conversionMap;
63  
64      static {
65          RENAME_MAP = new HashMap<>();
66          RENAME_MAP.put("addLicense", "add-license");
67  
68          Map<String, String> attributes = new HashMap<>();
69          attributes.put("editLicense", "true");
70          REQUIRED_ATTRIBUTES.put("copyright", attributes);
71          REQUIRED_ATTRIBUTES.put("editCopyright", attributes);
72          REQUIRED_ATTRIBUTES.put("force", attributes);
73          REQUIRED_ATTRIBUTES.put("editOverwrite", attributes);
74  
75          // Types that are specified as XML attributes in Ant.
76          ATTRIBUTE_TYPES.add(String.class);
77          ATTRIBUTE_TYPES.add(String[].class);
78          ATTRIBUTE_TYPES.add(Integer.class);
79          ATTRIBUTE_TYPES.add(Long.class);
80          ATTRIBUTE_TYPES.add(File.class);
81      }
82  
83      public static Map<String, String> getRenameMap() {
84          return new TreeMap<>(RENAME_MAP);
85      }
86  
87      /** The instance of the AntOptionCollection */
88      public static final AntOptionCollection INSTANCE = new Builder().build();
89  
90      /**
91       * Create an instance.
92       */
93      private AntOptionCollection(final Builder builder) {
94          super(builder);
95          conversionMap = builder.conversions;
96      }
97  
98      public Set<AntOption> convertedFrom(final AntOption antOption) {
99          return conversionMap.entrySet().stream().filter(e -> e.getValue().equals(antOption.getOption()))
100                 .map(e -> getMappedOption(e.getKey()))
101                 .filter(Optional::isPresent)
102                 .map(Optional::get)
103                 .collect(Collectors.toSet());
104     }
105 
106     /**
107      * If this option is converted to another option return that option otherwise
108      * return this option.
109      * @return the converted option.
110      */
111     public AntOption getActualAntOption(final AntOption antOption) {
112         Option opt = conversionMap.get(antOption.getOption());
113         return opt == null ? antOption : getMappedOption(opt).orElse(antOption);
114     }
115 
116     public boolean isAttribute(final AntOption antOption) {
117         Option opt = antOption.getOption();
118         return (!opt.hasArg() || opt.getArgs() == 1) && convertedFrom(antOption).isEmpty() &&
119                 ATTRIBUTE_TYPES.contains(opt.getType());
120     }
121 
122     public Map<String, String> getRequiredAttributes(final String name) {
123         return REQUIRED_ATTRIBUTES.get(name);
124     }
125 
126     BuildType buildType(final OptionCollection.ArgumentType type) {
127         return switch (type) {
128             case FILE, DIRORARCHIVE -> new BuildType("filename") {
129                 @Override
130                 protected String getMethodFormat(final AntOption antOption) {
131                     return "  <fileset file='%s' />\n";
132                 }
133             };
134             case NONE -> new BuildType("");
135             case COUNTERPATTERN -> new BuildType("cntr");
136             case EXPRESSION -> new BuildType("expr");
137             case STANDARDCOLLECTION -> new BuildType("std");
138             case LICENSEID, FAMILYID -> new BuildType("lst");
139             default -> new BuildType(type.getDisplayName()) {
140                 @Override
141                 protected String getMethodFormat(final AntOption antOption) {
142                     return String.format(DEFAULT_XML, WordUtils.uncapitalize(antOption.getArgName()));
143                 }
144             };
145         };
146     }
147 
148     public static Builder builder() {
149         return new Builder();
150     }
151 
152     /**
153      * Provides a new name for an option if it is renamed in the collection.
154      * @param name the option name.
155      * @return the collection name, may be the same as the option name.
156      */
157     static String rename(final String name) {
158         return StringUtils.defaultIfEmpty(RENAME_MAP.get(name), name);
159     }
160 
161     /**
162      * Creates the name for the option based on rules for conversion of CLI option names.
163      * @param option the standard option.
164      * @return the new Option name as a CasedString.
165      */
166     static CasedString createName(final Option option) {
167         List<String> pluralEndings = List.of("approved", "denied");
168         String name = rename(ArgumentTracker.extractKey(option));
169         CasedString casedName = new CasedString(CasedString.StringCase.KEBAB, name);
170         String[] segments = casedName.getSegments();
171         String lastSegment = segments[segments.length - 1];
172         if (option.hasArgs() && !lastSegment.endsWith("s") && !pluralEndings.contains(lastSegment)) {
173             segments[segments.length - 1] += "s";
174             casedName = new CasedString(CasedString.StringCase.KEBAB, segments);
175         }
176         return casedName.as(CasedString.StringCase.PASCAL);
177     }
178 
179     /**
180      * The Builder for the AntOptionCollection.
181      */
182     public static final class Builder extends UIOptionCollection.Builder<AntOption, Builder> {
183         /** Convert key to value type when generating code */
184         private final Map<Option, Option> conversions = new HashMap<>();
185 
186         private Builder() {
187             super(AntOption::new);
188             Arg.getOptions().getOptions()
189                     .stream().filter(o -> Objects.isNull(o.getLongOpt()))
190                     .forEach(this::unsupported);
191             unsupported(Arg.LOG_LEVEL)
192                     .unsupported(Arg.DIR)
193                     .unsupported(Arg.SOURCE)
194                     .unsupported(Arg.HELP_LICENSES)
195                     // conversions
196                     .convert(Arg.LICENSES_APPROVED_FILE, Arg.LICENSES_APPROVED)
197                     .convert(Arg.LICENSES_DENIED_FILE, Arg.LICENSES_DENIED)
198                     .convert(Arg.FAMILIES_APPROVED_FILE, Arg.FAMILIES_APPROVED)
199                     .convert(Arg.FAMILIES_DENIED_FILE, Arg.FAMILIES_DENIED)
200                     .convert(Arg.INCLUDE_FILE, Arg.INCLUDE)
201                     .convert(Arg.INCLUDE_STD, Arg.INCLUDE)
202                     .convert(Arg.EXCLUDE_FILE, Arg.EXCLUDE)
203                     .convert(Arg.EXCLUDE_STD, Arg.EXCLUDE);
204         }
205 
206         @Override
207         public AntOptionCollection build() {
208             return new AntOptionCollection(this);
209         }
210 
211         public Builder convert(final Arg from, final Arg to) {
212             Option mapTo = to.option();
213             for (Option option : from.group().getOptions()) {
214                 if (!option.equals(mapTo) && !option.isDeprecated()) {
215                     conversions.put(option, mapTo);
216                 }
217             }
218             return self();
219         }
220     }
221 
222     /**
223      * A mapping of data type of XML format.
224      */
225     public static class BuildType {
226         /**
227          * The configuration tag for this build type.
228          */
229         private final String tag;
230         /**
231          * If True adds the tag as the test extension.
232          */
233         private final boolean addExt;
234 
235         /**
236          * The constructor for the build type.
237          *
238          * @param tag the XML tag for this data type.
239          */
240         BuildType(final String tag) {
241             this.tag = tag;
242             this.addExt = StringUtils.isNotEmpty(tag);
243         }
244 
245         /**
246          * Gets the method based on how many arguments an Ant option requires.
247          *
248          * @param antOption the Ant option to check.
249          * @return the method format for the option.
250          */
251         protected String getMethodFormat(final AntOption antOption) {
252             return String.format(DEFAULT_XML, tag);
253         }
254 
255         /**
256          * Gets a string comprising the Ant XML pattern for this data type and the number of arguments expected by the Ant option.
257          *
258          * @param delegateOption the Ant option that the call is delegated to.
259          * @param antOption      the actual Ant option.
260          * @param data           the data for the actual Ant option.
261          * @return the Ant XML pattern for this data type.
262          */
263         public String getXml(final AntOption delegateOption, final AntOption antOption, final String data) {
264             String fmt = getMethodFormat(antOption);
265             String value = data == null ? WordUtils.uncapitalize(antOption.getArgName()) : data;
266             String inner = format(fmt, value);
267             return format("<%1$s>%2$s</%1$s>%n", delegateOption.getName(), inner);
268         }
269 
270         public String getXml(final AntOption antOption, final String data) {
271             AntOption delegateOption = antOption.getActualAntOption();
272             if (delegateOption.isAttribute()) {
273                 return "";
274             } else {
275                 return format(getMethodFormat(antOption), data);
276             }
277         }
278 
279         public String testName(final AntOption antOption) {
280             return addExt ? format("%s_%s", antOption.getName(), antOption.getArgName()) : antOption.getName();
281         }
282     }
283 }