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.SortedSet;
28  import java.util.TreeSet;
29  import java.util.stream.Collectors;
30  
31  import org.apache.commons.cli.Option;
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         MatcherBuilderTracker tracker = MatcherBuilderTracker.instance();
191         Set<Matcher> documentationSet = new TreeSet<>(Comparator.comparing(Matcher::getName));
192         for (Class<?> clazz : tracker.getClasses()) {
193             Description desc = DescriptionBuilder.buildMap(clazz);
194             documentationSet.add(new Matcher(desc, null));
195         }
196         return documentationSet;
197     }
198 
199     /**
200      * Gets the list of standard collections.
201      * @return the list of standard collections.
202      */
203     public List<StandardCollection> standardCollections() {
204         return Arrays.stream(org.apache.rat.config.exclusion.StandardCollection.values())
205                 .sorted(Comparator.comparing(Enum::name))
206                 .collect(Collectors.toList());
207     }
208 
209     /**
210      * Gets the list of stylesheets.
211      * @return the list of stylesheets.
212      */
213     public List<StyleSheets> styleSheets() {
214         return Arrays.stream(StyleSheets.values())
215                 .sorted(Comparator.comparing(StyleSheets::arg))
216                 .collect(Collectors.toList());
217     }
218 
219     /**
220      * Gets the {@link StringUtils} object in order to work with it in RAT Velocity templates.
221      * @return the org.apache.commons.lang3 StringUtils object.
222      * @see org.apache.commons.lang3.StringUtils
223      */
224     public StringUtils stringUtils() {
225         return new StringUtils();
226     }
227 
228     /**
229      * Gets a tab character.
230      * @return the tab character.
231      */
232     public String tab() {
233         return "\t";
234     }
235 
236     /**
237      * Gets two new lines.
238      * @return a string containing two new lines.
239      */
240     public String doubleLine() {
241         return "\n\n";
242     }
243 
244     /**
245      * Gets a list of license property descriptions.
246      * @return a list of license property descriptions.
247      */
248     public List<Description> licenseProperties() {
249         SortedSet<ILicense> licenses = licenseSetFactory.getLicenses(LicenseSetFactory.LicenseFilter.ALL);
250         Description licenseDescription = DescriptionBuilder.build(licenses.first());
251         List<Description> descriptions = new ArrayList<>(licenseDescription.filterChildren(d -> d.getType() == ComponentType.PARAMETER));
252         descriptions.sort(Comparator.comparing(Description::getCommonName));
253         return descriptions;
254     }
255 
256     /**
257      * Gets the list of defined licenses.
258      * @return the list of defined licenses.
259      */
260     public List<License> licenses() {
261         Set<ILicense> licenses = licenseSetFactory.getLicenses(LicenseSetFactory.LicenseFilter.ALL);
262         return licenses.stream().map(License::new).collect(Collectors.toList());
263     }
264 
265     /**
266      * Creates a string of spaces of the specified length.
267      * @param length the length of the string.
268      * @return a string of spaces of the specified length.
269      */
270     public String pad(final int length) {
271         return AbstractHelp.createPadding(length);
272     }
273 }