com.htmlhifive.tools.codeassist.core.messages.MessagesBase.java Source code

Java tutorial

Introduction

Here is the source code for com.htmlhifive.tools.codeassist.core.messages.MessagesBase.java

Source

/*
 * Copyright (C) 2012 NS Solutions Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    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 com.htmlhifive.tools.codeassist.core.messages;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;

import com.htmlhifive.tools.codeassist.core.logger.LogLevel;

/**
 * Messages?.
 * 
 * @author NS Solutions Corporation
 */
public abstract class MessagesBase {
    /**
     * ??.
     */
    private static final List<ResourceBundle> RESOURCE_BUNDLES = Collections
            .synchronizedList(new ArrayList<ResourceBundle>());

    /**
     * .
     */
    protected MessagesBase() {

        // no action
    }

    /**
     * ??.
     * 
     * @param bundleName ???.
     */
    protected static void addResourceBundleString(String bundleName) {

        synchronized (RESOURCE_BUNDLES) {
            ResourceBundle bundle = ResourceBundle.getBundle(bundleName);
            RESOURCE_BUNDLES.add(bundle);
        }
    }

    /**
     * ??.
     * 
     * @param bundle ?.
     */
    protected static void addResourceBundle(ResourceBundle bundle) {

        synchronized (RESOURCE_BUNDLES) {
            RESOURCE_BUNDLES.add(bundle);
        }
    }

    /**
     * ??.
     * 
     * @param key ?
     * @return 
     */
    protected static Message createMessage(String key) {

        return new Message(key);
    }

    /**
     * ????????.
     * 
     * @param key ?
     * @return ????
     */
    private static String getResourceBundleString(String key) {

        synchronized (RESOURCE_BUNDLES) {
            for (ResourceBundle resourceBundle : RESOURCE_BUNDLES) {
                if (resourceBundle.containsKey(key)) {
                    return resourceBundle.getString(key);
                }
            }
            return null;
        }
    }

    /**
     * <H3>?.</H3>
     * 
     * @author MessageGenerator
     */
    public static final class Message {

        /**
         * .
         */
        private final String key;

        /**
         * .
         */
        private final String text;

        /**
         * .
         */
        private final LogLevel level;

        /**
         * .
         * 
         * @param key 
         */
        private Message(String key) {

            this.key = key;
            String rawStr = getResourceBundleString(key);
            String[] strArray = StringUtils.split(rawStr, ",", 2);
            this.level = LogLevel.valueOf(strArray[0]);
            this.text = strArray[1];

        }

        /**
         * ??.
         * 
         * @return 
         */
        public String getKey() {

            return key;
        }

        /**
         * ??????.
         * 
         * @return ?????.
         */
        public String getText() {

            return text;
        }

        /**
         * ?.
         * 
         * @return .
         */
        public LogLevel getLevel() {

            return level;
        }

        /**
         * ???.
         * 
         * @param params .
         * @return ??.
         */
        public String format(Object... params) {

            return MessageFormat.format(text, params);
        }
    }

}