KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > velocity > components > AbstractDirective


1 package com.opensymphony.webwork.views.velocity.components;
2
3 import com.opensymphony.webwork.ServletActionContext;
4 import com.opensymphony.webwork.components.Component;
5 import com.opensymphony.xwork.util.OgnlValueStack;
6 import org.apache.velocity.context.InternalContextAdapter;
7 import org.apache.velocity.exception.MethodInvocationException;
8 import org.apache.velocity.exception.ParseErrorException;
9 import org.apache.velocity.exception.ResourceNotFoundException;
10 import org.apache.velocity.runtime.directive.Directive;
11 import org.apache.velocity.runtime.parser.node.Node;
12
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.Writer JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 /**
21  * User: patrick
22  * Date: Aug 5, 2005
23  * Time: 7:55:26 AM
24  */

25 public abstract class AbstractDirective extends Directive {
26     public String JavaDoc getName() {
27         return "ww" + getBeanName();
28     }
29
30     public abstract String JavaDoc getBeanName();
31
32     /**
33      * All components, unless otherwise stated, are LINE-level directives.
34      */

35     public int getType() {
36         return LINE;
37     }
38
39     protected abstract Component getBean(OgnlValueStack stack, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res);
40
41     public boolean render(InternalContextAdapter ctx, Writer JavaDoc writer, Node node) throws IOException JavaDoc, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
42         // get the bean
43
OgnlValueStack stack = (OgnlValueStack) ctx.get("stack");
44         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) stack.getContext().get(ServletActionContext.HTTP_REQUEST);
45         HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) stack.getContext().get(ServletActionContext.HTTP_RESPONSE);
46         Component bean = getBean(stack, req, res);
47
48         // get the parameters
49
Map JavaDoc params = createPropertyMap(ctx, node);
50         bean.copyParams(params);
51         //bean.addAllParameters(params);
52
bean.start(writer);
53
54         if (getType() == BLOCK) {
55             Node body = node.jjtGetChild(node.jjtGetNumChildren() - 1);
56             body.render(ctx, writer);
57         }
58
59         bean.end(writer);
60         return true;
61     }
62
63     /**
64      * create a Map of properties that the user has passed in. for example,
65      * <pre>
66      * #xxx("name=hello" "value=world" "template=foo")
67      * </pre>
68      * would yield a params that contains {["name", "hello"], ["value", "world"], ["template", "foo"]}
69      *
70      * @param node the Node passed in to the render method
71      * @return a Map of the user specified properties
72      * @throws org.apache.velocity.exception.ParseErrorException
73      * if the was an error in the format of the property
74      */

75     protected Map JavaDoc createPropertyMap(InternalContextAdapter contextAdapter, Node node) throws ParseErrorException, MethodInvocationException {
76         Map JavaDoc propertyMap = new HashMap JavaDoc();
77
78         int children = node.jjtGetNumChildren();
79         if (getType() == BLOCK) {
80             children--;
81         }
82
83         for (int index = 0, length = children; index < length; index++) {
84             this.putProperty(propertyMap, contextAdapter, node.jjtGetChild(index));
85         }
86
87         return propertyMap;
88     }
89
90     /**
91      * adds a given Node's key/value pair to the propertyMap. For example, if this Node contained the value "rows=20",
92      * then the key, rows, would be added to the propertyMap with the String value, 20.
93      *
94      * @param propertyMap a params containing all the properties that we wish to set
95      * @param node the parameter to set expressed in "name=value" format
96      */

97     protected void putProperty(Map JavaDoc propertyMap, InternalContextAdapter contextAdapter, Node node) throws ParseErrorException, MethodInvocationException {
98         // node.value uses the WebWorkValueStack to evaluate the directive's value parameter
99
String JavaDoc param = node.value(contextAdapter).toString();
100
101         int idx = param.indexOf("=");
102
103         if (idx != -1) {
104             String JavaDoc property = param.substring(0, idx);
105
106             String JavaDoc value = param.substring(idx + 1);
107             propertyMap.put(property, value);
108         } else {
109             throw new ParseErrorException("#" + this.getName() + " arguments must include an assignment operator! For example #tag( Component \"template=mytemplate\" ). #tag( TextField \"mytemplate\" ) is illegal!");
110         }
111     }
112 }
113
Popular Tags