1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.struts.faces.renderer;
18
19
20 import java.util.ArrayList;
21 import java.util.Iterator;
22
23 import javax.faces.component.UIComponent;
24 import javax.faces.component.UIParameter;
25 import javax.faces.component.ValueHolder;
26 import javax.faces.context.FacesContext;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.Globals;
31 import org.apache.struts.util.MessageResources;
32 import org.apache.struts.util.ResponseUtils;
33
34
35 /***
36 * <p><code>Renderer</code> implementation for the <code>message</code> tag
37 * from the <em>Struts-Faces Integration Library</em>.</p>
38 *
39 * @version $Rev: 421138 $ $Date: 2006-07-11 22:41:40 -0700 (Tue, 11 Jul 2006) $
40 */
41
42 public class MessageRenderer extends WriteRenderer {
43
44
45
46
47
48 /***
49 * <p>The <code>Log</code> instance for this class.</p>
50 */
51 private static Log log = LogFactory.getLog(MessageRenderer.class);
52
53
54
55
56
57
58
59
60 /***
61 * <p>Return the message format String to be processed for this message.
62 * </p>
63 *
64 * @param context FacesContext for the response we are creating
65 * @param component Component to be rendered
66 *
67 * @exception IllegalArgumentException if no MessageResources bundle
68 * can be found
69 * @exception IllegalArgumentException if no message key can be found
70 */
71 protected String getText(FacesContext context, UIComponent component) {
72
73
74 String bundle = (String) component.getAttributes().get("bundle");
75 if (bundle == null) {
76 bundle = Globals.MESSAGES_KEY;
77 }
78 MessageResources resources = (MessageResources)
79 context.getExternalContext().getApplicationMap().get(bundle);
80 if (resources == null) {
81 throw new IllegalArgumentException("MessageResources bundle " +
82 bundle + " not found");
83 }
84
85
86 Object value = component.getAttributes().get("key");
87 if (value == null) {
88 value = ((ValueHolder) component).getValue();
89 }
90 if (value == null) {
91 throw new NullPointerException("Component '" +
92 component.getClientId(context) +
93 "' has no current value");
94 }
95 String key = value.toString();
96
97
98 ArrayList list = new ArrayList();
99 Iterator kids = component.getChildren().iterator();
100 while (kids.hasNext()) {
101 UIComponent kid = (UIComponent) kids.next();
102 if (!(kid instanceof UIParameter)) {
103 continue;
104 }
105 list.add(((UIParameter) kid).getValue());
106 }
107 Object args[] = list.toArray(new Object[list.size()]);
108
109
110 String text = resources.getMessage(context.getViewRoot().getLocale(),
111 key, args);
112 Boolean filter = (Boolean) component.getAttributes().get("filter");
113 if (filter == null) {
114 filter = Boolean.FALSE;
115 }
116 if (filter.booleanValue()) {
117 return (ResponseUtils.filter(text));
118 } else {
119 return (text);
120 }
121
122 }
123
124
125 }