Source Code Cross Referenced for Messages.java in  » Database-ORM » castor » org » castor » util » 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 » Database ORM » castor » org.castor.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2006 Assaf Arkin, Ralf Joachim
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         * http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         *
016:         * $Id: Messages.java 6907 2007-03-28 21:24:52Z rjoachim $
017:         */
018:        package org.castor.util;
019:
020:        import java.text.MessageFormat;
021:        import java.util.Enumeration;
022:        import java.util.Hashtable;
023:        import java.util.Locale;
024:        import java.util.MissingResourceException;
025:        import java.util.ResourceBundle;
026:
027:        import org.apache.commons.logging.Log;
028:        import org.apache.commons.logging.LogFactory;
029:
030:        /**
031:         * I18N message formatting class. A static factory for obtaining
032:         * messages and formatting messages with arguments.
033:         * <p>
034:         * The resource file <tt>org.exolab.castor.util.resources.messages</tt>
035:         * contains a list of all the messages in English. Additional resource
036:         * files can be added for other languages and locales by placing them
037:         * in the same package with a language/locale prefix. See the I18N
038:         * documentation and use of resource bundles in the JDK docs.
039:         *
040:         * @author <a href="mailto:arkin AT intalio DOT com">Assaf Arkin</a>
041:         * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
042:         * @version $Revision: 6907 $ $Date: 2006-04-10 16:39:24 -0600 (Mon, 10 Apr 2006) $
043:         * @since 1.0.1
044:         */
045:        public final class Messages {
046:            //--------------------------------------------------------------------------
047:
048:            /** The <a href="http://jakarta.apache.org/commons/logging/">Jakarta Commons
049:             *  Logging </a> instance used for all logging. */
050:            private static final Log LOG = LogFactory.getLog(Messages.class);
051:
052:            /** The name of the resource holding all the messages in the English
053:             *  language. Resources for other languages and locales use the same
054:             *  name with a language/locale prefix. */
055:            public static final String RESOURCE_NAME = "org.castor.messages";
056:
057:            /** The resource bundle holds all the messages. */
058:            private static ResourceBundle _messages;
059:
060:            /** Once a format has been created once, it is cached here. */
061:            private static Hashtable _formats;
062:            //--------------------------------------------------------------------------
063:
064:            static {
065:                setDefaultLocale();
066:            }
067:
068:            //--------------------------------------------------------------------------
069:
070:            /**
071:             * Set the default locale to use for loading messages. Calling this method
072:             * will reload all the messages based on the new locale name.
073:             */
074:            public static void setDefaultLocale() {
075:                setLocale(Locale.getDefault());
076:            }
077:
078:            /**
079:             * Set the locale to use for loading messages. Calling this method
080:             * will reload all the messages based on the new locale name.
081:             * 
082:             * @param locale the locale for which a resource bundle is desired.
083:             */
084:            public static void setLocale(final Locale locale) {
085:                try {
086:                    _messages = ResourceBundle.getBundle(RESOURCE_NAME, locale);
087:                } catch (Exception except) {
088:                    _messages = new EmptyResourceBundle();
089:                    LOG.error("Failed to locate messages resource "
090:                            + RESOURCE_NAME);
091:                }
092:                _formats = new Hashtable();
093:            }
094:
095:            /**
096:             * Format the named message using a single argument and return the
097:             * full message text.
098:             *
099:             * @param message The message name
100:             * @param arg1 The first argument
101:             * @return The full message text
102:             */
103:            public static String format(final String message, final Object arg1) {
104:                return format(message, new Object[] { arg1 });
105:            }
106:
107:            /**
108:             * Format the named message using two argument and return the
109:             * full message text.
110:             *
111:             * @param message The message name
112:             * @param arg1 The first argument
113:             * @param arg2 The second argument
114:             * @return The full message text
115:             */
116:            public static String format(final String message,
117:                    final Object arg1, final Object arg2) {
118:                return format(message, new Object[] { arg1, arg2 });
119:            }
120:
121:            /**
122:             * Format the named message using three argument and return the
123:             * full message text.
124:             *
125:             * @param message The message name
126:             * @param arg1 The first argument
127:             * @param arg2 The second argument
128:             * @param arg3 The third argument
129:             * @return The full message text
130:             */
131:            public static String format(final String message,
132:                    final Object arg1, final Object arg2, final Object arg3) {
133:                return format(message, new Object[] { arg1, arg2, arg3 });
134:            }
135:
136:            /**
137:             * Format the named message using any number of arguments and return the
138:             * full message text.
139:             *
140:             * @param message The message name
141:             * @param args Argument list
142:             * @return The full message text
143:             */
144:            public static String format(final String message,
145:                    final Object[] args) {
146:
147:                try {
148:                    MessageFormat mf = (MessageFormat) _formats.get(message);
149:                    if (mf == null) {
150:                        String msg;
151:                        try {
152:                            msg = _messages.getString(message);
153:                        } catch (MissingResourceException except) {
154:                            return message;
155:                        }
156:                        mf = new MessageFormat(msg);
157:                        _formats.put(message, mf);
158:                    }
159:                    return mf.format(args);
160:                } catch (Exception except) {
161:                    return "An internal error occured while processing message "
162:                            + message;
163:                }
164:            }
165:
166:            /**
167:             * Return the text of the named message without formatting.
168:             *
169:             * @param message The message name
170:             * @return The full message text
171:             */
172:            public static String message(final String message) {
173:                try {
174:                    return _messages.getString(message);
175:                } catch (MissingResourceException except) {
176:                    return message;
177:                }
178:            }
179:
180:            //--------------------------------------------------------------------------
181:
182:            /**
183:             * Hide default constructor of utility class.
184:             */
185:            private Messages() {
186:            }
187:
188:            //--------------------------------------------------------------------------
189:
190:            /**
191:             * A empty resource bundle.
192:             */
193:            private static class EmptyResourceBundle extends ResourceBundle
194:                    implements  Enumeration {
195:                /**
196:                 * {@inheritDoc}
197:                 * @see java.util.ResourceBundle#getKeys()
198:                 */
199:                public Enumeration getKeys() {
200:                    return this ;
201:                }
202:
203:                /**
204:                 * {@inheritDoc}
205:                 * @see java.util.ResourceBundle#handleGetObject(java.lang.String)
206:                 */
207:                protected Object handleGetObject(final String name) {
208:                    return "[Missing message " + name + "]";
209:                }
210:
211:                /**
212:                 * {@inheritDoc}
213:                 * @see java.util.Enumeration#hasMoreElements()
214:                 */
215:                public boolean hasMoreElements() {
216:                    return false;
217:                }
218:
219:                /**
220:                 * {@inheritDoc}
221:                 * @see java.util.Enumeration#nextElement()
222:                 */
223:                public Object nextElement() {
224:                    return null;
225:                }
226:            }
227:
228:            //--------------------------------------------------------------------------
229:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.