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

com.phloc.commons.text.resource.XMLResourceBundle Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
/**
 * Copyright (C) 2006-2015 phloc systems
 * http://www.phloc.com
 * office[at]phloc[dot]com
 *
 * 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.phloc.commons.text.resource;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.WillNotClose;
import javax.annotation.concurrent.Immutable;

import com.phloc.commons.ValueEnforcer;
import com.phloc.commons.annotations.DevelopersNote;
import com.phloc.commons.collections.ContainerHelper;

/**
 * Helper class to handle XML based properties. It is read-only.
* See Resource.Control Javadocs * * @author Philip Helger */ // SKIPJDK5 @Immutable public final class XMLResourceBundle extends ResourceBundle { private final Map m_aValues = new HashMap (); @DevelopersNote ("Don't use it manually - use the static methods of this class!") XMLResourceBundle (@Nonnull @WillNotClose final InputStream aIS) throws IOException { ValueEnforcer.notNull (aIS, "InputStream"); // Read the main properties final Properties aProps = new Properties (); aProps.loadFromXML (aIS); // Convert to a non-synchronized map, as Properties access would be // synchronized at each call // Note: it is ensured that both key and value are Strings! for (final Map.Entry aEntry : aProps.entrySet ()) m_aValues.put ((String) aEntry.getKey (), (String) aEntry.getValue ()); } /** * More efficient version to retrieve the keySet * * @return all resource names */ @Override protected Set handleKeySet () { return m_aValues.keySet (); } /** * Main internal lookup * * @return the string matching the passed key */ @Override protected String handleGetObject (@Nullable final String sKey) { return m_aValues.get (sKey); } @Override public Enumeration getKeys () { return ContainerHelper.getEnumeration (m_aValues.keySet ()); } @Nonnull public static XMLResourceBundle getXMLBundle (@Nonnull final String sBaseName) { return (XMLResourceBundle) ResourceBundle.getBundle (sBaseName, new XMLResourceBundleControl ()); } @Nonnull public static XMLResourceBundle getXMLBundle (@Nonnull final String sBaseName, @Nonnull final Locale aLocale) { return (XMLResourceBundle) ResourceBundle.getBundle (sBaseName, aLocale, new XMLResourceBundleControl ()); } @Nonnull public static XMLResourceBundle getXMLBundle (@Nonnull final String sBaseName, @Nonnull final Locale aLocale, @Nonnull final ClassLoader aClassLoader) { return (XMLResourceBundle) ResourceBundle.getBundle (sBaseName, aLocale, aClassLoader, new XMLResourceBundleControl ()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy