ArduinoLibrary.java

/*******************************************************************************
 * jArduino: Arduino C++ Code Generation From Java
 * Copyright 2020 Tony Washer
 *
 * 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 net.sourceforge.jarduino.message;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import net.sourceforge.jarduino.util.ArduinoLogManager;
import net.sourceforge.jarduino.util.ArduinoLogger;

/**
 * Library resources.
 */
public final class ArduinoLibrary {
    /**
     * The LOGGER.
     */
    private static final ArduinoLogger LOGGER = ArduinoLogManager.getLogger(ArduinoLibrary.class);

    /**
     * The library source.
     */
    public static final String CODE = "jarduino.cpp";

    /**
     * The library header.
     */
    public static final String HEADER = "jarduino.h";

    /**
     * The library template.
     */
    public static final String TEMPLATE = "template.";

    /**
     * Private Constructor.
     */
    private ArduinoLibrary() {
    }

    /**
     * Load code to String.
     * @return the loaded code
     */
    public static String loadLibraryCode() {
        return loadResourceToString(CODE);
    }

    /**
     * Load header to String.
     * @return the loaded header
     */
    public static String loadLibraryHeader() {
        return loadResourceToString(HEADER);
    }

    /**
     * Load template to String.
     * @param pSuffix the template suffix
     * @return the loaded template
     */
    public static String loadLibraryTemplate(final String pSuffix) {
        return loadResourceToString(TEMPLATE + pSuffix);
    }

    /**
     * Load code to String.
     * @param pFile the file to load
     * @return the loaded code
     */
    private static String loadResourceToString(final String pFile) {
        /* Protect against exceptions */
        try (InputStream myStream = ArduinoLibrary.class.getResourceAsStream(pFile);
             InputStreamReader myInputReader = new InputStreamReader(myStream, StandardCharsets.UTF_8);
             BufferedReader myReader = new BufferedReader(myInputReader)) {

            /* Reset the builder */
            final StringBuilder myBuilder = new StringBuilder();

            /* Read the header entry */
            boolean bXtra = false;
            for (;;) {
                /* Read next line */
                final String myLine = myReader.readLine();
                if (myLine == null) {
                    break;
                }

                /* Add to the string buffer */
                if (bXtra) {
                    myBuilder.append(ArduinoChar.LF);
                }
                myBuilder.append(myLine);
                bXtra = true;
            }

            /* Build the string */
            return myBuilder.toString();

            /* Catch exceptions */
        } catch (IOException e) {
            /* Throw an exception */
            LOGGER.fatal("Failed to load resource: " + pFile, e);
            return "<FAIL>";
        }
    }
}