本文整理汇总了Java中org.mvel2.templates.TemplateError类的典型用法代码示例。如果您正苦于以下问题:Java TemplateError类的具体用法?Java TemplateError怎么用?Java TemplateError使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TemplateError类属于org.mvel2.templates包,在下文中一共展示了TemplateError类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: eval
import org.mvel2.templates.TemplateError; //导入依赖的package包/类
public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
if (preExpression != 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())));
}
}
示例2: eval
import org.mvel2.templates.TemplateError; //导入依赖的package包/类
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())));
}
}
示例3: Template
import org.mvel2.templates.TemplateError; //导入依赖的package包/类
public Template(URL url) {
String file = url.getFile();
this.name = file.substring(file.lastIndexOf('/') + 1);
try {
this.baseURI = url.toURI().toString();
this.compiled = loadCompiled(url);
} catch (URISyntaxException e) {
throw new TemplateError("Could not load template from template " + url, e);
}
}
示例4: loadCompiled
import org.mvel2.templates.TemplateError; //导入依赖的package包/类
/**
* Load a template given its {@code templateURL}.
*
* @param templateURL the template url
* @return the compiled template
*/
public static CompiledTemplate loadCompiled(URL templateURL) {
CompiledTemplate template = templateCache.get(templateURL);
if (template == null) {
InputStream is = null;
try {
is = templateURL.openStream();
} catch (IOException e) {
throw new TemplateError("Could not load template from template " + templateURL, e);
}
template = loadCompiled(is);
templateCache.put(templateURL, template);
}
return template;
}
示例5: testIncludeNamedNotFound
import org.mvel2.templates.TemplateError; //导入依赖的package包/类
@Test
public void testIncludeNamedNotFound() throws Exception {
Template template = new Template(getClass().getResource("testtemplates/include_named_not_found.templ"));
try {
template.render(NULL_MODEL);
fail("Was expecting to fail");
} catch (TemplateError ignore) {
}
}
示例6: render
import org.mvel2.templates.TemplateError; //导入依赖的package包/类
public String render(Model model, Map<String, Object> vars) {
vars = new HashMap<>(vars);
vars.put("options", options);
vars.put("skipFile", false);
vars.putAll(model.getVars());
vars.putAll(ClassKind.vars());
vars.putAll(MethodKind.vars());
vars.putAll(Case.vars());
TemplateRegistry registry = new SimpleTemplateRegistry() {
@Override
public CompiledTemplate getNamedTemplate(String name) {
try {
return super.getNamedTemplate(name);
} catch (TemplateError err) {
// Load error try to resolve from base uri
try {
URL url;
if (name.startsWith("/")) {
url = resolveURL(name.substring(1));
} else {
url = new URL(new URL(baseURI), name);
}
CompiledTemplate compiledTemplate = loadCompiled(url);
addNamedTemplate(name, compiledTemplate);
return compiledTemplate;
} catch (Exception ex) {
throw new TemplateError("Could not load template " + name + " from template " + baseURI, ex);
}
}
}
};
ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
try {
// Be sure to have mvel classloader as parent during evaluation as it will need mvel classes
// when generating code
Thread.currentThread().setContextClassLoader(TemplateRuntime.class.getClassLoader());
TemplateRuntime runtime = new TemplateRuntime(compiled.getTemplate(), registry, compiled.getRoot(), ".");
String blah = (String) runtime.execute(new StringBuilder(), null, new MapVariableResolverFactory(vars));
if (Boolean.TRUE.equals(vars.get("skipFile"))) {
return null;
}
return blah;
} finally {
Thread.currentThread().setContextClassLoader(currentCL);
}
}