KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jsp > ParamTag


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

5 package com.opensymphony.webwork.views.jsp;
6
7 import javax.servlet.jsp.JspException JavaDoc;
8 import java.util.Map JavaDoc;
9
10
11 /**
12  * This tag can be used to parameterize other tags, who implement
13  * the ParametricTag interface declared here.
14  * <p/>
15  * The IncludeTag and BeanTag are examples of such tags.
16  * <p/>
17  * The inner classes, Parametric and UnnamedParametric, are implemented by tags. They indicate that a
18  * particular Tag may have embedded params. For example, if we were wanted to use the ComponentTag
19  * and wanted to provide custom params to assist with the rendering, we could declare something like
20  * <p/>
21  * <pre>
22  * &lt;ui:component&gt;
23  * &lt;ui:param name="key" value="[0]"/&gt;
24  * &lt;ui:param name="value" value="[1]"/&gt;
25  * &lt;ui:param name="context" value="[2]"/&gt;
26  * &lt;/ui:component&gt;
27  * </pre>
28  * <p/>
29  * where the key will be the identifier and the value the result of an OGNL expression run against the current
30  * OgnlValueStack
31  *
32  * @author Rickard Öberg (rickard@dreambean.com)
33  * @see com.opensymphony.webwork.views.jsp.IncludeTag
34  * @see com.opensymphony.webwork.views.jsp.BeanTag
35  */

36 public class ParamTag extends WebWorkBodyTagSupport {
37     //~ Instance fields ////////////////////////////////////////////////////////
38

39     // Attributes ----------------------------------------------------
40
protected String JavaDoc nameAttr;
41     protected String JavaDoc valueAttr;
42
43     //~ Methods ////////////////////////////////////////////////////////////////
44

45     public void setName(String JavaDoc aName) {
46         nameAttr = aName;
47     }
48
49     public void setValue(String JavaDoc aName) {
50         valueAttr = aName;
51     }
52
53     // BodyTag implementation ----------------------------------------
54
public int doEndTag() throws JspException JavaDoc {
55         Parametric parametricTag = (Parametric) findAncestorWithClass(this, Parametric.class);
56
57         if (parametricTag != null) {
58             if (valueAttr != null) {
59                 if (parametricTag instanceof UnnamedParametric) {
60                     ((UnnamedParametric) parametricTag).addParameter(findValue(valueAttr));
61                 } else {
62                     String JavaDoc name = findString(nameAttr);
63
64                     if (name == null) {
65                         throw new JspException JavaDoc("No name found for following expression: " + nameAttr);
66                     }
67
68                     Object JavaDoc value = findValue(valueAttr);
69                     parametricTag.addParameter(name, value);
70                 }
71             } else {
72                 String JavaDoc content = null;
73
74                 if (!((bodyContent != null) && ((content = bodyContent.getString()).length() != 0))) {
75                     content = null; // No value
76
}
77
78                 if (parametricTag instanceof UnnamedParametric) {
79                     ((UnnamedParametric) parametricTag).addParameter(content);
80                 } else {
81                     parametricTag.addParameter(findString(nameAttr), content);
82                 }
83             }
84         }
85
86         return EVAL_PAGE;
87     }
88
89     //~ Inner Interfaces ///////////////////////////////////////////////////////
90

91     // Inner classes -------------------------------------------------
92
public interface Parametric {
93         public Map JavaDoc getParameters();
94
95         public void addParameter(String JavaDoc name, Object JavaDoc value);
96     }
97
98     public interface UnnamedParametric extends Parametric {
99         public void addParameter(Object JavaDoc value);
100     }
101 }
102
Popular Tags