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 org.apache.commons.cli.Option;
22  import org.apache.commons.lang3.StringUtils;
23  import org.apache.commons.text.WordUtils;
24  import org.apache.rat.ui.UIOption;
25  import org.apache.rat.ui.UIOptionCollection;
26  import org.apache.rat.utils.CasedString;
27  
28  import static java.lang.String.format;
29  
30  /**
31   * A representation of a Maven option based on a CLI option.
32   */
33  public final class MavenOption extends UIOption<MavenOption> {
34  
35      /** The format to start an XML entity */
36      private static final String XML_FMT = "<%s>";
37  
38      /**
39       * Constructor.
40       *
41       * @param option The CLI option
42       */
43      MavenOption(final UIOptionCollection<MavenOption> collection, final Option option) {
44          super(collection, option, MavenOptionCollection.createName(option));
45      }
46  
47      @Override
48      public String toString() {
49          return getName();
50      }
51  
52      /**
53       * Gets the method name for this option.
54       * @return the method name for this option.
55       */
56      public String getMethodName() {
57          return "set" + name.toCase(CasedString.StringCase.PASCAL);
58      }
59  
60      @Override
61      protected String cleanupName(final Option option) {
62          // only parse the option if we need to.
63          return option == this.option ? format(XML_FMT, this.name) : format(XML_FMT, MavenOptionCollection.createName(option));
64      }
65  
66      @Override
67      public String getText() {
68          return cleanupName(option);
69      }
70  
71      @Override
72      public String getExample() {
73          if (hasArgs()) {
74              return getExample(getArgName() + "1", getArgName() + "2");
75          }
76          if (hasArg()) {
77              return getExample(getArgName());
78          }
79              return getExample("");
80      }
81  
82      /**
83       * Create example text for the option.
84       * @param args the example arguments for the option.
85       * @return a formatted option.
86       */
87      public String getExample(final String... args) {
88          StringBuilder sb = new StringBuilder(String.format(XML_FMT, getName()));
89          if (hasArg()) {
90              if (hasArgs()) {
91                  sb.append(System.lineSeparator());
92                  for (String arg : args) {
93                      sb.append(String.format("  <%1$s>%2$s</%1$s>%n", WordUtils.uncapitalize(getArgName()), arg));
94                  }
95              } else {
96                  sb.append(args[0]);
97              }
98          } else {
99              sb.append(Boolean.TRUE);
100         }
101         sb.append("</").append(getName()).append(">");
102         return sb.toString();
103     }
104 
105     public String getMethodSignature(final String indent, final boolean multiple) {
106         StringBuilder sb = new StringBuilder();
107         if (isDeprecated()) {
108             sb.append(format("%s@Deprecated%n", indent));
109         }
110         String fname = name.toCase(CasedString.StringCase.CAMEL);
111         String args = option.hasArg() ? "String" : "boolean";
112         if (multiple) {
113             if (!(fname.endsWith("s") || fname.endsWith("Approved") || fname.endsWith("Denied"))) {
114                 fname = fname + "s";
115             }
116             args = args + "[]";
117         }
118 
119         return sb.append(format("%1$s%5$s%n%1$spublic void set%3$s(%4$s %2$s)",
120                         indent, name, fname, args, getPropertyAnnotation(fname)))
121                 .toString();
122     }
123 
124 
125     public String getPropertyAnnotation(final String fname) {
126         StringBuilder sb = new StringBuilder("@Parameter");
127         String property = option.hasArgs() ? null : format("property = \"rat.%s\"", fname);
128         String defaultValue = option.isDeprecated() ? null : getDefaultValue();
129         if (property != null || defaultValue != null) {
130             sb.append("(");
131             if (property != null) {
132                 sb.append(property).append(defaultValue != null ? ", " : StringUtils.EMPTY);
133             }
134             if (defaultValue != null) {
135                 sb.append(format("defaultValue = \"%s\"", defaultValue));
136             }
137             sb.append(")");
138         }
139         return sb.toString();
140     }
141 }