package org.flymine.util;

/*
 * Copyright (C) 2002-2003 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.Properties;
import java.util.Enumeration;
import java.io.InputStream;
import java.io.IOException;

/**
 * Convenience class for working with global properties
 *
 * @author Andrew Varley
 */
public class PropertiesUtil
{
    private PropertiesUtil() {
    }

    private static Properties globalProperties = new Properties();

    static {
        // Read Properties from the following files, if present on the classpath:
        // flymine.properties: Runtime Properties
        // flymine-build.properties: Buildtime properties
        try {
            InputStream is = PropertiesUtil.class.getClassLoader()
                .getResourceAsStream("flymine.properties");

            if (is != null) {
                globalProperties.load(is);
            }

        } catch (IOException e) {
            // Do nothing
        }
    }

    /**
     * Returns all FlyMine properties
     *
     * @return the global properties for FlyMine
     */
    public static Properties getProperties() {
        return globalProperties;
    }

    /**
     * Returns all Properties in props that begin with str
     *
     * @param str the String that the returned properties should start with
     * @param props the Properties to search through
     * @return a Properties object containing the subset of props
     */
    public static Properties getPropertiesStartingWith(String str, Properties props) {
        if (str == null) {
            throw new NullPointerException("str cannot be null");
        }
        if (props == null) {
            throw new NullPointerException("props cannot be null");
        }

        Properties subset = new Properties();
        Enumeration enum = props.keys();
        while (enum.hasMoreElements()) {
            String propertyName = (String) enum.nextElement();
            if (propertyName.startsWith(str)) {
                subset.put(propertyName, props.get(propertyName));
            }
        }
        return subset;
    }

    /**
     * Returns all global Properties that begin with str
     *
     * @param str the String that the returned properties should start with
     * @return a Properties object containing the subset of the global properties
     */
    public static Properties getPropertiesStartingWith(String str) {
        return getPropertiesStartingWith(str, globalProperties);
    }

    /**
     * Strips the give string off the keys of the given
     * Properties. For example, database.name=production =>
     * name=production.
     *
     * @param str the String to strip off
     * @param props the Properties object to change
     * @return a Properties object containing the same properties with
     * the initial string stripped off the keys
     */
    public static Properties stripStart(String str, Properties props) {
        if (str == null) {
            throw new NullPointerException("str cannot be null");
        }
        if (props == null) {
            throw new NullPointerException("props cannot be null");
        }
        Properties ret = new Properties();
        Enumeration enum = props.keys();
        while (enum.hasMoreElements()) {
            String propertyName = (String) enum.nextElement();
            if (propertyName.startsWith(str + ".")) {
                ret.put(propertyName.substring(str.length() + 1), props.get(propertyName));
            }
        }

        return ret;
    }


}

@@ -764,24 +764,32 @@
 %0A        // 
+default.
 flymine.prop
@@ -796,17 +796,24 @@
 erties: 
-R
+Common r
 untime P
@@ -844,14 +844,8 @@
 mine
--build
 .pro
@@ -857,13 +857,16 @@
 es: 
-Build
+User run
 time
@@ -958,16 +958,228 @@
 oader()%0A
+                .getResourceAsStream(%22default.flymine.properties%22);%0A%0A            if (is != null) %7B%0A                globalProperties.load(is);%0A            %7D%0A%0A            is = PropertiesUtil.class.getClassLoader()%0A
         

load default properties if there are any on the classpath