use of freemarker.template.TemplateException in project SpringStepByStep by JavaProgrammerLB.
the class FreeMarkerTemplateEngine method render.
@Override
public String render(ModelAndView modelAndView) {
try {
StringWriter stringWriter = new StringWriter();
Template template = configuration.getTemplate(modelAndView.getViewName());
template.process(modelAndView.getModel(), stringWriter);
return stringWriter.toString();
} catch (IOException e) {
throw new IllegalArgumentException();
} catch (TemplateException e) {
throw new IllegalArgumentException();
}
}
use of freemarker.template.TemplateException in project stanbol by apache.
the class ViewableWriter method renderPojo.
/**
* Old school classical freemarker rendering, no LD here
*/
public void renderPojo(Object pojo, final String templatePath, Writer out) {
Configuration freemarker = new Configuration();
freemarker.setDefaultEncoding("utf-8");
freemarker.setOutputEncoding("utf-8");
freemarker.setLocalizedLookup(false);
freemarker.setObjectWrapper(new DefaultObjectWrapper());
freemarker.setTemplateLoader(templateLoader);
try {
//should root be a map instead?
freemarker.getTemplate(templatePath).process(pojo, out);
out.flush();
} catch (IOException e) {
throw new RuntimeException("IOException while processing Template '" + templatePath + "' with Object '" + pojo + "' (class: " + pojo != null ? pojo.getClass().getName() : null + ")!", e);
} catch (TemplateException e) {
throw new RuntimeException(e);
}
}
use of freemarker.template.TemplateException in project engine by craftercms.
the class ExecuteControllerDirective method executeController.
protected void executeController(String path, Environment env) throws TemplateException {
Map<String, Object> scriptVariables = createScriptVariables(env);
SiteContext siteContext = SiteContext.getCurrent();
if (siteContext != null) {
ScriptFactory scriptFactory = siteContext.getScriptFactory();
if (scriptFactory == null) {
throw new IllegalStateException("No script factory associate to current site context '" + siteContext.getSiteName() + "'");
}
Script script;
try {
script = scriptFactory.getScript(path);
} catch (Exception e) {
throw new TemplateException("Unable to load controller at '" + path + "'", e, env);
}
executeController(script, scriptVariables, env);
} else {
throw new IllegalStateException("No current site context found");
}
}
use of freemarker.template.TemplateException in project engine by craftercms.
the class RenderComponentDirective method getComponent.
protected SiteItem getComponent(String componentPath, Environment env) throws TemplateException {
SiteItem currentPage = unwrap(KEY_CONTENT_MODEL, env.getVariable(CrafterPageView.KEY_CONTENT_MODEL), SiteItem.class, env);
if (currentPage != null) {
String basePath = FilenameUtils.getFullPath(currentPage.getStoreUrl());
URI baseUri = URI.create(basePath);
try {
componentPath = baseUri.resolve(componentPath).toString();
} catch (IllegalArgumentException e) {
throw new TemplateException("Invalid relative component URL " + componentPath, e, env);
}
}
SiteItem component;
try {
component = siteItemService.getSiteItem(componentPath);
} catch (Exception e) {
throw new TemplateException("Unable to load component " + componentPath + " from the underlying repository", e, env);
}
if (component == null) {
throw new TemplateException("No component found at path '" + componentPath + "'", env);
}
return component;
}
use of freemarker.template.TemplateException in project moco by dreamhead.
the class TemplateResourceReader method readFor.
@Override
public MessageContent readFor(final Optional<? extends Request> request) {
if (!request.isPresent()) {
throw new IllegalStateException("Request is required to render template");
}
MessageContent content = this.template.readFor(request);
try {
Template targetTemplate = createTemplate(content);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(stream);
targetTemplate.process(variables(request.get()), writer);
return content().withContent(stream.toByteArray()).build();
} catch (ParseException e) {
logger.error("Fail to parse template: {}", content.toString());
throw new MocoException(e);
} catch (IOException e) {
throw new MocoException(e);
} catch (TemplateException e) {
throw new MocoException(e);
}
}
Aggregations