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