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.anttasks;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import org.apache.rat.analysis.license.SimplePatternBasedLicense;
24  import org.apache.tools.ant.BuildException;
25  
26  /**
27   * Adapts {@link SimplePatternBasedLicense SimplePatternBasedLicense}
28   * to Ant's method naming conventions so it becomes easy to write
29   * substring based license matchers inside an Ant build file.
30   * @since Rat Antlib 0.8
31   */
32  public class SubstringLicenseMatcher extends SimplePatternBasedLicense {
33      private List<String> patterns = new ArrayList<String>();
34      private boolean validated = false;
35  
36      public void addConfiguredPattern(Pattern p) {
37          patterns.add(p.getSubstring());
38      }
39  
40      @Override
41      public String[] getPatterns() {
42          validate();
43          return patterns.toArray(new String[0]);
44      }
45  
46      private void validate() {
47          if (!validated) {
48              validated  = true;
49              if (patterns.size() == 0) {
50                  throw new BuildException("You must specify at least one nested"
51                                           + " pattern.");
52              }
53              if (getLicenseFamilyCategory() == null) {
54                  throw new BuildException("The licenseFamilyCategory attribute"
55                                           + " is required.");
56              }
57              if (getLicenseFamilyName() == null) {
58                  throw new BuildException("The licenseFamilyName attribute"
59                                           + " is required.");
60              }
61          }
62      }
63  
64      /**
65       * A simple wrapper around a substring.
66       */
67      public static class Pattern {
68          private String substring;
69          public void setSubstring(String s) { substring = s; }
70          public String getSubstring() { return substring; }
71      }
72  }