Source Code Cross Referenced for ActionMessagesTool.java in  » Template-Engine » Velocity » org » apache » velocity » tools » struts » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Template Engine » Velocity » org.apache.velocity.tools.struts 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 &lt;br&gt;
039:         *     #end
040:         *   #end
041:         *
042:         * Toolbox configuration:
043:         *
044:         * &lt;tool&gt;
045:         *   &lt;key&gt;messages&lt;/key&gt;
046:         *   &lt;scope&gt;request&lt;/scope&gt;
047:         *   &lt;class&gt;org.apache.velocity.tools.struts.ActionMessagesTool&lt;/class&gt;
048:         * &lt;/tool&gt;
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.