All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.jguiraffe.resources.impl.bundle.BundleResourceGroup Maven / Gradle / Ivy

There is a newer version: 1.4.1
Show newest version
/*
 * Copyright 2006-2010 The JGUIraffe Team.
 *
 * 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 net.sf.jguiraffe.resources.impl.bundle;

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;

import net.sf.jguiraffe.resources.ResourceGroup;

/**
 * 

* A specialized implementation of the ResourceGroup interface that * is backed by a java.util.ResourceBundle. *

*

* The methods required by the ResourceGroup interface are * delegated to the internally managed resource bunde. During construction this * bundle is loaded for the specified base name and locale. *

* * @author Oliver Heger * @version $Id: BundleResourceGroup.java 195 2010-08-30 19:54:41Z oheger $ */ class BundleResourceGroup implements ResourceGroup { /** Stores the bundle this implementation is based on. */ private final ResourceBundle bundle; /** Stores the name of this group. */ private final String name; /** Stores the locale of this group. */ private final Locale locale; /** * Creates a new instance of BundleResourceGroup and * initializes it. * * @param name the group's name * @param locale the locale * @throws java.util.MissingResourceException if the bundle cannot be found */ public BundleResourceGroup(String name, Locale locale) { this.name = name; this.locale = locale; bundle = initBundle(name, locale); } /** * Returns the bundle that is wrapped by this resource group. * * @return the underlying bundle */ public ResourceBundle getBundle() { return bundle; } /** * Returns the name of this resource group. * * @return the name of this group */ public Object getName() { return name; } /** * Returns a set with all keys contained in this resource group. * * @return a set with the defined keys */ public Set getKeys() { Set result = new HashSet(); for (Enumeration en = getBundle().getKeys(); en .hasMoreElements();) { result.add(en.nextElement()); } return result; } /** * Returns the locale of this group. * * @return the locale */ public Locale getLocale() { return locale; } /** * Returns the resource for the specified key. * * @param key the key * @return the resource for this key * @throws java.util.MissingResourceException if this key is unknown * @throws IllegalArgumentException for a null key */ public Object getResource(Object key) { if (key == null) { throw new IllegalArgumentException("Resource key must not be null!"); } return getBundle().getObject(key.toString()); } /** * Initializes the specified resource bundle. * * @param name the bundle's base name * @param locale the locale * @return the bundle instance * @throws java.util.MissingResourceException if the bundle cannot be found */ private ResourceBundle initBundle(String name, Locale locale) { return ResourceBundle.getBundle(name, locale); } }