PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/struts-2.2.1/src/xwork-core/src/main/java/com/opensymphony/xwork2/TextProviderSupport.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 327 lines | 114 code | 28 blank | 185 comment | 29 complexity | aaef764bcd0095d7466149e11e464ee6 MD5 | raw file
  1. /*
  2. * Copyright 2002-2006,2009 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.opensymphony.xwork2;
  17. import com.opensymphony.xwork2.util.LocalizedTextUtil;
  18. import com.opensymphony.xwork2.util.ValueStack;
  19. import java.util.*;
  20. /**
  21. * Default TextProvider implementation.
  22. *
  23. * @author Jason Carreira
  24. * @author Rainer Hermanns
  25. */
  26. public class TextProviderSupport implements ResourceBundleTextProvider {
  27. private Class clazz;
  28. private LocaleProvider localeProvider;
  29. private ResourceBundle bundle;
  30. /**
  31. * Default constructor
  32. */
  33. public TextProviderSupport() {
  34. }
  35. /**
  36. * Constructor.
  37. *
  38. * @param clazz a clazz to use for reading the resource bundle.
  39. * @param provider a locale provider.
  40. */
  41. public TextProviderSupport(Class clazz, LocaleProvider provider) {
  42. this.clazz = clazz;
  43. this.localeProvider = provider;
  44. }
  45. /**
  46. * Constructor.
  47. *
  48. * @param bundle the resource bundle.
  49. * @param provider a locale provider.
  50. */
  51. public TextProviderSupport(ResourceBundle bundle, LocaleProvider provider) {
  52. this.bundle = bundle;
  53. this.localeProvider = provider;
  54. }
  55. /**
  56. * @param bundle the resource bundle.
  57. */
  58. public void setBundle(ResourceBundle bundle) {
  59. this.bundle = bundle;
  60. }
  61. /**
  62. * @param clazz a clazz to use for reading the resource bundle.
  63. */
  64. public void setClazz(Class clazz) {
  65. this.clazz = clazz;
  66. }
  67. /**
  68. * @param localeProvider a locale provider.
  69. */
  70. public void setLocaleProvider(LocaleProvider localeProvider) {
  71. this.localeProvider = localeProvider;
  72. }
  73. /**
  74. * Checks if a key is available in the resource bundles associated with this action.
  75. * The resource bundles are searched, starting with the one associated
  76. * with this particular action, and testing all its superclasses' bundles.
  77. * It will stop once a bundle is found that contains the given text. This gives
  78. * a cascading style that allow global texts to be defined for an application base
  79. * class.
  80. */
  81. public boolean hasKey(String key) {
  82. String message;
  83. if (clazz != null) {
  84. message = LocalizedTextUtil.findText(clazz, key, getLocale(), null, new Object[0] );
  85. } else {
  86. message = LocalizedTextUtil.findText(bundle, key, getLocale(), null, new Object[0]);
  87. }
  88. return message != null;
  89. }
  90. /**
  91. * Get a text from the resource bundles associated with this action.
  92. * The resource bundles are searched, starting with the one associated
  93. * with this particular action, and testing all its superclasses' bundles.
  94. * It will stop once a bundle is found that contains the given text. This gives
  95. * a cascading style that allow global texts to be defined for an application base
  96. * class.
  97. *
  98. * @param key name of text to be found
  99. * @return value of named text
  100. */
  101. public String getText(String key) {
  102. return getText(key, key, Collections.emptyList());
  103. }
  104. /**
  105. * Get a text from the resource bundles associated with this action.
  106. * The resource bundles are searched, starting with the one associated
  107. * with this particular action, and testing all its superclasses' bundles.
  108. * It will stop once a bundle is found that contains the given text. This gives
  109. * a cascading style that allow global texts to be defined for an application base
  110. * class. If no text is found for this text name, the default value is returned.
  111. *
  112. * @param key name of text to be found
  113. * @param defaultValue the default value which will be returned if no text is found
  114. * @return value of named text
  115. */
  116. public String getText(String key, String defaultValue) {
  117. return getText(key, defaultValue, Collections.emptyList());
  118. }
  119. /**
  120. * Get a text from the resource bundles associated with this action.
  121. * The resource bundles are searched, starting with the one associated
  122. * with this particular action, and testing all its superclasses' bundles.
  123. * It will stop once a bundle is found that contains the given text. This gives
  124. * a cascading style that allow global texts to be defined for an application base
  125. * class. If no text is found for this text name, the default value is returned.
  126. *
  127. * @param key name of text to be found
  128. * @param defaultValue the default value which will be returned if no text is found
  129. * @return value of named text
  130. */
  131. public String getText(String key, String defaultValue, String arg) {
  132. List<Object> args = new ArrayList<Object>();
  133. args.add(arg);
  134. return getText(key, defaultValue, args);
  135. }
  136. /**
  137. * Get a text from the resource bundles associated with this action.
  138. * The resource bundles are searched, starting with the one associated
  139. * with this particular action, and testing all its superclasses' bundles.
  140. * It will stop once a bundle is found that contains the given text. This gives
  141. * a cascading style that allow global texts to be defined for an application base
  142. * class. If no text is found for this text name, the default value is returned.
  143. *
  144. * @param key name of text to be found
  145. * @param args a List of args to be used in a MessageFormat message
  146. * @return value of named text
  147. */
  148. public String getText(String key, List<Object> args) {
  149. return getText(key, key, args);
  150. }
  151. /**
  152. * Get a text from the resource bundles associated with this action.
  153. * The resource bundles are searched, starting with the one associated
  154. * with this particular action, and testing all its superclasses' bundles.
  155. * It will stop once a bundle is found that contains the given text. This gives
  156. * a cascading style that allow global texts to be defined for an application base
  157. * class. If no text is found for this text name, the default value is returned.
  158. *
  159. * @param key name of text to be found
  160. * @param args an array of args to be used in a MessageFormat message
  161. * @return value of named text
  162. */
  163. public String getText(String key, String[] args) {
  164. return getText(key, key, args);
  165. }
  166. /**
  167. * Get a text from the resource bundles associated with this action.
  168. * The resource bundles are searched, starting with the one associated
  169. * with this particular action, and testing all its superclasses' bundles.
  170. * It will stop once a bundle is found that contains the given text. This gives
  171. * a cascading style that allow global texts to be defined for an application base
  172. * class. If no text is found for this text name, the default value is returned.
  173. *
  174. * @param key name of text to be found
  175. * @param defaultValue the default value which will be returned if no text is found
  176. * @param args a List of args to be used in a MessageFormat message
  177. * @return value of named text
  178. */
  179. public String getText(String key, String defaultValue, List<Object> args) {
  180. Object[] argsArray = ((args != null && !args.equals(Collections.emptyList())) ? args.toArray() : null);
  181. if (clazz != null) {
  182. return LocalizedTextUtil.findText(clazz, key, getLocale(), defaultValue, argsArray);
  183. } else {
  184. return LocalizedTextUtil.findText(bundle, key, getLocale(), defaultValue, argsArray);
  185. }
  186. }
  187. /**
  188. * Get a text from the resource bundles associated with this action.
  189. * The resource bundles are searched, starting with the one associated
  190. * with this particular action, and testing all its superclasses' bundles.
  191. * It will stop once a bundle is found that contains the given text. This gives
  192. * a cascading style that allow global texts to be defined for an application base
  193. * class. If no text is found for this text name, the default value is returned.
  194. *
  195. * @param key name of text to be found
  196. * @param defaultValue the default value which will be returned if no text is found
  197. * @param args an array of args to be used in a MessageFormat message
  198. * @return value of named text
  199. */
  200. public String getText(String key, String defaultValue, String[] args) {
  201. if (clazz != null) {
  202. return LocalizedTextUtil.findText(clazz, key, getLocale(), defaultValue, args);
  203. } else {
  204. return LocalizedTextUtil.findText(bundle, key, getLocale(), defaultValue, args);
  205. }
  206. }
  207. /**
  208. * Gets a message based on a key using the supplied args, as defined in
  209. * {@link java.text.MessageFormat}, or, if the message is not found, a supplied
  210. * default value is returned. Instead of using the value stack in the ActionContext
  211. * this version of the getText() method uses the provided value stack.
  212. *
  213. * @param key the resource bundle key that is to be searched for
  214. * @param defaultValue the default value which will be returned if no message is found
  215. * @param args a list args to be used in a {@link java.text.MessageFormat} message
  216. * @param stack the value stack to use for finding the text
  217. * @return the message as found in the resource bundle, or defaultValue if none is found
  218. */
  219. public String getText(String key, String defaultValue, List<Object> args, ValueStack stack) {
  220. Object[] argsArray = ((args != null) ? args.toArray() : null);
  221. Locale locale;
  222. if (stack == null){
  223. locale = getLocale();
  224. }else{
  225. locale = (Locale) stack.getContext().get(ActionContext.LOCALE);
  226. }
  227. if (locale == null) {
  228. locale = getLocale();
  229. }
  230. if (clazz != null) {
  231. return LocalizedTextUtil.findText(clazz, key, locale, defaultValue, argsArray, stack);
  232. } else {
  233. return LocalizedTextUtil.findText(bundle, key, locale, defaultValue, argsArray, stack);
  234. }
  235. }
  236. /**
  237. * Gets a message based on a key using the supplied args, as defined in
  238. * {@link java.text.MessageFormat}, or, if the message is not found, a supplied
  239. * default value is returned. Instead of using the value stack in the ActionContext
  240. * this version of the getText() method uses the provided value stack.
  241. *
  242. * @param key the resource bundle key that is to be searched for
  243. * @param defaultValue the default value which will be returned if no message is found
  244. * @param args an array args to be used in a {@link java.text.MessageFormat} message
  245. * @param stack the value stack to use for finding the text
  246. * @return the message as found in the resource bundle, or defaultValue if none is found
  247. */
  248. public String getText(String key, String defaultValue, String[] args, ValueStack stack) {
  249. Locale locale;
  250. if (stack == null){
  251. locale = getLocale();
  252. }else{
  253. locale = (Locale) stack.getContext().get(ActionContext.LOCALE);
  254. }
  255. if (locale == null) {
  256. locale = getLocale();
  257. }
  258. if (clazz != null) {
  259. return LocalizedTextUtil.findText(clazz, key, locale, defaultValue, args, stack);
  260. } else {
  261. return LocalizedTextUtil.findText(bundle, key, locale, defaultValue, args, stack);
  262. }
  263. }
  264. /**
  265. * Get the named bundle.
  266. * <p/>
  267. * You can override the getLocale() methodName to change the behaviour of how
  268. * to choose locale for the bundles that are returned. Typically you would
  269. * use the TextProvider interface to get the users configured locale, or use
  270. * your own methodName to allow the user to select the locale and store it in
  271. * the session (by using the SessionAware interface).
  272. *
  273. * @param aBundleName bundle name
  274. * @return a resource bundle
  275. */
  276. public ResourceBundle getTexts(String aBundleName) {
  277. return LocalizedTextUtil.findResourceBundle(aBundleName, getLocale());
  278. }
  279. /**
  280. * Get the resource bundle associated with this action.
  281. * This will be based on the actual subclass that is used.
  282. *
  283. * @return resouce bundle
  284. */
  285. public ResourceBundle getTexts() {
  286. if (clazz != null) {
  287. return getTexts(clazz.getName());
  288. }
  289. return bundle;
  290. }
  291. /**
  292. * Get's the locale from the localeProvider.
  293. *
  294. * @return the locale from the localeProvider.
  295. */
  296. private Locale getLocale() {
  297. return localeProvider.getLocale();
  298. }
  299. }