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.net.MalformedURLException;
23  import java.net.URI;
24  import java.net.URISyntaxException;
25  import java.net.URL;
26  import java.util.Arrays;
27  import java.util.Collection;
28  import java.util.Set;
29  import java.util.SortedSet;
30  import java.util.TreeSet;
31  
32  import org.apache.rat.config.exclusion.StandardCollection;
33  import org.apache.rat.configuration.Format;
34  import org.apache.rat.configuration.LicenseReader;
35  import org.apache.rat.configuration.MatcherReader;
36  import org.apache.rat.license.ILicense;
37  import org.apache.rat.license.ILicenseFamily;
38  import org.apache.rat.license.LicenseSetFactory;
39  import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
40  import org.apache.rat.utils.DefaultLog;
41  
42  import static java.lang.String.format;
43  
44  /**
45   * A class that provides the standard system defaults for the ReportConfiguration.
46   * Properties in this class may be overridden or added to by configuration options in the various UIs.
47   * See the specific UI for details.
48   */
49  public final class Defaults {
50      /** The default configuration file from the package. */
51      private static final URI DEFAULT_CONFIG_URI;
52      /** The path to the default configuration file. */
53      private static final String DEFAULT_CONFIG_PATH = "/org/apache/rat/default.xml";
54      /** The default ARCHIVES processing style. */
55      public static final ReportConfiguration.Processing ARCHIVE_PROCESSING = ReportConfiguration.Processing.NOTIFICATION;
56      /** The default STANDARD processing style. */
57      public static final ReportConfiguration.Processing STANDARD_PROCESSING = ReportConfiguration.Processing.ABSENCE;
58      /** The default license families to list. */
59      public static final LicenseFilter LIST_FAMILIES = LicenseFilter.NONE;
60      /** The default licenses to list. */
61      public static final LicenseFilter LIST_LICENSES = LicenseFilter.NONE;
62  
63      /** The license set factory to build license sets based upon default options */
64      private final LicenseSetFactory setFactory;
65  
66      // TODO look at this static block with respect to the init() static method and figure out if we need both.
67      static {
68           URL url = Defaults.class.getResource(DEFAULT_CONFIG_PATH);
69           URI uri = null;
70           if (url == null) {
71               DefaultLog.getInstance().error(format("Unable to read '%s'", DEFAULT_CONFIG_PATH));
72           } else {
73               try {
74                   uri = url.toURI();
75               } catch (URISyntaxException e) {
76                   DefaultLog.getInstance().error("Unable to read " + url, e);
77               }
78           }
79           DEFAULT_CONFIG_URI = uri;
80      }
81      /**
82       * Initialize the system configuration reader.
83       */
84      public static void init() {
85          Format fmt = Format.from(DEFAULT_CONFIG_URI);
86          MatcherReader mReader = fmt.matcherReader();
87          if (mReader != null) {
88              mReader.addMatchers(DEFAULT_CONFIG_URI);
89              mReader.readMatcherBuilders();
90          } else {
91              DefaultLog.getInstance().error("Unable to construct MatcherReader from " + DEFAULT_CONFIG_URI);
92          }
93      }
94  
95      /**
96       * Builder constructs instances.
97       * @param uris The set of URIs to read.
98       */
99      private Defaults(final Set<URI> uris) {
100         this.setFactory = Defaults.readConfigFiles(uris);
101     }
102 
103     /**
104      * Gets a builder for a Defaults object.
105      * @return the Builder.
106      */
107     public static Builder builder() {
108         return new Builder();
109     }
110 
111     /**
112      * Reads the configuration files.
113      * @param uris the URIs to read.
114      */
115     private static LicenseSetFactory readConfigFiles(final Collection<URI> uris) {
116 
117         SortedSet<ILicense> licenses = new TreeSet<>();
118 
119         SortedSet<String> approvedLicenseCategories = new TreeSet<>();
120 
121         for (URI uri : uris) {
122             Format fmt = Format.from(uri);
123             MatcherReader mReader = fmt.matcherReader();
124             if (mReader != null) {
125                 mReader.addMatchers(uri);
126                 mReader.readMatcherBuilders();
127             }
128 
129             LicenseReader lReader = fmt.licenseReader();
130             if (lReader != null) {
131                 lReader.addLicenses(uri);
132                 licenses.addAll(lReader.readLicenses());
133                 lReader.approvedLicenseId().stream().map(ILicenseFamily::makeCategory).forEach(approvedLicenseCategories::add);
134             }
135         }
136 
137         LicenseSetFactory result = new LicenseSetFactory(licenses);
138         approvedLicenseCategories.forEach(result::approveLicenseCategory);
139         return result;
140     }
141 
142     /**
143      * Gets the default license set factory.
144      * @return the default license set factory.
145      */
146     public LicenseSetFactory getLicenseSetFactory() {
147         return setFactory;
148     }
149 
150     /**
151      * Gets the default exclusion processor.
152      * @return the default exclusion processor.
153      */
154     public Collection<StandardCollection> getStandardExclusion() {
155         return Arrays.asList(StandardCollection.MISC, StandardCollection.HIDDEN_DIR);
156     }
157 
158 
159     /**
160      * The Defaults builder.
161      */
162     public static final class Builder {
163         /** The list of URIs that we wil read to configure the Defaults */
164         private final Set<URI> fileNames = new TreeSet<>();
165 
166         private Builder() {
167             if (DEFAULT_CONFIG_URI == null) {
168                 DefaultLog.getInstance().error("Unable to read default.xml");
169             } else {
170                fileNames.add(DEFAULT_CONFIG_URI);
171             }
172         }
173 
174         /**
175          * Adds a URL to a configuration file to be read.
176          * @param uri the URI to add
177          * @return this Builder for chaining
178          */
179         public Builder add(final URI uri) {
180             fileNames.add(uri);
181             return this;
182         }
183 
184         /**
185          * Adds the name of a configuration file to be read.
186          * @param fileName the name of the file to add.
187          * @return this Builder for chaining
188          * @throws MalformedURLException in case the fileName cannot be found.
189          */
190         public Builder add(final String fileName) throws MalformedURLException {
191             return add(new File(fileName));
192         }
193 
194         /**
195          * Adds a configuration file to be read.
196          * @param file the File to add.
197          * @return this Builder for chaining
198          */
199         public Builder add(final File file) {
200             return add(file.toURI());
201         }
202 
203         /**
204          * Removes a file from the list of configuration files to process.
205          * @param uri the URI of the file to remove.
206          * @return this Builder for chaining
207          */
208         public Builder remove(final URI uri) {
209             fileNames.remove(uri);
210             return this;
211         }
212 
213         /**
214          * Removes a file name from the list of configuration files to process.
215          * @param fileName the fileName of the file to remove.
216          * @return this Builder for chaining
217          * @throws MalformedURLException in case the fileName cannot be found.
218          */
219         public Builder remove(final String fileName) throws MalformedURLException {
220             return remove(new File(fileName));
221         }
222 
223         /**
224          * Removes a file from the list of configuration files to process.
225          * @param file the File of the file to remove.
226          * @return this Builder for chaining
227          */
228         public Builder remove(final File file) {
229             return remove(file.toURI());
230         }
231 
232         /**
233          * Removes the default definitions from the list of files to process.
234          * @return this Builder for chaining
235          */
236         public Builder noDefault() {
237             return remove(DEFAULT_CONFIG_URI);
238         }
239 
240         /**
241          * Builds the defaults object.
242          * @return the current defaults object.
243          */
244         public Defaults build() {
245             return new Defaults(fileNames);
246         }
247     }
248 }