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.tools;
20  
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.apache.commons.cli.Option;
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.commons.text.WordUtils;
29  import org.apache.rat.OptionCollection;
30  import org.apache.rat.commandline.Arg;
31  
32  import static java.lang.String.format;
33  
34  /**
35   * A representation of a CLI option as a Maven option
36   */
37  public class MavenOption extends AbstractOption {
38      /**
39       * Default values for CLI options
40       */
41      private static final Map<Arg, String> DEFAULT_VALUES = new HashMap<>();
42      /**
43       * List of CLI Options that are not supported by Maven.
44       */
45      private static final Set<Option> UNSUPPORTED_LIST = new HashSet<>();
46  
47      static {
48          DEFAULT_VALUES.put(Arg.OUTPUT_FILE, "${project.build.directory}/rat.txt");
49          UNSUPPORTED_LIST.addAll(Arg.DIR.group().getOptions());
50          UNSUPPORTED_LIST.addAll(Arg.LOG_LEVEL.group().getOptions());
51          UNSUPPORTED_LIST.add(OptionCollection.HELP);
52      }
53  
54      /**
55       * Constructor.
56       *
57       * @param option The CLI option
58       */
59      MavenOption(final Option option) {
60          super(option, MavenGenerator.createName(option));
61      }
62  
63      /**
64       * Gets the set of options that are not supported by Ant.
65       *
66       * @return The set of options that are not supported by Ant.
67       */
68      public static Set<Option> getFilteredOptions() {
69          return UNSUPPORTED_LIST;
70      }
71  
72      @Override
73      protected String cleanupName(final Option option) {
74          return format("<%s>", MavenGenerator.createName(option));
75      }
76  
77      @Override
78      public String getDefaultValue() {
79          Arg arg = Arg.findArg(option);
80          String result = DEFAULT_VALUES.get(arg);
81          if (result == null) {
82              result = arg.defaultValue();
83          }
84          return result;
85      }
86  
87      public String getPropertyAnnotation(final String fname) {
88          StringBuilder sb = new StringBuilder("@Parameter");
89          String property = option.hasArgs() ? null : format("property = \"rat.%s\"", fname);
90          String defaultValue = option.isDeprecated() ? null : getDefaultValue();
91          if (property != null || defaultValue != null) {
92              sb.append("(");
93              if (property != null) {
94                  sb.append(property).append(defaultValue != null ? ", " : StringUtils.EMPTY);
95              }
96              if (defaultValue != null) {
97                  sb.append(format("defaultValue = \"%s\"", defaultValue));
98              }
99              sb.append(")");
100         }
101         return sb.toString();
102     }
103 
104     public String getMethodSignature(final String indent, final boolean multiple) {
105         StringBuilder sb = new StringBuilder();
106         if (isDeprecated()) {
107             sb.append(format("%s@Deprecated%n", indent));
108         }
109         String fname = WordUtils.capitalize(name);
110         String args = option.hasArg() ? "String" : "boolean";
111         if (multiple) {
112             if (!(fname.endsWith("s") || fname.endsWith("Approved") || fname.endsWith("Denied"))) {
113                 fname = fname + "s";
114             }
115             args = args + "[]";
116         }
117 
118         return sb.append(format("%1$s%5$s%n%1$spublic void set%3$s(%4$s %2$s)",
119                         indent, name, fname, args, getPropertyAnnotation(fname)))
120                 .toString();
121     }
122 
123     @Override
124     public String getExample() {
125         if (UNSUPPORTED_LIST.contains(option)) {
126             return "-- not supported --";
127         }
128         if (hasArg()) {
129             return format("<%1$s>%2$s</%1$s>", getName(), getArgName());
130         } else {
131             return format("<%s />", getName());
132         }
133     }
134 }