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.api;
20  
21  import java.util.HashSet;
22  import java.util.Set;
23  import java.util.SortedSet;
24  import java.util.TreeSet;
25  import java.util.function.Predicate;
26  import java.util.stream.Stream;
27  
28  import org.apache.rat.license.ILicense;
29  import org.apache.rat.license.ILicenseFamily;
30  import org.apache.tika.mime.MediaType;
31  
32  /**
33   * Data about the document under test..
34   */
35  public class MetaData {
36  
37      /** The list of matched licenses */
38      private final SortedSet<ILicense> matchedLicenses;
39      /** The list of License Family Categories that are approved */
40      private final Set<String> approvedLicenses;
41  
42      private MediaType mediaType;
43      private Document.Type documentType;
44      private String sampleHeader;
45  
46      /**
47       * Create metadata without a content type.
48       */
49      public MetaData() {
50          this.matchedLicenses = new TreeSet<>();
51          this.approvedLicenses = new HashSet<>();
52      }
53  
54      /**
55       * Gets the defined media type.
56       * @return the media type.
57       */
58      public MediaType getMediaType() {
59          return mediaType;
60      }
61  
62      /**
63       * Sets the defined media type.
64       * @param mediaType the media type.
65       */
66      public void setMediaType(MediaType mediaType) {
67          this.mediaType = mediaType;
68      }
69  
70      /**
71       * Determines if a matching license has been detected.
72       * @return true if there is a matching license.
73       */
74      public boolean detectedLicense() {
75          return !matchedLicenses.isEmpty();
76      }
77  
78      /**
79       * Sets the set of approved licenses.
80       * @param approvedLicenseFamilies the set of approved license families.
81       */
82      public void setApprovedLicenses(Set<ILicenseFamily> approvedLicenseFamilies) {
83          licenses().filter(lic -> approvedLicenseFamilies.contains(lic.getLicenseFamily()))
84                  .forEach(lic -> approvedLicenses.add(lic.getId()));
85      }
86  
87      /**
88       * Gets the stream of licenses that have been matched.
89       * @return the stream of licenses that have been matched.
90       */
91      public Stream<ILicense> licenses() {
92          return matchedLicenses.stream();
93      }
94  
95      /**
96       * Gets the stream of approved licenses that have been matched.
97       * @return the stream of approved licenses that have been matched.
98       */
99      public Stream<ILicense> approvedLicenses() {
100         return licenses().filter(this::isApproved);
101     }
102 
103     /**
104      * Determine if the license is an approved license.
105      * @param license the license to check;
106      * @return {@code true} if the license is in the list of approved licenses, {@code false} otherwise.
107      */
108     public boolean isApproved(ILicense license) {
109         return approvedLicenses.contains(license.getId());
110     }
111 
112     /**
113      * Gets the stream of unapproved licenses that have been matched.
114      * @return the stream of unapproved licenses that have been matched.
115      */
116     public Stream<ILicense> unapprovedLicenses() {
117         return licenses().filter(lic -> !isApproved(lic));
118     }
119 
120     /**
121      * Sets the sample header.  This is the header that was collected during processing.
122      * @param sampleHeader the sample header to use.
123      */
124     public void setSampleHeader(String sampleHeader) {
125         this.sampleHeader = sampleHeader;
126     }
127 
128     /**
129      * Gets the sample header.
130      * @return the smaple header.
131      */
132     public String getSampleHeader() {
133         return sampleHeader;
134     }
135 
136     /** 
137      * Sets the document type.
138      * @param type the document type for the document being recorded.
139      */
140     public void setDocumentType(Document.Type type) {
141         this.documentType = type;
142     }
143 
144     /**
145      * Gets the document type.
146      * @return the document type of the document that was recorded.
147      */
148     public Document.Type getDocumentType() {
149         return this.documentType;
150     }
151 
152     /**
153      * Add the license information to the metadata.
154      * @param license the license to add metadata for.
155      */
156     public void reportOnLicense(ILicense license) {
157         this.matchedLicenses.add(license);
158     }
159 
160     public void removeLicenses(Predicate<ILicense> filter) {
161         this.matchedLicenses.removeIf(filter);
162     }
163     
164     @Override
165     public String toString() {
166         return String.format( "MetaData[%s license, %s approved]", matchedLicenses.size(), approvedLicenses.size());
167     }
168 }