KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > tiles > util > TagUtils


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

18
19 package org.apache.struts.taglib.tiles.util;
20
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25
26 import org.apache.commons.beanutils.PropertyUtils;
27 import org.apache.struts.Globals;
28 import org.apache.struts.taglib.tiles.ComponentConstants;
29 import org.apache.struts.tiles.ComponentContext;
30 import org.apache.struts.tiles.ComponentDefinition;
31 import org.apache.struts.tiles.DefinitionsFactoryException;
32 import org.apache.struts.tiles.FactoryNotFoundException;
33 import org.apache.struts.tiles.NoSuchDefinitionException;
34 import org.apache.struts.tiles.TilesUtil;
35
36 /**
37  * Collection of utilities.
38  * This class also serves as an interface between Components and Struts. If
39  * you want to rip away Struts, simply reimplement some methods in this class.
40  * You can copy them from Struts.
41  *
42  */

43 public class TagUtils {
44
45     /** Debug flag */
46     public static final boolean debug = true;
47     
48     /**
49     * Get scope value from string value
50     * @param scopeName Scope as a String.
51     * @param defaultValue Returned default value, if not found.
52     * @return Scope as an <code>int</code>, or <code>defaultValue</code> if scope is <code>null</code>.
53     * @throws JspException Scope name is not recognized as a valid scope.
54     */

55     public static int getScope(String JavaDoc scopeName, int defaultValue) throws JspException JavaDoc {
56         if (scopeName == null) {
57             return defaultValue;
58         }
59         
60         if (scopeName.equalsIgnoreCase("component")) {
61             return ComponentConstants.COMPONENT_SCOPE;
62             
63         } else if (scopeName.equalsIgnoreCase("template")) {
64             return ComponentConstants.COMPONENT_SCOPE;
65             
66         } else if (scopeName.equalsIgnoreCase("tile")) {
67             return ComponentConstants.COMPONENT_SCOPE;
68             
69         } else {
70             return org.apache.struts.taglib.TagUtils.getInstance().getScope(
71                 scopeName);
72         }
73     }
74
75     /**
76      * Return the value of the specified property of the specified bean,
77      * no matter which property reference format is used, with no
78      * type conversions.
79      *
80      * @param bean Bean whose property is to be extracted.
81      * @param name Possibly indexed and/or nested name of the property
82      * to be extracted.
83      *
84      * @exception IllegalAccessException if the caller does not have
85      * access to the property accessor method
86      * @exception InvocationTargetException if the property accessor method
87      * throws an exception
88      * @exception NoSuchMethodException if an accessor method for this
89      * propety cannot be found.
90      * @deprecated Use PropertyUtils.getProperty() directly. This will be removed
91      * after Struts 1.2.
92      */

93     public static Object JavaDoc getProperty(Object JavaDoc bean, String JavaDoc name)
94         throws
95             IllegalAccessException JavaDoc,
96             InvocationTargetException JavaDoc,
97             NoSuchMethodException JavaDoc {
98
99         return PropertyUtils.getProperty(bean, name);
100     }
101
102     /**
103      * Retrieve bean from page context, using specified scope.
104      * If scope is not set, use <code>findAttribute()</code>.
105      *
106      * @param beanName Name of bean to retrieve.
107      * @param scopeName Scope or <code>null</code>. If <code>null</code>, bean is searched using
108      * findAttribute().
109      * @param pageContext Current pageContext.
110      * @return Requested bean or <code>null</code> if not found.
111      * @throws JspException Scope name is not recognized as a valid scope.
112      */

113     public static Object JavaDoc retrieveBean(String JavaDoc beanName, String JavaDoc scopeName, PageContext JavaDoc pageContext)
114         throws JspException JavaDoc {
115         
116         if (scopeName == null) {
117             return findAttribute(beanName, pageContext);
118         }
119
120         // Default value doesn't matter because we have already check it
121
int scope = getScope(scopeName, PageContext.PAGE_SCOPE);
122         
123         //return pageContext.getAttribute( beanName, scope );
124
return getAttribute(beanName, scope, pageContext);
125     }
126
127     /**
128      * Search attribute in different contexts.
129      * First, check in component context, then use pageContext.findAttribute().
130      * @param beanName Name of bean to retrieve.
131      * @param pageContext Current pageContext.
132      * @return Requested bean or <code>null</code> if not found.
133      */

134     public static Object JavaDoc findAttribute(String JavaDoc beanName, PageContext JavaDoc pageContext) {
135         ComponentContext compContext = ComponentContext.getContext(pageContext.getRequest());
136         
137         if (compContext != null) {
138             Object JavaDoc attribute = compContext.findAttribute(beanName, pageContext);
139             if (attribute != null) {
140                 return attribute;
141             }
142         }
143
144         // Search in pageContext scopes
145
return pageContext.findAttribute(beanName);
146     }
147
148     /**
149      * Get object from requested context. Return <code>null</code> if not found.
150      * Context can be "component" or normal JSP contexts.
151      * @param beanName Name of bean to retrieve.
152      * @param scope Scope from which bean must be retrieved.
153      * @param pageContext Current pageContext.
154      * @return Requested bean or <code>null</code> if not found.
155      */

156     public static Object JavaDoc getAttribute(String JavaDoc beanName, int scope, PageContext JavaDoc pageContext) {
157         if (scope == ComponentConstants.COMPONENT_SCOPE) {
158             ComponentContext compContext = ComponentContext.getContext(pageContext.getRequest());
159             return compContext.getAttribute(beanName);
160         }
161         return pageContext.getAttribute(beanName, scope);
162     }
163
164     /**
165      * Locate and return the specified property of the specified bean, from
166      * an optionally specified scope, in the specified page context.
167      *
168      * @param pageContext Page context to be searched.
169      * @param beanName Name of the bean to be retrieved.
170      * @param beanProperty Name of the property to be retrieved, or
171      * <code>null</code> to retrieve the bean itself.
172      * @param beanScope Scope to be searched (page, request, session, application)
173      * or <code>null</code> to use <code>findAttribute()</code> instead.
174      *
175      * @exception JspException Scope name is not recognized as a valid scope
176      * @exception JspException if the specified bean is not found
177      * @exception JspException if accessing this property causes an
178      * IllegalAccessException, IllegalArgumentException,
179      * InvocationTargetException, or NoSuchMethodException
180      */

181     public static Object JavaDoc getRealValueFromBean(
182         String JavaDoc beanName,
183         String JavaDoc beanProperty,
184         String JavaDoc beanScope,
185         PageContext JavaDoc pageContext)
186         throws JspException JavaDoc {
187             
188         try {
189             Object JavaDoc realValue;
190             Object JavaDoc bean = retrieveBean(beanName, beanScope, pageContext);
191             if (bean != null && beanProperty != null) {
192                 realValue = PropertyUtils.getProperty(bean, beanProperty);
193             } else {
194                 realValue = bean; // value can be null
195
}
196             return realValue;
197             
198         } catch (NoSuchMethodException JavaDoc ex) {
199             throw new JspException JavaDoc(
200                 "Error - component.PutAttributeTag : Error while retrieving value from bean '"
201                     + beanName
202                     + "' with property '"
203                     + beanProperty
204                     + "' in scope '"
205                     + beanScope
206                     + "'. (exception : "
207                     + ex.getMessage());
208                     
209         } catch (InvocationTargetException JavaDoc ex) {
210             throw new JspException JavaDoc(
211                 "Error - component.PutAttributeTag : Error while retrieving value from bean '"
212                     + beanName
213                     + "' with property '"
214                     + beanProperty
215                     + "' in scope '"
216                     + beanScope
217                     + "'. (exception : "
218                     + ex.getMessage());
219                     
220         } catch (IllegalAccessException JavaDoc ex) {
221             throw new JspException JavaDoc(
222                 "Error - component.PutAttributeTag : Error while retrieving value from bean '"
223                     + beanName
224                     + "' with property '"
225                     + beanProperty
226                     + "' in scope '"
227                     + beanScope
228                     + "'. (exception : "
229                     + ex.getMessage());
230         }
231     }
232
233     /**
234      * Store bean in requested context.
235      * If scope is <code>null</code>, save it in REQUEST_SCOPE context.
236      *
237      * @param pageContext Current pageContext.
238      * @param name Name of the bean.
239      * @param scope Scope under which bean is saved (page, request, session, application)
240      * or <code>null</code> to store in <code>request()</code> instead.
241      * @param value Bean value to store.
242      *
243      * @exception JspException Scope name is not recognized as a valid scope
244      */

245     public static void setAttribute(
246         PageContext JavaDoc pageContext,
247         String JavaDoc name,
248         Object JavaDoc value,
249         String JavaDoc scope)
250         throws JspException JavaDoc {
251             
252         if (scope == null)
253             pageContext.setAttribute(name, value, PageContext.REQUEST_SCOPE);
254         else if (scope.equalsIgnoreCase("page"))
255             pageContext.setAttribute(name, value, PageContext.PAGE_SCOPE);
256         else if (scope.equalsIgnoreCase("request"))
257             pageContext.setAttribute(name, value, PageContext.REQUEST_SCOPE);
258         else if (scope.equalsIgnoreCase("session"))
259             pageContext.setAttribute(name, value, PageContext.SESSION_SCOPE);
260         else if (scope.equalsIgnoreCase("application"))
261             pageContext.setAttribute(name, value, PageContext.APPLICATION_SCOPE);
262         else {
263             throw new JspException JavaDoc("Error - bad scope name '" + scope + "'");
264         }
265     }
266
267     /**
268      * Store bean in REQUEST_SCOPE context.
269      *
270      * @param pageContext Current pageContext.
271      * @param name Name of the bean.
272      * @param beanValue Bean value to store.
273      *
274      * @exception JspException Scope name is not recognized as a valid scope
275      */

276     public static void setAttribute(PageContext JavaDoc pageContext, String JavaDoc name, Object JavaDoc beanValue)
277         throws JspException JavaDoc {
278         pageContext.setAttribute(name, beanValue, PageContext.REQUEST_SCOPE);
279     }
280
281     /**
282      * Save the specified exception as a request attribute for later use.
283      *
284      * @param pageContext The PageContext for the current page.
285      * @param exception The exception to be saved.
286      */

287     public static void saveException(PageContext JavaDoc pageContext, Throwable JavaDoc exception) {
288         pageContext.setAttribute(Globals.EXCEPTION_KEY, exception, PageContext.REQUEST_SCOPE);
289     }
290
291     /**
292      * Get component definition by its name.
293      * @param name Definition name.
294      * @param pageContext The PageContext for the current page.
295      * @throws JspException -
296      */

297     public static ComponentDefinition getComponentDefinition(String JavaDoc name, PageContext JavaDoc pageContext)
298         throws JspException JavaDoc {
299             
300         try {
301             return TilesUtil.getDefinition(
302                 name,
303                 pageContext.getRequest(),
304                 pageContext.getServletContext());
305                 
306         } catch (NoSuchDefinitionException ex) {
307             throw new JspException JavaDoc(
308                 "Error : Can't get component definition for '"
309                     + name
310                     + "'. Check if this name exist in component definitions.");
311         } catch (FactoryNotFoundException ex) { // factory not found.
312
throw new JspException JavaDoc(ex.getMessage());
313             
314         } catch (DefinitionsFactoryException ex) {
315             if (debug)
316                 ex.printStackTrace();
317             // Save exception to be able to show it later
318
saveException(pageContext, ex);
319             throw new JspException JavaDoc(ex.getMessage());
320         }
321     }
322
323 }
324
Popular Tags