jp.co.ctc_g.jfw.core.internal.InternalMessages.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.ctc_g.jfw.core.internal.InternalMessages.java

Source

/*
 * Copyright (c) 2013 ITOCHU Techno-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 jp.co.ctc_g.jfw.core.internal;

import java.util.Enumeration;
import java.util.HashSet;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.commons.collections.iterators.IteratorEnumeration;

/**
 * <p>
 * ????J-Framework???????
 * J-Framework?????? ??????????????????
 * J-Framework ?????????????
 * </p>
 * <p>
 * ????????????
 * </p>
 * <ul>
 * <li>??</li>
 * </ul>
 * <p>
 * ??
 * </p>
 * <h4>??</h4>
 * <p>
 * J-Framework??? ???
 * ?????????????? ?Java???
 * ???????? ()??
 * 1??????1????Messages???????
 * ???<strong>??????ID()??????</strong>
 * ??ID?????????? <div
 * class="inline_caption">[]-[]#[]</div>
 * ?????????????? <div
 * class="inline_caption">W-INTERNAL#0001</div> ????????
 * </p>
 * <table>
 * <thead>
 * <tr>
 * <th>?</th>
 * <th>?</th>
 * <th></th>
 * </tr>
 * </thead> <tbody>
 * <tr>
 * <td>E</td>
 * <td>ERROR</td>
 * <td></td>
 * </tr>
 * <tr>
 * <td>W</td>
 * <td>WARN</td>
 * <td></td>
 * </tr>
 * <tr>
 * <td>I</td>
 * <td>INFO</td>
 * <td></td>
 * </tr>
 * <tr>
 * <td>D</td>
 * <td>DEBUG</td>
 * <td>?</td>
 * </tr>
 * <tr>
 * <td>O</td>
 * <td>OTHER</td>
 * <td>??????</td>
 * </tr>
 * </tbody>
 * <table>
 * <p>
 * ???????????
 * </p>
 *
 * <pre>
 * ResourceBundle bundle = Internals.getBundle(Foo.class);
 * </pre>
 *
 * <p>
 * ?????
 * </p>
 *
 * @author ITOCHU Techno-Solutions Corporation.
 */
public final class InternalMessages {

    /**
     * ??
     * ?????
     */
    private InternalMessages() {
    }

    /**
     * <p>
     * ?????
     * </p>
     */
    public static class DelegateResourceBundle extends ResourceBundle {

        private static final int INITIAL_CAPACITY = 128;
        private final ResourceBundle messageBundle;

        /**
         * ??
         * @param messageBundle ?
         * @see ResourceBundle
         */
        public DelegateResourceBundle(ResourceBundle messageBundle) {
            this.messageBundle = messageBundle;
        }

        /**
         * ??
         * @param messageBundle ?
         * @param parent ??
         */
        public DelegateResourceBundle(ResourceBundle messageBundle, ResourceBundle parent) {
            this.messageBundle = messageBundle;
            setParent(parent);
        }

        /**
         * {@inheritDoc}
         */
        @SuppressWarnings("unchecked")
        @Override
        public synchronized Enumeration<String> getKeys() {
            HashSet<String> set = new HashSet<String>(INITIAL_CAPACITY);

            addAllElements(set, messageBundle.getKeys());
            if (parent != null) {
                addAllElements(set, parent.getKeys());
            }
            return new IteratorEnumeration(set.iterator());
        }

        private void addAllElements(HashSet<String> set, Enumeration<String> e) {
            while (e.hasMoreElements()) {
                set.add(e.nextElement());
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        protected Object handleGetObject(String key) {
            try {
                return messageBundle.getObject(key);
            } catch (MissingResourceException e) {
                if (parent != null) {
                    return parent.getObject(key);
                } else {
                    throw e;
                }
            }
        }
    }

    private static final String MESSAGE_FILE = ".Messages";

    /**
     * ??????????
     * ??????????????? ??????
     * {@link MissingResourceException}????
     *
     * @param clazz
     *            ?????
     * @return ????
     * @throws MissingResourceException
     *             ??????
     * @see ResourceBundle
     */
    public static ResourceBundle getBundle(Class<?> clazz) {
        return ResourceBundle.getBundle(clazz.getPackage().getName() + MESSAGE_FILE);
    }

}