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 // sonar wants this to be configurable.
53 private static final String DEFAULT_CONFIG_PATH = "/org/apache/rat/default.xml"; // NOSONAR
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 */
189 public Builder add(final String fileName) {
190 return add(new File(fileName));
191 }
192
193 /**
194 * Adds a configuration file to be read.
195 * @param file the File to add.
196 * @return this Builder for chaining
197 */
198 public Builder add(final File file) {
199 return add(file.toURI());
200 }
201
202 /**
203 * Removes a file from the list of configuration files to process.
204 * @param uri the URI of the file to remove.
205 * @return this Builder for chaining
206 */
207 public Builder remove(final URI uri) {
208 fileNames.remove(uri);
209 return this;
210 }
211
212 /**
213 * Removes a file name from the list of configuration files to process.
214 * @param fileName the fileName of the file to remove.
215 * @return this Builder for chaining
216 */
217 public Builder remove(final String fileName) {
218 return remove(new File(fileName));
219 }
220
221 /**
222 * Removes a file from the list of configuration files to process.
223 * @param file the File of the file to remove.
224 * @return this Builder for chaining
225 */
226 public Builder remove(final File file) {
227 return remove(file.toURI());
228 }
229
230 /**
231 * Removes the default definitions from the list of files to process.
232 * @return this Builder for chaining
233 */
234 public Builder noDefault() {
235 return remove(DEFAULT_CONFIG_URI);
236 }
237
238 /**
239 * Builds the defaults object.
240 * @return the current defaults object.
241 */
242 public Defaults build() {
243 return new Defaults(fileNames);
244 }
245 }
246 }