KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > taglib > I18nMessageTag


1 /*
2  * $Id: I18nMessageTag.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.taglib;
26
27 import java.text.MessageFormat JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ResourceBundle JavaDoc;
31 import javax.servlet.jsp.JspException JavaDoc;
32 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
33 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
34
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.UtilJ2eeCompat;
37
38 /**
39  * I18nMessageTag - JSP tag to use a resource bundle to internationalize
40  * content in a web page.
41  *
42  * @author <a HREF="mailto:k3ysss@yahoo.com">Jian He</a>
43  * @author <a HREF="mailto:">Quake Wang</a>
44  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
45  * @version $Rev: 5462 $
46  * @since 2.0
47  */

48 public class I18nMessageTag extends BodyTagSupport JavaDoc {
49     
50     public static final String JavaDoc module = I18nMessageTag.class.getName();
51
52     private String JavaDoc key = null;
53
54     private String JavaDoc value = null;
55
56     private ResourceBundle JavaDoc bundle = null;
57
58     private final List JavaDoc arguments = new ArrayList JavaDoc();
59
60     public void setKey(String JavaDoc key) {
61         this.key = key;
62     }
63
64     public String JavaDoc getKey() {
65         return this.key;
66     }
67
68     public void setValue(String JavaDoc value) {
69         this.value = value;
70     }
71
72     public String JavaDoc getValue() {
73         return this.value;
74     }
75
76     public void setBundleId(String JavaDoc bundleId) {
77         this.bundle = (ResourceBundle JavaDoc) pageContext.getAttribute(bundleId);
78     }
79
80     public void addArgument(Object JavaDoc argument) {
81         this.arguments.add(argument);
82     }
83
84     public int doStartTag() throws JspException JavaDoc {
85         try {
86             if (this.bundle == null) {
87                 I18nBundleTag bundleTag = (I18nBundleTag) TagSupport.findAncestorWithClass(this, I18nBundleTag.class);
88
89                 if (bundleTag != null) {
90                     this.bundle = bundleTag.getBundle();
91                 }
92             }
93
94             if (this.bundle != null) this.value = this.bundle.getString(this.key);
95
96             /* this is a bad assumption, it won't necessarily be an ISO8859_1 charset, much better to just use the string as is
97             this.value = new String(s.getBytes("ISO8859_1"));
98              */

99         } catch (Exception JavaDoc e) {
100             if (UtilJ2eeCompat.useNestedJspException(pageContext.getServletContext())) {
101                 throw new JspException JavaDoc(e.getMessage(), e);
102             } else {
103                 Debug.logError(e, "Server does not support nested exceptions, here is the exception", module);
104                 throw new JspException JavaDoc(e.toString());
105             }
106         }
107
108         return EVAL_BODY_AGAIN;
109     }
110
111     public int doEndTag() throws JspException JavaDoc {
112         try {
113             if (this.value != null && this.arguments != null && this.arguments.size() > 0) {
114                 MessageFormat JavaDoc messageFormat = new MessageFormat JavaDoc(this.value);
115
116                 messageFormat.setLocale(this.bundle.getLocale());
117                 this.value = messageFormat.format(arguments.toArray());
118             }
119
120             if (this.value != null) this.pageContext.getOut().print(this.value);
121         } catch (Exception JavaDoc e) {
122             if (UtilJ2eeCompat.useNestedJspException(pageContext.getServletContext())) {
123                 throw new JspException JavaDoc(e.getMessage(), e);
124             } else {
125                 Debug.logError(e, "Server does not support nested exceptions, here is the exception", module);
126                 throw new JspException JavaDoc(e.toString());
127             }
128         }
129
130         return EVAL_PAGE;
131     }
132 }
133
Popular Tags