Search in sources :

Example 16 with ResourceNotFoundException

use of org.apache.velocity.exception.ResourceNotFoundException in project jaffa-framework by jaffa-projects.

the class FormPrintEngineVelocity method generate.

public void generate() throws FormPrintException {
    // make a Context and put data into it
    log.debug("Set Up Context");
    VelocityContext context = new VelocityContext();
    context.put("data", getDataSource());
    fmt = new FormFormatHelper();
    context.put("fmt", fmt);
    context.put("context", ContextManagerFactory.instance().getThreadContext());
    log.debug("data=" + BeanMoulder.printBean(getDataSource()));
    // Split the template file between path and name
    File templateFile = new File(getTemplateName());
    if (!templateFile.exists()) {
        String err = "Velocity Template Not Found - " + getTemplateName();
        log.error(err);
        throw new EngineProcessingException(err);
    }
    String path = templateFile.getParent();
    // (templateFile.getParent()==null?getTemplateName():getTemplateName().substring(templateFile.getParent().length()));
    String file = templateFile.getName();
    if (path != null) {
        Velocity.setProperty("file.resource.loader.path", path);
    }
    log.debug("SearchPath = " + path + ", template = " + file);
    // init the runtime engine.  Defaults are fine.
    try {
        Velocity.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem");
        Velocity.init();
        log.debug("Initialized Velocity");
    } catch (Exception e) {
        String err = "Velocity Initialization - " + e.getLocalizedMessage();
        log.error(err, e);
        throw new EngineProcessingException(err, e);
    }
    // Try and load the template
    Template template = null;
    try {
        log.debug("Velocity file.resource.loader.path property = " + Velocity.getProperty("file.resource.loader.path"));
        template = Velocity.getTemplate(file);
    } catch (ResourceNotFoundException e) {
        String err = "Error opening template - " + e.getLocalizedMessage();
        log.error(err, e);
        throw new EngineProcessingException(err, e);
    } catch (ParseErrorException e) {
        String err = "Template Parse Error - " + e.getLocalizedMessage();
        log.error(err, e);
        throw new EngineProcessingException(err, e);
    } catch (Exception e) {
        String err = "Template Load Error - " + e.getLocalizedMessage();
        log.error(err, e);
        throw new EngineProcessingException(err, e);
    }
    try {
        m_output = new StringWriter();
        // render a template
        template.merge(context, m_output);
        // log.debug("Generated outbut based on template...\n" + m_output.getBuffer().toString());
        m_processed = true;
    } catch (Exception e) {
        String err = "Velocity Error - " + e.getLocalizedMessage();
        log.error(err, e);
        throw new EngineProcessingException(err, e);
    }
}
Also used : StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) SplitString(org.jaffa.util.SplitString) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) EngineProcessingException(org.jaffa.modules.printing.services.exceptions.EngineProcessingException) FormPrintException(org.jaffa.modules.printing.services.exceptions.FormPrintException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) EngineProcessingException(org.jaffa.modules.printing.services.exceptions.EngineProcessingException) Template(org.apache.velocity.Template)

Example 17 with ResourceNotFoundException

use of org.apache.velocity.exception.ResourceNotFoundException in project carbon-apimgt by wso2.

the class GatewaySourceGeneratorImpl method getEndpointConfigStringFromTemplate.

@Override
public String getEndpointConfigStringFromTemplate(Endpoint endpoint) throws APITemplateException {
    StringWriter writer = new StringWriter();
    String templatePath = "resources" + File.separator + "template" + File.separator + "endpoint.xml";
    try {
        // build the context for template and apply the necessary decorators
        ConfigContext configcontext = new EndpointContext(endpoint, packageName);
        VelocityContext context = configcontext.getContext();
        VelocityEngine velocityengine = new VelocityEngine();
        velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityengine.init();
        Template template = velocityengine.getTemplate(templatePath);
        template.merge(context, writer);
    } catch (ResourceNotFoundException e) {
        log.error("Template " + templatePath + " not Found", e);
        throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
    } catch (ParseErrorException e) {
        log.error("Syntax error in " + templatePath, e);
        throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) EndpointContext(org.wso2.carbon.apimgt.core.template.EndpointContext) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) APITemplateException(org.wso2.carbon.apimgt.core.template.APITemplateException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) CompositeAPIConfigContext(org.wso2.carbon.apimgt.core.template.CompositeAPIConfigContext) ConfigContext(org.wso2.carbon.apimgt.core.template.ConfigContext) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext) ResourceConfigContext(org.wso2.carbon.apimgt.core.template.ResourceConfigContext) Template(org.apache.velocity.Template)

Example 18 with ResourceNotFoundException

use of org.apache.velocity.exception.ResourceNotFoundException in project BIMserver by opensourceBIM.

the class TemplateEngine method process.

public String process(Map<String, Object> context, TemplateIdentifier templateIdentifier) {
    StringWriter stringWriter = new StringWriter();
    VelocityContext velocityContext = new VelocityContext(context);
    try {
        velocityEngine.mergeTemplate(templateIdentifier.getFileName(), "UTF-8", velocityContext, stringWriter);
        return stringWriter.toString();
    } catch (ResourceNotFoundException e) {
        LOGGER.error("", e);
    } catch (ParseErrorException e) {
        LOGGER.error("", e);
    } catch (MethodInvocationException e) {
        LOGGER.error("", e);
    } catch (Exception e) {
        LOGGER.error("", e);
    }
    return "";
}
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) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 19 with ResourceNotFoundException

use of org.apache.velocity.exception.ResourceNotFoundException in project OpenOLAT by OpenOLAT.

the class VelocityTemplatesPreWarm method run.

@Override
public void run() {
    long start = System.nanoTime();
    log.info("Start filling the velocity template cache");
    final VelocityContext context = new VelocityContext();
    final AtomicInteger numOfTemplates = new AtomicInteger(0);
    final File root = new File(WebappHelper.getContextRoot(), "WEB-INF/classes");
    final Path fPath = root.toPath();
    try {
        if (Files.exists(fPath)) {
            Files.walkFileTree(fPath, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                    try {
                        String path = fPath.relativize(file).toString();
                        if (path.endsWith(".html") && path.contains("/_content/")) {
                            StringOutput writer = new StringOutput();
                            VelocityHelper.getInstance().mergeContent(path, context, writer, null);
                            numOfTemplates.incrementAndGet();
                        }
                    } catch (ResourceNotFoundException e) {
                        log.error("", e);
                    } catch (ParseErrorException e) {
                        log.error("", e);
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    } catch (IOException e) {
        log.error("", e);
    }
    log.info("Velocity cache filled with " + numOfTemplates + " templates in (ms): " + CodeHelper.nanoToMilliTime(start));
}
Also used : Path(java.nio.file.Path) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) FileVisitResult(java.nio.file.FileVisitResult) StringOutput(org.olat.core.gui.render.StringOutput) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 20 with ResourceNotFoundException

use of org.apache.velocity.exception.ResourceNotFoundException 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)

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