1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.PipedReader;
24 import java.io.PipedWriter;
25 import java.io.PrintWriter;
26 import java.io.Writer;
27
28 import org.apache.rat.api.RatException;
29 import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
30 import org.apache.rat.report.RatReport;
31 import org.apache.rat.report.claim.ClaimStatistic;
32 import org.apache.rat.report.xml.XmlReportFactory;
33 import org.apache.rat.report.xml.writer.IXmlWriter;
34 import org.apache.rat.report.xml.writer.impl.base.XmlWriter;
35
36
37
38
39 public class Reporter {
40
41
42
43
44 private static final String LICENSE_FAMILY_FORMAT = "\t%s: %s\n";
45
46
47
48
49 private static final String LICENSE_FORMAT = "%s:\t%s\n\t\t%s\n";
50
51 private Reporter() {
52
53 }
54
55
56
57
58
59
60
61
62 public static ClaimStatistic report(ReportConfiguration configuration) throws Exception {
63 if (configuration.getReportable() != null) {
64 if (configuration.isStyleReport()) {
65 try (PipedReader reader = new PipedReader();
66 PipedWriter writer = new PipedWriter(reader);
67 InputStream style = configuration.getStyleSheet().get();
68 PrintWriter reportWriter = configuration.getWriter().get();) {
69 ReportTransformer transformer = new ReportTransformer(reportWriter, style, reader);
70 Thread transformerThread = new Thread(transformer);
71 transformerThread.start();
72 final ClaimStatistic statistic = report(writer, configuration);
73 writer.flush();
74 writer.close();
75 transformerThread.join();
76 return statistic;
77 }
78 }
79 try (Writer writer = configuration.getWriter().get()) {
80 return report(writer, configuration);
81 }
82 }
83 return null;
84 }
85
86
87
88
89
90
91
92
93
94 private static ClaimStatistic report(Writer outputWriter, ReportConfiguration configuration)
95 throws IOException, RatException {
96 try (IXmlWriter writer = new XmlWriter(outputWriter)) {
97 final ClaimStatistic statistic = new ClaimStatistic();
98 RatReport report = XmlReportFactory.createStandardReport(writer, statistic, configuration);
99 report.startReport();
100 configuration.getReportable().run(report);
101 report.endReport();
102
103 return statistic;
104 } catch (Exception e) {
105 throw new IOException(e);
106 }
107 }
108
109
110
111
112
113
114 public static void listLicenseFamilies(ReportConfiguration configuration, LicenseFilter filter) throws IOException {
115 try (PrintWriter pw = configuration.getWriter().get()) {
116 pw.format("Families (%s):%n", filter);
117 configuration.getLicenseFamilies(filter)
118 .forEach(x -> pw.format(LICENSE_FAMILY_FORMAT, x.getFamilyCategory(), x.getFamilyName()));
119 pw.println();
120 }
121 }
122
123
124
125
126
127
128 public static void listLicenses(ReportConfiguration configuration, LicenseFilter filter) throws IOException {
129 try (PrintWriter pw = configuration.getWriter().get()) {
130 pw.format("Licenses (%s):%n", filter);
131 configuration.getLicenses(filter)
132 .forEach(lic -> pw.format(LICENSE_FORMAT, lic.getLicenseFamily().getFamilyCategory(),
133 lic.getLicenseFamily().getFamilyName(), lic.getNotes()));
134 pw.println();
135 }
136 }
137
138 }