use of org.apache.velocity.exception.ResourceNotFoundException in project jaffa-framework by jaffa-projects.
the class FormPrintEngineVelocity method generate.
public void generate() throws FormPrintException {
// make a Context and put data into it
log.debug("Set Up Context");
VelocityContext context = new VelocityContext();
context.put("data", getDataSource());
fmt = new FormFormatHelper();
context.put("fmt", fmt);
context.put("context", ContextManagerFactory.instance().getThreadContext());
log.debug("data=" + BeanMoulder.printBean(getDataSource()));
// Split the template file between path and name
File templateFile = new File(getTemplateName());
if (!templateFile.exists()) {
String err = "Velocity Template Not Found - " + getTemplateName();
log.error(err);
throw new EngineProcessingException(err);
}
String path = templateFile.getParent();
// (templateFile.getParent()==null?getTemplateName():getTemplateName().substring(templateFile.getParent().length()));
String file = templateFile.getName();
if (path != null) {
Velocity.setProperty("file.resource.loader.path", path);
}
log.debug("SearchPath = " + path + ", template = " + file);
// init the runtime engine. Defaults are fine.
try {
Velocity.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem");
Velocity.init();
log.debug("Initialized Velocity");
} catch (Exception e) {
String err = "Velocity Initialization - " + e.getLocalizedMessage();
log.error(err, e);
throw new EngineProcessingException(err, e);
}
// Try and load the template
Template template = null;
try {
log.debug("Velocity file.resource.loader.path property = " + Velocity.getProperty("file.resource.loader.path"));
template = Velocity.getTemplate(file);
} catch (ResourceNotFoundException e) {
String err = "Error opening template - " + e.getLocalizedMessage();
log.error(err, e);
throw new EngineProcessingException(err, e);
} catch (ParseErrorException e) {
String err = "Template Parse Error - " + e.getLocalizedMessage();
log.error(err, e);
throw new EngineProcessingException(err, e);
} catch (Exception e) {
String err = "Template Load Error - " + e.getLocalizedMessage();
log.error(err, e);
throw new EngineProcessingException(err, e);
}
try {
m_output = new StringWriter();
// render a template
template.merge(context, m_output);
// log.debug("Generated outbut based on template...\n" + m_output.getBuffer().toString());
m_processed = true;
} catch (Exception e) {
String err = "Velocity Error - " + e.getLocalizedMessage();
log.error(err, e);
throw new EngineProcessingException(err, e);
}
}
use of org.apache.velocity.exception.ResourceNotFoundException in project carbon-apimgt by wso2.
the class GatewaySourceGeneratorImpl method getEndpointConfigStringFromTemplate.
@Override
public String getEndpointConfigStringFromTemplate(Endpoint endpoint) throws APITemplateException {
StringWriter writer = new StringWriter();
String templatePath = "resources" + File.separator + "template" + File.separator + "endpoint.xml";
try {
// build the context for template and apply the necessary decorators
ConfigContext configcontext = new EndpointContext(endpoint, packageName);
VelocityContext context = configcontext.getContext();
VelocityEngine velocityengine = new VelocityEngine();
velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
velocityengine.init();
Template template = velocityengine.getTemplate(templatePath);
template.merge(context, writer);
} catch (ResourceNotFoundException e) {
log.error("Template " + templatePath + " not Found", e);
throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
} catch (ParseErrorException e) {
log.error("Syntax error in " + templatePath, e);
throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
}
return writer.toString();
}
use of org.apache.velocity.exception.ResourceNotFoundException in project BIMserver by opensourceBIM.
the class TemplateEngine method process.
public String process(Map<String, Object> context, TemplateIdentifier templateIdentifier) {
StringWriter stringWriter = new StringWriter();
VelocityContext velocityContext = new VelocityContext(context);
try {
velocityEngine.mergeTemplate(templateIdentifier.getFileName(), "UTF-8", velocityContext, stringWriter);
return stringWriter.toString();
} catch (ResourceNotFoundException e) {
LOGGER.error("", e);
} catch (ParseErrorException e) {
LOGGER.error("", e);
} catch (MethodInvocationException e) {
LOGGER.error("", e);
} catch (Exception e) {
LOGGER.error("", e);
}
return "";
}
use of org.apache.velocity.exception.ResourceNotFoundException in project OpenOLAT by OpenOLAT.
the class VelocityTemplatesPreWarm method run.
@Override
public void run() {
long start = System.nanoTime();
log.info("Start filling the velocity template cache");
final VelocityContext context = new VelocityContext();
final AtomicInteger numOfTemplates = new AtomicInteger(0);
final File root = new File(WebappHelper.getContextRoot(), "WEB-INF/classes");
final Path fPath = root.toPath();
try {
if (Files.exists(fPath)) {
Files.walkFileTree(fPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
try {
String path = fPath.relativize(file).toString();
if (path.endsWith(".html") && path.contains("/_content/")) {
StringOutput writer = new StringOutput();
VelocityHelper.getInstance().mergeContent(path, context, writer, null);
numOfTemplates.incrementAndGet();
}
} catch (ResourceNotFoundException e) {
log.error("", e);
} catch (ParseErrorException e) {
log.error("", e);
}
return FileVisitResult.CONTINUE;
}
});
}
} catch (IOException e) {
log.error("", e);
}
log.info("Velocity cache filled with " + numOfTemplates + " templates in (ms): " + CodeHelper.nanoToMilliTime(start));
}
use of org.apache.velocity.exception.ResourceNotFoundException in project simple-email by codylerum.
the class VelocityTemplate method merge.
@Override
public String merge(Map<String, Object> context) {
StringWriter writer = new StringWriter();
velocityContext = new VelocityContext(context);
try {
velocityEngine.evaluate(velocityContext, writer, "mailGenerated", template);
} catch (ResourceNotFoundException e) {
throw new TemplatingException("Unable to find template", e);
} catch (ParseErrorException e) {
throw new TemplatingException("Unable to find template", e);
} catch (MethodInvocationException e) {
throw new TemplatingException("Error processing method referenced in context", e);
}
return writer.toString();
}
Aggregations