KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > faces > taglib > LoadMessagesTag


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
17 package org.apache.struts.faces.taglib;
18
19
20 import java.util.Locale JavaDoc;
21
22 import javax.faces.context.FacesContext;
23
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
26
27 import org.apache.struts.Globals;
28 import org.apache.struts.faces.util.MessagesMap;
29 import org.apache.struts.util.MessageResources;
30
31
32 /**
33  * <p>Tag that exposes a specified <code>MessageResources</code> instance
34  * as <code>Map</code>, so that the embedded messages may be retrieved via
35  * value binding expressions.</p>
36  */

37
38 public class LoadMessagesTag extends TagSupport JavaDoc {
39
40
41     // ---------------------------------------------------------- Tag Attributes
42

43
44     /**
45      * <p>The name of the <code>MessageResources</code> to expose, or
46      * <code>null</code> for the default <code>MessageResources</code>
47      * for this application module.</p>
48      */

49     private String JavaDoc messages = null;
50     public void setMessages(String JavaDoc messages) {
51         this.messages = messages;
52     }
53
54
55     /**
56      * <p>The request attribute key under which a <code>Map</code>
57      * will be exposed.</p>
58      */

59     private String JavaDoc var = null;
60     public void setVar(String JavaDoc var) {
61         this.var = var;
62     }
63
64
65     // ------------------------------------------------------------- Tag Methods
66

67
68     /**
69      * <p>Expose a <code>Map</code> wrapping the specified
70      * <code>MessageResources</code> instance, for the <code>Locale</code>
71      * specified in the view root component of the current view.</p>
72      */

73     public int doStartTag() {
74
75         // Acquire the Locale to be wrapped
76
Locale JavaDoc locale =
77             FacesContext.getCurrentInstance().getViewRoot().getLocale();
78
79         // Acquire the MessageResources to be wrapped
80
MessageResources messages = null;
81         if (this.messages == null) {
82             messages = (MessageResources)
83                 pageContext.getAttribute(Globals.MESSAGES_KEY,
84                                          PageContext.REQUEST_SCOPE);
85             if (messages == null) {
86                 messages = (MessageResources)
87                     pageContext.getAttribute(Globals.MESSAGES_KEY,
88                                              PageContext.APPLICATION_SCOPE);
89             }
90         } else {
91             messages = (MessageResources)
92                 pageContext.getAttribute(this.messages,
93                                          PageContext.APPLICATION_SCOPE);
94         }
95
96         // Expose a Map instance under the specified request attribute key
97
pageContext.setAttribute(var,
98                                  new MessagesMap(messages, locale),
99                                  PageContext.REQUEST_SCOPE);
100
101         // Skip the body of this tag (if any)
102
return (SKIP_BODY);
103
104     }
105
106
107     /**
108      * <p>Release any resources allocated by this tag instance.</p>
109      */

110     public void release() {
111
112         this.messages = null;
113         this.var = null;
114
115     }
116
117
118 }
119
Popular Tags