1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbforms.util;
25
26 import java.util.Locale;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpSession;
30
31
32
33 /***
34 * base class for handling messages
35 *
36 * @author Henner Kollmann
37 */
38 public class MessageResources {
39 /*** DOCUMENT ME! */
40 public static String LOCALE_KEY = "org_dbforms_LOCALE";
41 private static MessageResource msgRes = null;
42
43 /***
44 * DOCUMENT ME!
45 *
46 * @param request DOCUMENT ME!
47 * @param locale DOCUMENT ME!
48 */
49 public static void setLocale(HttpServletRequest request,
50 Locale locale) {
51 HttpSession session = request.getSession();
52 session.setAttribute(MessageResources.LOCALE_KEY, locale);
53 }
54
55
56 /***
57 * DOCUMENT ME!
58 *
59 * @param request DOCUMENT ME!
60 *
61 * @return DOCUMENT ME!
62 */
63 public static Locale getLocale(HttpServletRequest request) {
64 if (request == null) {
65 return null;
66 }
67
68 HttpSession session = request.getSession();
69
70 if (session.getAttribute(MessageResources.LOCALE_KEY) == null) {
71 session.setAttribute(MessageResources.LOCALE_KEY, request.getLocale());
72 }
73
74 return (Locale) session.getAttribute(MessageResources.LOCALE_KEY);
75 }
76
77
78 /***
79 * Get the message from ResourceBundle. If not present, return the
80 * defaultMsg at the place of a null.
81 *
82 * @param request DOCUMENT ME!
83 * @param msg DOCUMENT ME!
84 *
85 * @return DOCUMENT ME!
86 */
87 public static String getMessage(HttpServletRequest request,
88 String msg) {
89 return getMessage(msg, getLocale(request), msg);
90 }
91
92
93 /***
94 * Get the message from ResourceBundle. If not present, return the
95 * defaultMsg at the place of a null. To avoid to doing this condition
96 * everywhere in the code ...
97 *
98 * @param msgString </code> : Message key to lookup.
99 * @param localeLocale </code> : Locale object to map message with good
100 * ResourceBundle.
101 * @param defaultMsgString </code> : String to return if the lookup message
102 * key is not found.
103 *
104 * @return <code>String</code> : Message resolve.
105 */
106 public static String getMessage(String msg,
107 Locale locale,
108 String defaultMsg) {
109 String s = getMessage(msg, locale);
110
111 if (Util.isNull(s)) {
112 s = defaultMsg;
113 }
114
115 return s;
116 }
117
118
119 /***
120 * Retrieve message from ResourceBundle. If the ResourceBundle is not yet
121 * cached, cache it and retreive message.
122 *
123 * @param msgString </code> : Message key to lookup.
124 * @param locLocale </code> : Locale object to map message with good
125 * ResourceBundle.
126 *
127 * @return <code>String</code> : Message resolve, null if not found.
128 */
129 public static String getMessage(String msg,
130 Locale loc) {
131 return (msgRes == null) ? null
132 : msgRes.getMessage(msg, loc);
133 }
134
135
136 /***
137 * Retrieve message from ResourceBundle and replace parameter "{x}" with
138 * values in parms array.
139 *
140 * @param msgString </code> : Message key to lookup.
141 * @param locLocale </code> : Locale object to map message with good
142 * ResourceBundle.
143 * @param parmsString[] </code> : Parameters to replace "{x}" in message .
144 *
145 * @return <code>String</code> : Message resolve with parameter replaced,
146 * null if message key not found.
147 */
148 public static String getMessage(String msg,
149 Locale loc,
150 String[] parms) {
151 return (msgRes == null) ? null
152 : msgRes.getMessage(msg, loc, parms);
153 }
154
155
156 /***
157 * DOCUMENT ME!
158 *
159 * @param subClass DOCUMENT ME!
160 */
161 public static void setSubClass(String subClass) {
162 msgRes = new MessageResource(subClass);
163 }
164 }