use of org.apache.velocity.exception.ResourceNotFoundException in project maven-plugins by apache.
the class AnnouncementMojo method processTemplate.
/**
* Create the velocity template
*
* @param context velocity context that has the parameter values
* @param outputDirectory directory where the file will be generated
* @param template velocity template which will the context be merged
* @param announcementFile The file name of the generated announcement
* @throws VelocityException in case of errors.
* @throws MojoExecutionException in case of errors.
*/
public void processTemplate(Context context, File outputDirectory, String template, String announcementFile) throws VelocityException, MojoExecutionException {
File f;
// Use the name of the template as a default value
if (StringUtils.isEmpty(announcementFile)) {
announcementFile = template;
}
try {
f = new File(outputDirectory, announcementFile);
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
VelocityEngine engine = velocity.getEngine();
engine.setApplicationAttribute("baseDirectory", basedir);
if (StringUtils.isEmpty(templateEncoding)) {
templateEncoding = ReaderFactory.FILE_ENCODING;
getLog().warn("File encoding has not been set, using platform encoding " + templateEncoding + ", i.e. build is platform dependent!");
}
Writer writer = new OutputStreamWriter(new FileOutputStream(f), templateEncoding);
Template velocityTemplate = engine.getTemplate(templateDirectory + "/" + template, templateEncoding);
velocityTemplate.merge(context, writer);
writer.flush();
writer.close();
getLog().info("Created template " + f);
} catch (ResourceNotFoundException rnfe) {
throw new ResourceNotFoundException("Template not found. ( " + templateDirectory + "/" + template + " )");
} catch (VelocityException ve) {
throw new VelocityException(ve.toString());
} catch (Exception e) {
if (e.getCause() != null) {
getLog().warn(e.getCause());
}
throw new MojoExecutionException(e.toString(), e.getCause());
}
}
use of org.apache.velocity.exception.ResourceNotFoundException in project maven-plugins by apache.
the class AnnouncementMojo method doGenerate.
protected void doGenerate(List<Release> releases, Release release) throws MojoExecutionException {
try {
ToolManager toolManager = new ToolManager(true);
Context context = toolManager.createContext();
if (getIntroduction() == null || getIntroduction().equals("")) {
setIntroduction(getUrl());
}
context.put("releases", releases);
context.put("groupId", getGroupId());
context.put("artifactId", getArtifactId());
context.put("version", getVersion());
context.put("packaging", getPackaging());
context.put("url", getUrl());
context.put("release", release);
context.put("introduction", getIntroduction());
context.put("developmentTeam", getDevelopmentTeam());
context.put("finalName", getFinalName());
context.put("urlDownload", getUrlDownload());
context.put("project", project);
if (announceParameters == null) {
// empty Map to prevent NPE in velocity execution
context.put("announceParameters", Collections.emptyMap());
} else {
context.put("announceParameters", announceParameters);
}
processTemplate(context, announcementDirectory, template, announcementFile);
} catch (ResourceNotFoundException rnfe) {
throw new MojoExecutionException("Resource not found.", rnfe);
} catch (VelocityException ve) {
throw new MojoExecutionException(ve.toString(), ve);
}
}
use of org.apache.velocity.exception.ResourceNotFoundException in project maven-plugins by apache.
the class ProjectResourceLoader method getResourceStream.
/**
* Get an InputStream so that the Runtime can build a template with it.
*
* @param templateName name of template to get
* @return InputStream containing the template
* @throws ResourceNotFoundException if template not found in the file template path.
*/
public synchronized InputStream getResourceStream(String templateName) throws ResourceNotFoundException {
/*
* Make sure we have a valid templateName.
*/
if (templateName == null || templateName.length() == 0) {
/*
* If we don't get a properly formed templateName then there's not much we can do. So we'll forget about
* trying to search any more paths for the template.
*/
throw new ResourceNotFoundException("Need to specify a file name or file path!");
}
String template = StringUtils.normalizePath(templateName);
if (template == null || template.length() == 0) {
String msg = "Project Resource loader error : argument " + template + " contains .. and may be trying to access " + "content outside of template root. Rejected.";
rsvc.getLog().error("ProjectResourceLoader : " + msg);
throw new ResourceNotFoundException(msg);
}
/*
* if a / leads off, then just nip that :)
*/
if (template.startsWith("/")) {
template = template.substring(1);
}
// MCHANGES-118 adding the basedir path
paths.add((String) rsvc.getApplicationAttribute("baseDirectory"));
for (String path : paths) {
InputStream inputStream = findTemplate(path, template);
if (inputStream != null) {
/*
* Store the path that this template came from so that we can check its modification time.
*/
templatePaths.put(templateName, path);
return inputStream;
}
}
/*
* We have now searched all the paths for templates and we didn't find anything so throw an exception.
*/
String msg = "ProjectResourceLoader Error: cannot find resource " + template;
throw new ResourceNotFoundException(msg);
}
use of org.apache.velocity.exception.ResourceNotFoundException in project maven-plugins by apache.
the class DefaultCheckstyleRssGenerator method generateRSS.
@Override
public void generateRSS(CheckstyleResults results, CheckstyleRssGeneratorRequest checkstyleRssGeneratorRequest) throws MavenReportException {
VelocityTemplate vtemplate = new VelocityTemplate(velocityComponent, CheckstyleReport.PLUGIN_RESOURCES);
vtemplate.setLog(checkstyleRssGeneratorRequest.getLog());
Context context = new VelocityContext();
context.put("results", results);
context.put("project", checkstyleRssGeneratorRequest.getMavenProject());
context.put("copyright", checkstyleRssGeneratorRequest.getCopyright());
context.put("levelInfo", SeverityLevel.INFO);
context.put("levelWarning", SeverityLevel.WARNING);
context.put("levelError", SeverityLevel.ERROR);
context.put("stringutils", new StringUtils());
try {
vtemplate.generate(checkstyleRssGeneratorRequest.getOutputDirectory().getPath() + "/checkstyle.rss", "checkstyle-rss.vm", context);
} catch (ResourceNotFoundException e) {
throw new MavenReportException("Unable to find checkstyle-rss.vm resource.", e);
} catch (MojoExecutionException | IOException | VelocityException e) {
throw new MavenReportException("Unable to generate checkstyle.rss.", e);
}
}
use of org.apache.velocity.exception.ResourceNotFoundException 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);
}
}
Aggregations