/* Copyright (c) Shantanu Kumar. All rights reserved.
 * The use and distribution terms for this software are covered by the
 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 * which can be found in the file LICENSE at the root of this distribution.
 * By using this software in any fashion, you are agreeing to be bound by
 * the terms of this license.
 * You must not remove this notice, or any other, from this software.
 */


package keypin;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.Properties;

public class PropertyConfigIO implements ConfigIO {

    @Override
    public boolean canRead(String filename) {
        return null != filename && filename.toLowerCase().endsWith(".properties");
    }

    @Override
    public Map<?, ?> readConfig(InputStream configFilename) throws Exception {
        final Properties props = new Properties();
        props.load(configFilename);
        return props;
    }

    @Override
    public boolean canWrite(String filename) {
        return canRead(filename);
    }

    @Override
    public void writeConfig(OutputStream out, Map<?, ?> config, boolean escape) throws Exception {
        Properties props = new Properties();
        if (escape) {
            for (Map.Entry<?, ?> entry: config.entrySet()) {
                props.setProperty((String) entry.getKey(), Config.escape((String) entry.getValue()));
            }
        } else {
            props.putAll(config);
        }
        props.store(out, "Generated by Keypin");
    }

}

/* Copyright (c) Shantanu Kumar. All rights reserved.
 * The use and distribution terms for this software are covered by the
 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 * which can be found in the file LICENSE at the root of this distribution.
 * By using this software in any fashion, you are agreeing to be bound by
 * the terms of this license.
 * You must not remove this notice, or any other, from this software.
 */


package keypin;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.Properties;

public class PropertyConfigIO implements ConfigIO {

    public static final PropertyConfigIO INSTANCE = new PropertyConfigIO();

    @Override
    public boolean canRead(String filename) {
        return null != filename && filename.toLowerCase().endsWith(".properties");
    }

    @Override
    public Map<?, ?> readConfig(InputStream configFilename) throws Exception {
        final Properties props = new Properties();
        props.load(configFilename);
        return props;
    }

    @Override
    public boolean canWrite(String filename) {
        return canRead(filename);
    }

    @Override
    public void writeConfig(OutputStream out, Map<?, ?> config, boolean escape) throws Exception {
        Properties props = new Properties();
        if (escape) {
            for (Map.Entry<?, ?> entry: config.entrySet()) {
                props.setProperty((String) entry.getKey(), Config.escape((String) entry.getValue()));
            }
        } else {
            props.putAll(config);
        }
        props.store(out, "Generated by Keypin");
    }

}

add static singleton instance to PropertyConfigIO