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;
20  
21  import java.io.File;
22  import java.io.FilenameFilter;
23  import java.io.InputStream;
24  import java.net.MalformedURLException;
25  import java.net.URL;
26  import java.util.Collection;
27  import java.util.Comparator;
28  import java.util.Set;
29  import java.util.SortedSet;
30  import java.util.TreeSet;
31  
32  import org.apache.commons.io.filefilter.FalseFileFilter;
33  import org.apache.commons.io.filefilter.IOFileFilter;
34  import org.apache.commons.io.function.IOSupplier;
35  import org.apache.rat.configuration.Format;
36  import org.apache.rat.configuration.LicenseReader;
37  import org.apache.rat.configuration.MatcherReader;
38  import org.apache.rat.license.ILicense;
39  import org.apache.rat.license.ILicenseFamily;
40  import org.apache.rat.license.LicenseSetFactory;
41  import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
42  import org.apache.rat.utils.Log;
43  import org.apache.rat.walker.NameBasedHiddenFileFilter;
44  
45  /**
46   * A class that provides the standard system defaults for the ReportConfiguration.
47   * Properties in this class may be overridden or added to by configuration options in the various UIs.
48   * See the specific UI for details.
49   */
50  public final class Defaults {
51  
52      /**
53       * The default configuration file from the package.
54       */
55      private static final URL DEFAULT_CONFIG_URL = Defaults.class.getResource("/org/apache/rat/default.xml");
56      /**
57       * The default XSLT stylesheet to produce a text output file.
58       */
59      public static final String PLAIN_STYLESHEET = "org/apache/rat/plain-rat.xsl";
60      /**
61       * The default XSLT stylesheet to produce a list of unapproved licenses.
62       */
63      public static final String UNAPPROVED_LICENSES_STYLESHEET = "org/apache/rat/unapproved-licenses.xsl";
64  
65      public static final FilenameFilter FILES_TO_IGNORE = FalseFileFilter.FALSE;
66  
67      public static final IOFileFilter DIRECTORIES_TO_IGNORE = NameBasedHiddenFileFilter.HIDDEN;
68  
69      public static final ReportConfiguration.Processing ARCHIVE_PROCESSING = ReportConfiguration.Processing.NOTIFICATION;
70  
71      public static final ReportConfiguration.Processing STANDARD_PROCESSING = ReportConfiguration.Processing.ABSENCE;
72  
73      public static final LicenseFilter LIST_FAMILIES = LicenseFilter.NONE;
74  
75      public static final LicenseFilter LIST_LICENSES = LicenseFilter.NONE;
76  
77      private final LicenseSetFactory setFactory;
78  
79  
80      /**
81       * Initialize the system configuration reader.
82       */
83      public static void init() {
84          Format fmt = Format.fromURL(DEFAULT_CONFIG_URL);
85          MatcherReader mReader = fmt.matcherReader();
86          mReader.addMatchers(DEFAULT_CONFIG_URL);
87          mReader.readMatcherBuilders();
88      }
89  
90      /**
91       * Builder constructs instances.
92       */
93      private Defaults(final Log log, final Set<URL> urls) {
94          this.setFactory = Defaults.readConfigFiles(log, urls);
95      }
96  
97      /**
98       * Gets a builder for a Defaults object.
99       * @return the Builder.
100      */
101     public static Builder builder() {
102         return new Builder();
103     }
104 
105     /**
106      * Reads the configuration files.
107      * @param urls the URLs to read.
108      */
109     private static LicenseSetFactory readConfigFiles(final Log log, final Collection<URL> urls) {
110 
111         SortedSet<ILicense> licenses = LicenseSetFactory.emptyLicenseSet();
112 
113         SortedSet<String> approvedLicenseIds = new TreeSet<>();
114 
115         for (URL url : urls) {
116             Format fmt = Format.fromURL(url);
117             MatcherReader mReader = fmt.matcherReader();
118             if (mReader != null) {
119                 mReader.addMatchers(url);
120                 mReader.readMatcherBuilders();
121             }
122 
123             LicenseReader lReader = fmt.licenseReader();
124             if (lReader != null) {
125                 lReader.setLog(log);
126                 lReader.addLicenses(url);
127                 licenses.addAll(lReader.readLicenses());
128                 lReader.approvedLicenseId().stream().map(ILicenseFamily::makeCategory).forEach(approvedLicenseIds::add);
129             }
130         }
131         return new LicenseSetFactory(licenses, approvedLicenseIds);
132     }
133 
134     /**
135      * Gets a supplier for the "plain" text stylesheet.
136      * @return an IOSupplier for the plain text stylesheet.
137      */
138     public static IOSupplier<InputStream> getPlainStyleSheet() {
139         return () -> Defaults.class.getClassLoader().getResourceAsStream(Defaults.PLAIN_STYLESHEET);
140     }
141 
142     /**
143      * Gets a supplier for the unapproved licences list stylesheet
144      * @return an IOSupplier for the unapproved licenses list stylesheet.
145      */
146     public static IOSupplier<InputStream> getUnapprovedLicensesStyleSheet() {
147         return () -> Defaults.class.getClassLoader().getResourceAsStream(Defaults.UNAPPROVED_LICENSES_STYLESHEET);
148     }
149 
150     /**
151      * Gets the sorted set of approved licenses for a given filter condition.
152      * @param filter define which type of licenses to return.
153      * @return sorted set of licenses.
154      */
155     public SortedSet<ILicense> getLicenses(final LicenseFilter filter) {
156         return setFactory.getLicenses(filter);
157     }
158 
159     /**
160      * Gets the sorted set of approved licenses for a given filter condition.
161      * @param filter define which type of licenses to return.
162      * @return sorted set of license families.
163      */
164     public SortedSet<ILicenseFamily> getLicenseFamilies(final LicenseFilter filter) {
165         return setFactory.getLicenseFamilies(filter);
166     }
167 
168     /**
169      * Gets the sorted set of approved license ids for a given filter condition.
170      * If no licenses have been explicitly listed as approved, all licenses are assumed to be approved.
171      * @param filter define which type of licenses to return.
172      * @return The sorted set of approved licenseIds.
173      */
174     public SortedSet<String> getLicenseIds(final LicenseFilter filter) {
175         return setFactory.getLicenseFamilyIds(filter);
176     }
177     
178     /**
179      * The Defaults builder.
180      */
181     public final static class Builder {
182         private final Set<URL> fileNames = new TreeSet<>(Comparator.comparing(URL::toString));
183 
184         private Builder() {
185             fileNames.add(DEFAULT_CONFIG_URL);
186         }
187 
188         /**
189          * Adds a URL to a configuration file to be read.
190          * 
191          * @param url the URL to add
192          * @return this Builder for chaining
193          */
194         public Builder add(final URL url) {
195             fileNames.add(url);
196             return this;
197         }
198 
199         /**
200          * Adds the name of a configuration file to be read.
201          * 
202          * @param fileName the name of the file to add.
203          * @return this Builder for chaining
204          * @throws MalformedURLException in case the fileName cannot be found.
205          */
206         public Builder add(final String fileName) throws MalformedURLException {
207             return add(new File(fileName));
208         }
209 
210         /**
211          * Adds a configuration file to be read.
212          * 
213          * @param file the File to add.
214          * @return this Builder for chaining
215          * @throws MalformedURLException in case the file cannot be found.
216          */
217         public Builder add(final File file) throws MalformedURLException {
218             return add(file.toURI().toURL());
219         }
220 
221         /**
222          * Removes a file from the list of configuration files to process.
223          * 
224          * @param url the URL of the file to remove.
225          * @return this Builder for chaining
226          */
227         public Builder remove(final URL url) {
228             fileNames.remove(url);
229             return this;
230         }
231 
232         /**
233          * Removes a file name from the list of configuration files to process.
234          * 
235          * @param fileName the fileName of the file to remove.
236          * @return this Builder for chaining
237          * @throws MalformedURLException in case the fileName cannot be found.
238          */
239         public Builder remove(final String fileName) throws MalformedURLException {
240             return remove(new File(fileName));
241         }
242 
243         /**
244          * Removes a file from the list of configuration files to process.
245          * 
246          * @param file the File of the file to remove.
247          * @return this Builder for chaining
248          * @throws MalformedURLException in case the file cannot be found.
249          */
250         public Builder remove(final File file) throws MalformedURLException {
251             return remove(file.toURI().toURL());
252         }
253 
254         /**
255          * Removes the default definitions from the list of files to process.
256          * 
257          * @return this Builder for chaining
258          */
259         public Builder noDefault() {
260             return remove(DEFAULT_CONFIG_URL);
261         }
262 
263         /**
264          * Builds the defaults object.
265          * @param log the Log to use to report errors when building the defaults.
266          * @return the current defaults object.
267          */
268         public Defaults build(final Log log) {
269             return new Defaults(log, fileNames);
270         }
271     }
272 }