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.license;
20  
21  import org.apache.rat.analysis.IHeaderMatcher;
22  import org.apache.rat.analysis.RatHeaderAnalysisException;
23  import org.apache.rat.api.Document;
24  import org.apache.rat.api.MetaData.Datum;
25  
26  import java.util.regex.Pattern;
27  
28  /**
29   * Matches a typical Copyright header line only based on a regex pattern
30   * which allows for one (starting) year or year range, and a
31   * configurable copyright owner.
32   * <br>
33   * <br>The matching is done case insensitive<br>
34   * <br>
35   * Example supported Copyright header lines, using copyright owner &quot;FooBar&quot;
36   * <ul>
37   * <li>* Copyright 2010 FooBar. *</li>
38   * <li>* Copyright 2010-2012 FooBar. *</li>
39   * <li>*copyright 2012 foobar*</li>
40   * </ul>
41   * <p>Note also that the copyright owner is appended to the regex pattern, so
42   * can support additional regex but also requires escaping where needed,<p>
43   * e.g. use &quot;FooBar \(www\.foobar\.com\)&quot; for matching &quot;FooBar (www.foobar.com)&quot;<p>
44   *
45   * @since Rat 0.9
46   */
47  public class CopyrightHeader extends BaseLicense implements IHeaderMatcher {
48  
49      public static final String COPYRIGHT_PREFIX_PATTERN_DEFN = ".*Copyright [0-9]{4}(\\-[0-9]{4})? ";
50  
51      private Pattern copyrightPattern;
52      private String copyrightOwner;
53      private boolean copyrightMatch = false;
54  
55      public CopyrightHeader() {
56      }
57  
58      protected CopyrightHeader(Datum licenseFamilyCategory, Datum licenseFamilyName, String notes) {
59          super(licenseFamilyCategory, licenseFamilyName, notes);
60      }
61  
62      protected CopyrightHeader(Datum licenseFamilyCategory, Datum licenseFamilyName, String notes, String copyrightOwner) {
63          this(licenseFamilyCategory, licenseFamilyName, notes);
64          setCopyrightOwner(copyrightOwner);
65      }
66  
67      // Called by ctor, so must not be overridden
68      public final void setCopyrightOwner(String copyrightOwner) {
69          this.copyrightOwner = copyrightOwner;
70          this.copyrightPattern = Pattern.compile(COPYRIGHT_PREFIX_PATTERN_DEFN + copyrightOwner + ".*", Pattern.CASE_INSENSITIVE);
71      }
72  
73      public String getCopyRightOwner() {
74          return copyrightOwner;
75      }
76  
77      public boolean hasCopyrightPattern() {
78          return copyrightPattern != null;
79      }
80  
81      protected boolean isCopyrightMatch() {
82          return copyrightMatch;
83      }
84  
85      protected boolean matchCopyright(String s) {
86          if (!copyrightMatch) {
87              copyrightMatch = copyrightPattern.matcher(s).matches();
88          }
89          return copyrightMatch;
90      }
91  
92      public boolean match(Document subject, String s) throws RatHeaderAnalysisException {
93          if (!copyrightMatch && matchCopyright(s)) {
94              reportOnLicense(subject);
95          }
96          return copyrightMatch;
97      }
98  
99      public void reset() {
100         copyrightMatch = false;
101     }
102 }