Package gui.tools

Source Code of gui.tools.PropertiesManager

/*
* Hamsam - Instant Messaging API
* Copyright (C) 2003 Raghu K
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package gui.tools;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.util.Properties;
import java.util.Vector;
import java.util.Enumeration;

/**
* <p>Titre : Hamsam Project</p>
* <p>Description : Instant Messenger</p>
* <p>Copyright : Copyright (c) 2003</p>
* <p>Soci�t� : </p>
* @author Fr�d�ric DIB
* @version 0.1
*/

public class PropertiesManager {
  private Properties p;
  private String path = null;
  private boolean ok = false;
  /**
   *Constructor
   */
  public PropertiesManager(String path) {
    this.path = path;
    this.loadProperties(path);
  }

  /**
   * Get and record informations from a file to a properties object.
   */
  public Properties loadProperties(String path) {
    try {
      FileInputStream fis = new FileInputStream(path);
      p = new Properties();
      p.load(fis);
      ok = true;
      return p;
    }
    catch (FileNotFoundException e) {
      e.printStackTrace();
      return null;
    }
    catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }

  /**
   *Update properties file.
   */
  public void saveProperties(String commentaires) {
    if (ok)
      try {
        FileOutputStream fos = new FileOutputStream(path);
        p.store(fos, commentaires);
      }
      catch (FileNotFoundException e) {
        e.printStackTrace();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
  }


  /**
   *Update properties file.
   */
  public void saveProperties(String path, String commentaires) {
    if (ok)
      try {
        FileOutputStream fos = new FileOutputStream(path);
        p.store(fos, commentaires);
      }
      catch (FileNotFoundException e) {
        e.printStackTrace();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
  }

  /**
   * Set the key value in the properties file.
   */
  public void setValue(String key, String val) {
    p.setProperty(key, val);
  }

  /**
   * Get the key value from the properties file.
   */
  public String getValue(String key) {
    String ret = p.getProperty(key);
    if (ret == null) {
      ret = new String("INVALID KEY - " + key);
    }
    return ret;
  }

  /**
   * Get the key value from the properties file.
   * Replace the $1$ item by value.
   */
  public String getValue(String key, String value) {
    String ret = "";
    String temp = getValue(key);

    if (!temp.substring(0, 10).equals("INVALID KEY")) {
      for (int i = 0; i < temp.length(); i++) {
        if ( (temp.charAt(i) == '$')
            && (temp.charAt(i + 1) == '1')
            && (temp.charAt(i + 2) == '$')) {
          ret = new String(ret + value);
          i = i + 2;
        }
        else {
          ret = new String(ret + temp.charAt(i));
        }
      }
    }
    return ret;
  }

  /**
   *Replace key value by newValue.
   */
  public void changeValue(String key, String newValue) {
    if (ok)
      p.setProperty(key, newValue);
  }

  /**
   * Return a Vector of all key in the properties file.
   */
  public Vector getKeyList() {
    Vector v = new Vector();
    Enumeration e = p.propertyNames();
    while (e.hasMoreElements()) {
      v.addElement(e.nextElement());
    }
    return v;
  }
}
TOP

Related Classes of gui.tools.PropertiesManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.