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

com.kapil.framework.reader.XMLConfigurationReader Maven / Gradle / Ivy

Go to download

This is a set of utilities and classes that I have found useful over the years. In my career spanning over a decade, I have time and again written the same code or some part of the code over and over again. I never found the time to collate the details in a reusable library. This project will be a collection of such files. The work that I have been doing is more than 5 years old, however the project has been conceived in 2011.

The newest version!
/*******************************************************************************
 * Copyright 2011 @ Kapil Viren Ahuja
 * 
 * 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.kapil.framework.reader;

import java.util.List;
import java.util.Locale;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.lang.exception.ExceptionUtils;

import com.kapil.framework.exception.impl.UnImplementedException;
import com.kapil.framework.logger.ILogger;
import com.kapil.framework.logger.LogFactory;

/**
 * Used to read configuration messages from a XML file. This class uses {@link XMLConfiguration} to read the values.
 * 
 * 
 * @author Kapil Viren Ahuja
 * 
 */
public final class XMLConfigurationReader implements IMessageReader
{
    private final static ILogger         LOGGER = LogFactory.getInstance().getLogger(XMLConfigurationReader.class);

    protected transient XMLConfiguration _config;


    /**
     * Made private so that instances cannot be created directly.
     * 
     */
    public XMLConfigurationReader(final String configFileName)
    {
        try
        {
            _config = new XMLConfiguration();
            _config.setDelimiterParsingDisabled(true);
            _config.setAttributeSplittingDisabled(true);
            _config.load(configFileName);
        }
        catch (ConfigurationException e)
        {
            LOGGER.error("Error initializing the Configuration reader, with the file [" + configFileName + "]. Please see below for details. \n"
                    + ExceptionUtils.getFullStackTrace(e));
        }
    }


    /**
     * Reads a boolean property from a data source, provided a string key to locate the property.
     * 
     * @param key Key to locate the property.
     * @return A boolean value.
     */
    public boolean getBooleanProperty(String name)
    {
        return _config == null ? null : _config.getBoolean(name);
    }


    /**
     * Reads an integer property from a data source, provided a string key to locate the property.
     * 
     * @param key Key to locate the property.
     * @return An integer value.
     */
    public int getIntegerProperty(String name)
    {
        return _config == null ? null : _config.getInt(name);
    }


    /**
     * Returns a list of value corresponding to a repeating property in the configuration file.
     * 
     * @param name A {@link java.lang.String} containing property name.
     * @return A {@link java.util.List} of {@link java.lang.String} values containing all values available for the
     *         property.
     */
    @SuppressWarnings("unchecked")
    public List getList(String name)
    {
        return _config == null ? null : _config.getList(name);
    }


    public String[] getPropertyArray(String name)
    {
        return _config == null ? null : _config.getStringArray(name);
    }


    /**
     * Reads a property from the configuration and returns its value to the caller.
     * 
     * @param name A {@link java.lang.String} specifying the name of the property to be fetched.
     * @return A {@link java.lang.String} containing property name.
     */
    public String getMessage(String key)
    {
        return _config == null ? null : _config.getString(key);
    }


    public String getMessage(String key, Locale locale)
    {
        throw new UnImplementedException(UnImplementedException.NOT_IMPLEMENTED_WITH_PARAMS, new String[] { "getMessage", "String, Locale" });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy