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

com.powsybl.glsk.api.io.GlskDocumentImporters Maven / Gradle / Ivy

/*
 * Copyright (c) 2020, RTE (http://www.rte-france.com)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.powsybl.glsk.api.io;

import com.powsybl.glsk.api.GlskDocument;
import com.powsybl.glsk.commons.GlskException;
import com.google.common.base.Suppliers;
import com.powsybl.commons.util.ServiceLoaderCache;

import java.io.*;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Supplier;

import static org.apache.commons.io.IOUtils.copy;

/**
 * @author Viktor Terrier {@literal }
 */
public final class GlskDocumentImporters {

    public static final String NO_IMPORTER_FOUND_FOR_THIS_FILE = "No importer found for this file";
    private static final Supplier> GLSK_IMPORTERS
        = Suppliers.memoize(() -> new ServiceLoaderCache<>(GlskDocumentImporter.class).getServices());

    private GlskDocumentImporters() {
    }

    public static GlskDocument importGlsk(String filePath) throws FileNotFoundException {
        return importGlsk(Path.of(filePath));
    }

    public static GlskDocument importGlsk(Path glskPath) throws FileNotFoundException {
        InputStream is = new FileInputStream(glskPath.toFile());
        return importGlsk(is);
    }

    private static byte[] getBytesFromInputStream(InputStream inputStream) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            copy(inputStream, baos);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
        return baos.toByteArray();
    }

    public static GlskDocument importGlsk(InputStream inputStream) {
        byte[] bytes = getBytesFromInputStream(inputStream);

        GlskDocumentImporter importer = findImporter(new ByteArrayInputStream(bytes));
        if (importer == null) {
            throw new GlskException(NO_IMPORTER_FOUND_FOR_THIS_FILE);
        }
        return importer.importGlsk(new ByteArrayInputStream(bytes));
    }

    public static GlskDocument importGlskWithCalculationDirections(InputStream inputStream) {
        byte[] bytes = getBytesFromInputStream(inputStream);

        GlskDocumentImporter importer = findImporter(new ByteArrayInputStream(bytes));
        if (importer == null) {
            throw new GlskException(NO_IMPORTER_FOUND_FOR_THIS_FILE);
        }
        return importer.importGlsk(new ByteArrayInputStream(bytes), true);
    }

    public static GlskDocument importAndValidateGlsk(InputStream inputStream) {
        byte[] bytes = getBytesFromInputStream(inputStream);

        GlskDocumentImporter importer = findImporter(new ByteArrayInputStream(bytes));
        if (importer == null) {

            throw new GlskException(NO_IMPORTER_FOUND_FOR_THIS_FILE);
        }
        return importer.importAndValidateGlsk(new ByteArrayInputStream(bytes), false);
    }

    public static GlskDocument importAndValidateGlskWithCalculationDirections(InputStream inputStream) {
        byte[] bytes = getBytesFromInputStream(inputStream);

        GlskDocumentImporter importer = findImporter(new ByteArrayInputStream(bytes));
        if (importer == null) {
            throw new GlskException(NO_IMPORTER_FOUND_FOR_THIS_FILE);
        }
        return importer.importAndValidateGlsk(new ByteArrayInputStream(bytes), true);
    }

    public static GlskDocumentImporter findImporter(InputStream inputStream) {
        byte[] bytes = getBytesFromInputStream(inputStream);

        for (GlskDocumentImporter importer : GLSK_IMPORTERS.get()) {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            if (importer.canImport(bais)) {
                return importer;
            }
        }
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy