Search in sources :

Example 1 with TemplateRuntime

use of org.mvel2.templates.TemplateRuntime 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 2 with TemplateRuntime

use of org.mvel2.templates.TemplateRuntime 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)

Example 3 with TemplateRuntime

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

the class ForEachNode method eval.

public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
    Iterator[] iters = new Iterator[item.length];
    Object o;
    for (int i = 0; i < iters.length; i++) {
        if ((o = MVEL.eval(expression[i], ctx, factory)) instanceof Iterable) {
            iters[i] = ((Iterable) o).iterator();
        } else if (o instanceof Object[]) {
            iters[i] = new ArrayIterator((Object[]) o);
        } else {
            throw new TemplateRuntimeError("cannot iterate object type: " + o.getClass().getName());
        }
    }
    Map<String, Object> locals = new HashMap<String, Object>();
    MapVariableResolverFactory localFactory = new MapVariableResolverFactory(locals, factory);
    int iterate = iters.length;
    while (true) {
        for (int i = 0; i < iters.length; i++) {
            if (!iters[i].hasNext()) {
                iterate--;
                locals.put(item[i], "");
            } else {
                locals.put(item[i], iters[i].next());
            }
        }
        if (iterate != 0) {
            nestedNode.eval(runtime, appender, ctx, localFactory);
            if (sepExpr != null) {
                for (Iterator it : iters) {
                    if (it.hasNext()) {
                        appender.append(String.valueOf(MVEL.eval(sepExpr, ctx, factory)));
                        break;
                    }
                }
            }
        } else
            break;
    }
    return next != null ? next.eval(runtime, appender, ctx, factory) : null;
}
Also used : TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) HashMap(java.util.HashMap) Iterator(java.util.Iterator) ArrayIterator(org.mvel2.templates.util.ArrayIterator) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ArrayIterator(org.mvel2.templates.util.ArrayIterator)

Example 4 with TemplateRuntime

use of org.mvel2.templates.TemplateRuntime 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 5 with TemplateRuntime

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

the class CompiledForEachNode method eval.

public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
    Iterator[] iters = new Iterator[item.length];
    Object o;
    for (int i = 0; i < iters.length; i++) {
        if ((o = MVEL.executeExpression(ce[i], ctx, factory)) instanceof Iterable) {
            iters[i] = ((Iterable) o).iterator();
        } else if (o instanceof Object[]) {
            iters[i] = new ArrayIterator((Object[]) o);
        } else if (o instanceof Integer) {
            iters[i] = new CountIterator((Integer) o);
        } else {
            throw new TemplateRuntimeError("cannot iterate object type: " + o.getClass().getName());
        }
    }
    Map<String, Object> locals = new HashMap<String, Object>();
    MapVariableResolverFactory localFactory = new MapVariableResolverFactory(locals, factory);
    int iterate = iters.length;
    while (true) {
        for (int i = 0; i < iters.length; i++) {
            if (!iters[i].hasNext()) {
                iterate--;
                locals.put(item[i], "");
            } else {
                locals.put(item[i], iters[i].next());
            }
        }
        if (iterate != 0) {
            nestedNode.eval(runtime, appender, ctx, localFactory);
            if (sepExpr != null) {
                for (Iterator it : iters) {
                    if (it.hasNext()) {
                        appender.append(String.valueOf(MVEL.executeExpression(cSepExpr, ctx, factory)));
                        break;
                    }
                }
            }
        } else
            break;
    }
    return next != null ? next.eval(runtime, appender, ctx, factory) : null;
}
Also used : CountIterator(org.mvel2.templates.util.CountIterator) HashMap(java.util.HashMap) TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) Iterator(java.util.Iterator) ArrayIterator(org.mvel2.templates.util.ArrayIterator) CountIterator(org.mvel2.templates.util.CountIterator) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ArrayIterator(org.mvel2.templates.util.ArrayIterator)

Aggregations

TemplateError (org.mvel2.templates.TemplateError)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)2 TemplateRuntimeError (org.mvel2.templates.TemplateRuntimeError)2 ArrayIterator (org.mvel2.templates.util.ArrayIterator)2 CompiledTemplate (org.mvel2.templates.CompiledTemplate)1 CountIterator (org.mvel2.templates.util.CountIterator)1