KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > ParamTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.taglib.core;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.struts.taglib.TagUtils;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.JspTagException JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * <p>Adds parameter to enclosing tag, which implements <code>com.blandware.atleap.webapp.taglib.core.ParamParent</code>
32  * interface. If no such tag is found, JspException will be thrown.<br />
33  * In <code>value</code> attribute instances of <code>java.util.Collection</code>, <code>java.lang.String</code> and
34  * <code>java.lang.String[]</code> are supported. If instance of another class is specified, it will be cast to
35  * <code>java.lang.String</code> using <code>java.lang.String.valueOf(Object)</code> method.
36  * </p>
37  * <p>
38  * Allowed attributes are (they are both required):
39  * <ul>
40  * <li>
41  * <b>name</b> - name of parameter
42  * </li>
43  * <li>
44  * <b>value</b> - value of parameter to assign
45  * </li>
46  * </ul>
47  * </p>
48  * <p><a HREF="ParamTag.java.htm"><i>View Source</i></a></p>
49  *
50  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
51  * @version $Revision: 1.5 $ $Date: 2005/10/12 13:34:53 $
52  * @jsp.tag name="param"
53  * body-content="empty"
54  * @see java.lang.String#valueOf(Object)
55  */

56 public class ParamTag extends SimpleTagSupport JavaDoc {
57
58     protected transient final Log log = LogFactory.getLog(ParamTag.class);
59
60     /**
61      * Name of parameter
62      */

63     protected String JavaDoc name;
64
65     /**
66      * Value of parameter
67      */

68     protected Object JavaDoc value;
69
70     /**
71      * Local parameters
72      */

73     protected Map JavaDoc parameters = new HashMap JavaDoc();
74
75     /**
76      * Returns name of parameter
77      *
78      * @return name of parameter
79      * @jsp.attribute required="true"
80      * rtexprvalue="true"
81      * type="java.lang.String"
82      * description="Name of parameter"
83      */

84     public String JavaDoc getName() {
85         return name;
86     }
87
88     /**
89      * Sets name of parameter
90      *
91      * @param name name of parameter to set
92      */

93     public void setName(String JavaDoc name) {
94         this.name = name;
95     }
96
97     /**
98      * Returns value of parameter
99      *
100      * @return value of parameter
101      * @jsp.attribute required="true"
102      * rtexprvalue="true"
103      * type="java.lang.Object"
104      * description="Value of parameter"
105      */

106     public Object JavaDoc getValue() {
107         return value;
108     }
109
110     /**
111      * Sets value of parameter
112      *
113      * @param value value of parameter to set
114      */

115     public void setValue(Object JavaDoc value) {
116         this.value = value;
117     }
118
119     /**
120      * Processes the tag
121      *
122      * @throws JspException
123      */

124     public void doTag() throws JspException JavaDoc {
125
126         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
127         ParamParent parent = (ParamParent) findAncestorWithClass(this, ParamParent.class);
128         if ( parent == null ) {
129             JspTagException JavaDoc e = new JspTagException JavaDoc("This tag is only valid when nested within tag which implements 'com.blandware.atleap.webapp.taglib.core.ParamParent' interface");
130             TagUtils.getInstance().saveException(pageContext, e);
131         }
132
133         // cast value to string
134
if ( !(value instanceof String JavaDoc) && !(value instanceof String JavaDoc[]) && !(value instanceof Collection JavaDoc) ) {
135             value = String.valueOf(value);
136         }
137
138         parent.addParameter(name, value);
139
140     }
141
142 }
143
Popular Tags