IM269 Coursework 3: PropertySet.java


// IM269 - Coursework 3: RMI Client/Server
// Semester A, 7th December 1998
// Eamonn Martin (BSc Computing)
// Student ID: 96/D59682
// efm001@unl.ac.uk

// RMI Hit Counter Log-File Implementation

package hitter;

// Class: PropertySet
// Reads and Writes sets of Properties through I/O streams

// A property-set starts with a line specifiying the set-ID in square-brackets.  This
// line is followed by a series of lines which each contain a single name/value pair.
// Names may be separated from values by a '=', a ':' or a space.  Lines beginning
// with a '!' or a '#' are ignored.  A property-set is terminated by an empty line.

import java.io.*;
import java.util.*;

public class PropertySet extends Properties {
	private static final String
		comment = "!#",					// Comment characters
		datasep = "=:";					// Separator characters

	// Return the index of the first character in string s from chars
	private static int charIndex(String s, String chars) {
		for (int i=0; i < s.length(); ++i)
			if (chars.indexOf(s.charAt(i))>=0) return i;
		return -1;
	}

	// Return the default data separator character
	private static char sep() { return datasep.charAt(0); }

	// Return the index of the data separator in a string
	private static int sep(String s) {
		int i = charIndex(s, datasep);			// Search for delimiters
		return (i < 0) ? s.indexOf(' ') : i;		// Else try for a space
	}

	// Return the property key from a string
	private static String getkey(String s) {
		int i = sep(s);					// Get data separator
		if (i>=0) s = s.substring(0, i);		// Extract property key
		return s.trim();				// Trim spaces
	}

	// Return the property value from a string
	private static String getvalue(String s) {
		int i = sep(s);					// Get data separator
		return (i < 0) ? "" : s.substring(i+1).trim();
	}

	// Load the next property set from an input stream
	public synchronized void load(InputStream i) throws IOException {
		boolean loaded = false;
		String s;
		int len;
		while ((s = Lib.readLine(i))!=null) {		// Read next line
			len = (s = s.trim()).length();		// Trim spaces, get length
			if (len==0)				// Empty line?
				if (!loaded) continue;		// Ignore leading blanks
				else break;			// End on trailing blank
			else if (comment.indexOf(s.charAt(0)) < 0) {
				loaded = true;			// Property set has begun
				if (s.startsWith("[") && s.endsWith("]"))
					s = sep() + s.substring(1, len-1);
				put(getkey(s), getvalue(s));	// Add property
			}
		}
	}

	// Write this property set to an output stream (ID first)
	public synchronized void save(OutputStream o) throws IOException {
		String s;
		if ((s = getProperty(""))!=null)		// Get property-set ID
			Lib.writeLine("[" + s + "]", o);	// Write property-set ID
		Enumeration e = propertyNames();		// Get property list
		while (e.hasMoreElements()) {			// Get next property
			s = (String)e.nextElement();		// Get property key
			if (s.length() > 0)			// Normal property key?
				Lib.writeLine(s + sep() + getProperty(s), o);
		}
		Lib.writeLine("", o);				// Terminating blank
	}

	// Load the next property set from an input stream (true if loaded)
	public boolean loadSet(InputStream i) {
		clear();					// Clear properties
		boolean ok = true;				// Exception flag
		try { load(i); } catch (IOException e) { ok = false; }
		return ok && !isEmpty();
	}

	// Add all properties in p to this properties list
	public void addProperties(Properties p) {
		Enumeration e = p.propertyNames();		// Get new names
		while (e.hasMoreElements()) {
			String s = (String)e.nextElement();	// Get next name
			put(s, p.getProperty(s));		// Add to this list
		}
	}

	// Return true if property key is defined with the given value
	public boolean hasProperty(String key, String value) {
		String s = getProperty(key);			// Get value (if any)
		return (s==null) ? false : !s.equals(value) ? false : true;
	}
}

Go To: IM269: Programming The Internet