001: package org.apache.velocity.tools.struts;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.struts.action.ActionMessage;
029: import org.apache.struts.action.ActionMessages;
030: import org.apache.struts.util.MessageResources;
031:
032: /**
033: * <p>View tool to work with the Struts action messages.</p>
034: * <p><pre>
035: * Template example(s):
036: * #if( $messages.exist() )
037: * #foreach( $e in $messages.all )
038: * $e <br>
039: * #end
040: * #end
041: *
042: * Toolbox configuration:
043: *
044: * <tool>
045: * <key>messages</key>
046: * <scope>request</scope>
047: * <class>org.apache.velocity.tools.struts.ActionMessagesTool</class>
048: * </tool>
049: * </pre></p>
050: *
051: * <p>This tool should only be used in the request scope.</p>
052: *
053: * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
054: * @author Nathan Bubna
055: * @since VelocityTools 1.1
056: * @version $Id: ActionMessagesTool.java 479724 2006-11-27 18:49:37Z nbubna $
057: */
058: public class ActionMessagesTool extends MessageResourcesTool {
059:
060: protected static final Log LOG = LogFactory
061: .getLog(ActionMessagesTool.class);
062:
063: /** A reference to the queued action messages. */
064: protected ActionMessages actionMsgs;
065:
066: /**
067: * Default constructor. Tool must be initialized before use.
068: */
069: public ActionMessagesTool() {
070: }
071:
072: /**
073: * Initializes this tool.
074: *
075: * @param obj the current ViewContext
076: * @throws IllegalArgumentException if the param is not a ViewContext
077: */
078: public void init(Object obj) {
079: //setup superclass instance members
080: super .init(obj);
081:
082: this .actionMsgs = StrutsUtils.getMessages(this .request);
083: }
084:
085: /*************************** Public Methods ***********************/
086:
087: /**
088: * <p>Returns <code>true</code> if there are action messages queued,
089: * otherwise <code>false</code>.</p>
090: */
091: public boolean exist() {
092: if (actionMsgs == null) {
093: return false;
094: }
095: return !actionMsgs.isEmpty();
096: }
097:
098: /**
099: * <p>Returns true if there are action messages queued for the specified
100: * category of messages, otherwise <code>false</code>.</p>
101: *
102: * @param property the category of messages to check for
103: */
104: public boolean exist(String property) {
105: if (actionMsgs == null) {
106: return false;
107: }
108: return (actionMsgs.size(property) > 0);
109: }
110:
111: /**
112: * Returns the number of action messages queued.
113: */
114: public int getSize() {
115: if (actionMsgs == null) {
116: return 0;
117: }
118: return actionMsgs.size();
119: }
120:
121: /**
122: * Returns the number of action messages queued for a particular property.
123: *
124: * @param property the category of messages to check for
125: */
126: public int getSize(String property) {
127: if (actionMsgs == null) {
128: return 0;
129: }
130: return actionMsgs.size(property);
131: }
132:
133: /**
134: * <p>
135: * This a convenience method and the equivalent of
136: * <code>$messages.get($messages.globalName)</code>.
137: * </p>
138: * <p>
139: * Returns the set of localized action messages as an
140: * list of strings for all action messages queued of the
141: * global category or <code>null</code> if no messages
142: * are queued for the specified category. If the message
143: * resources don't contain an action message for a
144: * particular message key, the key itself is used.
145: * </p>
146: *
147: * @return a list of all messages stored under the "global" property
148: */
149: public List getGlobal() {
150: return get(getGlobalName());
151: }
152:
153: /**
154: * Returns the set of localized action messages as an
155: * <code>java.util.List</code> of strings for all actionMsgs
156: * queued or <code>null</code> if no messages are queued.
157: * If the message resources don't contain a message for a
158: * particular key, the key itself is used as the message.
159: */
160: public List getAll() {
161: return get(null);
162: }
163:
164: /**
165: * Returns a List of all queued action messages using
166: * the specified message resource bundle.
167: *
168: * @param bundle the message resource bundle to use
169: * @see #getAll()
170: */
171: public List getAll(String bundle) {
172: return get(null, bundle);
173: }
174:
175: /**
176: * Returns the set of localized action messages as an
177: * <code>java.util.List</code> of strings for all actionMsgs
178: * queued of the specified category or <code>null</code>
179: * if no messages are queued for the specified category. If the
180: * message resources don't contain a message for a particular
181: * key, the key itself is used as the message.
182: *
183: * @param property the category of actionMsgs to operate on
184: */
185: public List get(String property) {
186: return get(property, null);
187: }
188:
189: /**
190: * Returns the set of localized action messages as a
191: * <code>java.util.List</code> of strings for all action messages
192: * queued of the specified category or <code>null</code>
193: * if no action messages are queued for the specified category. If the
194: * message resources don't contain an action message for a particular
195: * action key, the key itself is used as action message.
196: *
197: * @param property the category of actionMsgs to operate on
198: * @param bundle the message resource bundle to use
199: */
200: public List get(String property, String bundle) {
201: if (actionMsgs == null || actionMsgs.isEmpty()) {
202: return null;
203: }
204:
205: Iterator msgs;
206: if (property == null) {
207: msgs = actionMsgs.get();
208: } else {
209: msgs = actionMsgs.get(property);
210: }
211:
212: if (!msgs.hasNext()) {
213: return null;
214: }
215:
216: MessageResources res = getResources(bundle);
217: List list = new ArrayList();
218:
219: while (msgs.hasNext()) {
220: ActionMessage msg = (ActionMessage) msgs.next();
221:
222: String message = null;
223: if (res != null && msg.isResource()) {
224: message = res.getMessage(this .locale, msg.getKey(), msg
225: .getValues());
226:
227: if (message == null) {
228: LOG
229: .warn("Message for key "
230: + msg.getKey()
231: + " could not be found in message resources.");
232: }
233: }
234:
235: if (message == null) {
236: // if the resource bundle wasn't found or
237: // ActionMessage.isResource() returned false or the key
238: // wasn't found in the resources, then use the key
239: message = msg.getKey();
240: }
241: list.add(message);
242: }
243: return list;
244: }
245:
246: /**
247: * Returns the default "GLOBAL" category name that can be used for
248: * messages that are not associated with a particular property.
249: */
250: public String getGlobalName() {
251: return ActionMessages.GLOBAL_MESSAGE;
252: }
253:
254: }
|