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