KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > content > FieldValueTag


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.content;
17
18 import com.blandware.atleap.common.util.StringUtil;
19 import com.blandware.atleap.webapp.util.core.WebappUtil;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.struts.taglib.TagUtils;
23 import org.apache.struts.util.RequestUtils;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.PageContext JavaDoc;
28 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * <p>Searches for value in specified map containing pairs (localeIdentifier -&gt; message).
34  * If <code>language</code> attribute is not specified, it is taken from session by key
35  * <code>org.apache.struts.Globals.LOCALE_KEY</code>
36  * </p>
37  * <p>
38  * While searching, following rules are applied:<br />
39  * <ul>
40  * <li>
41  * 1. If valueMap is null or is empty, null is returned.
42  * </li>
43  * <li>
44  * 2. Otherwise, searching through valueMap for <code>language</code>
45  * </li>
46  * <li>
47  * 2.5 Following steps are applied only if <code>complexSearch</code> is "true"
48  * </li>
49  * <li>
50  * 3. If nothing has been found in step 2, searching for default locale for our application
51  * </li>
52  * <li>
53  * 4. If still not found, returning first mapping value from valueMap
54  * </li>
55  * </ul>
56  * </p>
57  * <p>
58  * Allowed attributes are:
59  * <ul>
60  * <li>
61  * <b>valueMap</b> - map of values from which some value will be taken
62  * </li>
63  * <li>
64  * <b>language</b> - content locale identifier for which the <b>valueMap</b>
65  * will be searched
66  * </li>
67  * <li>
68  * <b>complexSearch</b> - whether to use more complex rules: if no value was
69  * found for given language, search for value corresponding to default locale,
70  * and if still not found, return first value from map. May be "true" or
71  * "false", default is "false".
72  * </li>
73  * <li>
74  * <b>filter</b> - whether to encode HTML-sensitive characters (like ampersand)
75  * when writing result to page
76  * </li>
77  * <li>
78  * <b>var</b> - name of scope variable to export result to. If not given, result
79  * is written directly to page.
80  * </li>
81  * <li>
82  * <b>scope</b> - scope of variable to export result to
83  * </li>
84  * </ul>
85  * </p>
86  * <p><a HREF="FieldValueTag.java.htm"><i>View Source</i></a></p>
87  *
88  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
89  * @version $Revision: 1.10 $ $Date: 2005/09/21 13:45:57 $
90  * @jsp.tag name="fieldValue"
91  * body-content="empty"
92  * @see org.apache.struts.Globals#LOCALE_KEY
93  */

