Search in sources :

Example 41 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine 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 42 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project wcomponents by BorderTech.

the class VelocityEngineFactory method getVelocityEngine.

/**
 * <p>
 * Returns the VelocityEngine associated with this factory. If this is the first time we are using the engine,
 * create it and initialise it.</p>
 *
 * <p>
 * Note that velocity engines are hugely resource intensive, so we don't want too many of them. For the time being
 * we have a single instance stored as a static variable. This would only be a problem if the VelocityLayout class
 * ever wanted to use different engine configurations (unlikely).</p>
 *
 * @return the VelocityEngine associated with this factory.
 */
public static synchronized VelocityEngine getVelocityEngine() {
    if (engine == null) {
        String fileTemplates = ConfigurationProperties.getVelocityFileTemplates();
        boolean cacheTemplates = ConfigurationProperties.getVelocityCacheTemplates();
        VelocityEngine newEngine = new VelocityEngine();
        Properties props = new Properties();
        // "source mode" or not
        if (fileTemplates != null && !"".equals(fileTemplates)) {
            // Source mode
            LOG.info("Velocity engine running in source mode from " + fileTemplates);
            props.setProperty("resource.loader", "file,class");
            props.setProperty("file.resource.loader.path", fileTemplates);
            props.setProperty("file.resource.loader.cache", "false");
            props.setProperty("file.resource.loader.modificationCheckInterval", "2");
            props.setProperty("class.resource.loader.cache", "false");
            props.setProperty("class.resource.loader.modificationCheckInterval", "2");
            props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        } else {
            String cache = String.valueOf(cacheTemplates);
            props.setProperty("class.resource.loader.cache", cache);
            props.setProperty("resource.loader", "class");
            props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        }
        // Setup commons logging for velocity
        props.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "com.github.bordertech.wcomponents.velocity.VelocityLogger");
        // Set up access to the common velocity macros.
        props.setProperty(RuntimeConstants.VM_LIBRARY, ConfigurationProperties.getVelocityMacroLibrary());
        try {
            if (LOG.isInfoEnabled()) {
                // Dump properties
                StringWriter writer = new StringWriter();
                props.list(new PrintWriter(writer));
                LOG.info("Configuring velocity with the following properties...\n" + writer);
            }
            newEngine.init(props);
        } catch (Exception ex) {
            throw new SystemException("Failed to configure VelocityEngine", ex);
        }
        engine = newEngine;
    }
    return engine;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) SystemException(com.github.bordertech.wcomponents.util.SystemException) Properties(java.util.Properties) ConfigurationProperties(com.github.bordertech.wcomponents.util.ConfigurationProperties) SystemException(com.github.bordertech.wcomponents.util.SystemException) PrintWriter(java.io.PrintWriter)

Example 43 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine 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 44 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project traccar by tananaev.

the class Context method initEventsModule.

private static void initEventsModule() {
    geofenceManager = new GeofenceManager(dataManager);
    calendarManager = new CalendarManager(dataManager);
    notificationManager = new NotificationManager(dataManager);
    Properties velocityProperties = new Properties();
    velocityProperties.setProperty("file.resource.loader.path", Context.getConfig().getString("templates.rootPath", "templates") + "/");
    velocityProperties.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogChute");
    String address;
    try {
        address = config.getString("web.address", InetAddress.getLocalHost().getHostAddress());
    } catch (UnknownHostException e) {
        address = "localhost";
    }
    String webUrl = URIUtil.newURI("http", address, config.getInteger("web.port", 8082), "", "");
    webUrl = Context.getConfig().getString("web.url", webUrl);
    velocityProperties.setProperty("web.url", webUrl);
    velocityEngine = new VelocityEngine();
    velocityEngine.init(velocityProperties);
    motionEventHandler = new MotionEventHandler(tripsConfig);
    overspeedEventHandler = new OverspeedEventHandler(Context.getConfig().getLong("event.overspeed.minimalDuration") * 1000, Context.getConfig().getBoolean("event.overspeed.notRepeat"));
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) CalendarManager(org.traccar.database.CalendarManager) NotificationManager(org.traccar.database.NotificationManager) UnknownHostException(java.net.UnknownHostException) GeofenceManager(org.traccar.database.GeofenceManager) MotionEventHandler(org.traccar.events.MotionEventHandler) OverspeedEventHandler(org.traccar.events.OverspeedEventHandler) Properties(java.util.Properties)

Example 45 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine 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

VelocityEngine (org.apache.velocity.app.VelocityEngine)49 VelocityContext (org.apache.velocity.VelocityContext)23 StringWriter (java.io.StringWriter)21 Template (org.apache.velocity.Template)21 Properties (java.util.Properties)15 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)14 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)9 IOException (java.io.IOException)8 ParseErrorException (org.apache.velocity.exception.ParseErrorException)7 File (java.io.File)4 Writer (java.io.Writer)4 CommonsLogLogChute (org.apache.velocity.runtime.log.CommonsLogLogChute)4 SystemException (com.github.bordertech.wcomponents.util.SystemException)3 CopyrightManager (com.intellij.copyright.CopyrightManager)3 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 HashMap (java.util.HashMap)3 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2