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.velocity;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Comparator;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  import java.util.TreeSet;
28  import java.util.stream.Collectors;
29  
30  import org.apache.commons.cli.Option;
31  import org.apache.commons.collections4.set.UnmodifiableSortedSet;
32  import org.apache.commons.lang3.StringUtils;
33  import org.apache.rat.CLIOption;
34  import org.apache.rat.CLIOptionCollection;
35  import org.apache.rat.Defaults;
36  import org.apache.rat.OptionCollection;
37  import org.apache.rat.api.EnvVar;
38  import org.apache.rat.commandline.StyleSheets;
39  import org.apache.rat.config.exclusion.StandardCollection;
40  import org.apache.rat.config.parameters.ComponentType;
41  import org.apache.rat.config.parameters.Description;
42  import org.apache.rat.config.parameters.DescriptionBuilder;
43  import org.apache.rat.configuration.MatcherBuilderTracker;
44  import org.apache.rat.documentation.options.AntOption;
45  import org.apache.rat.documentation.options.AntOptionCollection;
46  import org.apache.rat.documentation.options.MavenOption;
47  import org.apache.rat.documentation.options.MavenOptionCollection;
48  import org.apache.rat.help.AbstractHelp;
49  import org.apache.rat.license.ILicense;
50  import org.apache.rat.license.LicenseSetFactory;
51  import org.apache.rat.ui.UIOption;
52  import org.apache.velocity.tools.config.DefaultKey;
53  import org.apache.velocity.tools.config.ValidScope;
54  
55  /**
56   * The Velocity RAT plugin that provides access to the RAT data.
57   * <p>
58   * DEVHINT: Be careful when removing methods as this may invalidate contents and functionality of the velocity templates.
59   * </p>
60   */
61  @SuppressWarnings("unused")
62  @DefaultKey("rat")
63  @ValidScope({"application"})
64  public class RatTool {
65  
66      // TODO remove this when velocity-tools 3.3 is available // NOSONAR
67      private static String[] charParser(final String charText) {
68          char[] chars = charText.toCharArray();
69          String[] result = new String[chars.length];
70          for (int i = 0; i < chars.length; i++) {
71              result[i] = String.valueOf(chars[i]);
72          }
73          return result;
74      }
75  
76      /**
77       * The characters to escape for Markdown.
78       */
79      // TODO remove this when velocity-tools 3.3 is available // NOSONAR
80      private static final String[] MARKDOWN_CHARS = charParser("\\`*_{}[]<>()#+-.!|");
81      /**
82       * The characters to escape for APT (Almost Plain Text).
83       */
84      // TODO remove this when velocity-tools 3.3 is available // NOSONAR
85      private static final String[] APT_CHARS = charParser("\\~=-+*[]<>{}");
86  
87      /**
88       * The license factory this tool uses.
89       */
90      private final LicenseSetFactory licenseSetFactory;
91  
92      /**
93       * The client option instance.
94       */
95      // visible for testing
96      private final CLIOptionCollection cliOptions = new CLIOptionCollection();
97  
98      /**
99       * The Ant option instance.
100      */
101     private final AntOptionCollection antOptions = new AntOptionCollection();
102 
103     /**
104      * The Maven option instance.
105      */
106     private final MavenOptionCollection mavenOptions = new MavenOptionCollection();
107 
108     /**
109      * Constructor.
110      */
111     public RatTool() {
112         Defaults defaults = Defaults.builder().build();
113         licenseSetFactory = defaults.getLicenseSetFactory();
114     }
115 
116     /**
117      * Gets the list of command line options.
118      * @return the list of command line options.
119      */
120     public List<Option> options() {
121         return cliOptions.getMappedOptions()
122         .map(UIOption::getOption).toList();
123     }
124 
125     /**
126      * Gets a map client option name to Ant Option.
127      * @return a map client option name to Ant Option.
128      */
129     public Map<String, AntOption> antOptions() {
130         return antOptions.getOptionMap();
131     }
132 
133     /**
134      * Gets a map client option name to CLI Option.
135      * @return a map client option name to CLI Option.
136      */
137     public Map<String, CLIOption> cliOptions() {
138         return cliOptions.getOptionMap();
139     }
140 
141     /**
142      * Gets a map client option name to Maven Option.
143      * @return a map client option name to Maven Option.
144      */
145     public Map<String, MavenOption> mvnOptions() {
146         return mavenOptions.getOptionMap();
147     }
148 
149     /**
150      * Escapes a text string.
151      * @param text the text to escape.
152      * @param chars the characters to escape.
153      * @return the escaped string.
154      */
155     private String escape(final String text, final String[] chars) {
156         if (text == null) {
157             return "";
158         }
159         String result = text;
160         for (String c : chars) {
161             result = result.replace(c, "\\" + c);
162         }
163         return result;
164     }
165 
166     /**
167      * Escapes a string for Markdown.
168      * @param text the text to escape.
169      * @return the text with the Markdown specific characters escaped.
170      */
171     public String markdownEscape(final String text) {
172         return escape(text, MARKDOWN_CHARS);
173     }
174 
175     /**
176      * Escapes a string for APT (almost plain text).
177      * @param text the text to escape.
178      * @return the text with the APT specific characters escaped.
179      */
180     public String aptEscape(final String text) {
181         return escape(text, APT_CHARS);
182     }
183 
184     /**
185      * Gets the list of argument types.
186      * @return a list of argument types.
187      */
188     public List<OptionCollection.ArgumentType> argumentTypes() {
189         return Arrays.stream(OptionCollection.ArgumentType.values()).filter(t -> t != OptionCollection.ArgumentType.NONE)
190                 .sorted(Comparator.comparing(OptionCollection.ArgumentType::getDisplayName))
191                 .collect(Collectors.toList());
192     }
193 
194     /**
195      * Gets the list of environment variables.
196      * @return a list of environment variables.
197      */
198     public List<EnvVar> environmentVariables() {
199         return Arrays.stream(EnvVar.values())
200                 .sorted(Comparator.comparing(EnvVar::name))
201                 .collect(Collectors.toList());
202     }
203 
204     /**
205      * Gets the set of matchers.
206      * @return the set of matchers.
207      */
208     public Set<Matcher> matchers() {
209         Set<Matcher> documentationSet = new TreeSet<>(Comparator.comparing(Matcher::getName));
210         for (Class<?> clazz : MatcherBuilderTracker.instance().getClasses()) {
211             Description desc = DescriptionBuilder.buildMap(clazz);
212             documentationSet.add(new Matcher(desc, null));
213         }
214         return documentationSet;
215     }
216 
217     /**
218      * Gets the list of standard collections.
219      * @return the list of standard collections.
220      */
221     public List<StandardCollection> standardCollections() {
222         return Arrays.stream(org.apache.rat.config.exclusion.StandardCollection.values())
223                 .sorted(Comparator.comparing(Enum::name))
224                 .collect(Collectors.toList());
225     }
226 
227     /**
228      * Gets the list of stylesheets.
229      * @return the list of stylesheets.
230      */
231     public List<StyleSheets> styleSheets() {
232         return Arrays.stream(StyleSheets.values())
233                 .sorted(Comparator.comparing(StyleSheets::arg))
234                 .collect(Collectors.toList());
235     }
236 
237     /**
238      * Gets the {@link StringUtils} object in order to work with it in RAT Velocity templates.
239      * @return the org.apache.commons.lang3 StringUtils object.
240      * @see org.apache.commons.lang3.StringUtils
241      */
242     public StringUtils stringUtils() {
243         return new StringUtils();
244     }
245 
246     /**
247      * Gets a tab character.
248      * @return the tab character.
249      */
250     public String tab() {
251         return "\t";
252     }
253 
254     /**
255      * Gets two new lines.
256      * @return a string containing two new lines.
257      */
258     public String doubleLine() {
259         return "\n\n";
260     }
261 
262     /**
263      * Gets a list of license property descriptions.
264      * @return a list of license property descriptions.
265      */
266     public List<Description> licenseProperties() {
267         UnmodifiableSortedSet<ILicense> licenses = licenseSetFactory.getLicenses(LicenseSetFactory.LicenseFilter.ALL);
268         Description licenseDescription = DescriptionBuilder.build(licenses.first());
269         List<Description> descriptions = new ArrayList<>(licenseDescription.filterChildren(d -> d.getType() == ComponentType.PARAMETER));
270         descriptions.sort(Comparator.comparing(Description::getCommonName));
271         return descriptions;
272     }
273 
274     /**
275      * Gets the list of defined licenses.
276      * @return the list of defined licenses.
277      */
278     public List<License> licenses() {
279         UnmodifiableSortedSet<ILicense> licenses = licenseSetFactory.getLicenses(LicenseSetFactory.LicenseFilter.ALL);
280         return licenses.stream().map(License::new).collect(Collectors.toList());
281     }
282 
283     /**
284      * Creates a string of spaces of the specified length.
285      * @param length the length of the string.
286      * @return a string of spaces of the specified length.
287      */
288     public String pad(final int length) {
289         return AbstractHelp.createPadding(length);
290     }
291 }