use of org.apache.velocity.exception.ParseErrorException in project jfinal by jfinal.
the class VelocityRender method render.
public void render() {
if (notInit) {
// Velocity.init("velocity.properties"); // setup
Velocity.init(properties);
notInit = false;
}
PrintWriter writer = null;
try {
/*
* Make a context object and populate with the data. This
* is where the Velocity engine gets the data to resolve the
* references (ex. $list) in the template
*/
VelocityContext context = new VelocityContext();
// Map root = new HashMap();
for (Enumeration<String> attrs = request.getAttributeNames(); attrs.hasMoreElements(); ) {
String attrName = attrs.nextElement();
context.put(attrName, request.getAttribute(attrName));
}
/*
* get the Template object. This is the parsed version of your
* template input file. Note that getTemplate() can throw
* ResourceNotFoundException : if it doesn't find the template
* ParseErrorException : if there is something wrong with the VTL
* Exception : if something else goes wrong (this is generally
* indicative of as serious problem...)
*/
Template template = Velocity.getTemplate(view);
/*
* Now have the template engine process your template using the
* data placed into the context. Think of it as a 'merge'
* of the template and the data to produce the output stream.
*/
response.setContentType(getContentType());
// BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
writer = response.getWriter();
template.merge(context, writer);
// flush and cleanup
writer.flush();
} catch (ResourceNotFoundException e) {
throw new RenderException("Example : error : cannot find template " + view, e);
} catch (ParseErrorException e) {
throw new RenderException("Example : Syntax error in template " + view + ":" + e, e);
} catch (Exception e) {
throw new RenderException(e);
} finally {
if (writer != null)
writer.close();
}
}
use of org.apache.velocity.exception.ParseErrorException in project jena by apache.
the class SimpleVelocity method process.
/** Process a template */
public static void process(String base, String path, Writer out, VelocityContext context) {
VelocityEngine velocity = new VelocityEngine();
// Turn off logging - catch exceptions and log ourselves
velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, velocityLogChute);
velocity.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, base);
velocity.init();
try {
Template temp = velocity.getTemplate(path);
temp.merge(context, out);
out.flush();
} catch (ResourceNotFoundException ex) {
velocityLog.error("Resource not found: " + ex.getMessage());
} catch (ParseErrorException ex) {
velocityLog.error("Parse error (" + path + ") : " + ex.getMessage());
} catch (MethodInvocationException ex) {
velocityLog.error("Method invocation exception (" + path + ") : " + ex.getMessage());
} catch (IOException ex) {
velocityLog.warn("IOException", ex);
}
}
use of org.apache.velocity.exception.ParseErrorException in project bazel by bazelbuild.
the class Page method write.
/**
* Renders the template and writes the output to the given file.
*
* Strips all trailing whitespace before writing to file.
*/
public void write(File outputFile) throws IOException {
StringWriter stringWriter = new StringWriter();
try {
engine.mergeTemplate(template, "UTF-8", context, stringWriter);
} catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
throw new IOException(e);
}
stringWriter.close();
String[] lines = stringWriter.toString().split(System.getProperty("line.separator"));
try (FileWriter fileWriter = new FileWriter(outputFile)) {
for (String line : lines) {
// Strip trailing whitespace then append newline before writing to file.
fileWriter.write(line.replaceFirst("\\s+$", "") + "\n");
}
}
}
use of org.apache.velocity.exception.ParseErrorException in project cachecloud by sohutv.
the class VelocityUtils method createText.
/**
* 邮件模板
*
* @param appDesc 应用信息
* @param appAudit 处理信息
* @param templatePath 模板路径
* @param customCharset 编码
*/
public static synchronized String createText(VelocityEngine engine, AppDesc appDesc, AppAudit appAudit, AppDailyData appDailyData, List<InstanceAlertValueResult> instanceAlertValueResultList, String templatePath, String customCharset) {
if (!StringUtils.isEmpty(customCharset)) {
charset = customCharset;
}
Properties p = new Properties();
p.setProperty("file.resource.loader.path", Thread.currentThread().getContextClassLoader().getResource("").getPath());
p.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
Velocity.init(p);
logger.info("velocity: init done.");
VelocityContext context = new VelocityContext();
context.put("appDesc", appDesc);
context.put("appAudit", appAudit);
context.put("appDailyData", appDailyData);
context.put("instanceAlertValueResultList", instanceAlertValueResultList);
context.put("numberTool", new NumberTool());
context.put("ccDomain", ConstUtils.CC_DOMAIN);
context.put("decimalFormat", new DecimalFormat("###,###"));
context.put("StringUtils", StringUtils.class);
FileOutputStream fos = null;
StringWriter writer = null;
try {
Template template = engine.getTemplate(templatePath);
writer = new StringWriter();
template.merge(context, writer);
} catch (ResourceNotFoundException ex) {
logger.error("error: velocity vm resource not found.", ex);
} catch (ParseErrorException ex) {
logger.error("error: velocity parse vm file error.", ex);
} catch (MethodInvocationException ex) {
logger.error("error: velocity template merge.", ex);
} catch (Exception ex) {
logger.error("error", ex);
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
logger.error("error: close writer", e);
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
logger.error("error: close output stream.", e);
}
}
logger.info("velocity: create text done.");
if (writer != null) {
return writer.toString();
}
return null;
}
use of org.apache.velocity.exception.ParseErrorException in project Asqatasun by Asqatasun.
the class CodeGeneratorMojo method execute.
@Override
public void execute() {
try {
isContextValid();
} catch (InvalidParameterException ipe) {
Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ipe);
return;
}
// Initializes engine
VelocityEngine ve = initializeVelocity();
Iterable<CSVRecord> records = getCsv();
if (records == null) {
return;
}
try {
initializeContext();
generate(ve, records);
cleanUpUnusedFiles();
} catch (IOException ex) {
Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
} catch (ResourceNotFoundException ex) {
Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseErrorException ex) {
Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
}
}
Aggregations