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);
}
}
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())));
}
}
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;
}
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);
}
}
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;
}
Aggregations