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