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  
23  import org.apache.rat.ConfigurationException;
24  import org.apache.rat.api.Document;
25  import org.apache.rat.api.MetaData;
26  import org.apache.rat.document.IDocumentAnalyser;
27  import org.apache.rat.document.RatDocumentAnalysisException;
28  import org.apache.rat.document.impl.guesser.ArchiveGuesser;
29  import org.apache.rat.document.impl.guesser.BinaryGuesser;
30  import org.apache.rat.document.impl.guesser.NoteGuesser;
31  import org.apache.rat.license.ILicense;
32  import org.apache.rat.utils.Log;
33  import org.apache.rat.utils.Log.Level;
34  
35  /**
36   * Creates default analysers.
37   */
38  public class DefaultAnalyserFactory {
39  
40      /**
41       * Creates a DocumentAnalyser from a collection of ILicenses.
42       * @param licenses The licenses to use in  the Analyser.
43       * @return A document analyser that uses the provides licenses.
44       */
45      public static IDocumentAnalyser createDefaultAnalyser(Log log, Collection<ILicense> licenses) {
46          if (licenses.isEmpty()) {
47              throw new ConfigurationException("At least one license must be defined");
48          }
49          log.debug("Licenses in Test");
50          licenses.forEach(log::debug);
51          return new DefaultAnalyser(log, new LicenseCollection(licenses));
52      }
53  
54      /**
55       * A DocumentAnalyser for the license
56       */
57      private final static class DefaultAnalyser implements IDocumentAnalyser {
58  
59          /**
60           * The license to analyze
61           */
62          private final ILicense license;
63          /** The log to use */
64          private final Log log;
65  
66          /**
67           * Constructs a DocumentAnalyser for the specified license.
68           * @param license The license to analyse
69           */
70          public DefaultAnalyser(final Log log, final ILicense license) {
71              this.license = license;
72              this.log = log;
73          }
74  
75          @Override
76          public void analyse(Document document) throws RatDocumentAnalysisException {
77              final MetaData.Datum documentCategory;
78              if (NoteGuesser.isNote(document)) {
79                  documentCategory = MetaData.RAT_DOCUMENT_CATEGORY_DATUM_NOTICE;
80              } else if (ArchiveGuesser.isArchive(document)) {
81                  documentCategory = MetaData.RAT_DOCUMENT_CATEGORY_DATUM_ARCHIVE;
82              } else if (BinaryGuesser.isBinary(document)) {
83                  documentCategory = MetaData.RAT_DOCUMENT_CATEGORY_DATUM_BINARY;
84              } else {
85                  documentCategory = MetaData.RAT_DOCUMENT_CATEGORY_DATUM_STANDARD;
86                  final DocumentHeaderAnalyser headerAnalyser = new DocumentHeaderAnalyser(log, license);
87                  headerAnalyser.analyse(document);
88              }
89              document.getMetaData().set(documentCategory);
90          }
91      }
92  }