Source Code Cross Referenced for TextProviderSupport.java in  » J2EE » webwork-2.2.6 » com » opensymphony » xwork » 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 » J2EE » webwork 2.2.6 » com.opensymphony.xwork 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2006 by OpenSymphony
003:         * All rights reserved.
004:         */
005:        package com.opensymphony.xwork;
006:
007:        import com.opensymphony.xwork.util.LocalizedTextUtil;
008:        import com.opensymphony.xwork.util.OgnlValueStack;
009:
010:        import java.util.*;
011:
012:        /**
013:         * Default TextProvider implementation.
014:         *
015:         * @author Jason Carreira
016:         * @author Rainer Hermanns
017:         */
018:        public class TextProviderSupport implements  TextProvider {
019:
020:            private Class clazz;
021:            private LocaleProvider localeProvider;
022:            private ResourceBundle bundle;
023:
024:            /**
025:             * Constructor.
026:             *
027:             * @param clazz   a clazz to use for reading the resource bundle.
028:             * @param provider  a locale provider.
029:             */
030:            public TextProviderSupport(Class clazz, LocaleProvider provider) {
031:                this .clazz = clazz;
032:                this .localeProvider = provider;
033:            }
034:
035:            /**
036:             * Constructor.
037:             *
038:             * @param bundle    the resource bundle.
039:             * @param provider  a locale provider.
040:             */
041:            public TextProviderSupport(ResourceBundle bundle,
042:                    LocaleProvider provider) {
043:                this .bundle = bundle;
044:                this .localeProvider = provider;
045:            }
046:
047:            /**
048:             * Checks if a key is available in the resource bundles associated with this action.
049:             * The resource bundles are searched, starting with the one associated
050:             * with this particular action, and testing all its superclasses' bundles.
051:             * It will stop once a bundle is found that contains the given text. This gives
052:             * a cascading style that allow global texts to be defined for an application base
053:             * class.
054:             */
055:            public boolean hasKey(String key) {
056:                String message = null;
057:                OgnlValueStack stack = ActionContext.getContext()
058:                        .getValueStack();
059:                if (clazz != null) {
060:                    message = LocalizedTextUtil.findText(clazz, key,
061:                            getLocale(), null, new Object[0], stack, false);
062:                } else {
063:                    message = LocalizedTextUtil.findText(bundle, key,
064:                            getLocale(), null, new Object[0], stack, false);
065:                }
066:                return message == null ? false : true;
067:            }
068:
069:            /**
070:             * Get a text from the resource bundles associated with this action.
071:             * The resource bundles are searched, starting with the one associated
072:             * with this particular action, and testing all its superclasses' bundles.
073:             * It will stop once a bundle is found that contains the given text. This gives
074:             * a cascading style that allow global texts to be defined for an application base
075:             * class.
076:             *
077:             * @param key name of text to be found
078:             * @return value of named text
079:             */
080:            public String getText(String key) {
081:                return getText(key, key, Collections.EMPTY_LIST);
082:            }
083:
084:            /**
085:             * Get a text from the resource bundles associated with this action.
086:             * The resource bundles are searched, starting with the one associated
087:             * with this particular action, and testing all its superclasses' bundles.
088:             * It will stop once a bundle is found that contains the given text. This gives
089:             * a cascading style that allow global texts to be defined for an application base
090:             * class. If no text is found for this text name, the default value is returned.
091:             *
092:             * @param key    name of text to be found
093:             * @param defaultValue the default value which will be returned if no text is found
094:             * @return value of named text
095:             */
096:            public String getText(String key, String defaultValue) {
097:                return getText(key, defaultValue, Collections.EMPTY_LIST);
098:            }
099:
100:            /**
101:             * Get a text from the resource bundles associated with this action.
102:             * The resource bundles are searched, starting with the one associated
103:             * with this particular action, and testing all its superclasses' bundles.
104:             * It will stop once a bundle is found that contains the given text. This gives
105:             * a cascading style that allow global texts to be defined for an application base
106:             * class. If no text is found for this text name, the default value is returned.
107:             *
108:             * @param key    name of text to be found
109:             * @param defaultValue the default value which will be returned if no text is found
110:             * @return value of named text
111:             */
112:            public String getText(String key, String defaultValue, String arg) {
113:                List args = new ArrayList();
114:                args.add(arg);
115:                return getText(key, defaultValue, args);
116:            }
117:
118:            /**
119:             * Get a text from the resource bundles associated with this action.
120:             * The resource bundles are searched, starting with the one associated
121:             * with this particular action, and testing all its superclasses' bundles.
122:             * It will stop once a bundle is found that contains the given text. This gives
123:             * a cascading style that allow global texts to be defined for an application base
124:             * class. If no text is found for this text name, the default value is returned.
125:             *
126:             * @param key name of text to be found
127:             * @param args      a List of args to be used in a MessageFormat message
128:             * @return value of named text
129:             */
130:            public String getText(String key, List args) {
131:                return getText(key, key, args);
132:            }
133:
134:            /**
135:             * Get a text from the resource bundles associated with this action.
136:             * The resource bundles are searched, starting with the one associated
137:             * with this particular action, and testing all its superclasses' bundles.
138:             * It will stop once a bundle is found that contains the given text. This gives
139:             * a cascading style that allow global texts to be defined for an application base
140:             * class. If no text is found for this text name, the default value is returned.
141:             *
142:             * @param key name of text to be found
143:             * @param args      an array of args to be used in a MessageFormat message
144:             * @return value of named text
145:             */
146:            public String getText(String key, String[] args) {
147:                return getText(key, key, args);
148:            }
149:
150:            /**
151:             * Get a text from the resource bundles associated with this action.
152:             * The resource bundles are searched, starting with the one associated
153:             * with this particular action, and testing all its superclasses' bundles.
154:             * It will stop once a bundle is found that contains the given text. This gives
155:             * a cascading style that allow global texts to be defined for an application base
156:             * class. If no text is found for this text name, the default value is returned.
157:             *
158:             * @param key    name of text to be found
159:             * @param defaultValue the default value which will be returned if no text is found
160:             * @param args         a List of args to be used in a MessageFormat message
161:             * @return value of named text
162:             */
163:            public String getText(String key, String defaultValue, List args) {
164:                Object[] argsArray = ((args != null && !args
165:                        .equals(Collections.EMPTY_LIST)) ? args.toArray()
166:                        : null);
167:                if (clazz != null) {
168:                    return LocalizedTextUtil.findText(clazz, key, getLocale(),
169:                            defaultValue, argsArray);
170:                } else {
171:                    return LocalizedTextUtil.findText(bundle, key, getLocale(),
172:                            defaultValue, argsArray);
173:                }
174:            }
175:
176:            /**
177:             * Get a text from the resource bundles associated with this action.
178:             * The resource bundles are searched, starting with the one associated
179:             * with this particular action, and testing all its superclasses' bundles.
180:             * It will stop once a bundle is found that contains the given text. This gives
181:             * a cascading style that allow global texts to be defined for an application base
182:             * class. If no text is found for this text name, the default value is returned.
183:             *
184:             * @param key          name of text to be found
185:             * @param defaultValue the default value which will be returned if no text is found
186:             * @param args         an array of args to be used in a MessageFormat message
187:             * @return value of named text
188:             */
189:            public String getText(String key, String defaultValue, String[] args) {
190:                if (clazz != null) {
191:                    return LocalizedTextUtil.findText(clazz, key, getLocale(),
192:                            defaultValue, args);
193:                } else {
194:                    return LocalizedTextUtil.findText(bundle, key, getLocale(),
195:                            defaultValue, args);
196:                }
197:            }
198:
199:            /**
200:             * Gets a message based on a key using the supplied args, as defined in
201:             * {@link java.text.MessageFormat}, or, if the message is not found, a supplied
202:             * default value is returned. Instead of using the value stack in the ActionContext
203:             * this version of the getText() method uses the provided value stack.
204:             *
205:             * @param key    the resource bundle key that is to be searched for
206:             * @param defaultValue the default value which will be returned if no message is found
207:             * @param args         a list args to be used in a {@link java.text.MessageFormat} message
208:             * @param stack        the value stack to use for finding the text
209:             * @return the message as found in the resource bundle, or defaultValue if none is found
210:             */
211:            public String getText(String key, String defaultValue, List args,
212:                    OgnlValueStack stack) {
213:                Object[] argsArray = ((args != null) ? args.toArray() : null);
214:                Locale locale = (Locale) stack.getContext().get(
215:                        ActionContext.LOCALE);
216:                if (locale == null) {
217:                    locale = getLocale();
218:                }
219:                if (clazz != null) {
220:                    return LocalizedTextUtil.findText(clazz, key, locale,
221:                            defaultValue, argsArray, stack);
222:                } else {
223:                    return LocalizedTextUtil.findText(bundle, key, locale,
224:                            defaultValue, argsArray, stack);
225:                }
226:            }
227:
228:            /**
229:             * Gets a message based on a key using the supplied args, as defined in
230:             * {@link java.text.MessageFormat}, or, if the message is not found, a supplied
231:             * default value is returned. Instead of using the value stack in the ActionContext
232:             * this version of the getText() method uses the provided value stack.
233:             *
234:             * @param key          the resource bundle key that is to be searched for
235:             * @param defaultValue the default value which will be returned if no message is found
236:             * @param args         an array args to be used in a {@link java.text.MessageFormat} message
237:             * @param stack        the value stack to use for finding the text
238:             * @return the message as found in the resource bundle, or defaultValue if none is found
239:             */
240:            public String getText(String key, String defaultValue,
241:                    String[] args, OgnlValueStack stack) {
242:                Locale locale = (Locale) stack.getContext().get(
243:                        ActionContext.LOCALE);
244:                if (locale == null) {
245:                    locale = getLocale();
246:                }
247:                if (clazz != null) {
248:                    return LocalizedTextUtil.findText(clazz, key, locale,
249:                            defaultValue, args, stack);
250:                } else {
251:                    return LocalizedTextUtil.findText(bundle, key, locale,
252:                            defaultValue, args, stack);
253:                }
254:
255:            }
256:
257:            /**
258:             * Get the named bundle.
259:             * <p/>
260:             * You can override the getLocale() methodName to change the behaviour of how
261:             * to choose locale for the bundles that are returned. Typically you would
262:             * use the TextProvider interface to get the users configured locale, or use
263:             * your own methodName to allow the user to select the locale and store it in
264:             * the session (by using the SessionAware interface).
265:             *
266:             * @param aBundleName bundle name
267:             * @return a resource bundle
268:             */
269:            public ResourceBundle getTexts(String aBundleName) {
270:                return LocalizedTextUtil.findResourceBundle(aBundleName,
271:                        getLocale());
272:            }
273:
274:            /**
275:             * Get the resource bundle associated with this action.
276:             * This will be based on the actual subclass that is used.
277:             *
278:             * @return resouce bundle
279:             */
280:            public ResourceBundle getTexts() {
281:                if (clazz != null) {
282:                    return getTexts(clazz.getName());
283:                }
284:                return bundle;
285:            }
286:
287:            /**
288:             * Get's the locale from the localeProvider.
289:             *
290:             * @return the locale from the localeProvider.
291:             */
292:            private Locale getLocale() {
293:                return localeProvider.getLocale();
294:            }
295:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.