001: /*
002: * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
018: * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
019: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
020: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
021: * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
022: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
024: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
025: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
027: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: */
030:
031: package rcm.util;
032:
033: import java.util.*;
034: import java.io.*;
035:
036: public class Config extends Properties {
037: File file;
038: IOException lastException;
039:
040: public Config(String fileBaseName) {
041: this (fileBaseName, null);
042: }
043:
044: public Config(File file) {
045: this (file, null);
046: }
047:
048: public Config(String fileBaseName, Config defaults) {
049: this (new File(getHomeDirectory(), fileBaseName), defaults);
050: }
051:
052: public Config(File file, Config defaults) {
053: super (defaults);
054:
055: this .file = file;
056: try {
057: FileInputStream in = new FileInputStream(file);
058: load(in);
059: in.close();
060: } catch (IOException e) {
061: lastException = e;
062: }
063: }
064:
065: public IOException getLastException() {
066: return lastException;
067: }
068:
069: // public String getProperty (String key, String defaultValue) {
070: // String val = super.getProperty (key, defaultValue);
071: // if (val != null && val == defaultValue)
072: // put (key, defaultValue);
073: // return val;
074: // }
075:
076: public void save() {
077: try {
078: FileOutputStream out = new FileOutputStream(file);
079: save(out, "");
080: out.close();
081: lastException = null;
082: } catch (IOException e) {
083: lastException = e;
084: }
085: }
086:
087: public int countKeysStartingWith(String prefix) {
088: int n = 0;
089: for (Enumeration e = propertyNames(); e.hasMoreElements();) {
090: String name = (String) e.nextElement();
091: if (name.startsWith(prefix))
092: ++n;
093: }
094: return n;
095: }
096:
097: public void removeAllKeysStartingWith(String prefix) {
098: Vector keysToDelete = new Vector();
099: for (Enumeration e = propertyNames(); e.hasMoreElements();) {
100: String name = (String) e.nextElement();
101: if (name.startsWith(prefix))
102: keysToDelete.addElement(name);
103: }
104:
105: for (Enumeration e = keysToDelete.elements(); e
106: .hasMoreElements();)
107: remove(e.nextElement());
108: }
109:
110: public static File getHomeDirectory() {
111: String homedir;
112: if ((homedir = System.getProperty("user.home")) == null
113: && (homedir = System.getProperty("user.dir")) == null)
114: homedir = ".";
115:
116: return new File(homedir);
117: }
118: }
|