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.io.IOException;
22  import java.io.Reader;
23  import java.util.Collection;
24  
25  import org.apache.rat.api.Document;
26  import org.apache.rat.document.DocumentAnalyser;
27  import org.apache.rat.license.ILicense;
28  import org.apache.rat.utils.DefaultLog;
29  
30  import static java.lang.String.format;
31  
32  /**
33   * A Document analyzer that analyses document headers for a license.
34   */
35  class DocumentHeaderAnalyser implements DocumentAnalyser {
36  
37      /** The license to analyse */
38      private final Collection<ILicense> licenses;
39      /** The matcher for generated headers */
40      private final IHeaderMatcher generatedMatcher;
41  
42      /**
43       * Constructs the HeaderAnalyser for the specific license.
44       * @param licenses The licenses to analyse
45       */
46      DocumentHeaderAnalyser(final IHeaderMatcher generatedMatcher, final Collection<ILicense> licenses) {
47          super();
48          this.generatedMatcher = generatedMatcher;
49          this.licenses = licenses;
50      }
51  
52      @Override
53      public void analyse(final Document document) {
54          if (!document.isIgnored()) {
55              try (Reader reader = document.reader()) {
56                  DefaultLog.getInstance().debug(format("Processing: %s", document));
57                  HeaderCheckWorker worker = new HeaderCheckWorker(generatedMatcher, reader, licenses, document);
58                  worker.read();
59              } catch (IOException e) {
60                  DefaultLog.getInstance().warn(String.format("Cannot read header of %s", document));
61                  document.getMetaData().setDocumentType(Document.Type.UNKNOWN);
62              } catch (RatHeaderAnalysisException e) {
63                  DefaultLog.getInstance().warn(String.format("Cannot process header of %s", document));
64                  document.getMetaData().setDocumentType(Document.Type.UNKNOWN);
65              }
66          }
67      }
68  }