001package org.apache.archiva.redback.users;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.text.MessageFormat;
023import java.util.MissingResourceException;
024import java.util.ResourceBundle;
025
026/**
027 * Localized Message Handling.
028 * 
029 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
030 *
031 */
032public class Messages
033{
034    private static final String BUNDLE_NAME = "org.apache.archiva.redback.users.messages"; //$NON-NLS-1$
035
036    private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle( BUNDLE_NAME );
037
038    /**
039     * Get a Message as-is from the Resource Bundle.
040     * 
041     * @param key the key for the message to get.
042     * @return the value of the key, or "!key!" if the key is not found.
043     */
044    public static String getString( String key )
045    {
046        try
047        {
048            return RESOURCE_BUNDLE.getString( key );
049        }
050        catch ( MissingResourceException e )
051        {
052            return '!' + key + '!';
053        }
054    }
055
056    /**
057     * Gets a Message from the Resource Bundle, with {1} and {2} style arguments.
058     * 
059     * @param key the key for the message to get.
060     * @param arg the argument to pass in.
061     * @return the value of the key, or "!key!" if the key is not found.
062     */
063    public static String getString( String key, Object arg )
064    {
065        return getString( key, new Object[] { arg } );
066    }
067
068    /**
069     * Gets a Message from the Resource Bundle, with {1} and {2} style arguments.
070     * 
071     * @param key the key for the message to get.
072     * @param args the arguments to pass in.
073     * @return the value of the key, or "!key!" if the key is not found.
074     */
075    public static String getString( String key, Object args[] )
076    {
077        try
078        {
079            String pattern = RESOURCE_BUNDLE.getString( key );
080            return MessageFormat.format( pattern, args );
081        }
082        catch ( MissingResourceException e )
083        {
084            return '!' + key + '!';
085        }
086    }
087
088    /**
089     * Prevent Instantiation.
090     */
091    private Messages()
092    {
093    }
094}