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.analysis;
20  
21  import java.util.Collection;
22  import java.util.Set;
23  import java.util.function.Predicate;
24  
25  import org.apache.rat.ConfigurationException;
26  import org.apache.rat.Defaults;
27  import org.apache.rat.ReportConfiguration;
28  import org.apache.rat.api.Document;
29  import org.apache.rat.api.RatException;
30  import org.apache.rat.document.IDocumentAnalyser;
31  import org.apache.rat.document.RatDocumentAnalysisException;
32  import org.apache.rat.license.ILicense;
33  import org.apache.rat.license.LicenseSetFactory;
34  import org.apache.rat.walker.ArchiveWalker;
35  
36  /**
37   * Creates default analysers.
38   */
39  public class DefaultAnalyserFactory {
40  
41      /**
42       * Creates a DocumentAnalyser from a collection of ILicenses.
43       * 
44       * @param configuration the ReportConfiguration
45       * @return A document analyser that uses the provides licenses.
46       */
47      public static IDocumentAnalyser createDefaultAnalyser(final ReportConfiguration configuration) {
48          Set<ILicense> licenses = configuration.getLicenses(LicenseSetFactory.LicenseFilter.ALL);
49          if (licenses.isEmpty()) {
50              throw new ConfigurationException("At least one license must be defined");
51          }
52          configuration.getLog().debug("Licenses in Test");
53          licenses.forEach(configuration.getLog()::debug);
54          return new DefaultAnalyser(configuration, licenses);
55      }
56  
57      /**
58       * A DocumentAnalyser a collection of licenses
59       */
60      private final static class DefaultAnalyser implements IDocumentAnalyser {
61  
62          /** The licenses to analyze */
63          private final Collection<ILicense> licenses;
64  
65          /** the Report Configuration */
66          private final ReportConfiguration configuration;
67  
68          /**
69           * Constructs a DocumentAnalyser for the specified license.
70           * @param config the ReportConfiguration
71           * @param licenses The licenses to analyse
72           */
73          public DefaultAnalyser(ReportConfiguration config, final Collection<ILicense> licenses) {
74              this.licenses = licenses;
75              this.configuration = config;
76          }
77  
78          /**
79           * Generates a predicate to filter out licenses that should not be reported.
80           * @param proc the processing status to filter.
81           * @return a Predicate to do the filtering.
82           */
83          private  Predicate<ILicense> licenseFilter(ReportConfiguration.Processing proc)  {
84              return license -> {
85                  switch (proc) {
86                      case PRESENCE:
87                          return !license.getLicenseFamily().equals(UnknownLicense.INSTANCE.getLicenseFamily());
88                      case ABSENCE:
89                          return true;
90                      default:
91                          return false;
92                  }
93              };
94          }
95  
96          @Override
97          public void analyse(Document document) throws RatDocumentAnalysisException {
98  
99              TikaProcessor.process(configuration.getLog(), document);
100             Predicate<ILicense> licensePredicate = null;
101 
102             switch (document.getMetaData().getDocumentType()) {
103             case STANDARD:
104                 licensePredicate = licenseFilter(configuration.getStandardProcessing()).negate();
105                 new DocumentHeaderAnalyser(configuration.getLog(), licenses).analyse(document);
106                 if (configuration.getStandardProcessing() != Defaults.STANDARD_PROCESSING) {
107                     document.getMetaData().removeLicenses(licensePredicate);
108                 }
109                 break;
110             case ARCHIVE:
111                 licensePredicate = licenseFilter(configuration.getArchiveProcessing());
112                 if (configuration.getArchiveProcessing() != ReportConfiguration.Processing.NOTIFICATION) {
113                     ArchiveWalker archiveWalker = new ArchiveWalker(configuration, document);
114                     try {
115                         for (Document doc : archiveWalker.getDocuments(configuration.getLog())) {
116                             analyse(doc);
117                             doc.getMetaData().licenses().filter(licensePredicate).forEach(lic -> document.getMetaData().reportOnLicense(lic));
118                         }
119                     } catch (RatException e) {
120                         throw new RatDocumentAnalysisException(e);
121                     }
122                 }
123                 break;
124             case NOTICE:
125             case BINARY:
126             case UNKNOWN:
127             default:
128                 break;
129             }
130         }
131     }
132 }