use of org.apache.velocity.exception.MethodInvocationException in project dbflute-core by dbflute.
the class DfAbstractTexenTask method fireVelocityProcess.
protected void fireVelocityProcess() {
assertBasicAntParameter();
// set up the encoding of templates from DBFlute property
setInputEncoding(getBasicProperties().getTemplateFileEncoding());
setOutputEncoding(getBasicProperties().getSourceFileEncoding());
try {
initializeGeneratorInstance();
final DfGenerator generator = setupGenerator();
final Context ctx = setupControlContext();
_log.info("generator.parse(\"" + controlTemplate + "\", ctx);");
generator.parse(controlTemplate, ctx);
generator.shutdown();
cleanup();
} catch (BuildException e) {
throw e;
} catch (MethodInvocationException e) {
final String method = e.getReferenceName() + "." + e.getMethodName() + "()";
String msg = "Exception thrown by " + method + ": control=" + controlTemplate;
throw new IllegalStateException(msg, e.getWrappedThrowable());
} catch (ParseErrorException e) {
throw new IllegalStateException("Velocity syntax error: control=" + controlTemplate, e);
} catch (ResourceNotFoundException e) {
throw new IllegalStateException("Resource not found: control=" + controlTemplate, e);
} catch (Exception e) {
throw new IllegalStateException("Generation failed: control=" + controlTemplate, e);
}
}
use of org.apache.velocity.exception.MethodInvocationException in project dbflute-core by dbflute.
the class DfFlutistGenerator method throwTemplateParsingException.
protected void throwTemplateParsingException(String inputTemplate, String specifiedInputEncoding, Throwable e) {
rethrowIfNestedException(inputTemplate, specifiedInputEncoding, e);
final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
br.addNotice("Failed to parse the input template.");
br.addItem("Input Template");
br.addElement(inputTemplate + " (" + specifiedInputEncoding + ")");
final Throwable cause;
if (e instanceof MethodInvocationException) {
cause = ((MethodInvocationException) e).getWrappedThrowable();
} else {
cause = e;
}
final String msg = br.buildExceptionMessage();
throw new DfTemplateParsingException(msg, cause);
}
use of org.apache.velocity.exception.MethodInvocationException 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.MethodInvocationException in project oap by oaplatform.
the class VelocityTemplateTransformer method transform.
public synchronized String transform(Template template) {
try {
VelocityContext context = new VelocityContext();
Map<String, Object> parameters = template.getParameters();
for (String key : parameters.keySet()) context.put(key, parameters.get(key));
StringWriter writer = new StringWriter();
engine.evaluate(context, writer, "mail", template.getContent());
writer.close();
return writer.toString();
} catch (ParseErrorException | MethodInvocationException | ResourceNotFoundException | IOException e) {
throw new MailException(e);
}
}
use of org.apache.velocity.exception.MethodInvocationException in project OpenOLAT by OpenOLAT.
the class VelocityHelper method merge.
/**
* @param template e.g. org/olat/demo/_content/index.html
* @param c the context
* @param theme the theme e.g. "accessibility" or "printing". may be null if the default theme ("") should be taken
* @return String the rendered template
*/
private void merge(String template, Context c, Writer wOut, String theme) {
try {
Template vtemplate = null;
if (isLogDebugEnabled())
logDebug("Merging template::" + template + " for theme::" + theme, null);
if (theme != null) {
// try the theme first, if resource not found exception, fallback to normal resource.
// e.g. try /_accessibility/index.html first, if not found, try /index.html.
// this allows for themes to only provide the delta to the default templates
// todo we could avoid those string operations, if the performance gain is measureable
int latestSlash = template.lastIndexOf('/');
StringBuilder sb = new StringBuilder(template.substring(0, latestSlash));
sb.append("/_").append(theme).append("/").append(template.substring(latestSlash + 1));
String themedTemplatePath = sb.toString();
// check cache
boolean notFound = resourcesNotFound.contains(themedTemplatePath);
if (!notFound) {
// never tried before -> try to load it
if (!ve.resourceExists(themedTemplatePath)) {
// this will happen once for each theme when a resource does not exist in its themed variant but only in the default theme.
if (!Settings.isDebuging()) {
resourcesNotFound.add(themedTemplatePath);
}
// for debugging, allow introduction of themed files without restarting the application
} else {
// template exists -> load it
vtemplate = ve.getTemplate(themedTemplatePath, VelocityModule.getInputEncoding());
}
}
// if not found, fallback to standard
if (vtemplate == null) {
vtemplate = ve.getTemplate(template, VelocityModule.getInputEncoding());
}
} else {
// no theme, load the standard template
vtemplate = ve.getTemplate(template, VelocityModule.getInputEncoding());
}
vtemplate.merge(c, wOut);
} catch (MethodInvocationException me) {
throw new OLATRuntimeException(VelocityHelper.class, "MethodInvocationException occured while merging template: methName:" + me.getMethodName() + ", refName:" + me.getReferenceName(), me.getWrappedThrowable());
} catch (Exception e) {
throw new OLATRuntimeException(VelocityHelper.class, "exception occured while merging template: " + e.getMessage(), e);
}
}
Aggregations