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.Map;
23  
24  import org.apache.commons.cli.Option;
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.commons.text.WordUtils;
27  import org.apache.rat.commandline.Arg;
28  
29  import static java.lang.String.format;
30  
31  /**
32   * A representation of a CLI option as a Maven option
33   */
34  public class MavenOption extends AbstractOption {
35      /** Default values for CLI options */
36      private static final Map<Arg, String> DEFAULT_VALUES = new HashMap<>();
37  
38      static {
39          DEFAULT_VALUES.put(Arg.OUTPUT_FILE, "${project.build.directory}/rat.txt");
40      }
41  
42      /**
43       * Constructor.
44       *
45       * @param option The CLI option
46       */
47      MavenOption(final Option option) {
48          super(option, MavenGenerator.createName(option));
49      }
50  
51      @Override
52      protected String cleanupName(final Option option) {
53          return format("<%s>", MavenGenerator.createName(option));
54      }
55  
56      @Override
57      public String getDefaultValue() {
58          Arg arg = Arg.findArg(option);
59          String result = DEFAULT_VALUES.get(arg);
60          if (result == null) {
61              result = arg.defaultValue();
62          }
63          return result;
64      }
65  
66      public String getPropertyAnnotation(final String fname) {
67          StringBuilder sb = new StringBuilder("@Parameter");
68          String property = option.hasArgs() ? null : format("property = \"rat.%s\"", fname);
69          String defaultValue = option.isDeprecated() ? null : getDefaultValue();
70          if (property != null || defaultValue != null) {
71              sb.append("(");
72              if (property != null) {
73                  sb.append(property).append(defaultValue != null ? ", " : StringUtils.EMPTY);
74              }
75              if (defaultValue != null) {
76                  sb.append(format("defaultValue = \"%s\"", defaultValue));
77              }
78              sb.append(")");
79          }
80          return sb.toString();
81      }
82  
83      public String getMethodSignature(final String indent, final boolean multiple) {
84          StringBuilder sb = new StringBuilder();
85          if (isDeprecated()) {
86              sb.append(format("%s@Deprecated%n", indent));
87          }
88          String fname = WordUtils.capitalize(name);
89          String args = option.hasArg() ? "String" : "boolean";
90          if (multiple) {
91              if (!(fname.endsWith("s") || fname.endsWith("Approved") || fname.endsWith("Denied"))) {
92                  fname = fname + "s";
93              }
94              args = args + "[]";
95          }
96  
97          return sb.append(format("%1$s%5$s%n%1$spublic void set%3$s(%4$s %2$s)",
98                          indent, name, fname, args, getPropertyAnnotation(fname)))
99                  .toString();
100     }
101 }