1 4 package com.opensymphony.webwork.portlet.velocity; 5 6 import org.apache.log4j.Category; 7 import org.apache.velocity.context.InternalContextAdapter; 8 import org.apache.velocity.exception.MethodInvocationException; 9 import org.apache.velocity.exception.ParseErrorException; 10 import org.apache.velocity.exception.ResourceNotFoundException; 11 import org.apache.velocity.runtime.RuntimeServices; 12 import org.apache.velocity.runtime.directive.Directive; 13 import org.apache.velocity.runtime.parser.node.Node; 14 15 import java.io.IOException ; 16 import java.io.Writer ; 17 18 22 public final class ParamDirective extends Directive { 23 24 private static Category log; 25 26 static { 27 log = Category.getInstance(com.opensymphony.webwork.portlet.velocity.ParamDirective.class); 28 } 29 30 public ParamDirective() { 31 } 32 33 public String getName() { 34 return "decoratorParam"; 35 } 36 37 public int getType() { 38 return 2; 39 } 40 41 public void init(RuntimeServices services, InternalContextAdapter adapter, Node node) throws Exception { 42 super.init(services, adapter, node); 43 int numArgs = node.jjtGetNumChildren(); 44 if (numArgs != 2) 45 services.error("#decoratorParam error: You must specify a param name and value."); 46 } 47 48 public boolean render(InternalContextAdapter adapter, Writer writer, Node node) throws IOException , ResourceNotFoundException, 49 ParseErrorException, MethodInvocationException { 50 ApplyDecoratorDirective.DirectiveStack stack = (ApplyDecoratorDirective.DirectiveStack) adapter 51 .get(ApplyDecoratorDirective.STACK_KEY); 52 if (stack == null) 53 throw new ParseErrorException("#decoratorParam error: You must nest this directive within a #applyDecorator directive"); 54 ApplyDecoratorDirective parent = stack.peek(); 55 if (parent == null) { 56 log.error("#decoratorParam error: You must nest this directive within a #applyDecorator directive"); 57 return false; 58 } else { 59 String paramName = (String ) node.jjtGetChild(0).value(adapter); 60 Object paramValue = node.jjtGetChild(1).value(adapter); 61 parent.addParameter(paramName, paramValue); 62 return true; 63 } 64 } 65 66 } | Popular Tags |