Search in sources :

Example 1 with TemplateError

use of org.mvel2.templates.TemplateError in project drools by kiegroup.

the class AbstractModel method getMappedOutputPojo.

public Map.Entry<String, String> getMappedOutputPojo() {
    Map<String, String> result = new HashMap<>();
    if (!templateRegistry.contains(getOutputPojoTemplateName())) {
        this.addOutputTemplateToRegistry(templateRegistry);
    }
    List<PMMLOutputField> dataFields = this.getOutputFields();
    Map<String, Object> vars = new HashMap<>();
    String className = this.getOutputPojoClassName();
    vars.put("pmmlPackageName", PMML_JAVA_PACKAGE_NAME);
    vars.put("className", className);
    vars.put("imports", new ArrayList<>());
    vars.put("dataFields", dataFields);
    vars.put("modelName", this.getModelId());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        TemplateRuntime.execute(templateRegistry.getNamedTemplate(this.getOutputPojoTemplateName()), null, new MapVariableResolverFactory(vars), baos);
    } catch (TemplateError te) {
        return null;
    } catch (TemplateRuntimeError tre) {
        // need to figure out logging here
        return null;
    }
    result.put(className, new String(baos.toByteArray()));
    return result.entrySet().iterator().next();
}
Also used : TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TemplateError(org.mvel2.templates.TemplateError)

Example 2 with TemplateError

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

the class CompiledIncludeNode method readInFile.

public static String readInFile(TemplateRuntime runtime, File file) {
    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 '" + file.getPath() + "': file not found.");
    } catch (IOException e) {
        throw new TemplateError("unknown I/O exception while including '" + file.getPath() + "' (stacktrace nested)", e);
    }
}
Also used : TemplateError(org.mvel2.templates.TemplateError)

Example 3 with TemplateError

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

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)

Example 4 with TemplateError

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

the class TemplateTools method readStream.

public static String readStream(InputStream instream) {
    try {
        byte[] buf = new byte[10];
        StringBuilder appender = new StringBuilder();
        int read;
        while ((read = instream.read(buf)) != -1) {
            for (int i = 0; i < read; i++) {
                appender.append((char) buf[i]);
            }
        }
        return appender.toString();
    } catch (NullPointerException e) {
        if (instream == null) {
            throw new TemplateError("null input stream", e);
        } else {
            throw e;
        }
    } catch (IOException e) {
        throw new TemplateError("unknown I/O exception while including (stacktrace nested)", e);
    }
}
Also used : TemplateError(org.mvel2.templates.TemplateError)

Example 5 with TemplateError

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

the class CompiledNamedIncludeNode method eval.

public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
    if (cPreExpression != null) {
        MVEL.executeExpression(cPreExpression, ctx, factory);
    }
    if (next != null) {
        String namedTemplate = MVEL.executeExpression(cIncludeExpression, ctx, factory, String.class);
        CompiledTemplate ct = runtime.getNamedTemplateRegistry().getNamedTemplate(namedTemplate);
        if (ct == null)
            throw new TemplateError("named template does not exist: " + namedTemplate);
        return next.eval(runtime, appender.append(String.valueOf(TemplateRuntime.execute(ct, ctx, factory, runtime.getNamedTemplateRegistry()))), ctx, factory);
    // return next.eval(runtime,
    // appender.append(String.valueOf(TemplateRuntime.execute(runtime.getNamedTemplateRegistry().getNamedTemplate(MVEL.executeExpression(cIncludeExpression, ctx, factory, String.class)), ctx, factory))), ctx, factory);
    } else {
        return appender.append(String.valueOf(TemplateRuntime.execute(runtime.getNamedTemplateRegistry().getNamedTemplate(MVEL.executeExpression(cIncludeExpression, ctx, factory, String.class)), ctx, factory, runtime.getNamedTemplateRegistry())));
    }
}
Also used : TemplateError(org.mvel2.templates.TemplateError) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

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