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