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  package org.apache.rat.documentation.options;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Locale;
26  import java.util.Map;
27  import java.util.Set;
28  import java.util.function.Predicate;
29  import java.util.stream.Collectors;
30  
31  import org.apache.commons.cli.Option;
32  import org.apache.commons.lang3.StringUtils;
33  import org.apache.commons.text.WordUtils;
34  import org.apache.rat.OptionCollection;
35  import org.apache.rat.commandline.Arg;
36  import org.apache.rat.utils.CasedString;
37  
38  import static java.lang.String.format;
39  
40  /**
41   * A representation of a CLI option as a Maven option
42   */
43  public class MavenOption extends AbstractOption {
44      /**
45       * Default values for CLI options
46       */
47      private static final Map<Arg, String> DEFAULT_VALUES = new HashMap<>();
48      /**
49       * List of CLI options that are not supported by Maven.
50       */
51      private static final Set<Option> UNSUPPORTED_LIST = new HashSet<>();
52      /** A mapping of external name to internal name if not standard. */
53      private static final Map<String, String> RENAME_MAP = new HashMap<>();
54  
55      /**
56       * Filter to remove options not supported by Maven.
57       */
58      private static final Predicate<Option> MAVEN_FILTER;
59  
60      static {
61          RENAME_MAP.put("addLicense", "add-license");
62          DEFAULT_VALUES.put(Arg.OUTPUT_FILE, "${project.build.directory}/rat.txt");
63          UNSUPPORTED_LIST.addAll(Arg.DIR.group().getOptions());
64          UNSUPPORTED_LIST.addAll(Arg.LOG_LEVEL.group().getOptions());
65          UNSUPPORTED_LIST.add(OptionCollection.HELP);
66  
67          Set<Option> filteredOptions = getFilteredOptions();
68          MAVEN_FILTER =  option -> !(filteredOptions.contains(option) || option.getLongOpt() == null);
69      }
70  
71      public static List<MavenOption> getMavenOptions() {
72          return OptionCollection.buildOptions().getOptions().stream().filter(MAVEN_FILTER)
73                  .map(MavenOption::new).collect(Collectors.toList());
74      }
75  
76      /**
77       * Constructor.
78       *
79       * @param option The CLI option
80       */
81      public MavenOption(final Option option) {
82          super(option, createName(option));
83      }
84  
85      public static Map<String, String> getRenameMap() {
86          return Collections.unmodifiableMap(RENAME_MAP);
87      }
88  
89      /**
90       * Gets the set of options that are not supported by Maven.
91       *
92       * @return The set of options that are not supported by Maven.
93       */
94      public static Set<Option> getFilteredOptions() {
95          return Collections.unmodifiableSet(UNSUPPORTED_LIST);
96      }
97  
98      /**
99       * Creates the Maven element name for the specified option.
100      * @param option The option to process.
101      * @return the Maven based name in camel-case syntax.
102      */
103     static String createName(final Option option) {
104         String name = StringUtils.defaultIfEmpty(option.getLongOpt(), option.getOpt());
105         name = StringUtils.defaultIfEmpty(RENAME_MAP.get(name), name).toLowerCase(Locale.ROOT);
106         return new CasedString(CasedString.StringCase.KEBAB, name).toCase(CasedString.StringCase.CAMEL);
107     }
108 
109     @Override
110     protected String cleanupName(final Option option) {
111         return format("<%s>", createName(option));
112     }
113 
114     @Override
115     public String getText() {
116         return cleanupName(option);
117     }
118 
119     @Override
120     public String getDefaultValue() {
121         Arg arg = Arg.findArg(option);
122         String result = DEFAULT_VALUES.get(arg);
123         if (result == null) {
124             result = arg.defaultValue();
125         }
126         return result;
127     }
128 
129     public String getPropertyAnnotation(final String fname) {
130         StringBuilder sb = new StringBuilder("@Parameter");
131         String property = option.hasArgs() ? null : format("property = \"rat.%s\"", fname);
132         String defaultValue = option.isDeprecated() ? null : getDefaultValue();
133         if (property != null || defaultValue != null) {
134             sb.append("(");
135             if (property != null) {
136                 sb.append(property).append(defaultValue != null ? ", " : StringUtils.EMPTY);
137             }
138             if (defaultValue != null) {
139                 sb.append(format("defaultValue = \"%s\"", defaultValue));
140             }
141             sb.append(")");
142         }
143         return sb.toString();
144     }
145 
146     public String getMethodSignature(final String indent, final boolean multiple) {
147         StringBuilder sb = new StringBuilder();
148         if (isDeprecated()) {
149             sb.append(format("%s@Deprecated%n", indent));
150         }
151         String fname = WordUtils.capitalize(name);
152         String args = option.hasArg() ? "String" : "boolean";
153         if (multiple) {
154             if (!(fname.endsWith("s") || fname.endsWith("Approved") || fname.endsWith("Denied"))) {
155                 fname = fname + "s";
156             }
157             args = args + "[]";
158         }
159 
160         return sb.append(format("%1$s%5$s%n%1$spublic void set%3$s(%4$s %2$s)",
161                         indent, name, fname, args, getPropertyAnnotation(fname)))
162                 .toString();
163     }
164 
165     @Override
166     public String getExample() {
167         if (UNSUPPORTED_LIST.contains(option)) {
168             return "-- not supported --";
169         }
170         if (hasArg()) {
171             return format("<%1$s>%2$s</%1$s>", getName(), getArgName());
172         } else {
173             return format("<%s />", getName());
174         }
175     }
176 }