001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.util;
018:
019: import java.text.MessageFormat;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.MissingResourceException;
023: import java.util.ResourceBundle;
024:
025: /**
026: * Formats messages.
027: *
028: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
029: * @version $Revision: 537957 $ $Date: 2007-05-14 12:31:08 -0700 (Mon, 14 May 2007) $
030: */
031: public class Messages {
032: /**
033: * Map from message code to MessageFormat object for the message.
034: */
035: private static Map messages = new HashMap();
036: private static ResourceBundle resources;
037:
038: private Messages() {
039: }
040:
041: /**
042: * Formats a message.
043: *
044: * @param code The message code.
045: * @return The formatted message.
046: */
047: public static String getString(final String code) {
048: return getString(code, new Object[0]);
049: }
050:
051: /**
052: * Formats a message.
053: *
054: * @param code The message code.
055: * @param param The message parameter.
056: * @return The formatted message.
057: */
058: public static String getString(final String code, final Object param) {
059: return getString(code, new Object[] { param });
060: }
061:
062: /**
063: * Formats a message.
064: *
065: * @param code The message code.
066: * @param params The message parameters.
067: * @return The formatted message.
068: */
069: public static String getString(final String code,
070: final Object[] params) {
071: try {
072: if (code == null) {
073: return null;
074: }
075:
076: final MessageFormat msg = findMessage(code);
077: return msg.format(params);
078: } catch (final MissingResourceException mre) {
079: return "Unknown message with code \"" + code + "\".";
080: }
081: }
082:
083: /**
084: * Locates a message by its code.
085: */
086: private static synchronized MessageFormat findMessage(
087: final String code) throws MissingResourceException {
088: // Check if the message is cached
089: MessageFormat msg = (MessageFormat) messages.get(code);
090: if (msg != null) {
091: return msg;
092: }
093:
094: // Locate the message
095: if (resources == null) {
096: resources = new CombinedResources(
097: "org.apache.commons.vfs.Resources");
098: }
099: final String msgText = resources.getString(code);
100: msg = new MessageFormat(msgText);
101: messages.put(code, msg);
102: return msg;
103: }
104: }
|