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

xyz.ottr.lutra.api.StandardTemplateManager Maven / Gradle / Ivy

Go to download

This Lutra submodule is intended for use as a Java library. It has dependencies to all other modules in Lutra, except those that are used for providing executable interfaces, such as command line and REST API interfaces. Lutra is the reference implementation of the OTTR framework. For more information about OTTR, see http://ottr.xyz.

There is a newer version: 0.6.19
Show newest version
package xyz.ottr.lutra.api;

/*-
 * #%L
 * lutra-api
 * %%
 * Copyright (C) 2018 - 2020 University of Oslo
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import lombok.Getter;
import org.apache.commons.io.IOUtils;
import xyz.ottr.lutra.OTTR;
import xyz.ottr.lutra.TemplateManager;
import xyz.ottr.lutra.model.Signature;
import xyz.ottr.lutra.store.TemplateStore;
import xyz.ottr.lutra.system.MessageHandler;
import xyz.ottr.lutra.system.Result;
import xyz.ottr.lutra.system.ResultConsumer;
import xyz.ottr.lutra.system.ResultStream;
import xyz.ottr.lutra.wottr.io.RDFIO;
import xyz.ottr.lutra.wottr.parser.WTemplateParser;

@Getter
public final class StandardTemplateManager extends TemplateManager {

    private static final String standardLibFolder = "tpl-library";
    private static final String templatesListFile = "templates-list.txt";


    public StandardTemplateManager() {
        this.loadFormats();
    }

    public MessageHandler loadStandardTemplateLibrary() {

        var standardLibrary = makeDefaultStore(getFormatManager());
        ResultConsumer consumer = new ResultConsumer<>(standardLibrary);
        var reader = ResultStream.innerFlatMapCompose(RDFIO.inputStreamReader(), new WTemplateParser());

        getLibraryPaths()
            .innerFilter(path -> !path.startsWith(OTTR.ns_library_package_prefix))
            .innerMap(this::getResourceAsStream)
            .innerFlatMap(reader)
            .forEach(consumer);

        super.getTemplateStore().registerStandardLibrary(standardLibrary);

        return consumer.getMessageHandler();
    }

    public ResultStream getLibraryPaths() {

        var templatesList = getResourceAsStream(templatesListFile);
        if (templatesList == null) {
            return ResultStream.of(Result.error("Failed to read index file of all templates in the standard library."));
        }

        List paths = new LinkedList<>();

        try {
            paths.addAll(IOUtils.readLines(templatesList, StandardCharsets.UTF_8));
        } catch (IOException ex) {
            return ResultStream.of(Result.error(ex.getMessage()));
        }
        return ResultStream.innerOf(paths);
    } 
    
    private InputStream getResourceAsStream(String path) {
        return this.getClass().getClassLoader().getResourceAsStream(standardLibFolder + "/" + path);
    }

    private void loadFormats() {
        for (StandardFormat format : StandardFormat.values()) {
            this.registerFormat(format.format);
        }
    }

    public TemplateStore getStandardLibrary() {
        return getTemplateStore().getStandardLibrary().get();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy