1 5 package com.opensymphony.webwork.views.velocity; 6 7 import com.opensymphony.webwork.views.jsp.ParamTag; 8 import com.opensymphony.xwork.ActionContext; 9 import com.opensymphony.xwork.util.OgnlValueStack; 10 import org.apache.velocity.context.InternalContextAdapter; 11 import org.apache.velocity.exception.MethodInvocationException; 12 import org.apache.velocity.exception.ParseErrorException; 13 import org.apache.velocity.exception.ResourceNotFoundException; 14 import org.apache.velocity.runtime.directive.Directive; 15 import org.apache.velocity.runtime.parser.node.Node; 16 17 import java.io.IOException ; 18 import java.io.Writer ; 19 20 21 40 public class ParamDirective extends Directive { 41 public String getName() { 42 return "param"; 43 } 44 45 public int getType() { 46 return LINE; 47 } 48 49 61 public boolean render(InternalContextAdapter contextAdapter, Writer writer, Node node) throws IOException , ResourceNotFoundException, ParseErrorException, MethodInvocationException { 62 Object object = contextAdapter.get(VelocityManager.TAG); 63 64 85 86 OgnlValueStack stack = ActionContext.getContext().getValueStack(); 87 stack.getContext().put(node.jjtGetChild(0).value(contextAdapter).toString(), node.jjtGetChild(1).value(contextAdapter)); 88 89 if ((object != null) && (object instanceof ParamTag.Parametric)) { 90 if ((node.jjtGetNumChildren() != 2) && (node.jjtGetNumChildren() != 3)) { 91 throw new ParseErrorException("#param directive requires two parameters, a key and a value. an optional flag to evaluate it may be included."); 92 } 93 94 Object key = node.jjtGetChild(0).value(contextAdapter); 95 Object value = node.jjtGetChild(1).value(contextAdapter); 96 97 if ((node.jjtGetNumChildren() == 3) && "TRUE".equalsIgnoreCase(node.jjtGetChild(2).value(contextAdapter).toString())) { 99 OgnlValueStack valueStack = ActionContext.getContext().getValueStack(); 100 value = valueStack.findValue(value.toString()); 101 } 102 103 ParamTag.Parametric parameterizedTag = (ParamTag.Parametric) object; 104 105 if (key != null) { 106 parameterizedTag.addParameter(key.toString(), value); 107 } 108 109 return true; 110 } else { 111 return false; 112 } 113 } 114 } 115 | Popular Tags |