1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.anttasks;
20
21 import org.apache.commons.io.IOUtils;
22 import org.apache.rat.Defaults;
23 import org.apache.rat.ReportConfiguration;
24 import org.apache.rat.analysis.IHeaderMatcher;
25 import org.apache.rat.analysis.util.HeaderMatcherMultiplexer;
26 import org.apache.rat.api.RatException;
27 import org.apache.rat.license.ILicenseFamily;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.Project;
30 import org.apache.tools.ant.Task;
31 import org.apache.tools.ant.taskdefs.LogOutputStream;
32 import org.apache.tools.ant.types.EnumeratedAttribute;
33 import org.apache.tools.ant.types.Resource;
34 import org.apache.tools.ant.types.ResourceCollection;
35 import org.apache.tools.ant.types.resources.Union;
36 import org.apache.tools.ant.util.FileUtils;
37
38 import javax.xml.transform.TransformerException;
39 import java.io.File;
40 import java.io.FileWriter;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.io.OutputStreamWriter;
44 import java.io.PrintWriter;
45 import java.util.ArrayList;
46 import java.util.List;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public class Report extends Task {
66
67
68
69
70 private Union nestedResources;
71
72
73
74 private ArrayList<IHeaderMatcher> licenseMatchers = new ArrayList<IHeaderMatcher>();
75
76 private ArrayList<ILicenseFamily> licenseNames = new ArrayList<ILicenseFamily>();
77
78
79
80
81 private boolean addDefaultLicenseMatchers = true;
82
83
84
85 private File reportFile;
86
87
88
89 private Format format = Format.PLAIN;
90
91
92
93 private Resource stylesheet;
94
95
96
97 private AddLicenseHeaders addLicenseHeaders = new AddLicenseHeaders(AddLicenseHeaders.FALSE);
98
99
100
101 private String copyrightMessage;
102
103
104
105
106
107 public void add(ResourceCollection rc) {
108 if (nestedResources == null) {
109 nestedResources = new Union();
110 }
111 nestedResources.add(rc);
112 }
113
114
115
116
117 public void add(IHeaderMatcher matcher) {
118 licenseMatchers.add(matcher);
119 }
120
121 public void add(ILicenseFamily license) {
122 licenseNames.add(license);
123 }
124
125
126
127
128 public void setAddDefaultLicenseMatchers(boolean addDefaultLicenseMatchers) {
129 this.addDefaultLicenseMatchers = addDefaultLicenseMatchers;
130 }
131
132
133
134
135
136 public void setReportFile(File f) {
137 reportFile = f;
138 }
139
140
141
142
143
144 public void setFormat(Format f) {
145 if (f == null) {
146 throw new IllegalArgumentException("format must not be null");
147 }
148 format = f;
149 }
150
151
152
153
154 public void setAddLicenseHeaders(AddLicenseHeaders pAdd) {
155 if (pAdd == null) {
156 throw new IllegalArgumentException("addLicenseHeaders must not be null");
157 }
158 addLicenseHeaders = pAdd;
159 }
160
161
162
163
164 public void setCopyrightMessage(String pMessage) {
165 copyrightMessage = pMessage;
166 }
167
168
169
170
171
172 public void addConfiguredStylesheet(Union u) {
173 if (stylesheet != null || u.size() != 1) {
174 throw new BuildException("You must not specify more than one"
175 + " stylesheet.");
176 }
177 stylesheet = (Resource) u.iterator().next();
178 }
179
180
181
182
183 @Override
184 public void execute() {
185 validate();
186
187 PrintWriter out = null;
188 try {
189 if (reportFile == null) {
190 out = new PrintWriter(
191 new OutputStreamWriter(
192 new LogOutputStream(this, Project.MSG_INFO)
193 )
194 );
195 } else {
196 out = new PrintWriter(new FileWriter(reportFile));
197 }
198 createReport(out);
199 out.flush();
200 } catch (IOException ioex) {
201 throw new BuildException(ioex);
202 } catch (TransformerException e) {
203 throw new BuildException(e);
204 } catch (InterruptedException e) {
205 throw new BuildException(e);
206 } catch (RatException e) {
207 throw new BuildException(e);
208 } finally {
209 IOUtils.closeQuietly(out);
210 }
211 }
212
213
214
215
216 private void validate() {
217 if (nestedResources == null) {
218 throw new BuildException("You must specify at least one file to"
219 + " create the report for.");
220 }
221 if (!addDefaultLicenseMatchers && licenseMatchers.size() == 0) {
222 throw new BuildException("You must specify at least one license"
223 + " matcher");
224 }
225 if (format.getValue().equals(Format.STYLED_KEY)) {
226 if (stylesheet == null) {
227 throw new BuildException("You must specify a stylesheet when"
228 + " using the 'styled' format");
229 }
230 if (!stylesheet.isExists()) {
231 throw new BuildException("Cannot find specified stylesheet '"
232 + stylesheet + "'");
233 }
234 } else if (stylesheet != null) {
235 log("Ignoring stylesheet '" + stylesheet + "' when using format '"
236 + format.getValue() + "'", Project.MSG_WARN);
237 }
238 }
239
240
241
242
243
244
245
246
247
248
249
250 private void createReport(PrintWriter out) throws IOException, TransformerException, InterruptedException, RatException {
251 final ReportConfiguration configuration = new ReportConfiguration();
252 configuration.setHeaderMatcher(new HeaderMatcherMultiplexer(getLicenseMatchers()));
253 configuration.setApprovedLicenseNames(getApprovedLicenseNames());
254 configuration.setApproveDefaultLicenses(addDefaultLicenseMatchers);
255
256 if (AddLicenseHeaders.FORCED.equalsIgnoreCase(addLicenseHeaders.getValue())) {
257 configuration.setAddingLicenses(true);
258 configuration.setAddingLicensesForced(true);
259 configuration.setCopyrightMessage(copyrightMessage);
260 } else if (AddLicenseHeaders.TRUE.equalsIgnoreCase(addLicenseHeaders.getValue())) {
261 configuration.setAddingLicenses(true);
262 configuration.setCopyrightMessage(copyrightMessage);
263 } else if (!AddLicenseHeaders.FALSE.equalsIgnoreCase(addLicenseHeaders.getValue())) {
264 throw new BuildException("Invalid value for addLicenseHeaders: " + addLicenseHeaders.getValue());
265 }
266 ResourceCollectionContainer rcElement = new ResourceCollectionContainer(nestedResources);
267 if (format.getValue().equals(Format.XML_KEY)) {
268 org.apache.rat.Report.report(rcElement, out, configuration);
269 } else {
270 InputStream style = null;
271 try {
272 if (format.getValue().equals(Format.PLAIN_KEY)) {
273 style = Defaults.getPlainStyleSheet();
274 } else if (format.getValue().equals(Format.STYLED_KEY)) {
275 style = stylesheet.getInputStream();
276 } else {
277 throw new BuildException("unsupported format '"
278 + format.getValue() + "'");
279 }
280 org.apache.rat.Report.report(out, rcElement, style,
281 configuration);
282 } finally {
283 FileUtils.close(style);
284 }
285 }
286 }
287
288
289
290
291
292 private List<IHeaderMatcher> getLicenseMatchers() {
293 List<IHeaderMatcher> matchers = new ArrayList<IHeaderMatcher>(Defaults.DEFAULT_MATCHERS);
294 if (addDefaultLicenseMatchers) {
295 int nestedSize = licenseMatchers.size();
296 if (nestedSize != 0) {
297 matchers.addAll(licenseMatchers);
298 }
299 } else {
300 matchers = new ArrayList<IHeaderMatcher>();
301 }
302 return matchers;
303 }
304
305 private ILicenseFamily[] getApprovedLicenseNames() {
306
307 ILicenseFamily[] results = null;
308 if (licenseNames.size() > 0) {
309 results = licenseNames.toArray(new ILicenseFamily[0]);
310 }
311 return results;
312 }
313
314
315
316
317 public static class Format extends EnumeratedAttribute {
318 static final String XML_KEY = "xml";
319 static final String STYLED_KEY = "styled";
320 static final String PLAIN_KEY = "plain";
321
322 static final Format PLAIN = new Format(PLAIN_KEY);
323
324 public Format() { super(); }
325
326 private Format(String s) {
327 this();
328 setValue(s);
329 }
330
331 @Override
332 public String[] getValues() {
333 return new String[] {
334 XML_KEY, STYLED_KEY, PLAIN_KEY
335 };
336 }
337 }
338
339
340
341
342 public static class AddLicenseHeaders extends EnumeratedAttribute {
343 static final String TRUE = "true";
344 static final String FALSE = "false";
345 static final String FORCED = "forced";
346
347 public AddLicenseHeaders() {}
348 public AddLicenseHeaders(String s) {
349 setValue(s);
350 }
351
352 @Override
353 public String[] getValues() {
354 return new String[] {
355 TRUE, FALSE, FORCED
356 };
357 }
358 }
359 }