001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.databinding.message;
020:
021: import org.apache.beehive.netui.tags.AbstractClassicTag;
022:
023: import javax.servlet.jsp.JspException;
024: import javax.servlet.jsp.tagext.SimpleTagSupport;
025: import java.util.ArrayList;
026: import java.util.List;
027:
028: /**
029: * <p>
030: * This tag provides a message schema, which can be parameterized to construct customizable messages.
031: * Curly-braces are used to identify argument place holders in the schema:
032: * <p/>
033: * For example, the following will format a message and place the result in a {@link javax.servlet.jsp.PageContext}
034: * attribute named <code>message</code>.
035: * <pre>
036: * <%
037: * pageContext.setAttribute("msgSkeleton", new String("Hello {0}. {1} {2}, the current date and time are {3}."));
038: * %>
039: * <netui-data:message value="${pageScope.msgSkeleton}" resultId="message">
040: * </pre>
041: * </p>
042: * <p>
043: * The followingn example defines a message schema, while the {@link MessageArg} tags provide the parameters that
044: * plug values into the schema. In the following example, the <netui-data:message> tag uses the <code>value</code>
045: * attribute to bind to the message schema (which was earlier added to the
046: * {@link javax.servlet.jsp.PageContext javax.servlet.jsp.PageContext} object. The two <netui-data:messageArg>
047: * tags provide the parameters to plug into the schema.
048: * <pre>
049: * <%
050: * pageContext.setAttribute("msgSkeleton", new String("To read about {0}, go to {1}."));
051: * %>
052: * ...
053: * <netui-data:message value="${pageScope.msgSkeleton}" resultId="message">
054: * <netui-data:messageArg value="messaging"/>
055: * <netui-data:messageArg value="my web page"/>
056: * </netui-data:message>
057: * ...
058: * <netui:span value="${pageScope.message}"/></pre>
059: * <p/>
060: * <p>The following message is output to the JSP page:<p>
061: * <p/>
062: * <pre>
063: * To read about messaging, go to my web page.
064: * </pre>
065: * </p>
066: *
067: * @jsptagref.tagdescription
068: * <p>
069: * This tag provides a message schema, which can be parameterized to construct customizable messages.
070: * Curly-braces are used to identify argument place holders in the schema:
071: * <p/>
072: * <pre>
073: * <%
074: * pageContext.setAttribute("msgSkeleton", new String("Hello {0}. {1} {2}, the current date and time are {3}."));
075: * %>
076: * <netui-data:message value="${pageScope.msgSkeleton}" resultId="message"></pre>
077: * @example
078: * <p>
079: * The followingn example defines a message schema, while the {@link MessageArg} tags provide the parameters that
080: * plug values into the schema. In the following example, the <netui-data:message> tag uses the <code>value</code>
081: * attribute to bind to the message schema (which was earlier added to the
082: * {@link javax.servlet.jsp.PageContext javax.servlet.jsp.PageContext} object. The two <netui-data:messageArg>
083: * tags provide the parameters to plug into the schema.
084: * <pre>
085: * <%
086: * pageContext.setAttribute("msgSkeleton", new String("To read about {0}, go to {1}."));
087: * %>
088: * ...
089: * <netui-data:message value="${pageScope.msgSkeleton}" resultId="message">
090: * <netui-data:messageArg value="messaging"/>
091: * <netui-data:messageArg value="my web page"/>
092: * </netui-data:message>
093: * ...
094: * <netui:span value="${pageScope.message}"/></pre>
095: * <p/>
096: * <p>The following message is output to the JSP page:<p>
097: * <p/>
098: * <pre>
099: * To read about messaging, go to my web page.
100: * </pre>
101: * </p>
102: *
103: * @deprecated This tag has been deprecated in favor of the i18n tags available in JSTL.
104: * @netui:tag name="message"
105: * deprecated="true"
106: * 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."
107: */
108: public class Message extends AbstractClassicTag {
109:
110: public static final String MESSAGE_ARG_KEY = "netui_bundleMessageArguments";
111:
112: private String _resultId = null;
113: private Object _value = null;
114: private List _argList = null;
115:
116: public String 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 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 value) {
160: _value = value;
161: }
162:
163: public int doStartTag() {
164: return EVAL_BODY_BUFFERED;
165: }
166:
167: public int doEndTag() throws JspException {
168: Object[] args = (_argList != null ? _argList.toArray() : null);
169:
170: if (hasErrors()) {
171: reportErrors();
172: localRelease();
173: return EVAL_PAGE;
174: }
175:
176: if (_value == null) {
177: localRelease();
178: return EVAL_PAGE;
179: }
180:
181: Object exprVal = null;
182: try {
183: exprVal = java.text.MessageFormat.format(_value.toString(),
184: args);
185: } catch (Exception e) {
186: String msg = "Error formatting message \""
187: + _value.toString() + "\". Cause: "
188: + e.getLocalizedMessage();
189: registerTagError(msg, null);
190: }
191:
192: if (hasErrors()) {
193: reportErrors();
194: localRelease();
195: return EVAL_PAGE;
196: }
197:
198: Message msgParent = null;
199: // if nested in a <netui-data:message ... /> tag, add the result of this tag as an argument.
200: if ((msgParent = (Message) SimpleTagSupport
201: .findAncestorWithClass(this , Message.class)) != null) {
202: msgParent.addMessageArgument(exprVal);
203: } else
204: pageContext.setAttribute(_resultId, exprVal);
205:
206: localRelease();
207: return EVAL_PAGE;
208: }
209:
210: public void addMessageArgument(Object messageArgument) {
211: if (_argList == null)
212: _argList = new ArrayList();
213:
214: _argList.add(messageArgument);
215: }
216:
217: protected void localRelease() {
218: super.localRelease();
219: _argList = null;
220: _value = null;
221: _resultId = null;
222: }
223: }
|