|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2025 Source Auditor Inc. |
| 3 | + * SPDX-FileType: SOURCE |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * <p> |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.spdx.library.model.v2.license; |
| 20 | + |
| 21 | +import org.spdx.core.IModelCopyManager; |
| 22 | +import org.spdx.core.InvalidSPDXAnalysisException; |
| 23 | +import org.spdx.storage.IModelStore; |
| 24 | +import org.spdx.storage.PropertyDescriptor; |
| 25 | + |
| 26 | +import javax.annotation.Nullable; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Optional; |
| 30 | +import java.util.Set; |
| 31 | + |
| 32 | +import static org.spdx.library.model.v2.SpdxConstantsCompatV2.SPDX_NAMESPACE; |
| 33 | + |
| 34 | +/** |
| 35 | + * Represents a license expression string which can not be parsed - used for error handling |
| 36 | + */ |
| 37 | +public class InvalidLicenseExpression extends AnyLicenseInfo { |
| 38 | + |
| 39 | + public static final PropertyDescriptor MESSAGE_PROPERTY = new PropertyDescriptor("invalidLicenseMessage", SPDX_NAMESPACE); |
| 40 | + public static final PropertyDescriptor LICENSE_EXPRESSION_PROPERTY = new PropertyDescriptor("invalidLicenseExpression", SPDX_NAMESPACE); |
| 41 | + public static final String INVALID_LICENSE_EXPRESSION_TYPE = "InvalidLicenseExpression"; |
| 42 | + /** |
| 43 | + * Create a new InvalidLicenseExpression object |
| 44 | + * @param modelStore container which includes the license |
| 45 | + * @param documentUri URI for the SPDX document containing the license |
| 46 | + * @param id identifier for the license |
| 47 | + * @param copyManager if non-null, allows for copying of any properties set which use other model stores or document URI's |
| 48 | + * @param create if true, create the license if it does not exist |
| 49 | + * @throws InvalidSPDXAnalysisException on error |
| 50 | + */ |
| 51 | + public InvalidLicenseExpression(IModelStore modelStore, String documentUri, String id, |
| 52 | + @Nullable IModelCopyManager copyManager, boolean create) |
| 53 | + throws InvalidSPDXAnalysisException { |
| 54 | + super(modelStore, documentUri, id, copyManager, create); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Create a new InvalidLicenseExpression object and initializes the message and licenseExpression |
| 59 | + * @param modelStore container which includes the license |
| 60 | + * @param documentUri URI for the SPDX document containing the license |
| 61 | + * @param id identifier for the license |
| 62 | + * @param copyManager if non-null, allows for copying of any properties set which use other model stores or document URI's |
| 63 | + * @param message Error message describing the nature of the invalid license expression |
| 64 | + * @param licenseExpression License expression string that caused the error |
| 65 | + * @throws InvalidSPDXAnalysisException on error |
| 66 | + */ |
| 67 | + public InvalidLicenseExpression(IModelStore modelStore, String documentUri, String id, |
| 68 | + @Nullable IModelCopyManager copyManager, String message, |
| 69 | + String licenseExpression) throws InvalidSPDXAnalysisException { |
| 70 | + super(modelStore, documentUri, id, copyManager, true); |
| 71 | + setMessage(message); |
| 72 | + setLicenseExpression(licenseExpression); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected List<String> _verify(Set<String> verifiedElementIds, String specVersion) { |
| 77 | + List<String> retval = new ArrayList<>(); |
| 78 | + try { |
| 79 | + retval.add(String.format("Invalid license expression '%s': %s", |
| 80 | + getLicenseExpression(), getMessage())); |
| 81 | + } catch(Exception e) { |
| 82 | + retval.add(String.format("Error getting properties: %s", e.getMessage())); |
| 83 | + } |
| 84 | + return retval; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String getType() { |
| 89 | + return INVALID_LICENSE_EXPRESSION_TYPE; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String toString() { |
| 94 | + try { |
| 95 | + return getLicenseExpression(); |
| 96 | + } catch (InvalidSPDXAnalysisException e) { |
| 97 | + return ""; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @return the error message associated with the license expression |
| 103 | + * @throws InvalidSPDXAnalysisException on storage related errors |
| 104 | + */ |
| 105 | + public String getMessage() throws InvalidSPDXAnalysisException { |
| 106 | + Optional<String> o = getStringPropertyValue(MESSAGE_PROPERTY); |
| 107 | + return o.orElse("[Error message not set]"); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param message the message to set |
| 112 | + * @throws InvalidSPDXAnalysisException on storage related errors |
| 113 | + */ |
| 114 | + public void setMessage(String message) throws InvalidSPDXAnalysisException { |
| 115 | + setPropertyValue(MESSAGE_PROPERTY, message); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @return the license expression which had parsing errors |
| 120 | + * @throws InvalidSPDXAnalysisException on storage related errors |
| 121 | + */ |
| 122 | + public String getLicenseExpression() throws InvalidSPDXAnalysisException { |
| 123 | + Optional<String> o = getStringPropertyValue(LICENSE_EXPRESSION_PROPERTY); |
| 124 | + return o.orElse("[Error message not set]"); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @param expression the license expression to set |
| 129 | + * @throws InvalidSPDXAnalysisException on storage related errors |
| 130 | + */ |
| 131 | + public void setLicenseExpression(String expression) throws InvalidSPDXAnalysisException { |
| 132 | + setPropertyValue(LICENSE_EXPRESSION_PROPERTY, expression); |
| 133 | + } |
| 134 | +} |
0 commit comments