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  import java.util.Optional;
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  import org.apache.commons.cli.Option;
28  import org.apache.commons.lang3.StringUtils;
29  import org.apache.rat.commandline.Arg;
30  
31  import static java.lang.String.format;
32  
33  public abstract class AbstractOption {
34      /** The pattern to match CLI options in text */
35      protected static final Pattern PATTERN = Pattern.compile("-(-[a-z0-9]+)+");
36      /** The CLI that the Maven option is wrapping */
37      protected final Option option;
38      /** The Maven name for the option */
39      protected final String name;
40  
41      /**
42       * Constructor.
43       *
44       * @param option The CLI option
45       */
46      AbstractOption(final Option option, final String name) {
47          this.option = option;
48          this.name = name;
49      }
50  
51      /**
52       * Return default value.
53       * @return default value or {@code null} if no argument given.
54       */
55      public String getDefaultValue() {
56          Arg arg = Arg.findArg(option);
57          return arg == null ? null : arg.defaultValue();
58      }
59  
60      protected abstract String cleanupName(Option option);
61  
62      /**
63       * Replaces CLI pattern options with Maven pattern options.
64       * @param str the string to clean.
65       * @return the string with CLI names replaced with Maven names.
66       */
67      protected String cleanup(final String str) {
68          String workingStr = str;
69          if (StringUtils.isNotBlank(workingStr)) {
70              Map<String, String> maps = new HashMap<>();
71              Matcher matcher = PATTERN.matcher(workingStr);
72              while (matcher.find()) {
73                  String key = matcher.group();
74                  String optKey = key.substring(2);
75                  Optional<Option> maybeResult = Arg.getOptions().getOptions().stream()
76                          .filter(o -> optKey.equals(o.getOpt()) || optKey.equals(o.getLongOpt())).findFirst();
77                  maybeResult.ifPresent(value -> maps.put(key, cleanupName(value)));
78              }
79              for (Map.Entry<String, String> entry : maps.entrySet()) {
80                  workingStr = workingStr.replaceAll(Pattern.quote(format("%s", entry.getKey())), entry.getValue());
81              }
82          }
83          return workingStr;
84      }
85  
86      /**
87       * Gets the Maven name for the CLI option.
88       * @return The Maven name for the CLI option.
89       */
90      public final String getName() {
91          return name;
92      }
93  
94      /**
95       * Gets the description escaped for XML format.
96       *
97       * @return the description or an empty string.
98       */
99      public final String getDescription() {
100         return cleanup(option.getDescription());
101     }
102 
103     /**
104      * Gets the simple class name for the data type for this option.
105      * Normally "String".
106      * @return the simple class name for the type.
107      */
108     public final Class<?> getType() {
109         return option.hasArg() ? ((Class<?>) option.getType()) : boolean.class;
110     }
111 
112     /**
113      * Gets the argument name if there is one.
114      * @return the Argument name
115      */
116     public final String getArgName() {
117         return option.getArgName();
118     }
119 
120     /**
121      * Determines if the option is deprecated.
122      * @return {@code true} if the option is deprecated
123      */
124     public final boolean isDeprecated() {
125         return option.isDeprecated();
126     }
127 
128     /**
129      * Determines if the option is required.
130      * @return {@code true} if the option is required.
131      */
132     public final boolean isRequired() {
133         return option.isRequired();
134     }
135 
136     /**
137      * Determine if the enclosed option expects an argument.
138      * @return {@code true} if the enclosed option expects at least one argument.
139      */
140     public final boolean hasArg() {
141         return option.hasArg();
142     }
143 
144     /**
145      * Returns {@code true} if the option has multiple arguments.
146      * @return {@code true} if the option has multiple arguments.
147      */
148     public final boolean hasArgs() {
149         return option.hasArgs();
150     }
151 
152     /**
153      * The key value for the option.
154      * @return the key value for the CLI argument map.
155      */
156     public final String keyValue() {
157         return format("\"%s\"", StringUtils.defaultIfEmpty(option.getLongOpt(), option.getOpt()));
158     }
159 
160     /**
161      * Gets the deprecated string if the option is deprecated, or an empty string otherwise.
162      * @return the deprecated string if the option is deprecated, or an empty string otherwise.
163      */
164     public final String getDeprecated() {
165         return  option.isDeprecated() ? cleanup(StringUtils.defaultIfEmpty(option.getDeprecated().toString(), StringUtils.EMPTY)) : StringUtils.EMPTY;
166     }
167 }