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