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 import java.util.Locale;
25
26 import org.apache.struts.Globals;
27 import org.apache.struts.faces.component.LoadMessagesComponent;
28 import org.apache.struts.faces.util.MessagesMap;
29 import org.apache.struts.faces.util.Utils;
30 import org.apache.struts.util.MessageResources;
31
32 import jakarta.faces.component.UIComponent;
33 import jakarta.faces.context.ExternalContext;
34 import jakarta.faces.context.FacesContext;
35
36 /**
37 * {@code Renderer} implementation for the {@code LoadMessages} tag
38 * from the <em>Struts-Faces Integration Library</em>.
39 *
40 * @version $Rev$ $Date$
41 */
42 public class LoadMessagesRenderer extends AbstractRenderer {
43
44
45 /**
46 * Expose a {@code MessagesMap} wrapping the specified
47 * {@code MessageResources} instance, for the {@code Locale}
48 * specified in the view root component of the current view.
49 */
50 public void encodeBegin(FacesContext context, UIComponent component) {
51
52 final LoadMessagesComponent comp = (LoadMessagesComponent)
53 component;
54
55 final ExternalContext econtext = context.getExternalContext();
56
57 // Acquire the Locale to be wrapped
58 final Locale locale =
59 FacesContext.getCurrentInstance().getViewRoot().getLocale();
60
61 // Acquire the MessageResources to be wrapped
62 MessageResources messages = null;
63 if (comp.getMessages() == null) {
64 messages = Utils.getMapValue(MessageResources.class,
65 econtext.getRequestMap(), Globals.MESSAGES_KEY);
66 if (messages == null) {
67 messages = Utils.getMapValue(MessageResources.class,
68 econtext.getApplicationMap(), Globals.MESSAGES_KEY);
69 }
70 } else {
71 messages = Utils.getMapValue(MessageResources.class,
72 econtext.getApplicationMap(), comp.getMessages());
73 }
74
75 // Expose a Map instance under the specified request attribute key
76 econtext.getRequestMap()
77 .put(comp.getVar(), new MessagesMap(messages, locale));
78 }
79 }