Search in sources :

Example 1 with ResourceNotFoundException

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

the class SimpleVelocity method process.

/** Process a template */
public static void process(String base, String path, Writer out, VelocityContext context) {
    VelocityEngine velocity = new VelocityEngine();
    // Turn off logging - catch exceptions and log ourselves
    velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, velocityLogChute);
    velocity.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
    velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, base);
    velocity.init();
    try {
        Template temp = velocity.getTemplate(path);
        temp.merge(context, out);
        out.flush();
    } catch (ResourceNotFoundException ex) {
        velocityLog.error("Resource not found: " + ex.getMessage());
    } catch (ParseErrorException ex) {
        velocityLog.error("Parse error (" + path + ") : " + ex.getMessage());
    } catch (MethodInvocationException ex) {
        velocityLog.error("Method invocation exception (" + path + ") : " + ex.getMessage());
    } catch (IOException ex) {
        velocityLog.warn("IOException", ex);
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Example 2 with ResourceNotFoundException

use of org.apache.velocity.exception.ResourceNotFoundException in project traccar by tananaev.

the class NotificationFormatter method getTemplate.

public static Template getTemplate(Event event, String path) {
    String templateFilePath;
    Template template;
    try {
        templateFilePath = Paths.get(path, event.getType() + ".vm").toString();
        template = Context.getVelocityEngine().getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
    } catch (ResourceNotFoundException error) {
        Log.warning(error);
        templateFilePath = Paths.get(path, "unknown.vm").toString();
        template = Context.getVelocityEngine().getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
    }
    return template;
}
Also used : ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Example 3 with ResourceNotFoundException

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

the class GatewaySourceGeneratorImpl method getConfigStringFromTemplate.

@Override
public String getConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources) throws APITemplateException {
    StringWriter writer = new StringWriter();
    String templatePath = "resources" + File.separator + "template" + File.separator + "template.xml";
    try {
        // build the context for template and apply the necessary decorators
        apiConfigContext.validate();
        ConfigContext configContext = new ResourceConfigContext(apiConfigContext, apiResources);
        VelocityContext context = configContext.getContext();
        VelocityEngine velocityengine = new VelocityEngine();
        velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
        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) CommonsLogLogChute(org.apache.velocity.runtime.log.CommonsLogLogChute) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ResourceConfigContext(org.wso2.carbon.apimgt.core.template.ResourceConfigContext) 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 4 with ResourceNotFoundException

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

the class GatewaySourceGeneratorImpl method getCompositeAPIConfigStringFromTemplate.

@Override
public String getCompositeAPIConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources, List<CompositeAPIEndpointDTO> compositeApiEndpoints) throws APITemplateException {
    StringWriter writer = new StringWriter();
    String templatePath = "resources" + File.separator + "template" + File.separator + "composite_template.xml";
    try {
        // build the context for template and apply the necessary decorators
        apiConfigContext.validate();
        CompositeAPIConfigContext configContext = new CompositeAPIConfigContext(apiConfigContext, apiResources, compositeApiEndpoints);
        VelocityContext context = configContext.getContext();
        VelocityEngine velocityengine = new VelocityEngine();
        velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
        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) CommonsLogLogChute(org.apache.velocity.runtime.log.CommonsLogLogChute) StringWriter(java.io.StringWriter) 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) Template(org.apache.velocity.Template)

Example 5 with ResourceNotFoundException

use of org.apache.velocity.exception.ResourceNotFoundException in project dbflute-core by dbflute.

the class DfAbstractTexenTask method fireVelocityProcess.

protected void fireVelocityProcess() {
    assertBasicAntParameter();
    // set up the encoding of templates from DBFlute property
    setInputEncoding(getBasicProperties().getTemplateFileEncoding());
    setOutputEncoding(getBasicProperties().getSourceFileEncoding());
    try {
        initializeGeneratorInstance();
        final DfGenerator generator = setupGenerator();
        final Context ctx = setupControlContext();
        _log.info("generator.parse(\"" + controlTemplate + "\", ctx);");
        generator.parse(controlTemplate, ctx);
        generator.shutdown();
        cleanup();
    } catch (BuildException e) {
        throw e;
    } catch (MethodInvocationException e) {
        final String method = e.getReferenceName() + "." + e.getMethodName() + "()";
        String msg = "Exception thrown by " + method + ": control=" + controlTemplate;
        throw new IllegalStateException(msg, e.getWrappedThrowable());
    } catch (ParseErrorException e) {
        throw new IllegalStateException("Velocity syntax error: control=" + controlTemplate, e);
    } catch (ResourceNotFoundException e) {
        throw new IllegalStateException("Resource not found: control=" + controlTemplate, e);
    } catch (Exception e) {
        throw new IllegalStateException("Generation failed: control=" + controlTemplate, e);
    }
}
Also used : Context(org.apache.velocity.context.Context) ParseErrorException(org.apache.velocity.exception.ParseErrorException) BuildException(org.apache.tools.ant.BuildException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) DfGenerator(org.dbflute.friends.velocity.DfGenerator) SQLException(java.sql.SQLException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Aggregations

ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)32 ParseErrorException (org.apache.velocity.exception.ParseErrorException)21 Template (org.apache.velocity.Template)17 VelocityContext (org.apache.velocity.VelocityContext)17 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