1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.views.velocity.components;
23
24 import java.io.IOException;
25 import java.io.Writer;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.apache.struts2.ServletActionContext;
33 import org.apache.struts2.components.Component;
34 import org.apache.velocity.context.InternalContextAdapter;
35 import org.apache.velocity.exception.MethodInvocationException;
36 import org.apache.velocity.exception.ParseErrorException;
37 import org.apache.velocity.exception.ResourceNotFoundException;
38 import org.apache.velocity.runtime.directive.Directive;
39 import org.apache.velocity.runtime.parser.node.Node;
40
41 import com.opensymphony.xwork2.ActionContext;
42 import com.opensymphony.xwork2.inject.Container;
43 import com.opensymphony.xwork2.util.ValueStack;
44
45 public abstract class AbstractDirective extends Directive {
46 public String getName() {
47 return "s" + getBeanName();
48 }
49
50 public abstract String getBeanName();
51
52 /***
53 * All components, unless otherwise stated, are LINE-level directives.
54 */
55 public int getType() {
56 return LINE;
57 }
58
59 protected abstract Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res);
60
61 public boolean render(InternalContextAdapter ctx, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
62
63 ValueStack stack = (ValueStack) ctx.get("stack");
64 HttpServletRequest req = (HttpServletRequest) stack.getContext().get(ServletActionContext.HTTP_REQUEST);
65 HttpServletResponse res = (HttpServletResponse) stack.getContext().get(ServletActionContext.HTTP_RESPONSE);
66 Component bean = getBean(stack, req, res);
67 Container container = (Container) stack.getContext().get(ActionContext.CONTAINER);
68 container.inject(bean);
69
70 Map params = createPropertyMap(ctx, node);
71 bean.copyParams(params);
72
73 bean.start(writer);
74
75 if (getType() == BLOCK) {
76 Node body = node.jjtGetChild(node.jjtGetNumChildren() - 1);
77 body.render(ctx, writer);
78 }
79
80 bean.end(writer, "");
81 return true;
82 }
83
84 /***
85 * create a Map of properties that the user has passed in. for example,
86 * <pre>
87 * #xxx("name=hello" "value=world" "template=foo")
88 * </pre>
89 * would yield a params that contains {["name", "hello"], ["value", "world"], ["template", "foo"]}
90 *
91 * @param node the Node passed in to the render method
92 * @return a Map of the user specified properties
93 * @throws org.apache.velocity.exception.ParseErrorException
94 * if the was an error in the format of the property
95 */
96 protected Map createPropertyMap(InternalContextAdapter contextAdapter, Node node) throws ParseErrorException, MethodInvocationException {
97 Map propertyMap = new HashMap();
98
99 int children = node.jjtGetNumChildren();
100 if (getType() == BLOCK) {
101 children--;
102 }
103
104 for (int index = 0, length = children; index < length; index++) {
105 this.putProperty(propertyMap, contextAdapter, node.jjtGetChild(index));
106 }
107
108 return propertyMap;
109 }
110
111 /***
112 * adds a given Node's key/value pair to the propertyMap. For example, if this Node contained the value "rows=20",
113 * then the key, rows, would be added to the propertyMap with the String value, 20.
114 *
115 * @param propertyMap a params containing all the properties that we wish to set
116 * @param node the parameter to set expressed in "name=value" format
117 */
118 protected void putProperty(Map propertyMap, InternalContextAdapter contextAdapter, Node node) throws ParseErrorException, MethodInvocationException {
119
120 String param = node.value(contextAdapter).toString();
121
122 int idx = param.indexOf("=");
123
124 if (idx != -1) {
125 String property = param.substring(0, idx);
126
127 String value = param.substring(idx + 1);
128 propertyMap.put(property, value);
129 } else {
130 throw new ParseErrorException("#" + this.getName() + " arguments must include an assignment operator! For example #tag( Component \"template=mytemplate\" ). #tag( TextField \"mytemplate\" ) is illegal!");
131 }
132 }
133 }