KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > databinding > message > Message


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.databinding.message;
19
20 import org.apache.beehive.netui.tags.AbstractClassicTag;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * <p>
29  * This tag provides a message schema, which can be parameterized to construct customizable messages.
30  * Curly-braces are used to identify argument place holders in the schema:
31  * <p/>
32  * For example, the following will format a message and place the result in a {@link javax.servlet.jsp.PageContext}
33  * attribute named <code>message</code>.
34  * <pre>
35  * &lt;%
36  * pageContext.setAttribute("msgSkeleton", new String("Hello {0}. {1} {2}, the current date and time are {3}."));
37  * %>
38  * &lt;netui-data:message value="${pageScope.msgSkeleton}" resultId="message">
39  * </pre>
40  * </p>
41  * <p>
42  * The followingn example defines a message schema, while the {@link MessageArg} tags provide the parameters that
43  * plug values into the schema. In the following example, the &lt;netui-data:message> tag uses the <code>value</code>
44  * attribute to bind to the message schema (which was earlier added to the
45  * {@link javax.servlet.jsp.PageContext javax.servlet.jsp.PageContext} object. The two &lt;netui-data:messageArg>
46  * tags provide the parameters to plug into the schema.
47  * <pre>
48  * &lt;%
49  * pageContext.setAttribute("msgSkeleton", new String("To read about {0}, go to {1}."));
50  * %&gt;
51  * ...
52  * &lt;netui-data:message value="${pageScope.msgSkeleton}" resultId="message"&gt;
53  * &lt;netui-data:messageArg value="messaging"/&gt;
54  * &lt;netui-data:messageArg value="my web page"/&gt;
55  * &lt;/netui-data:message&gt;
56  * ...
57  * &lt;netui:span value="${pageScope.message}"/&gt;</pre>
58  * <p/>
59  * <p>The following message is output to the JSP page:<p>
60  * <p/>
61  * <pre>
62  * To read about messaging, go to my web page.
63  * </pre>
64  * </p>
65  *
66  * @jsptagref.tagdescription
67  * <p>
68  * This tag provides a message schema, which can be parameterized to construct customizable messages.
69  * Curly-braces are used to identify argument place holders in the schema:
70  * <p/>
71  * <pre>
72  * &lt;%
73  * pageContext.setAttribute("msgSkeleton", new String("Hello {0}. {1} {2}, the current date and time are {3}."));
74  * %>
75  * &lt;netui-data:message value="${pageScope.msgSkeleton}" resultId="message"></pre>
76  * @example
77  * <p>
78  * The followingn example defines a message schema, while the {@link MessageArg} tags provide the parameters that
79  * plug values into the schema. In the following example, the &lt;netui-data:message> tag uses the <code>value</code>
80  * attribute to bind to the message schema (which was earlier added to the
81  * {@link javax.servlet.jsp.PageContext javax.servlet.jsp.PageContext} object. The two &lt;netui-data:messageArg>
82  * tags provide the parameters to plug into the schema.
83  * <pre>
84  * &lt;%
85  * pageContext.setAttribute("msgSkeleton", new String("To read about {0}, go to {1}."));
86  * %&gt;
87  * ...
88  * &lt;netui-data:message value="${pageScope.msgSkeleton}" resultId="message"&gt;
89  * &lt;netui-data:messageArg value="messaging"/&gt;
90  * &lt;netui-data:messageArg value="my web page"/&gt;
91  * &lt;/netui-data:message&gt;
92  * ...
93  * &lt;netui:span value="${pageScope.message}"/&gt;</pre>
94  * <p/>
95  * <p>The following message is output to the JSP page:<p>
96  * <p/>
97  * <pre>
98  * To read about messaging, go to my web page.
99  * </pre>
100  * </p>
101  *
102  * @deprecated This tag has been deprecated in favor of the i18n tags available in JSTL.
103  * @netui:tag name="message"
104  * deprecated="true"
105  * description="Allows you to format messages according to any sequence you want, using one or more values from arguments defined in MessageArg tag(s). The results are available to the page context."
106  */

107 public class Message
108     extends AbstractClassicTag {
109
110     public static final String JavaDoc MESSAGE_ARG_KEY = "netui_bundleMessageArguments";
111
112     private String JavaDoc _resultId = null;
113     private Object JavaDoc _value = null;
114     private List JavaDoc _argList = null;
115
116     public String JavaDoc getTagName() {
117         return "Message";
118     }
119
120     /**
121      * Set the attribute name under which the output formatted message will be available. The message
122      * will be stored in the JSP EL implicit object <code>pageScope</code>. If the value of this attribute
123      * is <code>foo</code>, the resulting message will be available with <code>${pageScope.foo}</code>.
124      *
125      * @jsptagref.attributedescription
126      * Set the attribute name under which the output formatted message will be available. The message
127      * will be stored in the JSP EL implicit object <code>pageScope</code>. If the value of this attribute
128      * is <code>foo</code>, the resulting message will be available with <code>${pageScope.foo}</code>.
129      * @jsptagref.attributesyntaxvalue <i>string_result</i>
130      * @netui:attribute required="true"
131      */

132     public void setResultId(String JavaDoc resultId) {
133         _resultId = resultId;
134     }
135
136     /**
137      * <p>
138      * Set the object to use when formatting a message. This value should be either a String or be convertable
139      * to a String via its {@link Object#toString()} method. In ordet for format the message, this value
140      * should appear as:
141      * <pre>
142      * Hello, {0}!
143      * </pre>
144      * where the <code>{0}</code> can be filled in during formatting via the {@link MessageArg} tag.
145      * </p>
146      * @jsptagref.attributedescription
147      * <p>
148      * Set the object to use when formatting a message. This value should be either a String or be convertable
149      * to a String via its {@link Object#toString()} method. In ordet for format the message, this value
150      * should appear as:
151      * <pre>
152      * Hello, {0}!
153      * </pre>
154      * where the <code>{0}</code> can be filled in during formatting via the {@link MessageArg} tag.
155      * </p>
156      * @jsptagref.attributesyntaxvalue <i>expression_value</i>
157      * @netui:attribute required="true" rtexprvalue="true"
158      */

159     public void setValue(Object JavaDoc value) {
160         _value = value;
161     }
162
163     public int doStartTag() {
164         return EVAL_BODY_BUFFERED;
165     }
166
167     public int doEndTag()
168             throws JspException JavaDoc {
169         Object JavaDoc[] args = (_argList != null ? _argList.toArray() : null);
170
171         if(hasErrors()) {
172             reportErrors();
173             localRelease();
174             return EVAL_PAGE;
175         }
176
177         if(_value == null) {
178             localRelease();
179             return EVAL_PAGE;
180         }
181
182         Object JavaDoc exprVal = null;
183         try {
184             exprVal = java.text.MessageFormat.format(_value.toString(), args);
185         }
186         catch(Exception JavaDoc e) {
187             String JavaDoc msg = "Error formatting message \"" + _value.toString() + "\". Cause: " + e.getLocalizedMessage();
188             registerTagError(msg, null);
189         }
190
191         if(hasErrors()) {
192             reportErrors();
193             localRelease();
194             return EVAL_PAGE;
195         }
196
197         Message msgParent = null;
198         // if nested in a <netui-data:message ... /> tag, add the result of this tag as an argument.
199
if((msgParent = (Message)SimpleTagSupport.findAncestorWithClass(this, Message.class)) != null) {
200             msgParent.addMessageArgument(exprVal);
201         }
202         else
203             pageContext.setAttribute(_resultId, exprVal);
204
205         localRelease();
206         return EVAL_PAGE;
207     }
208
209     public void addMessageArgument(Object JavaDoc messageArgument) {
210         if(_argList == null)
211             _argList = new ArrayList JavaDoc();
212
213         _argList.add(messageArgument);
214     }
215
216     protected void localRelease() {
217         super.localRelease();
218         _argList = null;
219         _value = null;
220         _resultId = null;
221     }
222 }
223
Popular Tags