001: /**********************************************************************************
002:
003: Feedzeo!
004: A free and open source RSS/Atom/RDF feed aggregator
005:
006: Copyright (C) 2005-2006 Anand Rao (anandrao@users.sourceforge.net)
007:
008: This library is free software; you can redistribute it and/or
009: modify it under the terms of the GNU Lesser General Public
010: License as published by the Free Software Foundation; either
011: version 2.1 of the License, or (at your option) any later version.
012:
013: This library is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: Lesser General Public License for more details.
017:
018: You should have received a copy of the GNU Lesser General Public
019: License along with this library; if not, write to the Free Software
020: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
021:
022: ************************************************************************************/package util;
023:
024: import java.util.*;
025: import java.io.*;
026:
027: /**
028: *
029: * @author Anand Rao
030: */
031:
032: public class PropertyFile extends Properties {
033:
034: private final boolean DEBUG_CLASS = false; //debug flag to enable/disable debugging
035:
036: private final int READ_MODE = 0;
037: private final int WRITE_MODE = 1;
038: private final int NO_MODE = -1;
039:
040: private int mode = NO_MODE;
041:
042: private String filename;
043: private FileInputStream in;
044:
045: public PropertyFile(String filename, char fmode) {
046: super ();
047: this .filename = filename;
048: if (fmode == 'r') {
049: try {
050: in = new FileInputStream(filename);
051: load(in);
052: mode = READ_MODE;
053: list(System.out);
054: } catch (Exception e) {
055: ExceptionUtil.reportException(e);
056: }
057: }
058: if (fmode == 'w') {
059: mode = WRITE_MODE;
060: }
061: System.out.println("Propertyfile mode:" + mode);
062: }
063:
064: public void writeProperty(String key, String value) {
065: if (mode == WRITE_MODE)
066: setProperty(key, value);
067: }
068:
069: public String readProperty(String key) {
070:
071: if (DEBUG_CLASS) {
072: System.out.println("in readproperty");
073: System.out.println("Propertyfile mode:" + mode);
074: }
075:
076: if (mode == READ_MODE) {
077: if (DEBUG_CLASS)
078: System.out.println("Properytfile reading key:" + key
079: + " val:" + getProperty(key));
080: return getProperty(key).trim();
081: }
082: return null;
083: }
084:
085: public void saveAndClose() {
086: if (mode == WRITE_MODE) {
087: try {
088: FileOutputStream out = new FileOutputStream(filename);
089: store(out, null);
090: out.close();
091: } catch (Exception e) {
092: ExceptionUtil.reportException(e);
093: }
094: }
095: }
096:
097: public void close() {
098: if (mode == READ_MODE) {
099: try {
100: in.close();
101: } catch (Exception e) {
102: ExceptionUtil.reportException(e);
103: }
104: }
105: }
106: }
|