KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > taglibs > resourcebundle > MessageTag


1 package org.jahia.taglibs.resourcebundle;
2
3
4 import java.io.IOException JavaDoc;
5 import java.text.MessageFormat JavaDoc;
6 import java.util.Locale JavaDoc;
7 import java.util.MissingResourceException JavaDoc;
8
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpSession JavaDoc;
11 import javax.servlet.jsp.JspException JavaDoc;
12 import javax.servlet.jsp.JspWriter JavaDoc;
13 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
14
15 import org.jahia.engines.EngineMessage;
16 import org.jahia.params.ParamBean;
17 import org.jahia.resourcebundle.JahiaResourceBundle;
18
19
20 /**
21  * Support for Jahia Message ResourceBundle within Jahia
22  *
23  * Returns the requested resource.
24  *
25  * @see JahiaResourceBundle
26  * @see SetAdminResourceBundleTag
27  * @see JahiaEnginesResources.properties
28  *
29  * @author Khue Nguyen
30  */

31 public class MessageTag extends TagSupport JavaDoc {
32
33     private static org.apache.log4j.Logger logger =
34             org.apache.log4j.Logger.getLogger(MessageTag.class);
35
36     private static final String JavaDoc CLASS_NAME = MessageTag.class.getName();
37
38     private String JavaDoc key = null;
39     private String JavaDoc name = null;
40     private boolean display = true;
41     private String JavaDoc property = null;
42
43     public void setKey(String JavaDoc key) {
44         if ( key == null )
45             key = "";
46         this.key = key;
47     }
48
49     public String JavaDoc getName() {
50         return name;
51     }
52     public void setName(String JavaDoc name) {
53         this.name = name;
54     }
55     public boolean isDisplay() {
56         return display;
57     }
58     public void setDisplay(boolean display) {
59         this.display = display;
60     }
61     public String JavaDoc getProperty() {
62         return property;
63     }
64     public void setProperty(String JavaDoc property) {
65         this.property = property;
66     }
67
68     public int doStartTag() {
69
70         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)pageContext.getRequest();
71         String JavaDoc resValue = null;
72
73         Locale JavaDoc currentLocale = request.getLocale();
74         HttpSession JavaDoc session = pageContext.getSession();
75         if (session != null) {
76             if (session.getAttribute(ParamBean.SESSION_LOCALE) != null) {
77                 currentLocale = (Locale JavaDoc) session.getAttribute(ParamBean.
78                     SESSION_LOCALE);
79             }
80         }
81
82         try {
83
84             String JavaDoc keyName = null;
85             if (key != null) {
86                 resValue = JahiaResourceBundle
87                     .getMessageResource(key,
88                                         currentLocale);
89             } else if (name != null) {
90                 EngineMessage message = (EngineMessage) pageContext.findAttribute(name);
91                 if (message != null) {
92                     String JavaDoc keyValue = JahiaResourceBundle
93                         .getMessageResource(message.getKey(),
94                                             currentLocale);
95                     if (message.getValues() != null) {
96                         MessageFormat JavaDoc msgFormat = new MessageFormat JavaDoc(keyValue);
97                         msgFormat.setLocale(currentLocale);
98                         resValue = msgFormat.format(message.getValues());
99                     } else {
100                         resValue = keyValue;
101                     }
102
103                 } else {
104                     logger.error("Couldn't find any EngineMessage bean with name " + name + "!");
105                 }
106             }
107
108             if (key != null) {
109             }
110
111         } catch ( MissingResourceException JavaDoc mre ) {
112             logger.warn("Couldn't find resource : ", mre);
113         }
114
115         if (resValue == null) {
116             resValue = "";
117         }
118
119         try {
120             JspWriter JavaDoc out = pageContext.getOut();
121             out.print( resValue );
122         } catch (IOException JavaDoc ioe) {
123             logger.error("IO error while displaying message : ", ioe);
124         }
125
126         return SKIP_BODY;
127
128     }
129
130     public int doEndTag() throws JspException JavaDoc {
131         // let's reinitialize the tag variables to allow tag object reuse in
132
// pooling.
133
key = null;
134         name = null;
135         property = null;
136         display = true;
137         return EVAL_PAGE;
138     }
139
140
141 }
142
Popular Tags