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

com.metaeffekt.artifact.terms.model.LocalTermsMetaDataLicenseTextProvider Maven / Gradle / Ivy

There is a newer version: 0.134.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.terms.model;

import com.metaeffekt.artifact.analysis.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class LocalTermsMetaDataLicenseTextProvider implements LicenseTextProvider {
    private static final Logger LOG = LoggerFactory.getLogger(LocalTermsMetaDataLicenseTextProvider.class);

    private final NormalizationMetaData normalizationMetaData;

    public LocalTermsMetaDataLicenseTextProvider(NormalizationMetaData normalizationMetaData) {
        this.normalizationMetaData = normalizationMetaData;
    }

    @Override
    public Map resolve(Collection canonicalNames) {
        final Map map = new HashMap<>();

        for (String canonicalName : canonicalNames) {
            TermsMetaData tmd = normalizationMetaData.getTermsMetaData(canonicalName);

            if (tmd == null) {
                LOG.debug("Trying to find name [{}] in history.", canonicalName);
                tmd = normalizationMetaData.findUsingCanonicalNameInHistory(canonicalName);
            }

            if (tmd != null) {
                final File licenseFile = ResolverUtils.findLicenseFor(tmd, normalizationMetaData);
                if (licenseFile != null && licenseFile.exists()) {
                    try {
                        final LicenseTextEntry licenseTextEntry = new LicenseTextEntry(
                                tmd.getCanonicalName(), licenseFile.getAbsolutePath(),
                                FileUtils.readFileToString(licenseFile, FileUtils.ENCODING_UTF_8));
                        map.put(canonicalName, licenseTextEntry);
                    } catch (IOException e) {
                        LOG.error("Could not read license text for tmd [{}] (tmd [{}]) from path [{}].", canonicalName,
                                tmd.getCanonicalName(), licenseFile.getPath());
                    }
                }
            }
        }
        return map;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy