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.report.claim.util;
20  
21  import java.util.List;
22  
23  import org.apache.rat.api.Document;
24  import org.apache.rat.api.RatException;
25  import org.apache.rat.document.IDocumentAnalyser;
26  import org.apache.rat.document.RatDocumentAnalysisException;
27  import org.apache.rat.report.RatReport;
28  import org.apache.rat.report.xml.XmlReportFactory;
29  import org.apache.rat.report.xml.writer.IXmlWriter;
30  
31  /**
32   * Executes a RatReport that multiplexes the running of multiple RatReports
33   */
34  public class ClaimReporterMultiplexer implements RatReport {
35  
36      private final IDocumentAnalyser analyser;
37      private final List<? extends RatReport> reporters;
38      private final boolean dryRun;
39  
40      private final IXmlWriter writer;
41  
42      /**
43       * A multiplexer to run multiple claim reports.
44       * @param dryRun true if this is a dry run.
45       * @param analyser the analyser to use.
46       * @param reporters the reports to execute.
47       */
48      public ClaimReporterMultiplexer(final IXmlWriter writer, final boolean dryRun, final IDocumentAnalyser analyser,
49                                      final List<? extends RatReport> reporters) {
50          this.analyser  = analyser;
51          this.reporters = reporters;
52          this.dryRun = dryRun;
53          this.writer = writer;
54      }
55  
56      @Override
57      public void report(Document document) throws RatException {
58          if (!dryRun) {
59              if (analyser != null) {
60                  try {
61                      analyser.analyse(document);
62                  } catch (RatDocumentAnalysisException e) {
63                      throw new RatException(e.getMessage(), e);
64                  }
65              }
66              for (RatReport report : reporters) {
67                  report.report(document);
68              }
69          }
70      }
71  
72      @Override
73      public void startReport() throws RatException {
74          XmlReportFactory.startReport(writer);
75          for (RatReport report : reporters) {
76              report.startReport();
77          }
78      }
79  
80      @Override
81      public void endReport() throws RatException {
82          for (RatReport report : reporters) {
83              report.endReport();
84          }
85          XmlReportFactory.endReport(writer);
86      }
87  }