KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > portlet > velocity > ParamDirective


1 /*
2  * Copyright (c) 2005 Opensymphony. All Rights Reserved.
3  */

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 JavaDoc;
16 import java.io.Writer JavaDoc;
17
18 /**
19  * @author <a HREF="mailto:hu_pengfei@yahoo.com.cn">Henry Hu </a>
20  * @since 2005-7-18
21  */

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 JavaDoc 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 JavaDoc {
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 JavaDoc writer, Node node) throws IOException JavaDoc, 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 JavaDoc paramName = (String JavaDoc) node.jjtGetChild(0).value(adapter);
60             Object JavaDoc paramValue = node.jjtGetChild(1).value(adapter);
61             parent.addParameter(paramName, paramValue);
62             return true;
63         }
64     }
65
66 }
Popular Tags