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;
27  
28  /**
29   * Looks for documents contain the OASIS copyright claim plus derivative work clause.
30   * Perhaps need to match more.
31   */
32  public class OASISLicense extends BaseLicense implements IHeaderMatcher {
33  
34      private static final String COPYRIGHT_PATTERN_DEFN = ".*Copyright.*OASIS Open.*";
35      private static final String CLAUSE_DEFN
36      = ".*thisdocumentandtranslationsofitmaybecopiedandfurnishedtoothersandderivativeworks" +
37              "thatcommentonorotherwiseexplainitorassistinitsimplementationmaybeprepared" +
38              "copiedpublishedanddistributed.*";
39      
40      private static final Pattern COPYRIGHT_PATTERN = Pattern.compile(COPYRIGHT_PATTERN_DEFN);
41      private static final Pattern CLAUSE_PATTERN = Pattern.compile(CLAUSE_DEFN);
42  
43      boolean copyrightMatch = false;
44      final StringBuffer buffer = new StringBuffer();
45      
46      public OASISLicense() {
47          super(MetaData.RAT_LICENSE_FAMILY_CATEGORY_DATUM_OASIS, MetaData.RAT_LICENSE_FAMILY_NAME_DATUM_OASIS_OPEN_LICENSE, "No modifications allowed");
48      }
49  
50      public boolean match(Document subject, String line) throws RatHeaderAnalysisException {
51          boolean result = false;
52          if (copyrightMatch) {
53              line = line.toLowerCase();
54              buffer.append(line);
55              prune(buffer); 
56              final boolean clauseMatch = CLAUSE_PATTERN.matcher(buffer).matches();
57              if (clauseMatch) {
58                  result = true;
59                  reportOnLicense(subject);
60              }
61              
62          } else {
63              copyrightMatch = COPYRIGHT_PATTERN.matcher(line).matches();
64          }
65          return result;
66      }
67      
68      private void prune(final StringBuffer buffer) {
69          final int length = buffer.length();
70          for (int i=length;i>0;) {
71              char at = buffer.charAt(--i);
72              if (at < 'a' || at > 'z')
73              {
74                  buffer.deleteCharAt(i);
75              }
76          }
77      }
78  
79      public void reset() {
80          copyrightMatch = false;
81          buffer.delete(0, buffer.length());
82      }
83  }