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

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

The 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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Set;

public class ResolverUtils {
    private static final Logger LOG = LoggerFactory.getLogger(ResolverUtils.class);

    /**
     * Tries to find a licensetext for the given tmd, taking into account named equivalences and other complications.
     *
     * @param tmd to find a text for (trying the tmd itself first)
     * @param normMeta used to get tmds from in case they need resolving
     * @return the file to be read as a source for the license text
     */
    public static File findLicenseFor(final TermsMetaData tmd, NormalizationMetaData normMeta) {
        final Set visited = new HashSet<>();
        TermsMetaData currentTmd = tmd;

        // try to recursively solve namedEquivalence
        while (!visited.contains(currentTmd.getCanonicalName())) {
            visited.add(currentTmd.getCanonicalName());

            if (currentTmd.getLicenseFile() != null) {
                File licenseFileToRead = new File(currentTmd.getLicenseFile());
                if (Files.isRegularFile(licenseFileToRead.toPath()) && licenseFileToRead.exists()) {
                    return licenseFileToRead;
                } else {
                    LOG.error(
                            "Tmd [{}] had bad license file [{}] while looking for text for [{}]: Not a file.",
                            currentTmd.getCanonicalName(),
                            licenseFileToRead,
                            tmd.getCanonicalName()
                    );
                }
            }

            // try to resolve by equivalence
            String namedEquivalence = tmd.getNamedEquivalence();
            if (namedEquivalence == null) break;
            currentTmd = normMeta.findTermsMetaData(namedEquivalence);
            if (currentTmd == null) {
                LOG.error(
                        "No Tmd found for named equivalence [{}], named in license [{}].",
                        namedEquivalence,
                        tmd.getCanonicalName()
                );
                break;
            }
        }

        // failed to find a valid license file to read
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy