All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.metaeffekt.artifact.analysis.spdxbom.InventorySpdxExportTask Maven / Gradle / Ivy

There is a newer version: 0.132.0
Show newest version
/*
 * 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;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.metaeffekt.artifact.terms.model.LicenseTextProvider;
import com.metaeffekt.artifact.terms.model.NormalizationMetaData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

public class InventorySpdxExportTask {

    private final static Logger LOG = LoggerFactory.getLogger(InventorySpdxExportTask.class);

    private List approvedAttribute;

    private List attributeIgnoreInclusionList;

    private NormalizationMetaData normalizationMetaData;

    private LicenseTextProvider licenseTextProvider;

    public void configure(NormalizationMetaData normalizationMetaData, LicenseTextProvider licenseTextProvider) {
        this.normalizationMetaData = normalizationMetaData;
        this.licenseTextProvider = licenseTextProvider;
    }

    public void configure(File extraConfigDir) {
        // prepare mappers for lists of approved, ignored etc. attributes (lists of strings)
        final ObjectMapper jsonObjectMapper = new ObjectMapper();
        final TypeFactory jsonTypeFactory = jsonObjectMapper.getTypeFactory();
        final CollectionType jsonStringListType = jsonTypeFactory.constructCollectionType(ArrayList.class, String.class);

        // try to read extra config
        if (extraConfigDir != null) {

            final File approvedAttributesFile = new File(extraConfigDir, "approvedAttributes.json");
            try {
                if (approvedAttributesFile.exists()) {
                    try (Reader reader = Files.newBufferedReader(approvedAttributesFile.toPath(), StandardCharsets.UTF_8)) {
                        approvedAttribute = jsonObjectMapper.readValue(reader, jsonStringListType);
                    }
                }
            } catch (Exception e) {
                LOG.error("Cannot read external configuration data in [{}].", approvedAttributesFile.getAbsolutePath());
            }

            final File attributeIgnoreInclusionFile = new File(extraConfigDir, "ignoredAttributes.json");
            try {
                if (attributeIgnoreInclusionFile.exists()) {
                    try (Reader reader = Files.newBufferedReader(attributeIgnoreInclusionFile.toPath(), StandardCharsets.UTF_8)) {
                        attributeIgnoreInclusionList = jsonObjectMapper.readValue(reader, jsonStringListType);
                    }
                }
            } catch (Exception e) {
                LOG.error("Cannot read external configuration data in [{}].", attributeIgnoreInclusionFile.getAbsolutePath());
            }
        }
    }

    // FIXME: exposing exporter; check whether there is a better solution; good: what to do with the result is subject
    //   to the consumer; eg. write to an output file; otherwise further signatures can be integrated here
    public InventorySpdxExporter buildExporter() {
        return new InventorySpdxExporter(approvedAttribute, attributeIgnoreInclusionList,
                normalizationMetaData, licenseTextProvider);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy