Source code: org/apache/struts/taglib/bean/WriteTag.java
1 /*
2 * $Id: WriteTag.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.bean;
20
21 import java.math.BigDecimal;
22 import java.math.BigInteger;
23 import java.text.DecimalFormat;
24 import java.text.Format;
25 import java.text.NumberFormat;
26 import java.text.SimpleDateFormat;
27 import java.util.Locale;
28
29 import javax.servlet.jsp.JspException;
30 import javax.servlet.jsp.tagext.TagSupport;
31
32 import org.apache.struts.taglib.TagUtils;
33 import org.apache.struts.util.MessageResources;
34
35 /**
36 * Tag that retrieves the specified property of the specified bean, converts
37 * it to a String representation (if necessary), and writes it to the current
38 * output stream, optionally filtering characters that are sensitive in HTML.
39 *
40 * @version $Rev: 54929 $ $Date: 2004-10-16 09:38:42 -0700 (Sat, 16 Oct 2004) $
41 */
42 public class WriteTag extends TagSupport {
43
44 /**
45 * The key to search default format string for java.sql.Timestamp in resources.
46 */
47 public static final String SQL_TIMESTAMP_FORMAT_KEY =
48 "org.apache.struts.taglib.bean.format.sql.timestamp";
49
50 /**
51 * The key to search default format string for java.sql.Date in resources.
52 */
53 public static final String SQL_DATE_FORMAT_KEY =
54 "org.apache.struts.taglib.bean.format.sql.date";
55
56 /**
57 * The key to search default format string for java.sql.Time in resources.
58 */
59 public static final String SQL_TIME_FORMAT_KEY =
60 "org.apache.struts.taglib.bean.format.sql.time";
61
62 /**
63 * The key to search default format string for java.util.Date in resources.
64 */
65 public static final String DATE_FORMAT_KEY =
66 "org.apache.struts.taglib.bean.format.date";
67
68 /**
69 * The key to search default format string for int (byte, short, etc.) in resources.
70 */
71 public static final String INT_FORMAT_KEY =
72 "org.apache.struts.taglib.bean.format.int";
73
74 /**
75 * The key to search default format string for float (double, BigDecimal) in
76 * resources.
77 */
78 public static final String FLOAT_FORMAT_KEY =
79 "org.apache.struts.taglib.bean.format.float";
80
81 /**
82 * The message resources for this package.
83 */
84 protected static MessageResources messages =
85 MessageResources.getMessageResources(
86 "org.apache.struts.taglib.bean.LocalStrings");
87
88 // ------------------------------------------------------------- Properties
89
90 /**
91 * Filter the rendered output for characters that are sensitive in HTML?
92 */
93 protected boolean filter = true;
94
95 public boolean getFilter() {
96 return (this.filter);
97 }
98
99 public void setFilter(boolean filter) {
100 this.filter = filter;
101 }
102
103 /**
104 * Should we ignore missing beans and simply output nothing?
105 */
106 protected boolean ignore = false;
107
108 public boolean getIgnore() {
109 return (this.ignore);
110 }
111
112 public void setIgnore(boolean ignore) {
113 this.ignore = ignore;
114 }
115
116 /**
117 * Name of the bean that contains the data we will be rendering.
118 */
119 protected String name = null;
120
121 public String getName() {
122 return (this.name);
123 }
124
125 public void setName(String name) {
126 this.name = name;
127 }
128
129 /**
130 * Name of the property to be accessed on the specified bean.
131 */
132 protected String property = null;
133
134 public String getProperty() {
135 return (this.property);
136 }
137
138 public void setProperty(String property) {
139 this.property = property;
140 }
141
142 /**
143 * The scope to be searched to retrieve the specified bean.
144 */
145 protected String scope = null;
146
147 public String getScope() {
148 return (this.scope);
149 }
150
151 public void setScope(String scope) {
152 this.scope = scope;
153 }
154
155 /**
156 * The format string to be used as format to convert
157 * value to String.
158 */
159 protected String formatStr = null;
160
161 public String getFormat() {
162 return (this.formatStr);
163 }
164
165 public void setFormat(String formatStr) {
166 this.formatStr = formatStr;
167 }
168
169 /**
170 * The key to search format string in applciation resources
171 */
172 protected String formatKey = null;
173
174 public String getFormatKey() {
175 return (this.formatKey);
176 }
177
178 public void setFormatKey(String formatKey) {
179 this.formatKey = formatKey;
180 }
181
182 /**
183 * The session scope key under which our Locale is stored.
184 */
185 protected String localeKey = null;
186
187 public String getLocale() {
188 return (this.localeKey);
189 }
190
191 public void setLocale(String localeKey) {
192 this.localeKey = localeKey;
193 }
194
195 /**
196 * The servlet context attribute key for our resources.
197 */
198 protected String bundle = null;
199
200 public String getBundle() {
201 return (this.bundle);
202 }
203
204 public void setBundle(String bundle) {
205 this.bundle = bundle;
206 }
207
208 // --------------------------------------------------------- Public Methods
209
210 /**
211 * Process the start tag.
212 *
213 * @exception JspException if a JSP exception has occurred
214 */
215 public int doStartTag() throws JspException {
216
217 // Look up the requested bean (if necessary)
218 if (ignore) {
219 if (TagUtils.getInstance().lookup(pageContext, name, scope) == null) {
220 return (SKIP_BODY); // Nothing to output
221 }
222 }
223
224 // Look up the requested property value
225 Object value = TagUtils.getInstance().lookup(pageContext, name, property, scope);
226
227 if (value == null) {
228 return (SKIP_BODY); // Nothing to output
229 }
230
231 // Convert value to the String with some formatting
232 String output = formatValue(value);
233
234 // Print this property value to our output writer, suitably filtered
235 if (filter) {
236 TagUtils.getInstance().write(pageContext, TagUtils.getInstance().filter(output));
237 } else {
238 TagUtils.getInstance().write(pageContext, output);
239 }
240
241 // Continue processing this page
242 return (SKIP_BODY);
243
244 }
245
246 /**
247 * Retrieve format string from message bundle and return null if
248 * message not found or message string.
249 *
250 * @param formatKey value to use as key to search message in bundle
251 * @exception JspException if a JSP exception has occurred
252 */
253 protected String retrieveFormatString(String formatKey) throws JspException {
254 String result =
255 TagUtils.getInstance().message(
256 pageContext,
257 this.bundle,
258 this.localeKey,
259 formatKey);
260
261 if ((result != null)
262 && !(result.startsWith("???") && result.endsWith("???"))) {
263
264 return result;
265
266 } else {
267 return null;
268 }
269
270 }
271
272 /**
273 * Format value according to specified format string (as tag attribute or
274 * as string from message resources) or to current user locale.
275 *
276 * When a format string is retrieved from the message resources,
277 * <code>applyLocalizedPattern</code> is used. For more about localized
278 * patterns, see
279 * <http://www.dei.unipd.it/corsi/fi2ae-docs/source/jdk1.1.7/src/java/text/resources/>.
280 * (To obtain the correct value for some characters, you may need to view
281 * the file in a hex editor and then use the Unicode escape form in the
282 * property resources file.)
283 *
284 * @param valueToFormat value to process and convert to String
285 * @exception JspException if a JSP exception has occurred
286 */
287 protected String formatValue(Object valueToFormat) throws JspException {
288 Format format = null;
289 Object value = valueToFormat;
290 Locale locale = TagUtils.getInstance().getUserLocale(pageContext, this.localeKey);
291 boolean formatStrFromResources = false;
292 String formatString = formatStr;
293
294 // Return String object as is.
295 if (value instanceof java.lang.String) {
296 return (String) value;
297 } else {
298
299 // Try to retrieve format string from resources by the key from formatKey.
300 if ((formatString == null) && (formatKey != null)) {
301 formatString = retrieveFormatString(this.formatKey);
302 if (formatString != null) {
303 formatStrFromResources = true;
304 }
305 }
306
307 // Prepare format object for numeric values.
308 if (value instanceof Number) {
309
310 if (formatString == null) {
311 if ((value instanceof Byte)
312 || (value instanceof Short)
313 || (value instanceof Integer)
314 || (value instanceof Long)
315 || (value instanceof BigInteger)) {
316
317 formatString = retrieveFormatString(INT_FORMAT_KEY);
318
319 } else if (
320 (value instanceof Float)
321 || (value instanceof Double)
322 || (value instanceof BigDecimal)) {
323
324 formatString = retrieveFormatString(FLOAT_FORMAT_KEY);
325 }
326
327 if (formatString != null) {
328 formatStrFromResources = true;
329 }
330 }
331
332 if (formatString != null) {
333 try {
334 format = NumberFormat.getNumberInstance(locale);
335 if (formatStrFromResources) {
336 ((DecimalFormat) format).applyLocalizedPattern(
337 formatString);
338 } else {
339 ((DecimalFormat) format).applyPattern(formatString);
340 }
341
342 } catch (IllegalArgumentException e) {
343 JspException ex =
344 new JspException(
345 messages.getMessage("write.format", formatString));
346 TagUtils.getInstance().saveException(pageContext, ex);
347 throw ex;
348 }
349 }
350
351 } else if (value instanceof java.util.Date) {
352
353 if (formatString == null) {
354
355 if (value instanceof java.sql.Timestamp) {
356 formatString =
357 retrieveFormatString(SQL_TIMESTAMP_FORMAT_KEY);
358
359 } else if (value instanceof java.sql.Date) {
360 formatString = retrieveFormatString(SQL_DATE_FORMAT_KEY);
361
362 } else if (value instanceof java.sql.Time) {
363 formatString = retrieveFormatString(SQL_TIME_FORMAT_KEY);
364
365 } else if (value instanceof java.util.Date) {
366 formatString = retrieveFormatString(DATE_FORMAT_KEY);
367 }
368
369 }
370
371 if (formatString != null) {
372 format = new SimpleDateFormat(formatString, locale);
373 }
374 }
375 }
376
377 if (format != null) {
378 return format.format(value);
379 } else {
380 return value.toString();
381 }
382
383 }
384
385 /**
386 * Release all allocated resources.
387 */
388 public void release() {
389
390 super.release();
391 filter = true;
392 ignore = false;
393 name = null;
394 property = null;
395 scope = null;
396 formatStr = null;
397 formatKey = null;
398 localeKey = null;
399 bundle = null;
400
401 }
402
403 }