Annotation of java/classes/org/w3c/util/ObservableProperties.java, revision 1.1
1.1 ! abaird 1: // ObservableProperties.java
! 2: // $Id$
! 3: // (c) COPYRIGHT MIT and INRIA, 1996.
! 4: // Please first read the full copyright statement in file COPYRIGHT.html
! 5:
! 6: package w3c.util;
! 7:
! 8: import java.util.* ;
! 9: import java.io.File ;
! 10:
! 11: /**
! 12: * An enhanced property class that provides support to monitor changes.
! 13: * This class extends the basic properties class of Java, by providing
! 14: * monitoring support. It also provides more type conversion.
! 15: * @see PropertyMonitoring
! 16: */
! 17:
! 18: public class ObservableProperties extends Properties {
! 19: PropertyMonitoring observers[] = null ;
! 20: int observers_count = 0 ;
! 21:
! 22: /**
! 23: * Subscribe for property monitoring.
! 24: * @param observer The object that handles the PropertyMonitoring
! 25: * interface.
! 26: */
! 27:
! 28: public synchronized void registerObserver (PropertyMonitoring o) {
! 29: // Try looking for an empty slot:
! 30: for (int i = 0 ; i < observers.length ; i++) {
! 31: if ( observers[i] == null ) {
! 32: observers[i] = o ;
! 33: return ;
! 34: }
! 35: }
! 36: // Add the observer to the registered oned, resizing array if needed
! 37: if ( observers_count + 1 >= observers.length ) {
! 38: PropertyMonitoring m[]=new PropertyMonitoring[observers.length*2];
! 39: System.arraycopy (observers, 0, m, 0, observers.length) ;
! 40: observers = m ;
! 41: }
! 42: observers[observers_count++] = o;
! 43: }
! 44:
! 45: /**
! 46: * Unsubscribe this object from the observers list.
! 47: * @param observer The observer to unsubscribe.
! 48: * @return A boolean <strong>true</strong> if object was succesfully
! 49: * unsubscribed, <strong>false</strong> otherwise.
! 50: */
! 51:
! 52: public synchronized boolean unregisterObserver (PropertyMonitoring o) {
! 53: for (int i = 0 ; i < observers.length ; i++) {
! 54: if ( observers[i] == o ) {
! 55: observers[i] = null ;
! 56: return true ;
! 57: }
! 58: }
! 59: return false ;
! 60: }
! 61:
! 62: /**
! 63: * Update a property value.
! 64: * Assign a value to a property. If the property value has really changed
! 65: * notify our observers of the change.
! 66: * @param name The name of the property to assign.
! 67: * @param value The new value for this property, or <strong>null</strong>
! 68: * if the property setting is to be cancelled.
! 69: * @return A boolean <strong>true</strong> if change was accepted by
! 70: * our observers, <strong>false</strong> otherwise.
! 71: */
! 72:
! 73: public synchronized boolean putValue (String name, String value) {
! 74: // If null value, remove the prop definition:
! 75: if ( value == null ) {
! 76: super.remove(name) ;
! 77: return true ;
! 78: }
! 79: // Otherwise, proceed:
! 80: String old = (String) get (value) ;
! 81: if ( (old == null) || (! old.equals (value)) ) {
! 82: super.put (name, value) ;
! 83: for (int i = 0 ; i < observers.length ; i++) {
! 84: if ( observers[i] == null )
! 85: continue ;
! 86: if ( ! observers[i].propertyChanged (name) ) {
! 87: if ( old != null )
! 88: super.put (name, old) ;
! 89: return false ;
! 90: }
! 91: }
! 92: }
! 93: return true ;
! 94: }
! 95:
! 96: /**
! 97: * Get this property value, as a boolean.
! 98: * @param name The name of the property to be fetched.
! 99: * @param def The default value, if the property isn't defined.
! 100: * @return A Boolean instance.
! 101: */
! 102:
! 103: public boolean getBoolean(String name, boolean def) {
! 104: String v = getProperty(name, null);
! 105: if ( v != null )
! 106: return "true".equalsIgnoreCase(v) ? true : false ;
! 107: return def ;
! 108: }
! 109:
! 110: /**
! 111: * Get this property value, as a String.
! 112: * @param name The name of the property to be fetched.
! 113: * @param def The default value, if the property isn't defined.
! 114: * @return An instance of String.
! 115: */
! 116:
! 117: public String getString(String name, String def) {
! 118: String v = getProperty (name, null);
! 119: if ( v != null )
! 120: return v ;
! 121: return def ;
! 122: }
! 123:
! 124: /**
! 125: * Get this property as a String array.
! 126: * By convention, properties that are get as string arrays should be
! 127: * encoded as a <strong>|</strong> separated list of Strings.
! 128: * @param name The property's name.
! 129: * @param def The default value (if undefined).
! 130: * @return A String array, or <strong>null</strong> if the property
! 131: * is undefined.
! 132: */
! 133:
! 134: public String[] getStringArray(String name, String def[]) {
! 135: String v = getProperty(name, null);
! 136: if ( v == null )
! 137: return def;
! 138: // Parse the property value:
! 139: StringTokenizer st = new StringTokenizer(v, "|");
! 140: int len = st.countTokens();
! 141: String ret[] = new String[len];
! 142: for (int i = 0 ; i < ret.length ; i++) {
! 143: ret[i] = st.nextToken();
! 144: }
! 145: return ret;
! 146: }
! 147:
! 148: /**
! 149: * Get this property value, as an integer.
! 150: * @param name The name of the property to be fetched.
! 151: * @param def The default value, if the property isn't defined.
! 152: * @return An instance of Integer.
! 153: */
! 154:
! 155: public int getInteger(String name, int def) {
! 156: String v = getProperty (name, null);
! 157: if ( v != null ) {
! 158: try {
! 159: if (v.startsWith("0x")) {
! 160: return Integer.valueOf(v.substring(2), 16).intValue();
! 161: }
! 162: if (v.startsWith("#")) {
! 163: return Integer.valueOf(v.substring(1), 16).intValue();
! 164: }
! 165: if (v.startsWith("0")) {
! 166: return Integer.valueOf(v.substring(1), 8).intValue();
! 167: }
! 168: return Integer.valueOf(v).intValue();
! 169: } catch (NumberFormatException e) {
! 170: }
! 171: }
! 172: return def ;
! 173: }
! 174:
! 175: /**
! 176: * Get this property value, as a File.
! 177: * @param name The name of the property to be fetched.
! 178: * @param def The default value, if the property isn't defined.
! 179: * @return An instance of File.
! 180: */
! 181:
! 182: public File getFile(String name, File def) {
! 183: String v = getProperty(name, null);
! 184: if ( v != null )
! 185: return new File (v) ;
! 186: return def ;
! 187: }
! 188:
! 189: /**
! 190: * Build an httpdProperties instance from a Properties instance.
! 191: * @param props The Properties instance.
! 192: */
! 193:
! 194: public ObservableProperties(Properties props) {
! 195: super (props) ;
! 196: this.observers = new PropertyMonitoring[5] ;
! 197: this.observers_count = 0 ;
! 198: }
! 199:
! 200: }
! 201:
! 202:
Webmaster