com.metaeffekt.artifact.analysis.spdxbom.mapper.MappingUtils Maven / Gradle / Ivy
/*
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.metaeffekt.artifact.analysis.spdxbom.mapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.metaeffekt.artifact.analysis.spdxbom.context.SpdxDocumentContext;
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.SpdxConstants;
import org.spdx.library.model.Annotation;
import org.spdx.library.model.enumerations.AnnotationType;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class MappingUtils {
/**
* Creates an annotation with a json string with the provided key-value pairs.
* This is used to represent values that aren't otherwise being mapped from our formats to spdx.
*
* @param documentContext context for which the annotation should be generated
* @param overflowMap the map of key-value pairs for writing
*
* @return returns the created annotation for the specified document and key-value pairs
*
* @throws InvalidSPDXAnalysisException thrown if createAnnotation fails
*/
public static Annotation getOverflowAnnotation(SpdxDocumentContext documentContext, Map overflowMap)
throws InvalidSPDXAnalysisException {
if (overflowMap.isEmpty()) {
throw new IllegalArgumentException("Map must not be empty.");
}
final MapType type = TypeFactory.defaultInstance().constructMapType(HashMap.class, String.class, String.class);
ObjectMapper objectMapper = new ObjectMapper();
String jsonString;
try {
jsonString = objectMapper.writerWithDefaultPrettyPrinter().forType(type).writeValueAsString(overflowMap);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
final SimpleDateFormat spdxDateFormat = new SimpleDateFormat(SpdxConstants.SPDX_DATE_FORMAT);
return documentContext.getSpdxDocument().createAnnotation(
"Tool: " + documentContext.getSpdxDocumentSpec().getTool(),
AnnotationType.OTHER,
spdxDateFormat.format(new Date()),
jsonString
);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy