1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.util;
23
24 import com.opensymphony.xwork2.util.ValueStack;
25 import com.opensymphony.xwork2.util.logging.Logger;
26 import com.opensymphony.xwork2.util.logging.LoggerFactory;
27 import com.opensymphony.xwork2.TextProvider;
28
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.LinkedList;
32
33 /***
34 * Helper methods to access text from TextProviders
35 */
36 public class TextProviderHelper {
37
38 private static final Logger LOG = LoggerFactory.getLogger(TextProviderHelper.class);
39
40 /***
41 * <p>Get a message from the first TextProvider encountered in the stack.
42 * If the first TextProvider doesn't provide the message the default message is returned.</p>
43 * <p>The search for a TextProvider is iterative from the root of the stack.</p>
44 * <p>This method was refactored from {@link org.apache.struts2.components.Text} to use a
45 * consistent implementation across UIBean components.</p>
46 * @param key the message key in the resource bundle
47 * @param defaultMessage the message to return if not found (evaluated for OGNL)
48 * @param args an array args to be used in a {@link java.text.MessageFormat} message
49 * @param stack the value stack to use for finding the text
50 *
51 * @return the message if found, otherwise the defaultMessage
52 */
53 public static String getText(String key, String defaultMessage, List<String> args, ValueStack stack) {
54 String msg = null;
55 TextProvider tp = null;
56
57 for (Iterator iterator = stack.getRoot().iterator(); iterator.hasNext();) {
58 Object o = iterator.next();
59
60 if (o instanceof TextProvider) {
61 tp = (TextProvider) o;
62 msg = tp.getText(key, null, args, stack);
63
64 break;
65 }
66 }
67
68 if (msg == null) {
69
70 msg = stack.findString(defaultMessage);
71 if (msg == null) {
72
73 msg = defaultMessage;
74 }
75
76 if (LOG.isWarnEnabled()) {
77 if (tp != null) {
78 LOG.warn("The first TextProvider in the ValueStack ("+tp.getClass().getName()+") could not locate the message resource with key '"+key+"'");
79 } else {
80 LOG.warn("Could not locate the message resource '"+key+"' as there is no TextProvider in the ValueStack.");
81 }
82 if (msg.equals(defaultMessage)) {
83 LOG.warn("The default value expression '"+defaultMessage+"' was evaluated and did not match a property. The literal value '"+defaultMessage+"' will be used.");
84 } else {
85 LOG.warn("The default value expression '"+defaultMessage+"' evaluated to '"+msg+"'");
86 }
87 }
88 }
89 return msg;
90 }
91
92 /***
93 * <p>Get a message from the first TextProvider encountered in the stack.
94 * If the first TextProvider doesn't provide the message the default message is returned.</p>
95 * <p>The search for a TextProvider is iterative from the root of the stack.</p>
96 * <p>This method was refactored from {@link org.apache.struts2.components.Text} to use a
97 * consistent implementation across UIBean components.</p>
98 * @param key the message key in the resource bundle
99 * @param defaultMessage the message to return if not found
100 * @param stack the value stack to use for finding the text
101 *
102 * @return the message if found, otherwise the defaultMessage
103 */
104 public static String getText(String key, String defaultMessage,ValueStack stack) {
105 return getText(key, defaultMessage, new LinkedList<String>(), stack);
106 }
107 }