At 9:22 PM -0800 10/30/98, John Keiser wrote:
> Apparently we don't have this one. System uses it. It's not too
>big. Any
>takers?
>--John Keiser
Taken. I don't have cvs access, I'm a newcomer, so I'm gonna put
(possibly) unwanted code in everyone's mailbox. Okay? I code on a Mac, so
most aspects of the project are beyond my reach. This wasn't, it was fun!
David Himelright
------Properties.java------
package java.util;
/*
* java.util.Properties clone
* Written by David Himelright<[EMAIL PROTECTED]>, placed into the care
* of the Classpath Project and the FSF. You can have it quickly or you
* can have it done right... you got it quickly. There's some redundant code
* here, copy-paste is faster than thinking.
*/
import java.io.DataInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
public
class Properties
extends Hashtable {
protected Properties defaults;
private boolean useDefaults = false;
/**
* default constructor creates an empty properties object
*/
public Properties() {
defaults = null;
}
/**
* create a properties object with defaults as defaults
* @param defaults
*/
public Properties(Properties defaults) {
this.defaults = defaults;
useDefaults = true;
}
/**
* @param key
* @return value associated with key
*/
public String getProperty(String key) {
if(containsKey(key))
return (String)get(key);
if(useDefaults)
if(defaults.containsKey(key))
return (String)defaults.get(key);
return null;
}
/**
* Look in this for a key, if found, return the associated property
as String.
* If defaults exist, check there for a key, if found, return the
associated
* property as String. If nothing turns up, return the defaultValue.
* @param key search key
* @param defaultValue contingency result
* @return value associated with key, if none, return default value
*/
public String getProperty(String key, String defaultValue) {
if(containsKey(key))
return (String)get(key);
if(useDefaults)
if(defaults.containsKey(key))
return (String)defaults.get(key);
return defaultValue;
}
/**
* Prints all properties (system, default, local) to a PrintStream
* @param out list all properties to the given PrintStream
*/
public void list(PrintStream out) {
Properties tempProps = System.getProperties();
String tempStr;
Enumeration e;
for(e = keys(); e.hasMoreElements();) {
tempStr = (String)e.nextElement();
tempProps.put(tempStr,getProperty(tempStr));
}
if(useDefaults) {
for(e = defaults.keys(); e.hasMoreElements();) {
tempStr = (String)e.nextElement();
tempProps.put(tempStr,getProperty(tempStr));
}
}
for(e = tempProps.keys(); e.hasMoreElements();) {
tempStr = (String)e.nextElement();
out.println(tempStr +"=" +
tempProps.getProperty(tempStr));
}
}
/**
* List all properties (system, default, local) to a PrintWriter.
* copy-paste of list(PrintStream).
* @param out list all properties to the given PrintStream
*/
public void list(PrintWriter out) {
Properties tempProps = new Properties(System.getProperties());
String tempStr;
Enumeration e;
for(e = keys(); e.hasMoreElements();) {
tempStr = (String)e.nextElement();
tempProps.put(tempStr,getProperty(tempStr));
}
if(useDefaults) {
for(e = defaults.keys(); e.hasMoreElements();) {
tempStr = (String)e.nextElement();
tempProps.put(tempStr,getProperty(tempStr));
}
}
for(e = tempProps.keys(); e.hasMoreElements();) {
tempStr = (String)e.nextElement();
out.println(tempStr +"=" +
tempProps.getProperty(tempStr));
}
}
/**
* XXX this method uses the problematic DataInputStream
* @in InputStream from which to read properties
*/
public synchronized void load(InputStream in) {
DataInputStream din = new DataInputStream(in);
String line;
int ix;
try {
while((line = din.readLine()) != null) {
ix = line.indexOf("=");
put(line.substring(0,ix),
line.substring(ix+1, line.length()));
}
in.close();
} catch (IOException e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
}
}
/**
* @return an Enumeration full of property names from this instance
of Props
*/
public Enumeration propertyNames() {
return new PropertiesEnumeration(defaults.keys(), keys());
}
/**
* write this properties list to an OutputStream
* @param out OutputStream I'm writing at
* @param comment optional comment header (preceded by '#')
*/
public synchronized void save(OutputStream out, String comment) {
PrintStream ps = new PrintStream(out);
String temp;
if(comment != null) ps.println("#" + comment);
for(Enumeration e = keys(); e.hasMoreElements();) {
temp = (String)e.nextElement();
ps.println(temp + "=" + getProperty(temp));
}
}
}
------PropertiesEnumeration.java------
package java.util;
/**
* Properties support class merges two enumerations into one
*/
class PropertiesEnumeration
extends Vector
implements Enumeration {
private int curElement;
public PropertiesEnumeration(Enumeration e1, Enumeration e2) {
curElement = 0;
String temp;
while(e1.hasMoreElements()) {
temp = (String)e1.nextElement();
if(!contains(temp)) addElement(temp);
}
while(e2.hasMoreElements()) {
temp = (String)e2.nextElement();
if(!contains(temp)) addElement(temp);
}
}
public boolean hasMoreElements() {
if(curElement < size()) return true;
else return false;
}
public Object nextElement() {
return elementAt(curElement++);
}
}
------PropsTest.java------
import java.awt.*;
import java.io.*;
import java.util.*;
public class PropsTest {
public static void main(String args[]) {
System.out.println( "Props test:" );
//deal with files
FileDialog testFD = new FileDialog(new Frame(), "get a
properties file", FileDialog.LOAD);
testFD.show();
File testF = new File(testFD.getDirectory(), testFD.getFile());
FileInputStream testFIS = null;
try { testFIS = new FileInputStream(testF); }
catch (IOException e) { System.err.println("Error loading
props file"); }
//run test
Properties test = new Properties(System.getProperties());
test.put("pengin", "tux");
test.put("foo", "bar");
test.load(testFIS);
test.list(System.out);
System.out.println("Done.");
}
}
------test.properties------
meat=pork
path=cobblestone
chocolate=godiva