Search in sources :

Example 6 with TemplateError

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

the class CompiledNamedIncludeNode method eval.

public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
    factory = new StackDelimiterResolverFactory(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 : StackDelimiterResolverFactory(org.mvel2.integration.impl.StackDelimiterResolverFactory) TemplateError(org.mvel2.templates.TemplateError) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 7 with TemplateError

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

the class CompiledIncludeNode method readInFile.

public static String readInFile(TemplateRuntime runtime, File file) {
    FileInputStream instream = null;
    BufferedReader in = null;
    final StringBuilder appender = new StringBuilder();
    try {
        instream = openInputStream(file);
        runtime.getRelPath().push(file.getParent());
        in = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
        String currentLine;
        while ((currentLine = in.readLine()) != null) {
            appender.append(currentLine);
        }
        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);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                throw new TemplateError("cannot close the reader on template file '" + file.getPath() + "'.");
            }
        }
        if (instream != null) {
            try {
                instream.close();
            } catch (IOException e) {
                throw new TemplateError("cannot close the stream on template file '" + file.getPath() + "'.");
            }
        }
    }
}
Also used : TemplateError(org.mvel2.templates.TemplateError)

Example 8 with TemplateError

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

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 9 with TemplateError

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

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 10 with TemplateError

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

the class AbstractModel method getMappedRuleUnit.

@Override
public Map.Entry<String, String> getMappedRuleUnit() {
    Map<String, String> result = new HashMap<>();
    if (!templateRegistry.contains(this.getRuleUnitTemplateName())) {
        this.addRuleUnitTemplateToRegistry(templateRegistry);
    }
    Map<String, Object> vars = new HashMap<>();
    String className = this.getRuleUnitClassName();
    vars.put("pmmlPackageName", this.getModelPackageName());
    vars.put("className", className);
    vars.put("pojoInputClassName", PMMLRequestData.class.getName());
    if (this instanceof Miningmodel) {
        vars.put("miningPojoClassName", this.getMiningPojoClassName());
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        TemplateRuntime.execute(templateRegistry.getNamedTemplate(this.getRuleUnitTemplateName()), null, new MapVariableResolverFactory(vars), baos);
    } catch (TemplateError te) {
        return null;
    } catch (TemplateRuntimeError tre) {
        // need to figure out logging here
        return null;
    }
    result.put(this.getModelPackageName() + "." + className, new String(baos.toByteArray()));
    return result.isEmpty() ? null : result.entrySet().iterator().next();
}
Also used : TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) PMMLRequestData(org.kie.api.pmml.PMMLRequestData) HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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