/* 
 * Created on 2004/08/04 
 * Author aki@www.xucker.jpn.org 
 * License Apache2.0 or Common Public License 
 */ 
package org.jpn.xucker.commons.util; 
 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Properties; 
 
/** 
 *  
 *   
 */ 
public class FileProperties implements ApplicationProperties { 
    Properties properties; 
 
    private File file; 
     
    private FileNotFoundAction action; 
 
    public boolean isChanged() { 
        return changed; 
    } 
 
    public void setChanged(boolean changed) { 
        this.changed = changed; 
    } 
 
    private boolean changed; 
 
    public FileProperties(File file) { 
      this(file,null); 
    } 
        public FileProperties(File file, FileNotFoundAction action) { 
             
        this.file = file; 
        this.action=action; 
        properties = new Properties(); 
        
    } 
         
    public void load(){ 
        try { 
            if (file.exists()) { 
                properties.load(new FileInputStream(file)); 
            } else { 
                if (action != null) { 
                    action.exec(file, ""); 
                } 
            } 
 
        } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
    } 
 
    public void set(String key, int value) { 
        if (get(key) == null) { 
            changed = true; 
        } else { 
            if (getInt(key, -1) != value) { 
                changed = true; 
            } 
        } 
        set(key, "" + value); 
    } 
 
    public int getInt(String key, int defaultValue) { 
        int v = defaultValue; 
        String s = get(key); 
        if (s != null) { 
            try { 
                v = Integer.parseInt(s); 
            } catch (Exception e) { 
            } 
            ; 
        } 
 
        return v; 
    } 
 
    public void remove(String key) { 
        properties.remove(key); 
    } 
 
    public void set(String key, String value) { 
        if (value == null) { 
            throw new RuntimeException("value is null"); 
        } 
        String old = (String) properties.get(key); 
        if (old == null && value != null) { 
            changed = true; 
        } else { 
            if (!old.equals(value)) { 
                changed = true; 
            } 
        } 
 
        properties.put(key, value); 
    } 
 
    public String[] getKeys() { 
        return (String[]) properties.keySet().toArray( 
                new String[properties.size()]); 
    } 
 
    public String get(String key) { 
 
        return properties.getProperty(key); 
    } 
 
    public String get(String key, String defaultValue) { 
        return properties.getProperty(key, defaultValue); 
    } 
 
    public void save() { 
        try { 
            properties.store(new FileOutputStream(file), null); 
            changed=false; 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
 
    /** 
     * @return 
     */ 
    public String getFilePath() { 
        return file.getAbsolutePath(); 
    } 
 
    /** 
     * @param show_normal_html 
     * @return 
     */ 
    public boolean getBoolean(String key) { 
 
        String value = get(key); 
        if (value != null && value.equals("true")) { 
            return true; 
        } 
        return false; 
    } 
 
    public boolean getBoolean(String key, boolean defaultValue) { 
 
        String value = get(key); 
        if (value == null) 
            return defaultValue; 
        else { 
            if (value.equals("true")) 
                return true; 
        } 
        return false; 
    } 
 
    /** 
     * @param show_normal_html 
     * @param selection 
     */ 
    public void setBoolean(String key, boolean selection) { 
        if (get(key) == null) { 
            changed = true; 
        } else { 
            if (getBoolean(key) != selection) { 
                changed = true; 
            } 
        } 
        set(key, "" + selection); 
    } 
 
    /** 
     * @return 
     */ 
    public boolean avaiable() { 
        return file.exists(); 
    } 
}
    
    |