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.generation;
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;
27  
28  public class GeneratedLicenseNotRequired implements IHeaderMatcher {
29  
30      private static final String[] EMPTY_STRING_ARRAY = new String[0];
31  
32      private static final Pattern[] EMPTY_PATTERN_ARRAY = new Pattern[0];
33  
34      private static final String[] DEFAULT_PHRASES = {
35          "generated by Cayenne",
36          "Generated By:JJTree",
37          "Generated By:JavaCC",
38          "THIS FILE IS AUTOMATICALLY GENERATED",
39          "NOTE: this file is autogenerated by XBeans",
40          "This file was automatically generated by ",
41          "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!",
42          "# Microsoft Developer Studio Generated NMAKE File",
43          "# Microsoft Developer Studio Generated Build File",
44          "Generated from configure.ac by autoheader",
45          "generated automatically by aclocal",
46          "build.xml generated by maven from project.xml",
47          "This file was generated by",
48          "This file has been automatically generated.",
49          "Automatically generated - do not modify!",
50          "Javadoc style sheet",
51          "SOURCE FILE GENERATATED",
52          "Generated by the Batik",
53          "this file is autogenerated",
54          "This class was autogenerated",
55          "Generated by Maven",
56          "Autogenerated by Thrift",
57          "DO NOT EDIT THIS FILE - it is machine generated",
58          "This class was generated by"};
59      
60      
61      // Uses either patterns or strings currently (not both)
62      private final Pattern[] linePatterns;
63      private final String[] phrases;
64      
65      public GeneratedLicenseNotRequired() {
66          this(DEFAULT_PHRASES);
67      }
68      
69      public GeneratedLicenseNotRequired(final Pattern[] linePatterns) {
70          this.linePatterns = linePatterns;
71          this.phrases = EMPTY_STRING_ARRAY;
72      }
73  
74      public GeneratedLicenseNotRequired(final String[] lines) {
75          this.linePatterns = EMPTY_PATTERN_ARRAY;
76          this.phrases = lines;
77      }
78  
79      public boolean match(Document subject, String line) throws RatHeaderAnalysisException {
80          boolean result = false;
81          for (Pattern pat : linePatterns) {
82              if (pat.matcher(line).matches()) {
83                  result = true;
84                  reportOnLicense(subject);
85                  break;
86              }
87          }
88          for(String phrase : phrases) {
89              if (line.contains(phrase)) {
90                  result = true;
91                  reportOnLicense(subject);
92                  break;
93              }                
94          }
95          return result;
96      }
97  
98      private void reportOnLicense(Document subject) {
99          subject.getMetaData().set(MetaData.RAT_LICENSE_FAMILY_CATEGORY_DATUM_GEN);
100     }
101 
102     public void reset() {
103         // stateless
104     }
105 
106 }