use of org.apache.velocity.exception.MethodInvocationException 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();
}
use of org.apache.velocity.exception.MethodInvocationException in project wcomponents by BorderTech.
the class VelocityRenderer method paintXml.
/**
* Paints the component in XML using the Velocity Template.
*
* @param component the component to paint.
* @param writer the writer to send the HTML output to.
*/
public void paintXml(final WComponent component, final Writer writer) {
if (LOG.isDebugEnabled()) {
LOG.debug("paintXml called for component class " + component.getClass());
}
String templateText = null;
if (component instanceof AbstractWComponent) {
AbstractWComponent abstractComp = ((AbstractWComponent) component);
templateText = abstractComp.getTemplateMarkUp();
}
try {
Map<String, WComponent> componentsByKey = new HashMap<>();
VelocityContext context = new VelocityContext();
fillContext(component, context, componentsByKey);
VelocityWriter velocityWriter = new VelocityWriter(writer, componentsByKey, UIContextHolder.getCurrent());
if (templateText != null) {
VelocityEngine engine = VelocityEngineFactory.getVelocityEngine();
engine.evaluate(context, velocityWriter, component.getClass().getSimpleName(), templateText);
} else {
Template template = getTemplate(component);
if (template == null) {
LOG.warn("VelocityRenderer invoked for a component with no template: " + component.getClass().getName());
} else {
template.merge(context, velocityWriter);
}
}
velocityWriter.close();
if (component instanceof VelocityProperties) {
((VelocityProperties) component).mapUsed();
}
} catch (ResourceNotFoundException rnfe) {
LOG.error("Could not find template '" + url + "' for component " + component.getClass().getName(), rnfe);
} catch (ParseErrorException pee) {
// syntax error : problem parsing the template
LOG.error("Parse problems", pee);
} catch (MethodInvocationException mie) {
// something invoked in the template
// threw an exception
Throwable wrapped = mie.getWrappedThrowable();
LOG.error("Problems with velocity", mie);
if (wrapped != null) {
LOG.error("Wrapped exception...", wrapped);
}
} catch (Exception e) {
LOG.error("Problems with velocity", e);
}
}
use of org.apache.velocity.exception.MethodInvocationException in project appbundle-maven-plugin by federkasten.
the class CreateApplicationBundleMojo method writeInfoPlist.
/**
* Writes an Info.plist file describing this bundle.
*
* @param infoPlist The file to write Info.plist contents to
* @param files A list of file names of the jar files to add in $JAVAROOT
* @throws MojoExecutionException
*/
private void writeInfoPlist(File infoPlist, List<String> files) throws MojoExecutionException {
Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new MojoLogChute(this));
Velocity.setProperty("file.resource.loader.path", TARGET_CLASS_ROOT);
try {
Velocity.init();
} catch (Exception ex) {
throw new MojoExecutionException("Exception occured in initializing velocity", ex);
}
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("mainClass", mainClass);
velocityContext.put("cfBundleExecutable", javaLauncherName);
velocityContext.put("bundleName", cleanBundleName(bundleName));
velocityContext.put("workingDirectory", workingDirectory);
if (embeddJre && jrePath != null) {
velocityContext.put("jrePath", "JRE");
velocityContext.put("jreFullPath", "");
} else if (embeddJre && jreFullPath != null) {
velocityContext.put("jrePath", "");
velocityContext.put("jreFullPath", jreFullPath);
} else {
velocityContext.put("jrePath", "");
velocityContext.put("jreFullPath", "");
}
if (iconFile == null) {
velocityContext.put("iconFile", "GenericJavaApp.icns");
} else {
File f = searchFile(iconFile, project.getBasedir());
velocityContext.put("iconFile", (f != null && f.exists() && f.isFile()) ? f.getName() : "GenericJavaApp.icns");
}
velocityContext.put("version", version);
velocityContext.put("jvmVersion", jvmVersion);
StringBuilder options = new StringBuilder();
options.append("<array>").append("\n ");
for (String jvmOption : defaultJvmOptions) {
options.append(" ").append("<string>").append(jvmOption).append("</string>").append("\n");
}
options.append(" ").append("<string>").append("-Xdock:name=" + bundleName).append("</string>").append("\n");
if (jvmOptions != null) {
for (String jvmOption : jvmOptions) {
options.append(" ").append("<string>").append(jvmOption).append("</string>").append("\n");
}
}
options.append(" ").append("</array>");
velocityContext.put("jvmOptions", options);
StringBuilder jarFiles = new StringBuilder();
jarFiles.append("<array>").append("\n");
for (String file : files) {
jarFiles.append(" ").append("<string>").append(file).append("</string>").append("\n");
}
if (additionalClasspath != null) {
for (String pathElement : additionalClasspath) {
jarFiles.append(" ").append("<string>").append(pathElement).append("</string>");
}
}
jarFiles.append(" ").append("</array>");
velocityContext.put("classpath", jarFiles.toString());
try {
File sourceInfoPlist = new File(TARGET_CLASS_ROOT, dictionaryFile);
if (sourceInfoPlist.exists() && sourceInfoPlist.isFile()) {
String encoding = detectEncoding(sourceInfoPlist);
getLog().debug("Detected encoding " + encoding + " for dictionary file " + dictionaryFile);
Writer writer = new OutputStreamWriter(new FileOutputStream(infoPlist), encoding);
Template template = Velocity.getTemplate(dictionaryFile, encoding);
template.merge(velocityContext, writer);
writer.close();
} else {
Writer writer = new OutputStreamWriter(new FileOutputStream(infoPlist), "UTF-8");
velocity.getEngine().mergeTemplate(dictionaryFile, "UTF-8", velocityContext, writer);
writer.close();
}
} catch (IOException ex) {
throw new MojoExecutionException("Could not write Info.plist to file " + infoPlist, ex);
} catch (ParseErrorException ex) {
throw new MojoExecutionException("Error parsing " + dictionaryFile, ex);
} catch (ResourceNotFoundException ex) {
throw new MojoExecutionException("Could not find resource for template " + dictionaryFile, ex);
} catch (MethodInvocationException ex) {
throw new MojoExecutionException("MethodInvocationException occured merging Info.plist template " + dictionaryFile, ex);
} catch (Exception ex) {
throw new MojoExecutionException("Exception occured merging Info.plist template " + dictionaryFile, ex);
}
}
use of org.apache.velocity.exception.MethodInvocationException in project new-cloud by xie-summer.
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.MethodInvocationException in project maven-plugins by apache.
the class ProcessRemoteResourcesMojo method copyResourceIfExists.
protected boolean copyResourceIfExists(File file, String relFileName, VelocityContext context) throws IOException, MojoExecutionException {
for (Resource resource : resources) {
File resourceDirectory = new File(resource.getDirectory());
if (!resourceDirectory.exists()) {
continue;
}
// TODO - really should use the resource includes/excludes and name mapping
File source = new File(resourceDirectory, relFileName);
File templateSource = new File(resourceDirectory, relFileName + TEMPLATE_SUFFIX);
if (!source.exists() && templateSource.exists()) {
source = templateSource;
}
if (source.exists() && !source.equals(file)) {
if (source == templateSource) {
Reader reader = null;
Writer writer = null;
DeferredFileOutputStream os = new DeferredFileOutputStream(velocityFilterInMemoryThreshold, file);
try {
if (encoding != null) {
reader = new InputStreamReader(new FileInputStream(source), encoding);
writer = new OutputStreamWriter(os, encoding);
} else {
reader = ReaderFactory.newPlatformReader(source);
writer = WriterFactory.newPlatformWriter(os);
}
velocity.evaluate(context, writer, "", reader);
writer.close();
writer = null;
reader.close();
reader = null;
} catch (ParseErrorException e) {
throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
} catch (MethodInvocationException e) {
throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
} catch (ResourceNotFoundException e) {
throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
} finally {
IOUtil.close(writer);
IOUtil.close(reader);
}
fileWriteIfDiffers(os);
} else if (resource.isFiltering()) {
MavenFileFilterRequest req = setupRequest(resource, source, file);
try {
fileFilter.copyFile(req);
} catch (MavenFilteringException e) {
throw new MojoExecutionException("Error filtering resource: " + source, e);
}
} else {
FileUtils.copyFile(source, file);
}
// exclude the original (so eclipse doesn't complain about duplicate resources)
resource.addExclude(relFileName);
return true;
}
}
return false;
}
Aggregations