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  
24  import org.apache.rat.api.Document;
25  import org.apache.rat.document.IDocumentAnalyser;
26  import org.apache.rat.document.RatDocumentAnalysisException;
27  import org.apache.rat.license.ILicense;
28  import org.apache.rat.utils.Log;
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 IDocumentAnalyser {
36  
37      /**
38       * The license to analyse
39       */
40      private final ILicense license;
41      /** the logger to use */
42      private final Log log;
43  
44      /**
45       * Constructs the HeaderAnalyser for the specific license.
46       * 
47       * @param license The license to analyse
48       */
49      public DocumentHeaderAnalyser(final Log log, final ILicense license) {
50          super();
51          this.license = license;
52          this.log = log;
53      }
54  
55      @Override
56      public void analyse(Document document) throws RatDocumentAnalysisException {
57          try (Reader reader = document.reader()) {
58              log.debug(format("Processing: %s", document));
59              HeaderCheckWorker worker = new HeaderCheckWorker(reader, license, document);
60              worker.read();
61          } catch (IOException e) {
62              throw new RatDocumentAnalysisException("Cannot read header", e);
63          } catch (RatHeaderAnalysisException e) {
64              throw new RatDocumentAnalysisException("Cannot analyse header", e);
65          }
66      }
67  
68  }