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