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.function.Function;
22  
23  import org.apache.commons.cli.Option;
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.commons.text.WordUtils;
26  import org.apache.rat.ConfigurationException;
27  import org.apache.rat.ui.UIOption;
28  import org.apache.rat.utils.CasedString;
29  
30  import static java.lang.String.format;
31  
32  /**
33   * A representation of a Maven option based on an option.
34   */
35  public final class MavenOption extends UIOption<MavenOption> {
36  
37      /**
38       * The format to start an XML entity.
39       */
40      private static final String XML_FMT = "<%s>";
41  
42      /**
43       * Constructor.
44       *
45       * @param builder The MavenOption builder.
46       */
47      private MavenOption(final MavenOptionBuilder builder) {
48          super(builder);
49      }
50  
51      @Override
52      public String toString() {
53          return getName();
54      }
55  
56      /**
57       * Gets the method name for this option.
58       * @return the method name for this option.
59       */
60      public String getMethodName() {
61          return "set" + name.toCase(CasedString.StringCase.PASCAL);
62      }
63  
64      @Override
65      protected String cleanupName(final Option option) {
66          // only parse the option if we need to.
67          return format(XML_FMT, option == this.option ? this.name :
68                  MavenOptionBuilder.createName(optionCollection.rename(option)));
69      }
70  
71      @Override
72      public String getText() {
73          return cleanupName(option);
74      }
75  
76      @Override
77      public String getExample() {
78          if (hasArgs()) {
79              return getExample(getArgName() + "1", getArgName() + "2");
80          }
81          if (hasArg()) {
82              return getExample(getArgName());
83          }
84              return getExample("");
85      }
86  
87      /**
88       * Create example text for the option.
89       * @param args the example arguments for the option.
90       * @return a formatted option.
91       */
92      public String getExample(final String... args) {
93          StringBuilder sb = new StringBuilder(String.format(XML_FMT, getName()));
94          if (hasArg()) {
95              if (hasArgs()) {
96                  sb.append(System.lineSeparator());
97                  for (String arg : args) {
98                      sb.append(String.format("  <%1$s>%2$s</%1$s>%n", WordUtils.uncapitalize(getArgName()), arg));
99                  }
100             } else {
101                 sb.append(args[0]);
102             }
103         } else {
104             sb.append(Boolean.TRUE);
105         }
106         sb.append("</").append(getName()).append(">");
107         return sb.toString();
108     }
109 
110     /**
111      * Gets the Maven Mojo method signature for this Option.
112      * @param indent the number of spaces to indent the text.
113      * @param multiple {@code true} if this option takes multiple arguments.
114      * @return the method signature for this Option.
115      */
116     public String getMethodSignature(final String indent, final boolean multiple) {
117         StringBuilder sb = new StringBuilder();
118         if (isDeprecated()) {
119             sb.append(format("%s@Deprecated%n", indent));
120         }
121         // the camel case name for this option.
122         String camelName = name.toCase(CasedString.StringCase.CAMEL);
123         if (camelName == null) {
124             throw new ConfigurationException("Name must not be null");
125         }
126         String args = option.hasArg() ? "String" : "boolean";
127         if (multiple) {
128             if (!(camelName.endsWith("s") || camelName.endsWith("Approved") || camelName.endsWith("Denied"))) {
129                 camelName = camelName + "s";
130             }
131             args = args + "[]";
132         }
133 
134         return sb.append(format("%1$s%5$s%n%1$spublic void set%3$s(%4$s %2$s)",
135                         indent, name, camelName, args, getParameterAnnotation(camelName)))
136                 .toString();
137     }
138 
139     /**
140      * Creates the {@code @Parameter} annotation for this option.
141      * @param camelName the camel cased name for this option.
142      * @return the string that is the parameter annotation.
143      */
144     public String getParameterAnnotation(final String camelName) {
145         StringBuilder sb = new StringBuilder("@Parameter");
146         String property = option.hasArgs() ? null : format("property = \"rat.%s\"", camelName);
147         String defaultValue = option.isDeprecated() ? null : getDefaultValue();
148         if (property != null || defaultValue != null) {
149             sb.append("(");
150             if (property != null) {
151                 sb.append(property).append(defaultValue != null ? ", " : StringUtils.EMPTY);
152             }
153             if (defaultValue != null) {
154                 sb.append(format("defaultValue = \"%s\"", defaultValue));
155             }
156             sb.append(")");
157         }
158         return sb.toString();
159     }
160 
161     /**
162      * The builder for the MavenOptions.
163      */
164     public static class MavenOptionBuilder extends UIOption.Builder<MavenOption, MavenOptionBuilder> {
165 
166         @Override
167         protected Function<Option, CasedString> getNameFactory() {
168             return o -> createName(optionCollection().rename(o));
169         }
170 
171         @Override
172         protected MavenOption doBuild() {
173             return new MavenOption(this);
174         }
175 
176         /**
177          * Create the cased name
178          * @param key the renamed key from the collection.
179          * @return the name for the key.
180          * @see MavenOptionCollection#rename(Option)
181          */
182         static CasedString createName(final String key) {
183             return new CasedString(CasedString.StringCase.KEBAB, key).as(CasedString.StringCase.PASCAL);
184         }
185     }
186 }