Search in sources :

Example 1 with VelocityProperties

use of com.github.bordertech.wcomponents.velocity.VelocityProperties in project wcomponents by BorderTech.

the class VelocityRenderer method fillContext.

/**
 * Fills the given velocity context with data from the component which is being rendered. A map of components is
 * also built up, in order to support deferred rendering.
 *
 * @param component the current component being rendered.
 * @param context the velocity context to modify.
 * @param componentsByKey a map to store components for deferred rendering.
 */
private void fillContext(final WComponent component, final VelocityContext context, final Map<String, WComponent> componentsByKey) {
    // Also make the component available under the "this" key.
    context.put("this", component);
    // Make the UIContext available under the "uicontext" key.
    UIContext uic = UIContextHolder.getCurrent();
    context.put("uicontext", uic);
    context.put("uic", uic);
    if (component instanceof VelocityProperties) {
        Map<?, ?> map = ((VelocityProperties) component).getVelocityMap();
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            String key = (String) entry.getKey();
            Object value = entry.getValue();
            context.put(key, value);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Handling children");
    }
    // As well as going into their own named slots, visible children are also
    // placed into a list called children
    ArrayList<String> children = new ArrayList<>();
    if (component instanceof Container) {
        Container container = (Container) component;
        for (int i = 0; i < container.getChildCount(); i++) {
            WComponent child = container.getChildAt(i);
            String tag = child.getTag();
            if (tag != null || child.isVisible()) {
                // The key needs to be something which would never be output by a Velocity template.
                String key = "<VelocityLayout" + child.getId() + "/>";
                componentsByKey.put(key, child);
                if (tag != null) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Adding child " + tag + " to context");
                    }
                    addToContext(context, tag, key);
                }
                if (child.isVisible()) {
                    children.add(key);
                }
            }
        }
        context.put("children", children);
    }
    // Put the context in the context
    context.put("context", context);
}
Also used : AbstractWComponent(com.github.bordertech.wcomponents.AbstractWComponent) WComponent(com.github.bordertech.wcomponents.WComponent) Container(com.github.bordertech.wcomponents.Container) UIContext(com.github.bordertech.wcomponents.UIContext) ArrayList(java.util.ArrayList) VelocityProperties(com.github.bordertech.wcomponents.velocity.VelocityProperties) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with VelocityProperties

use of com.github.bordertech.wcomponents.velocity.VelocityProperties 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)

Aggregations

AbstractWComponent (com.github.bordertech.wcomponents.AbstractWComponent)2 WComponent (com.github.bordertech.wcomponents.WComponent)2 VelocityProperties (com.github.bordertech.wcomponents.velocity.VelocityProperties)2 HashMap (java.util.HashMap)2 Container (com.github.bordertech.wcomponents.Container)1 UIContext (com.github.bordertech.wcomponents.UIContext)1 WTemplate (com.github.bordertech.wcomponents.WTemplate)1 SystemException (com.github.bordertech.wcomponents.util.SystemException)1 VelocityWriter (com.github.bordertech.wcomponents.velocity.VelocityWriter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Template (org.apache.velocity.Template)1 VelocityContext (org.apache.velocity.VelocityContext)1 VelocityEngine (org.apache.velocity.app.VelocityEngine)1 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)1 ParseErrorException (org.apache.velocity.exception.ParseErrorException)1 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)1