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 org.apache.rat.Defaults;
22  import org.apache.rat.api.Document;
23  import org.apache.rat.api.MetaData;
24  import org.apache.rat.config.ConfigurationUtil;
25  import org.apache.rat.document.IDocumentAnalyser;
26  import org.apache.rat.document.RatDocumentAnalysisException;
27  import org.apache.rat.license.ILicenseFamily;
28  
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.HashSet;
32  import java.util.List;
33  import java.util.Set;
34  
35  public class DefaultPolicy implements IDocumentAnalyser {
36      private List<String> approvedLicenseNames;
37  
38      /**
39       * Creates a policy that matches the default licenses.
40       * Mainly used for testing purposes.
41       */
42      DefaultPolicy() {
43          this(new ArrayList<String>(0), true);
44      }
45  
46      public DefaultPolicy(final ILicenseFamily[] approvedLicenses, boolean mergeWithDefault) {
47          this(ConfigurationUtil.toNames(approvedLicenses), mergeWithDefault);
48      }
49  
50      public DefaultPolicy(final List<String> approvedLicenseNames, boolean mergeWithDefault) {
51          this.approvedLicenseNames = new ArrayList<String>();
52  
53          if (approvedLicenseNames == null || approvedLicenseNames.isEmpty()) {
54              // used in tests only, no additional licenses given but defaults requested
55              if(mergeWithDefault) {
56                  this.approvedLicenseNames = new ArrayList<String>(Defaults.DEFAULT_LICENSE_FAMILIES);
57              }
58          } else {
59              // avoid duplicate entries and merge with defaults if requested
60              Set<String> mergedLicenses = new HashSet<String>(approvedLicenseNames);
61              if(mergeWithDefault) {
62                  mergedLicenses.addAll(Defaults.DEFAULT_LICENSE_FAMILIES);
63              }
64              this.approvedLicenseNames = new ArrayList<String>(mergedLicenses);
65          }
66          Collections.sort(this.approvedLicenseNames);
67      }
68  
69      public void analyse(final Document subject) throws RatDocumentAnalysisException {
70          if (subject != null) {
71              final String name = subject.getMetaData().value(MetaData.RAT_URL_LICENSE_FAMILY_NAME);
72              if (name != null) {
73                  final boolean isApproved = Collections.binarySearch(approvedLicenseNames, name) >= 0;
74                  reportLicenseApprovalClaim(subject, isApproved);
75              }
76          }
77      }
78  
79      public void reportLicenseApprovalClaim(final Document subject, final boolean isAcceptable) {
80          subject.getMetaData().set(//
81           isAcceptable ? MetaData.RAT_APPROVED_LICENSE_DATIM_TRUE: MetaData.RAT_APPROVED_LICENSE_DATIM_FALSE);
82      }
83  
84      public List<String> getApprovedLicenseNames() {
85          return Collections.unmodifiableList(approvedLicenseNames);
86      }
87  }