FileDocCategorySizeDatePackage
AbstractDirective.javaAPI DocExample5584Mon Jul 23 13:26:50 BST 2007org.apache.struts2.views.velocity.components

AbstractDirective

public abstract class AbstractDirective extends org.apache.velocity.runtime.directive.Directive

Fields Summary
Constructors Summary
Methods Summary
protected java.util.MapcreatePropertyMap(org.apache.velocity.context.InternalContextAdapter contextAdapter, org.apache.velocity.runtime.parser.node.Node node)
create a Map of properties that the user has passed in. for example,
#xxx("name=hello" "value=world" "template=foo")
would yield a params that contains {["name", "hello"], ["value", "world"], ["template", "foo"]}

param
node the Node passed in to the render method
return
a Map of the user specified properties
throws
org.apache.velocity.exception.ParseErrorException if the was an error in the format of the property

        Map propertyMap = new HashMap();

        int children = node.jjtGetNumChildren();
        if (getType() == BLOCK) {
            children--;
        }

        for (int index = 0, length = children; index < length; index++) {
            this.putProperty(propertyMap, contextAdapter, node.jjtGetChild(index));
        }

        return propertyMap;
    
protected abstract org.apache.struts2.components.ComponentgetBean(com.opensymphony.xwork2.util.ValueStack stack, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

public abstract java.lang.StringgetBeanName()

public java.lang.StringgetName()

        return "s" + getBeanName();
    
public intgetType()
All components, unless otherwise stated, are LINE-level directives.

        return LINE;
    
protected voidputProperty(java.util.Map propertyMap, org.apache.velocity.context.InternalContextAdapter contextAdapter, org.apache.velocity.runtime.parser.node.Node node)
adds a given Node's key/value pair to the propertyMap. For example, if this Node contained the value "rows=20", then the key, rows, would be added to the propertyMap with the String value, 20.

param
propertyMap a params containing all the properties that we wish to set
param
node the parameter to set expressed in "name=value" format

        // node.value uses the StrutsValueStack to evaluate the directive's value parameter
        String param = node.value(contextAdapter).toString();

        int idx = param.indexOf("=");

        if (idx != -1) {
            String property = param.substring(0, idx);

            String value = param.substring(idx + 1);
            propertyMap.put(property, value);
        } else {
            throw new ParseErrorException("#" + this.getName() + " arguments must include an assignment operator!  For example #tag( Component \"template=mytemplate\" ).  #tag( TextField \"mytemplate\" ) is illegal!");
        }
    
public booleanrender(org.apache.velocity.context.InternalContextAdapter ctx, java.io.Writer writer, org.apache.velocity.runtime.parser.node.Node node)

        // get the bean
        ValueStack stack = (ValueStack) ctx.get("stack");
        HttpServletRequest req = (HttpServletRequest) stack.getContext().get(ServletActionContext.HTTP_REQUEST);
        HttpServletResponse res = (HttpServletResponse) stack.getContext().get(ServletActionContext.HTTP_RESPONSE);
        Component bean = getBean(stack, req, res);
        Container container = Dispatcher.getInstance().getConfigurationManager().getConfiguration().getContainer();
        container.inject(bean);
        // get the parameters
        Map params = createPropertyMap(ctx, node);
        bean.copyParams(params);
        //bean.addAllParameters(params);
        bean.start(writer);

        if (getType() == BLOCK) {
            Node body = node.jjtGetChild(node.jjtGetNumChildren() - 1);
            body.render(ctx, writer);
        }

        bean.end(writer, "");
        return true;