KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

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 JavaDoc;
18 import java.io.Writer JavaDoc;
19
20
21 /**
22  * <p/>
23  * The ParamDirective allows for additional parameters to be passed in to a given tag before the tag is rendered. By
24  * default these parameters are not evaluated through the value stack. However, the user can specify using a third
25  * parameter that they are evaluated.
26  * </p>
27  * <p/>
28  * For example:
29  * </p>
30  * <pre>
31  * #param( hello $world ) - adds the velocity resolved value of world to the tag
32  * #param( hello world true) - adds the ognl resolved value of world to the tag
33  * #param( hello world false) - adds the string world to the tag
34  * </pre>
35  *
36  * @author Matt Ho <a HREF="mailto:matt@enginegreen.com">&lt;matt@enginegreen.com&gt;</a>
37  * @deprecated Automatic JSP tag support doesn't work well and is likely to break. Please use the native Velocity
38  * tags introduced in WebWork 2.2
39  */

40 public class ParamDirective extends Directive {
41     public String JavaDoc getName() {
42         return "param";
43     }
44
45     public int getType() {
46         return LINE;
47     }
48
49     /**
50      * @param contextAdapter
51      * @param writer
52      * @param node
53      * @throws java.io.IOException
54      * @throws org.apache.velocity.exception.ResourceNotFoundException
55      *
56      * @throws org.apache.velocity.exception.ParseErrorException
57      *
58      * @throws org.apache.velocity.exception.MethodInvocationException
59      *
60      */

61     public boolean render(InternalContextAdapter contextAdapter, Writer JavaDoc writer, Node node) throws IOException JavaDoc, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
62         Object JavaDoc object = contextAdapter.get(VelocityManager.TAG);
63
64         //Fixed for Param -- Added by Henry Hu
65
/*
66          * Param Usage Example:
67          * <p>Custom Component Example:</p>
68             #wwcomponent ("template=components/datefield.vm")
69                 #param ("label" "Date")
70                 #param ("name" "mydatefield")
71                 #param ("mysize" 3)
72            #end
73            <br/>
74            
75            datefield.vm:
76            #set ($name = $stack.getContext().get("name"))
77            #set ($label = $stack.getContext().get("label"))
78            #set ($size = $stack.getContext().get("mysize"))
79            #set ($yearsize = $size * 2)
80             $label:
81             <input type="text" name="${name}.day" size="$size" /> /
82             <input type="text" name="${name}.month" size="$size" /> /
83             <input type="text" name="${name}.year" size="$yearsize" /> (dd/mm/yyyy)
84            **/

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 JavaDoc key = node.jjtGetChild(0).value(contextAdapter);
95             Object JavaDoc value = node.jjtGetChild(1).value(contextAdapter);
96
97             // if there are ever 3 params, the we should look up the value against the value stack
98
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