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  
27  /**
28   * @since Rat 0.8
29   */
30  public class SimplePatternBasedLicense extends BaseLicense implements IHeaderMatcher {
31      private String[] patterns;
32  
33      public SimplePatternBasedLicense() {
34      }
35  
36      protected SimplePatternBasedLicense(Datum pLicenseFamilyCategory, Datum pLicenseFamilyName,
37              String pNotes, String[] pPatterns) {
38          super(pLicenseFamilyCategory, pLicenseFamilyName, pNotes);
39          setPatterns(pPatterns);
40      }
41      
42      public String[] getPatterns() {
43          return patterns;
44      }
45  
46      public void setPatterns(String[] pPatterns) {
47          patterns = pPatterns;
48      }
49  
50      protected boolean matches(String pLine) {
51          if (pLine != null) {
52              final String[] pttrns = getPatterns();
53              if (pttrns != null) {
54                  for (String pttrn : pttrns) {
55                      if (pLine.indexOf(pttrn, 0) >= 0) {
56                          return true;
57                      }
58                  }
59              }
60          }
61          return false;
62      }
63      
64      public void reset() {
65          // Nothing to do
66      }
67  
68      public boolean match(Document pSubject, String pLine) throws RatHeaderAnalysisException {
69          final boolean result = matches(pLine);
70          if (result) {
71              reportOnLicense(pSubject);
72          }
73          return result;
74      }
75  }