/*
 * Copyright 2015 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

package org.eclipse.webdav.client;

import java.text.MessageFormat;
import java.util.*;

public class Policy {
    private static String bundleName = "org.eclipse.webdav.client.messages"; //$NON-NLS-1$
    private static ResourceBundle bundle = ResourceBundle.getBundle(bundleName, Locale.getDefault());

    /**
     * Lookup the message with the given ID in this catalog
     */
    public static String bind(String id) {
        return bind(id, (String[]) null);
    }

    /**
     * Lookup the message with the given ID in this catalog and bind its
     * substitution locations with the given string.
     */
    public static String bind(String id, String binding) {
        return bind(id, new String[] {binding});
    }

    /**
     * Lookup the message with the given ID in this catalog and bind its
     * substitution locations with the given strings.
     */
    public static String bind(String id, String binding1, String binding2) {
        return bind(id, new String[] {binding1, binding2});
    }

    /**
     * Lookup the message with the given ID in this catalog and bind its
     * substitution locations with the given string values.
     */
    public static String bind(String id, String[] bindings) {
        if (id == null)
            return "No message available";//$NON-NLS-1$
        String message = null;
        try {
            message = bundle.getString(id);
        } catch (MissingResourceException e) {
            // If we got an exception looking for the message, fail gracefully by just returning
            // the id we were looking for.  In most cases this is semi-informative so is not too bad.
            return "Missing message: " + id + "in: " + bundleName;//$NON-NLS-1$ //$NON-NLS-2$
        }
        if (bindings == null)
            return message;
        return MessageFormat.format(message, bindings);
    }
}
