Example usage for org.apache.commons.lang.text StrBuilder asWriter

List of usage examples for org.apache.commons.lang.text StrBuilder asWriter

Introduction

In this page you can find the example usage for org.apache.commons.lang.text StrBuilder asWriter.

Prototype

public Writer asWriter() 

Source Link

Document

Gets this builder as a Writer that can be written to.

Usage

From source file:edu.stolaf.cs.wmrserver.TransformProcessor.java

private String wrap(SubnodeConfiguration languageConf, String transformTypeString, String transformSource)
        throws IOException {
    // Get any wrapping code appropriate for the specified language,
    // including the shebang

    StrBuilder prefix = new StrBuilder();
    StrBuilder suffix = new StrBuilder();

    // Include the shebang???
    String interp = languageConf.getString("interpreter");
    if (interp != null && !interp.isEmpty())
        prefix.append("#!" + languageConf.getString("interpreter") + "\n");

    // Get the file extension appropriate for the language
    String scriptExtension = languageConf.getString("extension", "");
    if (!scriptExtension.isEmpty())
        scriptExtension = "." + scriptExtension;

    // Include the wrapper libraries, which potentially handle I/O, etc.
    File libDir = getLibraryDirectory(languageConf);
    if (libDir != null) {
        if (!libDir.isDirectory() || !libDir.canRead())
            throw new IOException("Library directory for " + transformTypeString + " did not exist "
                    + "or was not readable: " + libDir.toString());

        Reader libReader = null;//from ww w  .j  a  v  a  2s.c  o m
        try {
            File libFile = new File(libDir, transformTypeString + "-prefix" + scriptExtension);
            if (libFile.isFile() && libFile.canRead()) {
                libReader = new FileReader(libFile);
                IOUtils.copy(libReader, prefix.asWriter());
                IOUtils.closeQuietly(libReader);
            }

            libFile = new File(libDir, transformTypeString + "-suffix" + scriptExtension);
            if (libFile.isFile() && libFile.canRead()) {
                libReader = new FileReader(libFile);
                IOUtils.copy(libReader, suffix.asWriter());
            }
        } catch (IOException ex) {
            throw new IOException("Could not add the wrapper library to the " + transformTypeString
                    + " code due to an exception.", ex);
        } finally {
            IOUtils.closeQuietly(libReader);
        }
    }

    return prefix.toString() + "\n" + transformSource + "\n" + suffix.toString();
}