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.nio.charset.Charset; 22 import java.util.SortedSet; 23 import java.util.TreeSet; 24 import java.util.function.Predicate; 25 import java.util.stream.Stream; 26 27 import org.apache.rat.license.ILicense; 28 import org.apache.rat.utils.DefaultLog; 29 import org.apache.tika.mime.MediaType; 30 31 /** 32 * Data about the document under test. 33 */ 34 public class MetaData { 35 36 /** The list of matched licenses */ 37 private final SortedSet<ILicense> matchedLicenses; 38 /** The list of License Family Categories that are approved */ 39 private Predicate<ILicense> approvalPredicate; 40 /** The charset for this document */ 41 private Charset charset; 42 /** The media type for this document */ 43 private MediaType mediaType; 44 /** The document type for this document */ 45 private Document.Type documentType; 46 /** The flag for directory types */ 47 private boolean isDirectory; 48 49 /** 50 * Create metadata without a content type. 51 */ 52 public MetaData() { 53 this.matchedLicenses = new TreeSet<>(); 54 this.approvalPredicate = x -> { 55 DefaultLog.getInstance().error("Approved Predicate was not set."); 56 throw new IllegalStateException("Approved Predicate was not set."); 57 }; 58 } 59 60 /** 61 * Gets the charset for the document. If the charset was not set will return the system default charset. 62 * @return the charset for the document 63 */ 64 public Charset getCharset() { 65 return charset == null ? Charset.defaultCharset() : charset; 66 } 67 68 /** 69 * Sets the charset for the document. If set to {@code null} the system default charset will be used. 70 * @param charset the charset to use. 71 */ 72 public void setCharset(final Charset charset) { 73 this.charset = charset; 74 } 75 76 /** 77 * Returns {@code true} if {@link #setCharset} has been called. 78 * @return {@code true} if {@link #setCharset} has been called. 79 */ 80 public boolean hasCharset() { 81 return charset != null; 82 } 83 84 /** 85 * Gets the defined media type. 86 * @return the media type. 87 */ 88 public MediaType getMediaType() { 89 return mediaType; 90 } 91 92 /** 93 * Sets the defined media type. 94 * @param mediaType the media type. 95 */ 96 public void setMediaType(final MediaType mediaType) { 97 this.mediaType = mediaType; 98 } 99 100 /** 101 * Determines if a matching license has been detected. 102 * @return true if there is a matching license. 103 */ 104 public boolean detectedLicense() { 105 return !matchedLicenses.isEmpty(); 106 } 107 108 /** 109 * Sets the set of approved licenses. 110 * @param approvalPredicate the predicate to validate licenses. 111 */ 112 public void setApprovalPredicate(final Predicate<ILicense> approvalPredicate) { 113 this.approvalPredicate = approvalPredicate; 114 } 115 116 /** 117 * Gets the stream of licenses that have been matched. 118 * @return the stream of licenses that have been matched. 119 */ 120 public Stream<ILicense> licenses() { 121 return matchedLicenses.stream(); 122 } 123 124 /** 125 * Gets the stream of approved licenses that have been matched. 126 * @return the stream of approved licenses that have been matched. 127 */ 128 public Stream<ILicense> approvedLicenses() { 129 return licenses().filter(this::isApproved); 130 } 131 132 /** 133 * Determine if the license is an approved license. 134 * @param license the license to check. 135 * @return {@code true} if the license is in the list of approved licenses, {@code false} otherwise. 136 */ 137 public boolean isApproved(final ILicense license) { 138 return approvalPredicate.test(license); 139 } 140 141 /** 142 * Gets the stream of unapproved licenses that have been matched. 143 * @return the stream of unapproved licenses that have been matched. 144 */ 145 public Stream<ILicense> unapprovedLicenses() { 146 return licenses().filter(lic -> !isApproved(lic)); 147 } 148 149 /** 150 * Sets the document type. 151 * @param type the document type for the document being recorded. 152 */ 153 public void setDocumentType(final Document.Type type) { 154 this.documentType = type; 155 } 156 157 /** 158 * Set the directory flag. 159 * @param state the state to set the directory flag in. 160 */ 161 public void setIsDirectory(final boolean state) { 162 this.isDirectory = state; 163 } 164 165 /** 166 * Return {@code true} if the directory flag was set. 167 * @return the directory flag. 168 */ 169 public boolean isDirectory() { 170 return isDirectory; 171 } 172 173 /** 174 * Gets the document type. 175 * @return the document type of the document that was recorded. 176 */ 177 public Document.Type getDocumentType() { 178 return this.documentType; 179 } 180 181 /** 182 * Add the license information to the metadata. 183 * @param license the license to add metadata for. 184 */ 185 public void reportOnLicense(final ILicense license) { 186 this.matchedLicenses.add(license); 187 } 188 189 /** 190 * Remove matched licenses based on a predicate. Will remove licenses for which the predicate 191 * returns true. 192 * @param filter the predicate to use. 193 */ 194 public void removeLicenses(final Predicate<ILicense> filter) { 195 this.matchedLicenses.removeIf(filter); 196 } 197 198 @Override 199 public String toString() { 200 return String.format("MetaData[%s license, %s approved]", matchedLicenses.size(), 201 matchedLicenses.stream().filter(approvalPredicate).count()); 202 } 203 }