use of org.apache.velocity.exception.ResourceNotFoundException 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;
}
use of org.apache.velocity.exception.ResourceNotFoundException 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.ResourceNotFoundException in project jop by jop-devel.
the class Report method generateFile.
private void generateFile(String templateName, File outFile, VelocityContext ctx) throws Exception {
Template template;
try {
template = Velocity.getTemplate(templateName);
} catch (ResourceNotFoundException ignored) {
template = Velocity.getTemplate("com/jopdesign/wcet/report/" + templateName);
}
FileWriter fw = new FileWriter(outFile);
template.merge(ctx, fw);
fw.close();
}
use of org.apache.velocity.exception.ResourceNotFoundException 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);
}
}
use of org.apache.velocity.exception.ResourceNotFoundException in project cayenne by apache.
the class ClassGeneratorResourceLoader method getResourceReader.
/**
* Returns resource as InputStream. Searches resource in a following places:
* <ol>
* <li>thread class loader</li>
* <li>this class's class loader</li>
* <li>tries a path relative to a <i>root</i> path, if set</li>
* <li>an absolute path</li>
* </ol>
*/
@Override
public synchronized Reader getResourceReader(String name, String charset) throws ResourceNotFoundException {
Reader stream;
stream = loadFromThreadClassLoader(name);
if (stream != null) {
return stream;
}
stream = loadFromThisClassLoader(name);
if (stream != null) {
return stream;
}
stream = loadFromRelativePath(name);
if (stream != null) {
return stream;
}
stream = loadFromAbsPath(name);
if (stream != null) {
return stream;
}
throw new ResourceNotFoundException("Couldn't find resource '" + name + "'. Searched filesystem path and classpath");
}
Aggregations