94 public class FieldValueTag extends SimpleTagSupport JavaDoc {
95
96     protected transient final Log log = LogFactory.getLog(FieldValueTag.class);
97
98     /**
99      * Map to get value from
100      */

101     protected Map JavaDoc valueMap;
102
103     /**
104      * Locale to get value for. By default it is taken from session
105      */

106     protected String JavaDoc language;
107
108     /**
109      * If this attribute is set to <code>true</code> and no value in map found for
110      * currently active locale, <code>null</code> will be returned, otherwise more complex
111      * search will be executed
112      */

113     protected Boolean JavaDoc complexSearch = Boolean.FALSE;
114
115     /**
116      * Whether or not to replace characters, which are sensitive in HTML, with their entity equivalents.<br />
117      */

118     protected Boolean JavaDoc filter = Boolean.TRUE;
119
120     /**
121      * Name of variable to export value
122      */

123     protected String JavaDoc var;
124
125     /**
126      * Scope to export variable to
127      */

128     protected String JavaDoc scope;
129
130     /**
131      * Returns map of values
132      *
133      * @return map of values
134      * @jsp.attribute required="true"
135      * rtexprvalue="true"
136      * type="java.util.Map"
137      * description="Map to get value from"
138      */

139     public Map JavaDoc getValueMap() {
140         return valueMap;
141     }
142
143     /**
144      * Sets map of values
145      *
146      * @param valueMap map of values to set
147      */

148     public void setValueMap(Map JavaDoc valueMap) {
149         this.valueMap = valueMap;
150     }
151
152     /**
153      * Returns language for which to obtain value
154      *
155      * @return language
156      * @jsp.attribute required="false"
157      * rtexprvalue="true"
158      * type="java.lang.String"
159      * description="Locale to get value for"
160      */

161     public String JavaDoc getLanguage() {
162         return language;
163     }
164
165     /**
166      * Sets language for which to obtain value
167      *
168      * @param language language to set
169      */

170     public void setLanguage(String JavaDoc language) {
171         this.language = language;
172     }
173
174     /**
175      * Returns whether more complex search should be performed
176      *
177      * @return whether more complex search should be performed
178      * @see #complexSearch
179      * @jsp.attribute required="false"
180      * rtexprvalue="true"
181      * type="java.lang.Boolean"
182      * description="Whether or not to execute complex search, if none is found for currently active locale"
183      */

184     public Boolean JavaDoc getComplexSearch() {
185         return complexSearch;
186     }
187
188     /**
189      * Sets whether more complex search should be performed
190      *
191      * @param complexSearch whether more complex search should be performed
192      * @see #complexSearch
193      */

194     public void setComplexSearch(Boolean JavaDoc complexSearch) {
195         this.complexSearch = complexSearch;
196     }
197
198     /**
199      * Returns whether or not to replace characters, which are sensitive in
200      * HTML, with their entity equivalents.
201      *
202      * @return whether to replace characters, which are sensitive in HTML
203      * @jsp.attribute required="false"
204      * rtexprvalue="true"
205      * type="java.lang.Boolean"
206      * description="Whether or not to replace characters, which are sensitive in HTML, with their entity equivalents"
207      */

208     public Boolean JavaDoc getFilter() {
209         return filter;
210     }
211
212     /**
213      * Sets whether or not to replace characters, which are sensitive in HTML,
214      * with their entity equivalents.
215      *
216      * @param filter whether to replace characters, which are sensitive in HTML
217      */

218     public void setFilter(Boolean JavaDoc filter) {
219         this.filter = filter;
220     }
221
222     /**
223      * Returns name of variable that will accept the result
224      *
225      * @return name of variable
226      * @jsp.attribute required="false"
227      * rtexprvalue="true"
228      * type="java.lang.String"
229      * description="Name of variable to export message"
230      */

231     public String JavaDoc getVar() {
232         return var;
233     }
234
235     /**
236      * Sets name of variable that will accept the result
237      *
238      * @param var name of variable to set
239      */

240     public void setVar(String JavaDoc var) {
241         this.var = var;
242     }
243
244     /**
245      * Returns variable scope
246      *
247      * @return variable scope
248      * @jsp.attribute required="false"
249      * rtexprvalue="true"
250      * type="java.lang.String"
251      * description="Scope to export variable to"
252      */

253     public String JavaDoc getScope() {
254         return scope;
255     }
256
257     /**
258      * Sets variable scope
259      *
260      * @param scope variable scope to set
261      */

262     public void setScope(String JavaDoc scope) {
263         this.scope = scope;
264     }
265
266     /**
267      * Processes the tag
268      *
269      * @throws JspException
270      */

271     public void doTag() throws JspException JavaDoc {
272
273         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
274         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
275
276         if ( language == null ) {
277             Locale JavaDoc locale = RequestUtils.getUserLocale(request, null);
278             if ( locale == null ) {
279                 locale = Locale.getDefault();
280             }
281             language = locale.getLanguage();
282         }
283
284         if ( complexSearch == null ) {
285             complexSearch = Boolean.FALSE;
286         }
287
288         if ( filter == null ) {
289             filter = Boolean.TRUE;
290         }
291
292         String JavaDoc fieldValue = WebappUtil.getFieldValue(valueMap, language, (HttpServletRequest JavaDoc) pageContext.getRequest(), !complexSearch.booleanValue());
293
294         if ( fieldValue == null ) {
295             fieldValue = new String JavaDoc();
296         }
297
298         if ( filter.booleanValue() ) {
299             fieldValue = StringUtil.htmlEncode(fieldValue);
300         }
301
302         TagUtils tagUtils = TagUtils.getInstance();
303         if ( var != null ) {
304             // save value in specified scope
305
int varScope = PageContext.PAGE_SCOPE;
306             if ( scope != null ) {
307                 varScope = tagUtils.getScope(scope);
308             }
309             pageContext.setAttribute(var, fieldValue, varScope);
310
311         } else {
312             // write message directly to page
313
tagUtils.write(pageContext, fieldValue);
314         }
315
316     }
317
318 }
319
Popular Tags