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