KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > util > MessageResources


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/util/MessageResources.java,v 1.17 2004/08/18 12:26:09 hkollmann Exp $
3  * $Revision: 1.17 $
4  * $Date: 2004/08/18 12:26:09 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.util;
25
26 import java.util.Locale JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpSession JavaDoc;
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 JavaDoc 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 JavaDoc request,
50                                 Locale JavaDoc locale) {
51       HttpSession JavaDoc 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 JavaDoc getLocale(HttpServletRequest JavaDoc request) {
64       if (request == null) {
65          return null;
66       }
67
68       HttpSession JavaDoc 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 JavaDoc) 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 JavaDoc getMessage(HttpServletRequest JavaDoc request,
88                                    String JavaDoc 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 JavaDoc getMessage(String JavaDoc msg,
107                                    Locale JavaDoc locale,
108                                    String JavaDoc defaultMsg) {
109       String JavaDoc 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 JavaDoc getMessage(String JavaDoc msg,
130                                    Locale JavaDoc 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 JavaDoc getMessage(String JavaDoc msg,
149                                    Locale JavaDoc loc,
150                                    String JavaDoc[] 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 JavaDoc subClass) {
162       msgRes = new MessageResource(subClass);
163    }
164 }
165
Popular Tags