KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > strutsel > taglib > utils > HashMapMessageResources


1 /*
2  * $Id: HashMapMessageResources.java 54933 2004-10-16 17:04:52Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.strutsel.taglib.utils;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Locale JavaDoc;
23
24 import org.apache.struts.util.MessageResources;
25 import org.apache.struts.util.MessageResourcesFactory;
26
27
28 /**
29  * This is a simple derived class of <code>MessageResources</code> which is
30  * just used for testing custom tags which use the
31  * <code>MessageResources</code> class.
32  */

33 public class HashMapMessageResources
34     extends MessageResources {
35
36     private HashMap JavaDoc messages = new HashMap JavaDoc();
37
38     public HashMapMessageResources(MessageResourcesFactory factory,
39                                    String JavaDoc config) {
40         super(factory, config);
41     }
42
43     public HashMapMessageResources(MessageResourcesFactory factory,
44                                    String JavaDoc config, boolean returnNull) {
45         super(factory, config, returnNull);
46     }
47
48     public void addMessage(String JavaDoc key, String JavaDoc value) {
49         addMessage("", key, value);
50     }
51
52     public void addMessage(String JavaDoc locale, String JavaDoc key, String JavaDoc value) {
53         messages.put(locale + "." + key, value);
54     }
55
56     /**
57     * Returns a text message for the specified key, for the specified locale.
58     * If no result is found for the given locale, the locale is "trimmed" off
59     * the end of more specific locale modifiers to check for a match. If no
60     * match is found with the trimmed locale, the current "default" locale is
61     * checked, if it is different from the given locale. If still no match is
62     * found, an empty locale specifier is used.
63     *
64     * This method is copied directly from the
65     * <code>PropertyMessageResources</code> class.
66     */

67     public String JavaDoc getMessage(Locale JavaDoc locale, String JavaDoc key) {
68
69         // Initialize variables we will require
70
String JavaDoc localeKey = localeKey(locale);
71         String JavaDoc originalKey = messageKey(localeKey, key);
72         String JavaDoc messageKey = null;
73         String JavaDoc message = null;
74         int underscore = 0;
75         boolean addIt = false; // Add if not found under the original key
76

77         // Loop from specific to general Locales looking for this message
78
while (true) {
79
80             // Load this Locale's messages if we have not done so yet
81
loadLocale(localeKey);
82
83             // Check if we have this key for the current locale key
84
messageKey = messageKey(localeKey, key);
85
86             synchronized (messages) {
87                 message = (String JavaDoc)messages.get(messageKey);
88                 if (message != null) {
89                     if (addIt)
90                         messages.put(originalKey, message);
91                     return (message);
92                 }
93             }
94
95             // Strip trailing modifiers to try a more general locale key
96
addIt = true;
97             underscore = localeKey.lastIndexOf("_");
98
99             if (underscore < 0)
100
101                 break;
102
103             localeKey = localeKey.substring(0, underscore);
104         }
105
106         // Try the default locale if the current locale is different
107
if (!defaultLocale.equals(locale)) {
108             localeKey = localeKey(defaultLocale);
109             messageKey = messageKey(localeKey, key);
110             loadLocale(localeKey);
111
112             synchronized (messages) {
113                 message = (String JavaDoc)messages.get(messageKey);
114                 if (message != null) {
115                     if (addIt)
116                         messages.put(originalKey, message);
117                     return (message);
118                 }
119             }
120         }
121
122         // As a last resort, try the default Locale
123
localeKey = "";
124         messageKey = messageKey(localeKey, key);
125         loadLocale(localeKey);
126
127         synchronized (messages) {
128             message = (String JavaDoc)messages.get(messageKey);
129
130             if (message != null) {
131                 if (addIt)
132                     messages.put(originalKey, message);
133                 return (message);
134             }
135         }
136
137         // Return an appropriate error indication
138
if (returnNull)
139             return (null);
140         else
141             return ("???" + messageKey(locale, key) + "???");
142     }
143
144     protected void loadLocale(String JavaDoc localeKey) {
145     }
146 }
147
Popular Tags