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

io.engineblock.core.MarkdownDocInfo Maven / Gradle / Ivy

Go to download

Runtime artifact for engineblock; This module ties the core libraries, provided drivers, and API into a single executable jar

There is a newer version: 2.12.65
Show newest version
package io.engineblock.core;

import io.engineblock.activityapi.core.ActivityType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;

public class MarkdownDocInfo {
    private final static Logger logger = LoggerFactory.getLogger(MarkdownDocInfo.class);

    public static Optional forHelpTopic(String topic) {
        String help = null;
        try {
            help = new MarkdownDocInfo().forActivityInstance(topic);
            return Optional.ofNullable(help);
        } catch (Exception e) {
            logger.debug("Did not find help topic for activity instance: "  + topic);
        }

        try {
            help = new MarkdownDocInfo().forResourceMarkdown(topic, "docs/");
            return Optional.ofNullable(help);
        } catch (Exception e) {
            logger.debug("Did not find help topic for generic markdown file: " + topic + "(.md)");
        }

        return Optional.empty();

    }

    public String forResourceMarkdown(String s, String... additionalSearchPaths) {
        String docFileName = s + ".md";

        List searchIn = new ArrayList<>();
        searchIn.add(docFileName);
        Arrays.stream(additionalSearchPaths).map(path -> path + docFileName).forEach(searchIn::add);

        logger.info("loading doc file for topic:" + docFileName);

        Optional found = searchIn.stream().map(
                getClass().getClassLoader()::getResourceAsStream)
                .filter(Objects::nonNull)
                .findFirst();

        InputStream stream = found.orElseThrow(
                () -> new RuntimeException("Unable to find docstream in classpath: " + docFileName)
        );

        String docInfo = "";
        try (BufferedReader buffer = new BufferedReader(new InputStreamReader(stream))) {
            docInfo = buffer.lines().collect(Collectors.joining("\n"));
        } catch (Throwable t) {
            throw new RuntimeException("Unable to buffer data from docstream: " + docFileName + ":" + t);
        }

        return docInfo;
    }

    public String forActivityInstance(String s) {
        ActivityType activityType = ActivityType.FINDER.getOrThrow(s);
        return forResourceMarkdown(activityType.getName()+".md", "docs/");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy