001: // Copyright 2004, 2005 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.hivemind.impl;
016:
017: import java.util.Locale;
018: import java.util.MissingResourceException;
019: import java.util.ResourceBundle;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: /**
025: * A wrapper around {@link java.util.ResourceBundle} that makes it easier to access and format
026: * messages.
027: *
028: * @author Howard Lewis Ship
029: */
030: public class MessageFormatter extends AbstractMessages {
031: private Log _log;
032:
033: private ResourceBundle _bundle;
034:
035: public MessageFormatter(Log log, ResourceBundle bundle) {
036: _log = log;
037: _bundle = bundle;
038: }
039:
040: /**
041: * Assumes that the bundle name is the same as the reference class, with "Messages" stripped
042: * off, and "Strings" appended.
043: *
044: * @since 1.1
045: */
046: public MessageFormatter(Class referenceClass) {
047: this (referenceClass, getStringsName(referenceClass));
048: }
049:
050: public MessageFormatter(Class referenceClass, String name) {
051: this (LogFactory.getLog(referenceClass), referenceClass, name);
052: }
053:
054: public MessageFormatter(Log log, Class referenceClass, String name) {
055: this (log, getResourceBundleName(referenceClass, name));
056: }
057:
058: public MessageFormatter(Log log, String bundleName) {
059: this (log, ResourceBundle.getBundle(bundleName));
060: }
061:
062: protected String findMessage(String key) {
063: try {
064: return _bundle.getString(key);
065: } catch (MissingResourceException ex) {
066: _log.error("Missing resource key: " + key + ".");
067: return null;
068: }
069: }
070:
071: protected Locale getLocale() {
072: return Locale.getDefault();
073: }
074:
075: private static String getStringsName(Class referenceClass) {
076: String className = referenceClass.getName();
077:
078: int lastDotIndex = className.lastIndexOf('.');
079:
080: String justClass = className.substring(lastDotIndex + 1);
081:
082: int mpos = justClass.indexOf("Messages");
083:
084: return justClass.substring(0, mpos) + "Strings";
085: }
086:
087: private static String getResourceBundleName(Class referenceClass,
088: String name) {
089: String packageName = null;
090: if (referenceClass.getPackage() != null) {
091: packageName = referenceClass.getPackage().getName();
092: } else {
093: final int lastDotIndex = referenceClass.getName()
094: .lastIndexOf('.');
095: packageName = (lastDotIndex == -1 ? "" : referenceClass
096: .getName().substring(0, lastDotIndex));
097:
098: }
099: return packageName.equals("") ? name : packageName + "." + name;
100: }
101: }
|