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 org.apache.rat.api.Document;
22  import org.apache.rat.api.MetaData;
23  
24  public class BaseLicense {
25      private String licenseFamilyCategory;
26      private String licenseFamilyName;
27      private String notes;
28  
29      public BaseLicense() {
30      }
31  
32      public BaseLicense(final MetaData.Datum licenseFamilyCategory, final MetaData.Datum licenseFamilyName, final String notes)
33      {
34          if (!MetaData.RAT_URL_LICENSE_FAMILY_CATEGORY.equals(licenseFamilyCategory.getName())) {
35              throw new IllegalStateException("Expected " + MetaData.RAT_URL_LICENSE_FAMILY_CATEGORY
36                      + ", got " + licenseFamilyCategory.getName());
37          }
38          setLicenseFamilyCategory(licenseFamilyCategory.getValue());
39          if (!MetaData.RAT_URL_LICENSE_FAMILY_NAME.equals(licenseFamilyName.getName())) {
40              throw new IllegalStateException("Expected " + MetaData.RAT_URL_LICENSE_FAMILY_NAME
41                      + ", got " + licenseFamilyName.getName());
42          }
43          setLicenseFamilyName(licenseFamilyName.getValue());
44          setNotes(notes);
45      }
46  
47      public String getLicenseFamilyCategory() {
48          return licenseFamilyCategory;
49      }
50  
51      public void setLicenseFamilyCategory(String pDocumentCategory) {
52          licenseFamilyCategory = pDocumentCategory;
53      }
54  
55      public String getLicenseFamilyName() {
56          return licenseFamilyName;
57      }
58  
59      public void setLicenseFamilyName(String pLicenseFamilyCategory) {
60          licenseFamilyName = pLicenseFamilyCategory;
61      }
62  
63      public String getNotes() {
64          return notes;
65      }
66  
67      public void setNotes(String pNotes) {
68          notes = pNotes;
69      }
70  
71      public final void reportOnLicense(Document subject) {
72          final MetaData metaData = subject.getMetaData();
73          metaData.set(new MetaData.Datum(MetaData.RAT_URL_HEADER_SAMPLE, notes));
74          final String licFamilyCategory = getLicenseFamilyCategory();
75          metaData.set(new MetaData.Datum(MetaData.RAT_URL_HEADER_CATEGORY, licFamilyCategory));
76          metaData.set(new MetaData.Datum(MetaData.RAT_URL_LICENSE_FAMILY_CATEGORY, licFamilyCategory));
77          metaData.set(new MetaData.Datum(MetaData.RAT_URL_LICENSE_FAMILY_NAME, getLicenseFamilyName()));
78      }
79  
80      protected static final String prune(String text) {
81          final int length = text.length();
82          final StringBuilder buffer = new StringBuilder(length);
83          for (int i = 0; i < length; i++) {
84              char at = text.charAt(i);
85              if (Character.isLetterOrDigit(at)) {
86                  buffer.append(at);
87              }
88          }
89          return buffer.toString();
90      }
91  
92  }