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.policy;
20  
21  import java.util.Arrays;
22  
23  import org.apache.rat.api.Document;
24  import org.apache.rat.api.MetaData;
25  import org.apache.rat.api.MetaData.Datum;
26  import org.apache.rat.document.IDocumentAnalyser;
27  import org.apache.rat.document.RatDocumentAnalysisException;
28  import org.apache.rat.license.ILicenseFamily;
29  
30  public class DefaultPolicy implements IDocumentAnalyser {
31      private static final String[] APPROVED_LICENSES = {
32          MetaData.RAT_LICENSE_FAMILY_NAME_VALUE_APACHE_LICENSE_VERSION_2_0,
33          MetaData.RAT_LICENSE_FAMILY_NAME_VALUE_OASIS_OPEN_LICENSE,
34          MetaData.RAT_LICENSE_FAMILY_NAME_VALUE_W3C_SOFTWARE_COPYRIGHT,
35          MetaData.RAT_LICENSE_FAMILY_NAME_VALUE_W3C_DOCUMENT_COPYRIGHT,
36          MetaData.RAT_LICENSE_FAMILY_NAME_VALUE_MODIFIED_BSD_LICENSE
37      };
38      
39      private static final String[] toNames(final ILicenseFamily[] approvedLicenses) {
40          String[] results = null;
41          if (approvedLicenses != null) {
42              final int length = approvedLicenses.length;
43              results = new String[length];
44              for (int i=0;i<length;i++) {
45                  results[i] = approvedLicenses[i].getFamilyName();
46              }
47          }
48          return results;
49      }
50      ;
51      private final String[] approvedLicenseNames;
52      
53      public DefaultPolicy() {
54          this(APPROVED_LICENSES);
55      }
56      
57      public DefaultPolicy(final ILicenseFamily[] approvedLicenses) {
58          this(toNames(approvedLicenses));
59      }
60  
61      public DefaultPolicy(final String[] approvedLicenseNames) {
62          if (approvedLicenseNames == null) {
63              this.approvedLicenseNames = APPROVED_LICENSES;
64          } else {
65              final int length = approvedLicenseNames.length;
66              this.approvedLicenseNames = new String[length];
67              System.arraycopy(approvedLicenseNames, 0, this.approvedLicenseNames, 0, length);
68          }
69          Arrays.sort(this.approvedLicenseNames);
70      }
71  
72      public void reportLicenseApprovalClaim(final Document subject, final boolean isAcceptable) {
73          final Datum datum;
74          if (isAcceptable) {
75              datum = MetaData.RAT_APPROVED_LICENSE_DATIM_TRUE;
76          } else {
77              datum = MetaData.RAT_APPROVED_LICENSE_DATIM_FALSE;
78          }
79          subject.getMetaData().set(datum);
80      }
81      
82      public void analyse(final Document subject) throws RatDocumentAnalysisException {
83          if (subject != null) {
84              final String name = subject.getMetaData().value(MetaData.RAT_URL_LICENSE_FAMILY_NAME);
85              if (name != null) {
86                  final boolean isApproved = Arrays.binarySearch(approvedLicenseNames, name) >= 0;
87                  reportLicenseApprovalClaim(subject, isApproved);
88              }
89          }
90      }
91  }