下面列出了freemarker.template.TemplateNotFoundException#freemarker.core.ParseException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 判断模板是否存在
*
* @throws Exception
*/
public static boolean isExistTemplate(String tplName) throws Exception {
try {
Template mytpl = _tplConfig.getTemplate(tplName, "UTF-8");
if (mytpl == null) {
return false;
}
} catch (Exception e) {
//update-begin--Author:scott Date:20180320 for:解决问题 - 错误提示sql文件不存在,实际问题是sql freemarker用法错误-----
if (e instanceof ParseException) {
log.error(e.getMessage(), e.fillInStackTrace());
throw new Exception(e);
}
log.debug("----isExistTemplate----" + e.toString());
//update-end--Author:scott Date:20180320 for:解决问题 - 错误提示sql文件不存在,实际问题是sql freemarker用法错误------
return false;
}
return true;
}
/**
* 判断模板是否存在
*
* @throws Exception
*/
public static boolean isExistTemplate(String tplName) throws Exception {
try {
Template mytpl = _tplConfig.getTemplate(tplName, "UTF-8");
if (mytpl == null) {
return false;
}
} catch (Exception e) {
//update-begin--Author:scott Date:20180320 for:解决问题 - 错误提示sql文件不存在,实际问题是sql freemarker用法错误-----
if (e instanceof ParseException) {
log.error(e.getMessage(), e.fillInStackTrace());
throw new Exception(e);
}
log.debug("----isExistTemplate----" + e.toString());
//update-end--Author:scott Date:20180320 for:解决问题 - 错误提示sql文件不存在,实际问题是sql freemarker用法错误------
return false;
}
return true;
}
/**
* 判断模板是否存在
*
* @throws Exception
*/
public static boolean isExistTemplate(String tplName) throws Exception {
try {
Template mytpl = _tplConfig.getTemplate(tplName, "UTF-8");
if (mytpl == null) {
return false;
}
} catch (Exception e) {
//update-begin--Author:scott Date:20180320 for:解决问题 - 错误提示sql文件不存在,实际问题是sql freemarker用法错误-----
if (e instanceof ParseException) {
log.error(e.getMessage(), e.fillInStackTrace());
throw new Exception(e);
}
log.debug("----isExistTemplate----" + e.toString());
//update-end--Author:scott Date:20180320 for:解决问题 - 错误提示sql文件不存在,实际问题是sql freemarker用法错误------
return false;
}
return true;
}
private void writeStatus(Writer writer, StatusDescription status, int indentSize) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
logger.debug("writeStatus - context: {}, status: {}, description: {}", currentContext, status.getStatus(), status.getDescription());
if(status.getStatus() != PASSED) {
createNode(writer, "Status", NodeType.STATUS, status.getStatus(), null, indentSize);
TemplateWrapper statusTableTemplate = templateWrapperFactory.createWrapper("status_table.ftlh");
statusTableTemplate.setData("status", status.getStatus());
statusTableTemplate.setData("description", status.getDescription());
statusTableTemplate.setData("exception", status.getCause());
statusTableTemplate.setData("id", ++nodeId * 1000);
statusTableTemplate.write(writer, indentSize + 2);
closeNode(writer, indentSize);
}
}
/**
* @param templateFilePath
* @param destFilePath
* @param configuration
* @param model
* @param override
* @param append
* @throws ParseException
* @throws MalformedTemplateNameException
* @throws IOException
* @throws TemplateException
*/
public static void generateFileByFile(String templateFilePath, String destFilePath, Configuration configuration, Object model, boolean override, boolean append)
throws MalformedTemplateNameException, ParseException, IOException, TemplateException {
Template t = configuration.getTemplate(templateFilePath);
File destFile = new File(destFilePath);
if (override || append || !destFile.exists()) {
File parent = destFile.getParentFile();
if (null != parent) {
parent.mkdirs();
}
try (FileOutputStream outputStream = new FileOutputStream(destFile, append); FileLock fileLock = outputStream.getChannel().tryLock();) {
Writer out = new OutputStreamWriter(outputStream, DEFAULT_CHARSET);
t.process(model, out);
}
log.info(destFilePath + " saved!");
} else {
log.error(destFilePath + " already exists!");
}
}
/**
* The error message (and sometimes also the class), and then the same with the cause exception, and so on. Doesn't
* contain the stack trace or other location information.
*/
public static String getMessageWithCauses(final Throwable exc) {
StringBuilder sb = new StringBuilder();
Throwable curExc = exc;
while (curExc != null) {
if (curExc != exc) {
sb.append("\n\nCaused by:\n");
}
String msg = curExc.getMessage();
if (msg == null || !(curExc instanceof TemplateException || curExc instanceof ParseException)) {
sb.append(curExc.getClass().getName()).append(": ");
}
sb.append(msg);
curExc = curExc.getCause();
}
return sb.toString();
}
private void writeParametersTable(int id, String messageName, List<ActionParameter> parameters, boolean hasHeaders) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
createNode(testCaseWriter, "Input Parameters", NodeType.INPUT, null, null, 7);
TemplateWrapper testCaseActionParametersTemplate = templateWrapperFactory.createWrapper("test_case_parameters_table.ftlh");
testCaseActionParametersTemplate.setData("tableId", id);
testCaseActionParametersTemplate.setData("message_name", messageName);
testCaseActionParametersTemplate.setData("parameters", parameters);
testCaseActionParametersTemplate.setData("hasHeaders", hasHeaders);
testCaseActionParametersTemplate.write(testCaseWriter, 9);
closeNode(testCaseWriter, 7);
}
@Test
public void testTemplateWithSyntaxError() {
FreeMarkerServiceResponse serviceResponse = getService().executeTemplate(new ExecuteTemplateArgs()
.templateSourceCode("test ${xx").dataModel(Collections.emptyMap()));
assertThat(serviceResponse.isSuccesful(), is(false));
assertThat(serviceResponse.getFailureReason(), instanceOf(ParseException.class));
}
private String internalRender(String templateFile, Map<String, Object> data) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
if (!templateFile.endsWith(TEMPLATE_EXTENSION)) {
templateFile += TEMPLATE_EXTENSION;
}
Template template = config.getTemplate(templateFile);
Writer output = new StringWriter();
template.process(data, output);
return output.toString();
}
public Template createTemplate(String templatePath)
throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException {
return freemarkerConfig.getTemplate(templatePath);
}
public TemplateWrapper createWrapper(String templateName) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException {
return new TemplateWrapper(configuration.getTemplate(templateName));
}
private void writeElements(Writer writer, List<Object> elements, int indentSize) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
List<Verification> verifications = new ArrayList<>();
for(Object element : elements) {
if(element instanceof Message) {
if(!verifications.isEmpty()) {
writeVerifications(writer, verifications, indentSize);
verifications.clear();
}
writeMessage(writer, (Message)element, indentSize);
} else if(element instanceof Verification) {
verifications.add((Verification)element);
} else if(element instanceof ReportTable) {
if(!verifications.isEmpty()) {
writeVerifications(writer, verifications, indentSize);
verifications.clear();
}
writeTable(writer, null, (ReportTable)element, indentSize);
} else if(element instanceof Action) {
writeAction((Action)element);
} else if (element instanceof ActionGroup) {
ActionGroup group = (ActionGroup) element;
createNode(testCaseWriter, group.getName(), group.getDescription(), NodeType.ACTION, group.getStatus(),
null, 5, null, null, Collections.emptyList(), null, true);
writeElements(writer, group.getElements(), indentSize);
String linkToReport = group.getLinkToReport();
if(StringUtils.isNotBlank(linkToReport)) {
createNode(testCaseWriter, "Report", NodeType.DESCRIPTION, null, null, 7);
writeLine(testCaseWriter, "<a href='" + linkToReport + "'>Link to report</a>", 9);
closeNode(testCaseWriter, 7);
}
closeNode(testCaseWriter, 7);
} else if(element instanceof Throwable) {
writeException(testCaseWriter, (Throwable)element);
} else if(element instanceof ParametersTable) {
ParametersTable table = (ParametersTable)element;
writeParametersTable(table.getId(), table.getMessageName(), table.getParameters(), table.isHasHeaders());
}
}
if(!verifications.isEmpty()) {
writeVerifications(writer, verifications, indentSize);
verifications.clear();
}
}
protected String renderTemplate(String tmplName, Map<String, Object> model) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
StringWriter out = new StringWriter();
Template template = freemarkerConfiguration.getTemplate(tmplName + ".ftl");
template.process(model, out);
return out.toString();
}
/**
* 解析电子邮件模板内容.
* @param templateLocation - 电子邮件模板相对路径
* @param model - 电子邮件的附加信息
* @return 解析后的电子邮件内容
* @throws TemplateException
* @throws IOException
* @throws ParseException
* @throws MalformedTemplateNameException
* @throws TemplateNotFoundException
*/
public String getMailContent(String templateLocation, Map<String, Object> model)
throws TemplateNotFoundException, MalformedTemplateNameException,
ParseException, IOException, TemplateException {
model.put("baseUrl", baseUrl);
return FreeMarkerTemplateUtils.processTemplateIntoString(
freeMarkerConfigurer.getConfiguration().getTemplate(templateLocation), model);
}
/**
* @param writer
* @param template
* @param configuration
* @param model
* @throws TemplateNotFoundException
* @throws MalformedTemplateNameException
* @throws ParseException
* @throws IOException
* @throws TemplateException
*/
public static void generateStringByPath(Writer writer, String template, Configuration configuration, Object model)
throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
Template tpl = configuration.getTemplate(template, null, null, null, true, true);
tpl.process(model, writer);
}