KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > mlw > vlh > web > tag > AddParamTag


1 /**
2  * Copyright (c) 2003 held jointly by the individual authors. This library is
3  * free software; you can redistribute it and/or modify it under the terms of
4  * the GNU Lesser General Public License as published by the Free Software
5  * Foundation; either version 2.1 of the License, or (at your option) any later
6  * version. This library is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
9  * License for more details. You should have received a copy of the GNU Lesser
10  * General Public License along with this library; if not, write to the Free
11  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
12  * USA. > http://www.gnu.org/copyleft/lesser.html >
13  * http://www.opensource.org/licenses/lgpl-license.php
14  */

15 package net.mlw.vlh.web.tag;
16
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18
19 import javax.servlet.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
21
22 import net.mlw.vlh.web.tag.support.ParamAddable;
23 import net.mlw.vlh.web.tag.support.Spacer;
24 import net.mlw.vlh.web.util.JspUtils;
25
26 import org.apache.commons.beanutils.PropertyUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /**
31  * The tag that append parameters to url for any paramaddable tag in table,such
32  * are tag for root and tag for action.
33  *
34  * @author Andrej Zachar
35  * @version $Revision: 1.11 $ $Date: 2005/11/23 14:51:53 $
36  */

37 public class AddParamTag extends BodyTagSupport JavaDoc
38 {
39
40    private static final long serialVersionUID = 6118198463511925234L;
41
42    /**
43     * Logger for this class
44     */

45    private static final Log LOGGER = LogFactory.getLog(AddParamTag.class);
46
47    /** The name of the parameter used in rendered URL. */
48    private String JavaDoc name = null;
49
50    /**
51     * The name of the dynamic property, which values are used like the
52     * attribute value, but dynamicaly retrevied for the each row.
53     */

54    private String JavaDoc property = null;
55
56    /**
57     * The static value of the attribute name used in rendered URL like
58     * url?(ACTION_TEMP_PARAM_PREFIX)name=value .
59     */

60    private String JavaDoc value = null;
61
62    /**
63     * if true, append prefix ACTION_TEMP_PARAM_PREFIX before every action
64     * parameter.
65     */

66    private boolean temp = false;
67
68    /**
69     * @throws NoSuchMethodException
70     * @throws InvocationTargetException
71     * @throws IllegalAccessException
72     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
73     */

74    public int doStartTag() throws JspException JavaDoc
75    {
76       ParamAddable parent = (ParamAddable) JspUtils.getParent(this, ParamAddable.class);
77
78       if ((property != null) && (value != null))
79       {
80          final String JavaDoc message = "For one parameter" + name
81                + "you can not use two values (first dynamic from the property, 2nd static from the value";
82          LOGGER.error(message);
83          throw new JspException JavaDoc(message);
84       }
85
86       if (name == null)
87       {
88          // use the same name as the name of property, if name is not
89
// specified
90
name = property;
91       }
92
93       if (property != null)
94       {
95          DefaultRowTag rowTag = (DefaultRowTag) JspUtils.getParent(this, DefaultRowTag.class);
96          Object JavaDoc bean = pageContext.getAttribute(rowTag.getBeanName());
97          if (bean != null && !(bean instanceof Spacer))
98          {
99             try
100             {
101                value = String.valueOf(PropertyUtils.getProperty(bean, property));
102             }
103             catch (Exception JavaDoc e)
104             {
105                LOGGER.error("<vlh:addParam> Error getting property '" + property + "'.");
106                value = null;
107             }
108          }
109          else
110          {
111             if (LOGGER.isDebugEnabled())
112             {
113                LOGGER.debug("Row's JavaBean '" + rowTag.getBeanName() + "' is null or Valuelist is empty!");
114             }
115             value = null;
116          }
117       }
118
119       if (value == null)
120       {
121          if (LOGGER.isInfoEnabled())
122          {
123             LOGGER.info("The param '" + addActionParamPrefix(name) + "' is skiped, because it's value is null!");
124          }
125       }
126       else
127       {
128
129          parent.addParam(addActionParamPrefix(name), value);
130          if (LOGGER.isDebugEnabled())
131          {
132             LOGGER.debug("The param '" + addActionParamPrefix(name) + "' was added with the value '" + value + "'.");
133          }
134       }
135
136       release();
137
138       return SKIP_BODY;
139    }
140
141    /**
142     * @param param
143     * Add action param prefix if it is a temp parameter.
144     */

145    protected String JavaDoc addActionParamPrefix(String JavaDoc param)
146    {
147       if (isTemp())
148       {
149          return ActionTag.ACTION_TEMP_PARAM_PREFIX + param;
150       }
151       else
152       {
153          return param;
154       }
155    }
156
157    /**
158     * @see javax.servlet.jsp.tagext.BodyTagSupport#doAfterBody()
159     */

160    public int doAfterBody() throws JspException JavaDoc
161    {
162       bodyContent.clearBody();
163       return SKIP_BODY;
164    }
165
166    /**
167     * @see javax.servlet.jsp.tagext.Tag#doEndTag()
168     */

169    public int doEndTag() throws JspException JavaDoc
170    {
171       int result = super.doEndTag();
172       reset();
173       return result;
174    }
175
176    /**
177     * @return Returns the property.
178     */

179    public String JavaDoc getProperty()
180    {
181       return this.property;
182    }
183
184    /**
185     * @param property
186     * The property to set.
187     */

188    public void setProperty(String JavaDoc property)
189    {
190       this.property = property;
191    }
192
193    /**
194     * @return Returns the name.
195     */

196    public String JavaDoc getName()
197    {
198       return this.name;
199    }
200
201    /**
202     * @param name
203     * The url parameter name to set.
204     */

205    public void setName(String JavaDoc name)
206    {
207       this.name = name;
208    }
209
210    /**
211     * @return Returns the value.
212     */

213    public String JavaDoc getValue()
214    {
215       return this.value;
216    }
217
218    /**
219     * @param value The value to set.
220     */

221    public void setValue(String JavaDoc value)
222    {
223       this.value = value;
224    }
225
226    /**
227     * @return Returns the temp.
228     */

229    public boolean isTemp()
230    {
231       return temp;
232    }
233
234    /**
235     * Default is false. If true,
236     * append prefix ACTION_TEMP_PARAM_PREFIX before every action parameter.
237     * @param temp The temp to set.
238     */

239    public void setTemp(boolean temp)
240    {
241       this.temp = temp;
242    }
243
244    private void reset()
245    {
246       this.name = null;
247       this.property = null;
248       this.temp = false;
249       this.value = null;
250    }
251
252    /**
253     * Called on a Tag handler to release state.
254     * The page compiler guarantees that JSP page implementation
255     * objects will invoke this method on all tag handlers,
256     * but there may be multiple invocations on doStartTag and doEndTag in between.
257     *
258     * @see javax.servlet.jsp.tagext.Tag#release()
259     */

260    public void release()
261    {
262       super.release();
263       reset();
264    }
265 }
Popular Tags