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.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   * Class that executes the report as defined in a ReportConfiguration.
38   */
39  public class Reporter {
40  
41      /*
42       * Format used for listing license families
43       */
44      private static final String LICENSE_FAMILY_FORMAT = "\t%s: %s\n";
45  
46      /**
47       * Format used for listing licenses.
48       */
49      private static final String LICENSE_FORMAT = "%s:\t%s\n\t\t%s\n";
50      
51      private Reporter() {
52          // Do not instantiate
53      }
54  
55      /**
56       * Execute the report.
57       * 
58       * @param configuration The report configuration.
59       * @return the currently collected numerical statistics.
60       * @throws Exception in case of errors.
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       * Execute the report.
88       * @param outputWriter the writer to send output to.
89       * @param configuration The report configuration.
90       * @return the currently collected numerical statistics.
91       * @throws IOException in case of I/O errors.
92       * @throws RatException in case of internal errors.
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      * lists the license families information on the configured output stream.
111      * @param configuration The configuration for the system
112      * @throws IOException if PrintWriter can not be retrieved from configuration.
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      * lists the licenses on the configured output stream.
125      * @param configuration The configuration for the system
126      * @throws IOException if PrintWriter can not be retrieved from configuration.
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 }