Search in sources :

Example 51 with Template

use of org.apache.velocity.Template in project cayenne by apache.

the class ClassGenerationAction method getTemplate.

protected Template getTemplate(TemplateType type) {
    String templateName = customTemplateName(type);
    if (templateName == null) {
        templateName = defaultTemplateName(type);
    }
    // Velocity < 1.5 has some memory problems, so we will create a VelocityEngine every time,
    // and store templates in an internal cache, to avoid uncontrolled memory leaks...
    // Presumably 1.5 fixes it.
    Template template = templateCache.get(templateName);
    if (template == null) {
        Properties props = new Properties();
        props.put("resource.loader", "cayenne");
        props.put("cayenne.resource.loader.class", ClassGeneratorResourceLoader.class.getName());
        props.put("cayenne.resource.loader.cache", "false");
        VelocityEngine velocityEngine = new VelocityEngine();
        velocityEngine.init(props);
        template = velocityEngine.getTemplate(templateName);
        templateCache.put(templateName, template);
    }
    return template;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) Properties(java.util.Properties) Template(org.apache.velocity.Template)

Example 52 with Template

use of org.apache.velocity.Template 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 53 with Template

use of org.apache.velocity.Template in project cayenne by apache.

the class SQLTemplateResourceManager method getResource.

/**
 * Returns a Velocity Resource which is a Template for the given SQL.
 */
public Resource getResource(String resourceName, int resourceType, String encoding) throws ResourceNotFoundException, ParseErrorException {
    synchronized (templateCache) {
        Template resource = templateCache.get(resourceName);
        if (resource == null) {
            resource = new Template();
            resource.setRuntimeServices(rsvc);
            resource.setResourceLoader(this);
            resource.setName(resourceName);
            resource.setEncoding(encoding);
            resource.process();
            templateCache.put(resourceName, resource);
        }
        return resource;
    }
}
Also used : Template(org.apache.velocity.Template)

Example 54 with Template

use of org.apache.velocity.Template in project mybatis-generator-gui-extension by spawpaw.

the class SCVXGeneratorPlugin method renderTemplateAsString.

public String renderTemplateAsString(String templateFile, VelocityContext ctx) {
    VelocityEngine ve = new VelocityEngine();
    ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    ve.init();
    Template t = ve.getTemplate("template/" + templateFile);
    StringWriter sw = new StringWriter();
    t.merge(ctx, sw);
    return sw.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) Template(org.apache.velocity.Template)

Example 55 with Template

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

Aggregations

Template (org.apache.velocity.Template)59 VelocityContext (org.apache.velocity.VelocityContext)41 StringWriter (java.io.StringWriter)28 VelocityEngine (org.apache.velocity.app.VelocityEngine)21 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)12 IOException (java.io.IOException)11 ParseErrorException (org.apache.velocity.exception.ParseErrorException)8 Writer (java.io.Writer)7 File (java.io.File)6 Map (java.util.Map)6 Properties (java.util.Properties)6 FileWriter (java.io.FileWriter)5 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)5 PrintWriter (java.io.PrintWriter)4 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)4 SystemException (com.github.bordertech.wcomponents.util.SystemException)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 FileOutputStream (java.io.FileOutputStream)3 HashMap (java.util.HashMap)3 UIContext (com.github.bordertech.wcomponents.UIContext)2