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;
20  
21  import java.util.Collection;
22  import java.util.Collections;
23  
24  import org.apache.rat.analysis.matchers.AbstractMatcherContainer;
25  import org.apache.rat.license.ILicense;
26  import org.apache.rat.license.ILicenseFamily;
27  
28  /**
29   * A collection of ILicenses that acts as a single License for purposes of Analysis.
30   * <p>
31   * This class process each license in turn on each {@code matches(String)} call.  When a match is found the 
32   * ILicenseFamily for the matching license is captured and used as the family for this license. If no matching 
33   * license has been found the default {@code dummy} license category is used.
34   */
35  class LicenseCollection extends AbstractMatcherContainer implements ILicense {
36  
37      private static final ILicenseFamily DEFAULT = ILicenseFamily.builder().setLicenseFamilyCategory("Dummy")
38              .setLicenseFamilyName("HeaderMatcherCollection default license family").build();
39      private final Collection<ILicense> enclosed;
40      private ILicense matchingLicense;
41      private State lastState;
42  
43      /**
44       * Constructs the LicenseCollection from the provided ILicense collection.
45       * @param enclosed The collection of ILicenses to compose this License implementation from.  May not be null.
46       */
47      public LicenseCollection(Collection<ILicense> enclosed) {
48          super(enclosed);
49          this.enclosed = Collections.unmodifiableCollection(enclosed);
50          this.matchingLicense = null;
51          this.lastState = State.i;
52      }
53  
54      @Override
55      public String getId() {
56          return "Default License Collection";
57      }
58  
59      @Override
60      public void reset() {
61          enclosed.forEach(ILicense::reset);
62          this.lastState = State.i;
63          this.matchingLicense = null;
64      }
65  
66      @Override
67      public State matches(String line) {
68          State dflt = State.f;
69          for (ILicense license : enclosed) {
70              switch (license.matches(line)) {
71              case t:
72                  this.matchingLicense = license;
73                  lastState = State.t;
74                  return State.t;
75              case i:
76                  dflt = State.i;
77                  break;
78              default:
79                  // do nothing
80                  break;
81              }
82          }
83          lastState = dflt;
84          return dflt;
85      }
86  
87      @Override
88      public State currentState() {
89          if (lastState == State.t) {
90              return lastState;
91          }
92          for (ILicense license : enclosed) {
93              switch (license.currentState()) {
94              case t:
95                  this.matchingLicense = license;
96                  lastState = State.t;
97                  return lastState;
98              case i:
99                  lastState = State.i;
100                 return lastState;
101             case f:
102                 // do nothing;
103                 break;
104             }
105         }
106         lastState = State.f;
107         return lastState;
108     }
109 
110     @Override
111     public int compareTo(ILicense arg0) {
112         return getLicenseFamily().compareTo(arg0.getLicenseFamily());
113     }
114 
115     @Override
116     public ILicenseFamily getLicenseFamily() {
117         return matchingLicense == null ? DEFAULT : matchingLicense.getLicenseFamily();
118     }
119 
120     @Override
121     public String getNotes() {
122         return matchingLicense == null ? null : matchingLicense.getNotes();
123     }
124 
125     @Override
126     public String derivedFrom() {
127         return matchingLicense == null ? null : matchingLicense.derivedFrom();
128     }
129     
130     @Override
131     public String getName() {
132         return getLicenseFamily().getFamilyName();
133     }
134 }