Search in sources :

Example 11 with MethodInvocationException

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();
}
Also used : StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) TemplatingException(com.outjected.email.api.TemplatingException)

Example 12 with MethodInvocationException

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);
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) SystemException(com.github.bordertech.wcomponents.util.SystemException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template) WTemplate(com.github.bordertech.wcomponents.WTemplate) AbstractWComponent(com.github.bordertech.wcomponents.AbstractWComponent) AbstractWComponent(com.github.bordertech.wcomponents.AbstractWComponent) WComponent(com.github.bordertech.wcomponents.WComponent) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) VelocityProperties(com.github.bordertech.wcomponents.velocity.VelocityProperties) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) VelocityWriter(com.github.bordertech.wcomponents.velocity.VelocityWriter)

Example 13 with MethodInvocationException

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);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template) FileOutputStream(java.io.FileOutputStream) MojoLogChute(sh.tak.appbundler.logging.MojoLogChute) OutputStreamWriter(java.io.OutputStreamWriter) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Example 14 with MethodInvocationException

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;
}
Also used : NumberTool(org.apache.velocity.tools.generic.NumberTool) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) DecimalFormat(java.text.DecimalFormat) FileOutputStream(java.io.FileOutputStream) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) IOException(java.io.IOException) Properties(java.util.Properties) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Example 15 with MethodInvocationException

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) Resource(org.apache.maven.model.Resource) RemoteResourcesBundleXpp3Reader(org.apache.maven.plugin.resources.remote.io.xpp3.RemoteResourcesBundleXpp3Reader) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) SupplementalDataModelXpp3Reader(org.apache.maven.plugin.resources.remote.io.xpp3.SupplementalDataModelXpp3Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) FileReader(java.io.FileReader) FileInputStream(java.io.FileInputStream) MavenFileFilterRequest(org.apache.maven.shared.filtering.MavenFileFilterRequest) OutputStreamWriter(java.io.OutputStreamWriter) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter)

Aggregations

MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)18 ParseErrorException (org.apache.velocity.exception.ParseErrorException)13 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)13 IOException (java.io.IOException)9 Template (org.apache.velocity.Template)8 VelocityContext (org.apache.velocity.VelocityContext)8 StringWriter (java.io.StringWriter)7 FileOutputStream (java.io.FileOutputStream)3 SystemException (com.github.bordertech.wcomponents.util.SystemException)2 File (java.io.File)2 FileWriter (java.io.FileWriter)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 Writer (java.io.Writer)2 DecimalFormat (java.text.DecimalFormat)2 Properties (java.util.Properties)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 NumberTool (org.apache.velocity.tools.generic.NumberTool)2 AbstractWComponent (com.github.bordertech.wcomponents.AbstractWComponent)1 WComponent (com.github.bordertech.wcomponents.WComponent)1