Search in sources :

Example 11 with TemplateError

use of org.mvel2.templates.TemplateError in project mvel by mikebrock.

the class TemplateTools method readInFile.

public static String readInFile(File file) {
    try {
        FileChannel fc = new FileInputStream(file).getChannel();
        ByteBuffer buf = allocateDirect(10);
        StringBuilder appender = new StringBuilder();
        int read;
        while (true) {
            buf.rewind();
            if ((read = fc.read(buf)) != -1) {
                buf.rewind();
                for (; read != 0; read--) {
                    appender.append((char) buf.get());
                }
            } else {
                break;
            }
        }
        fc.close();
        return appender.toString();
    } catch (FileNotFoundException e) {
        throw new TemplateError("cannot include template '" + file.getName() + "': file not found.");
    } catch (IOException e) {
        throw new TemplateError("unknown I/O exception while including '" + file.getName() + "' (stacktrace nested)", e);
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) TemplateError(org.mvel2.templates.TemplateError) ByteBuffer(java.nio.ByteBuffer)

Example 12 with TemplateError

use of org.mvel2.templates.TemplateError in project mvel by mvel.

the class IncludeNode method readInFile.

public static String readInFile(TemplateRuntime runtime, String fileName) {
    File file = new File(String.valueOf(runtime.getRelPath().peek()) + "/" + fileName);
    try {
        FileInputStream instream = new FileInputStream(file);
        BufferedInputStream bufstream = new BufferedInputStream(instream);
        runtime.getRelPath().push(file.getParent());
        byte[] buf = new byte[10];
        int read;
        int i;
        StringBuilder appender = new StringBuilder();
        while ((read = bufstream.read(buf)) != -1) {
            for (i = 0; i < read; i++) {
                appender.append((char) buf[i]);
            }
        }
        bufstream.close();
        instream.close();
        runtime.getRelPath().pop();
        return appender.toString();
    } catch (FileNotFoundException e) {
        throw new TemplateError("cannot include template '" + fileName + "': file not found.");
    } catch (IOException e) {
        throw new TemplateError("unknown I/O exception while including '" + fileName + "' (stacktrace nested)", e);
    }
}
Also used : TemplateError(org.mvel2.templates.TemplateError)

Aggregations

TemplateError (org.mvel2.templates.TemplateError)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ByteBuffer (java.nio.ByteBuffer)2 FileChannel (java.nio.channels.FileChannel)2 HashMap (java.util.HashMap)2 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)2 CompiledTemplate (org.mvel2.templates.CompiledTemplate)2 TemplateRuntimeError (org.mvel2.templates.TemplateRuntimeError)2 PMMLRequestData (org.kie.api.pmml.PMMLRequestData)1 StackDelimiterResolverFactory (org.mvel2.integration.impl.StackDelimiterResolverFactory)1