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

com.eva.properties.PropertiesFactory Maven / Gradle / Ivy

Go to download

Advanced properties with object factories, references and inheritance.

There is a newer version: 0.3
Show newest version
/*
 * $Id: PropertiesFactory.java 56 2007-03-02 18:27:13Z max $
 * 
 * Copyright (c) 2006-2007 Maximilian Antoni. All rights reserved.
 * 
 * This software is licensed as described in the file LICENSE.txt, which you
 * should have received as part of this distribution. The terms are also
 * available at http://www.maxantoni.de/projects/eva-properties/license.txt.
 */
package com.eva.properties;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import java.util.Map;

/**
 * provides static factory methods for creating property maps and property
 * lists.
 * 
 * @author Max Antoni
 * @version $Revision: 56 $
 */
public class PropertiesFactory {

    /**
     * is the name of the property that references the base path of the
     * properties file. This may be a file or a URL, depending on the mechanism
     * used to load the properties file.
     */
    public static final String DATASOURCE_BASE = "datasource-base";

    /**
     * specifies whether property files loaded via a proxy are initialized with
     * the loading properties file as the parent.
     */
    public static final boolean DEFAULT_PROXY_PARENT = true;
    
    /**
     * flags debug mode. In debug mode recursive references are detected and log
     * messages may be more detailed. Debug mode is also active when the log
     * level is at least FINE.
     */
    public static boolean DEBUG = false;
    
    /**
     * creates an empty properties map.
     * 
     * @return the properties map.
     */
    public static Map createMap() {
        return new MapProperties(null);
    }
    
    /**
     * creates a copy of the given properties map.
     * 
     * @param inMap the properties map.
     * @return the copy of the properties map.
     * @throws IllegalArgumentException if the map was not a properties map.
     */
    public static Map copy(Map inMap) {
        if(inMap == null) {
            throw new NullPointerException();
        }
        if(inMap instanceof MapProperties) {
            Properties properties = (Properties) inMap;
            return (MapProperties) properties.copy(properties.getParent());
        }
        throw new IllegalArgumentException("Not a properties map.");
    }
    
    /**
     * creates a properties map with an initialization map. If the given map is
     * a properties instance, it will be used as the parent for the new
     * properties map. Otherwise each entry of the given map will be transfered
     * to the new properties map.
     * 
     * @param inMap the map for initialization.
     * @return the properties map.
     */
    public static Map createMap(Map inMap) {
        if(inMap instanceof Properties) {
            return new MapProperties((Properties) inMap);
        }
        MapProperties properties = new MapProperties(null);
        properties.putAll(inMap);
        return properties;
    }

    /**
     * creates a properties map from the given data source.
     * 
     * @param inDataSource the data source.
     * @return the properties map.
     * @throws IOException if it is thrown by the data source.
     */
    public static Map createMap(DataSource inDataSource) throws IOException {
        return createMap(inDataSource, DEFAULT_PROXY_PARENT);
    }
    
    /**
     * creates a properties map from the given data source.
     * 
     * @param inDataSource the data source.
     * @param inProxyParent see {@link #DEFAULT_PROXY_PARENT}
     * @return the properties map.
     * @throws IOException
     */
    public static Map createMap(DataSource inDataSource, boolean inProxyParent)
            throws IOException {
        MapProperties properties = new MapProperties(null);
        PropertiesParser.readMap(properties, inDataSource.getReader(),
                inProxyParent);
        properties.putInternal(DATASOURCE_BASE, inDataSource.getDelegateBase());
        ClassLoader classLoader = inDataSource.getClassLoader();
        if(classLoader != null) {
            properties.putInternal("classloader", classLoader);
        }
        return properties;
    }

    public static Map createMap(String inPath) throws IOException {
        return createMap(inPath, DEFAULT_PROXY_PARENT);
    }
    
    public static Map createMap(String inPath, boolean inProxyParent)
            throws IOException {
        return createMap(new DataSource(inPath), inProxyParent);
    }
    
    public static Map createMap(File inFile) throws IOException {
        return createMap(inFile, DEFAULT_PROXY_PARENT);
    }
    
    public static Map createMap(File inFile, boolean inProxyParent)
            throws IOException {
        return createMap(new DataSource(inFile), inProxyParent);
    }
    
    public static Map createMap(ClassLoader inClassLoader, String inPath)
            throws IOException {
        return createMap(new DataSource(inClassLoader, inPath));
    }
    
    public static Map createMap(Reader inReader) throws IOException {
        return createMap(inReader, DEFAULT_PROXY_PARENT);
    }
    
    public static Map createMap(Reader inReader, boolean inProxyParent)
            throws IOException {
        MapProperties properties = new MapProperties(null);
        PropertiesParser.readMap(properties, inReader, inProxyParent);
        return properties;
    }
    
    public static List createList() {
        return new ListProperties(null);
    }
    
    public static List createList(List inList) {
        if(inList instanceof Properties) {
            return new ListProperties((Properties) inList);
        }
        ListProperties properties = new ListProperties(null);
        properties.addAll(inList);
        return properties;
    }

    public static List createList(Map inMap) {
        if(inMap instanceof Properties) {
            return new ListProperties((Properties) inMap);
        }
        throw new IllegalArgumentException("MapProperties expected.");
    }
    
    public static List createList(DataSource inDataSource) throws IOException {
        return createList(inDataSource, DEFAULT_PROXY_PARENT);
    }
    
    public static List createList(DataSource inDataSource, boolean inProxyParent)
            throws IOException {
        ListProperties properties = new ListProperties(null);
        PropertiesParser.readList(properties, inDataSource.getReader(),
                inProxyParent);
        return properties;
    }

    public static List createList(String inPath) throws IOException {
        return createList(inPath, DEFAULT_PROXY_PARENT);
    }

    public static List createList(String inPath, boolean inProxyParent)
            throws IOException {
        return createList(new DataSource(inPath), inProxyParent);
    }
    
    public static List createList(File inFile) throws IOException {
        return createList(inFile, DEFAULT_PROXY_PARENT);
    }
    
    public static List createList(File inFile, boolean inProxyParent)
            throws IOException {
        return createList(new DataSource(inFile), inProxyParent);
    }
    
    public static List createList(ClassLoader inClassLoader, String inPath)
            throws IOException {
        return createList(new DataSource(inClassLoader, inPath));
    }

    public static List createList(Reader inReader) throws IOException {
        return createList(inReader, DEFAULT_PROXY_PARENT);
    }
    
    public static List createList(Reader inReader, boolean inProxyParent)
            throws IOException {
        ListProperties properties = new ListProperties(null);
        PropertiesParser.readList(properties, inReader, inProxyParent);
        return properties;
    }
    
    private PropertiesFactory() {
        // prevent instanciation.
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy