Search in sources :

Example 6 with ResourceNotFoundException

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());
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileOutputStream(java.io.FileOutputStream) VelocityException(org.apache.velocity.exception.VelocityException) OutputStreamWriter(java.io.OutputStreamWriter) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) VelocityException(org.apache.velocity.exception.VelocityException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Example 7 with ResourceNotFoundException

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);
    }
}
Also used : Context(org.apache.velocity.context.Context) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) VelocityException(org.apache.velocity.exception.VelocityException) ToolManager(org.apache.velocity.tools.ToolManager) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 8 with ResourceNotFoundException

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);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 9 with ResourceNotFoundException

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);
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Context(org.apache.velocity.context.Context) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) VelocityContext(org.apache.velocity.VelocityContext) StringUtils(org.codehaus.plexus.util.StringUtils) VelocityException(org.apache.velocity.exception.VelocityException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 10 with ResourceNotFoundException

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);
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) PrintWriter(java.io.PrintWriter) Template(org.apache.velocity.Template)

Aggregations

ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)31 ParseErrorException (org.apache.velocity.exception.ParseErrorException)21 VelocityContext (org.apache.velocity.VelocityContext)17 Template (org.apache.velocity.Template)16 IOException (java.io.IOException)15 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)14 StringWriter (java.io.StringWriter)11 File (java.io.File)7 VelocityEngine (org.apache.velocity.app.VelocityEngine)7 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 FileOutputStream (java.io.FileOutputStream)4 FileWriter (java.io.FileWriter)4 Writer (java.io.Writer)4 SystemException (com.github.bordertech.wcomponents.util.SystemException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 PrintWriter (java.io.PrintWriter)3 Context (org.apache.velocity.context.Context)3 VelocityException (org.apache.velocity.exception.VelocityException)3 WComponent (com.github.bordertech.wcomponents.WComponent)2 FileInputStream (java.io.FileInputStream)2