1 /*
2 * $Id$
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 package org.apache.struts.faces.renderer;
23
24
25 import java.util.ArrayList;
26
27 import org.apache.struts.Globals;
28 import org.apache.struts.util.MessageResources;
29 import org.apache.struts.util.ResponseUtils;
30
31 import jakarta.faces.component.UIComponent;
32 import jakarta.faces.component.UIParameter;
33 import jakarta.faces.component.ValueHolder;
34 import jakarta.faces.context.FacesContext;
35
36
37 /**
38 * <p><code>Renderer</code> implementation for the <code>message</code> tag
39 * from the <em>Struts-Faces Integration Library</em>.</p>
40 *
41 * @version $Rev$ $Date$
42 */
43
44 public class MessageRenderer extends WriteRenderer {
45
46
47 // -------------------------------------------------------- Static Variables
48
49
50 // ---------------------------------------------------------- Public Methods
51
52
53 // ------------------------------------------------------- Protected Methods
54
55
56 /**
57 * <p>Return the message format String to be processed for this message.
58 * </p>
59 *
60 * @param context FacesContext for the response we are creating
61 * @param component Component to be rendered
62 *
63 * @exception IllegalArgumentException if no MessageResources bundle
64 * can be found
65 * @exception IllegalArgumentException if no message key can be found
66 */
67 protected String getText(FacesContext context, UIComponent component) {
68
69 // Look up the MessageResources bundle to be used
70 String bundle = (String) component.getAttributes().get("bundle");
71 if (bundle == null) {
72 bundle = Globals.MESSAGES_KEY;
73 }
74 MessageResources resources = (MessageResources)
75 context.getExternalContext().getApplicationMap().get(bundle);
76 if (resources == null) { // FIXME - i18n
77 throw new IllegalArgumentException("MessageResources bundle " +
78 bundle + " not found");
79 }
80
81 // Look up the message key to be used
82 Object value = component.getAttributes().get("key");
83 if (value == null) {
84 value = ((ValueHolder) component).getValue();
85 }
86 if (value == null) { // FIXME - i18n
87 throw new NullPointerException("Component '" +
88 component.getClientId(context) +
89 "' has no current value");
90 }
91 String key = value.toString();
92
93 // Build the substitution arguments list
94 ArrayList<Object> list = new ArrayList<>();
95 for (UIComponent kid : component.getChildren()) {
96 if (!(kid instanceof UIParameter)) {
97 continue;
98 }
99 list.add(((UIParameter) kid).getValue());
100 }
101 Object args[] = list.toArray(new Object[0]);
102
103 // Look up the requested message
104 String text = resources.getMessage(context.getViewRoot().getLocale(),
105 key, args);
106 Boolean filter = (Boolean) component.getAttributes().get("filter");
107 if (filter == null) {
108 filter = Boolean.FALSE;
109 }
110 if (filter.booleanValue()) {
111 return (ResponseUtils.filter(text));
112 } else {
113 return (text);
114 }
115
116 }
117
118
119 }