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 ; 14 import javax.servlet.http.HttpServletResponse ; 15 import java.io.IOException ; 16 import java.io.Writer ; 17 import java.util.HashMap ; 18 import java.util.Map ; 19 20 25 public abstract class AbstractDirective extends Directive { 26 public String getName() { 27 return "ww" + getBeanName(); 28 } 29 30 public abstract String getBeanName(); 31 32 35 public int getType() { 36 return LINE; 37 } 38 39 protected abstract Component getBean(OgnlValueStack stack, HttpServletRequest req, HttpServletResponse res); 40 41 public boolean render(InternalContextAdapter ctx, Writer writer, Node node) throws IOException , ResourceNotFoundException, ParseErrorException, MethodInvocationException { 42 OgnlValueStack stack = (OgnlValueStack) ctx.get("stack"); 44 HttpServletRequest req = (HttpServletRequest ) stack.getContext().get(ServletActionContext.HTTP_REQUEST); 45 HttpServletResponse res = (HttpServletResponse ) stack.getContext().get(ServletActionContext.HTTP_RESPONSE); 46 Component bean = getBean(stack, req, res); 47 48 Map params = createPropertyMap(ctx, node); 50 bean.copyParams(params); 51 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 75 protected Map createPropertyMap(InternalContextAdapter contextAdapter, Node node) throws ParseErrorException, MethodInvocationException { 76 Map propertyMap = new HashMap (); 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 97 protected void putProperty(Map propertyMap, InternalContextAdapter contextAdapter, Node node) throws ParseErrorException, MethodInvocationException { 98 String param = node.value(contextAdapter).toString(); 100 101 int idx = param.indexOf("="); 102 103 if (idx != -1) { 104 String property = param.substring(0, idx); 105 106 String 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 |