%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.fmt.MessageTag |
|
|
1 | /* |
|
2 | * Copyright 2002,2004 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 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 | 1 | 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 | 12 | public MessageTag() { |
57 | 12 | params = new ArrayList(); |
58 | 12 | } |
59 | ||
60 | /** |
|
61 | * Adds an argument (for parametric replacement) to this tag's message. |
|
62 | */ |
|
63 | public void addParam(Object arg) { |
|
64 | 7 | params.add(arg); |
65 | 7 | } |
66 | ||
67 | public void doTag(XMLOutput output) throws JellyTagException { |
|
68 | ||
69 | 12 | Object keyInput = null; |
70 | 12 | if (this.key != null) { |
71 | 4 | keyInput = this.key.evaluate(context); |
72 | // process <param> sub tags |
|
73 | 4 | invokeBody(output); |
74 | } |
|
75 | else { |
|
76 | // get key from body |
|
77 | 8 | keyInput = getBodyText(); |
78 | } |
|
79 | ||
80 | ||
81 | 12 | if ((keyInput == null) || keyInput.equals("")) { |
82 | try { |
|
83 | 0 | output.write("??????"); |
84 | 0 | } catch (SAXException e) { |
85 | 0 | throw new JellyTagException(e); |
86 | } |
|
87 | 0 | return; |
88 | } |
|
89 | ||
90 | 12 | Object bundleInput = null; |
91 | 12 | if (this.bundle != null) { |
92 | 0 | bundleInput = this.bundle.evaluate(context); |
93 | } |
|
94 | 12 | if (bundleInput != null && bundleInput instanceof LocalizationContext) { |
95 | 0 | locCtxt = (LocalizationContext) bundleInput; |
96 | } |
|
97 | ||
98 | 12 | String prefix = null; |
99 | 12 | if (locCtxt == null) { |
100 | 12 | Tag t = findAncestorWithClass(this, BundleTag.class); |
101 | 12 | if (t != null) { |
102 | // use resource bundle from parent <bundle> tag |
|
103 | 12 | BundleTag parent = (BundleTag) t; |
104 | 12 | locCtxt = parent.getLocalizationContext(); |
105 | 12 | prefix = parent.getPrefixAsString(); |
106 | } else { |
|
107 | 0 | locCtxt = BundleTag.getLocalizationContext(context); |
108 | } |
|
109 | } else { |
|
110 | // localization context taken from 'bundle' attribute |
|
111 | 0 | if (locCtxt.getLocale() != null) { |
112 | // TODO |
|
113 | // SetLocaleSupport.setResponseLocale(pageContext, |
|
114 | // locCtxt.getLocale()); |
|
115 | } |
|
116 | } |
|
117 | ||
118 | 12 | String message = UNDEFINED_KEY + keyInput + UNDEFINED_KEY; |
119 | 12 | if (locCtxt != null) { |
120 | 12 | ResourceBundle bundle = locCtxt.getResourceBundle(); |
121 | 12 | if (bundle != null) { |
122 | try { |
|
123 | // prepend 'prefix' attribute from parent bundle |
|
124 | 12 | if (prefix != null) { |
125 | 6 | keyInput = prefix + keyInput; |
126 | } |
|
127 | 12 | message = bundle.getString(keyInput.toString()); |
128 | // Perform parametric replacement if required |
|
129 | 9 | if (!params.isEmpty()) { |
130 | 3 | Object[] messageArgs = params.toArray(); |
131 | 3 | MessageFormat formatter = new MessageFormat(""); |
132 | 3 | if (locCtxt.getLocale() != null) { |
133 | 0 | formatter.setLocale(locCtxt.getLocale()); |
134 | } |
|
135 | 3 | formatter.applyPattern(message); |
136 | 3 | message = formatter.format(messageArgs); |
137 | } |
|
138 | 9 | } catch (MissingResourceException mre) { |
139 | 3 | message = UNDEFINED_KEY + keyInput + UNDEFINED_KEY; |
140 | } |
|
141 | } |
|
142 | } |
|
143 | ||
144 | ||
145 | 12 | if (scope != null) { |
146 | 0 | if (var != null) { |
147 | 0 | context.setVariable(var, scope, message); |
148 | } |
|
149 | else { |
|
150 | 0 | throw new JellyTagException( "If 'scope' is specified, 'var' must be defined for this tag" ); |
151 | } |
|
152 | } |
|
153 | else { |
|
154 | 12 | if (var != null) { |
155 | 12 | context.setVariable(var, message); |
156 | } |
|
157 | else { |
|
158 | // write the message |
|
159 | try { |
|
160 | 0 | output.write(message); |
161 | 0 | } catch (SAXException e) { |
162 | 0 | throw new JellyTagException(e); |
163 | } |
|
164 | } |
|
165 | } |
|
166 | 12 | } |
167 | ||
168 | /** Setter for property key. |
|
169 | * @param key New value of property key. |
|
170 | * |
|
171 | */ |
|
172 | public void setKey(Expression key) { |
|
173 | 4 | this.key = key; |
174 | 4 | } |
175 | ||
176 | /** Setter for property bundle. |
|
177 | * @param bundle New value of property bundle. |
|
178 | * |
|
179 | */ |
|
180 | public void setBundle(Expression bundle) { |
|
181 | 0 | this.bundle = bundle; |
182 | 0 | } |
183 | ||
184 | /** Setter for property var. |
|
185 | * @param var New value of property var. |
|
186 | * |
|
187 | */ |
|
188 | public void setVar(String var) { |
|
189 | 12 | this.var = class="keyword">var; |
190 | 12 | } |
191 | ||
192 | /** Setter for property scope. |
|
193 | * @param scope New value of property scope. |
|
194 | * |
|
195 | */ |
|
196 | public void setScope(String scope) { |
|
197 | 0 | this.scope = scope; |
198 | 0 | } |
199 | ||
200 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |