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