01: // Copyright 2006 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.ioc.internal.util;
16:
17: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newCaseInsensitiveMap;
18:
19: import java.util.Enumeration;
20: import java.util.Locale;
21: import java.util.Map;
22: import java.util.ResourceBundle;
23:
24: import org.apache.tapestry.ioc.Messages;
25: import org.apache.tapestry.ioc.util.AbstractMessages;
26:
27: /**
28: * Implementation of {@link org.apache.tapestry.ioc.Messages} based around a
29: * {@link java.util.ResourceBundle}.
30: */
31: public class MessagesImpl extends AbstractMessages {
32: private final Map<String, String> _properties = newCaseInsensitiveMap();
33:
34: /**
35: * Finds the messages for a given Messages utility class. Strings the trailing "Messages" and
36: * replaces it with "Strings" to form the base path. Loads the bundle using the default locale,
37: * and the class' class loader.
38: *
39: * @param forClass
40: * @return Messages for the class
41: */
42: public static Messages forClass(Class forClass) {
43: String className = forClass.getName();
44: String stringsClassName = className.replaceAll("Messages$",
45: "Strings");
46:
47: ResourceBundle bundle = ResourceBundle.getBundle(
48: stringsClassName, Locale.getDefault(), forClass
49: .getClassLoader());
50:
51: return new MessagesImpl(bundle);
52: }
53:
54: public MessagesImpl(ResourceBundle bundle) {
55: // Our best (threadsafe) chance to determine all the available keys.
56: Enumeration<String> e = bundle.getKeys();
57: while (e.hasMoreElements()) {
58: String key = e.nextElement();
59: String value = bundle.getString(key);
60:
61: _properties.put(key, value);
62: }
63: }
64:
65: @Override
66: protected String valueForKey(String key) {
67: return _properties.get(key);
68: }
69:
70: }
|