|
| 1 | +/** |
| 2 | + * Copyright (c) 2024 Source Auditor Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 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 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 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 | +package org.spdx.library.conversion; |
| 19 | + |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.Optional; |
| 26 | + |
| 27 | +import org.spdx.core.InvalidSPDXAnalysisException; |
| 28 | +import org.spdx.library.model.v3.SpdxConstantsV3; |
| 29 | +import org.spdx.library.model.v3.SpdxModelClassFactory; |
| 30 | +import org.spdx.library.model.v3.core.ExternalMap; |
| 31 | +import org.spdx.library.model.v3.core.Hash; |
| 32 | +import org.spdx.storage.IModelStore; |
| 33 | +import org.spdx.storage.IModelStore.IdType; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author Gary O'Neall |
| 37 | + * |
| 38 | + * Information about an ExternalMap captured from an ExternalDocumentRef |
| 39 | + * |
| 40 | + */ |
| 41 | +public class ExternalMapInfo { |
| 42 | + |
| 43 | + private String docRefId; |
| 44 | + private String externalDocumentUri; |
| 45 | + private Optional<Hash> externalDocumentHash; |
| 46 | + private Collection<ExternalMap> docImports; |
| 47 | + |
| 48 | + private Map<String, ExternalMap> existingExternalMap = Collections.synchronizedMap(new HashMap<>()); |
| 49 | + |
| 50 | + /** |
| 51 | + * @param docRefId ID of the ExternalDocRef from the SPDX Spec version 2 SPDX Document containing the reference |
| 52 | + * @param externalDocumentUri External document URI for the external document being referenced |
| 53 | + * @param externalDocumentHash Optional Hash of the external SPDX document |
| 54 | + * @param docImports SPDX Spec version 3 collection of doc imports |
| 55 | + */ |
| 56 | + public ExternalMapInfo(String docRefId, String externalDocumentUri, Optional<Hash> externalDocumentHash, |
| 57 | + Collection<ExternalMap> docImports) { |
| 58 | + this.docRefId = docRefId; |
| 59 | + this.externalDocumentUri = externalDocumentUri; |
| 60 | + this.externalDocumentHash = externalDocumentHash; |
| 61 | + this.docImports = docImports; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * If the externalUri has not already been added, create an ExternalMap and add it to the docImports |
| 66 | + * @param externalUri External URI to add |
| 67 | + * @param modelStore model store where the ExternalMap is to be stored |
| 68 | + * @return the existing or added External map |
| 69 | + * @throws InvalidSPDXAnalysisException on error creating the ExternalMap |
| 70 | + */ |
| 71 | + public ExternalMap addExternalMap(String externalUri, IModelStore modelStore) throws InvalidSPDXAnalysisException { |
| 72 | + synchronized(existingExternalMap) { |
| 73 | + ExternalMap retval = existingExternalMap.get(externalUri); |
| 74 | + if (Objects.isNull(retval)) { |
| 75 | + retval = (ExternalMap)SpdxModelClassFactory.getModelObject(modelStore, |
| 76 | + modelStore.getNextId(IdType.Anonymous), SpdxConstantsV3.CORE_EXTERNAL_MAP, null, true, null); |
| 77 | + retval.setExternalSpdxId(externalUri); |
| 78 | + retval.setLocationHint(this.externalDocumentUri); |
| 79 | + if (externalDocumentHash.isPresent()) { |
| 80 | + retval.getVerifiedUsings().add(externalDocumentHash.get()); |
| 81 | + } |
| 82 | + docImports.add(retval); |
| 83 | + } |
| 84 | + return retval; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @return the docRefId |
| 90 | + */ |
| 91 | + public String getDocRefId() { |
| 92 | + return docRefId; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @return the externalDocumentUri |
| 97 | + */ |
| 98 | + public String getExternalDocumentUri() { |
| 99 | + return externalDocumentUri; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @return the externalDocumentHash |
| 104 | + */ |
| 105 | + public Optional<Hash> getExternalDocumentHash() { |
| 106 | + return externalDocumentHash; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return the docImports |
| 111 | + */ |
| 112 | + public Collection<ExternalMap> getDocImports() { |
| 113 | + return docImports; |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments