1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.fmt;
17
18 import org.apache.commons.jelly.JellyTagException;
19 import org.apache.commons.jelly.XMLOutput;
20 import org.apache.commons.jelly.Tag;
21 import org.apache.commons.jelly.TagSupport;
22 import org.apache.commons.jelly.expression.Expression;
23
24 import org.xml.sax.SAXException;
25
26 import java.text.MessageFormat;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.ResourceBundle;
30 import java.util.MissingResourceException;
31
32 /***
33 * Support for tag handlers for <message>, the lookup up
34 * localized message tag in JSTL.
35 * @author <a href="mailto:willievu@yahoo.com">Willie Vu</a>
36 * @version 1.1
37 *
38 * @task decide how to implement setResponseLocale
39 */
40 public class MessageTag extends TagSupport {
41
42 private static final String UNDEFINED_KEY = "???";
43
44 private Expression key;
45 private Expression bundle;
46
47 private LocalizationContext locCtxt;
48
49 private String var;
50
51 private String scope;
52
53 private List params;
54
55 /*** Creates a new instance of MessageTag */
56 public MessageTag() {
57 params = new ArrayList();
58 }
59
60 /***
61 * Adds an argument (for parametric replacement) to this tag's message.
62 */
63 public void addParam(Object arg) {
64 params.add(arg);
65 }
66
67 public void doTag(XMLOutput output) throws JellyTagException {
68
69 Object keyInput = null;
70 if (this.key != null) {
71 keyInput = this.key.evaluate(context);
72
73 invokeBody(output);
74 }
75 else {
76
77 keyInput = getBodyText();
78 }
79
80
81 if ((keyInput == null) || keyInput.equals("")) {
82 try {
83 output.write("??????");
84 } catch (SAXException e) {
85 throw new JellyTagException(e);
86 }
87 return;
88 }
89
90 Object bundleInput = null;
91 if (this.bundle != null) {
92 bundleInput = this.bundle.evaluate(context);
93 }
94 if (bundleInput != null && bundleInput instanceof LocalizationContext) {
95 locCtxt = (LocalizationContext) bundleInput;
96 }
97
98 String prefix = null;
99 if (locCtxt == null) {
100 Tag t = findAncestorWithClass(this, BundleTag.class);
101 if (t != null) {
102
103 BundleTag parent = (BundleTag) t;
104 locCtxt = parent.getLocalizationContext();
105 prefix = parent.getPrefixAsString();
106 } else {
107 locCtxt = BundleTag.getLocalizationContext(context);
108 }
109 } else {
110
111 if (locCtxt.getLocale() != null) {
112
113
114
115 }
116 }
117
118 String message = UNDEFINED_KEY + keyInput + UNDEFINED_KEY;
119 if (locCtxt != null) {
120 ResourceBundle bundle = locCtxt.getResourceBundle();
121 if (bundle != null) {
122 try {
123
124 if (prefix != null) {
125 keyInput = prefix + keyInput;
126 }
127 message = bundle.getString(keyInput.toString());
128
129 if (!params.isEmpty()) {
130 Object[] messageArgs = params.toArray();
131 MessageFormat formatter = new MessageFormat("");
132 if (locCtxt.getLocale() != null) {
133 formatter.setLocale(locCtxt.getLocale());
134 }
135 formatter.applyPattern(message);
136 message = formatter.format(messageArgs);
137 }
138 } catch (MissingResourceException mre) {
139 message = UNDEFINED_KEY + keyInput + UNDEFINED_KEY;
140 }
141 }
142 }
143
144
145 if (scope != null) {
146 if (var != null) {
147 context.setVariable(var, scope, message);
148 }
149 else {
150 throw new JellyTagException( "If 'scope' is specified, 'var' must be defined for this tag" );
151 }
152 }
153 else {
154 if (var != null) {
155 context.setVariable(var, message);
156 }
157 else {
158
159 try {
160 output.write(message);
161 } catch (SAXException e) {
162 throw new JellyTagException(e);
163 }
164 }
165 }
166 }
167
168 /*** Setter for property key.
169 * @param key New value of property key.
170 *
171 */
172 public void setKey(Expression key) {
173 this.key = key;
174 }
175
176 /*** Setter for property bundle.
177 * @param bundle New value of property bundle.
178 *
179 */
180 public void setBundle(Expression bundle) {
181 this.bundle = bundle;
182 }
183
184 /*** Setter for property var.
185 * @param var New value of property var.
186 *
187 */
188 public void setVar(String var) {
189 this.var = var;
190 }
191
192 /*** Setter for property scope.
193 * @param scope New value of property scope.
194 *
195 */
196 public void setScope(String scope) {
197 this.scope = scope;
198 }
199
200 }