From: Stephen C. <cr...@us...> - 2005-09-24 14:32:53
|
Update of /cvsroot/lexi/lexi/src/com/jmonkey/export In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23706/src/com/jmonkey/export Modified Files: Registry.java RegistryImpl.java RegistryPropertyException.java Log Message: Reworked the Registry get* methods so to get rid of the 'defaultValue' param and associated initialization side-effect. Added an 'initGroup' method to the Registry API. Reworked the calls that initialize the properties of blank Registries. Index: Registry.java =================================================================== RCS file: /cvsroot/lexi/lexi/src/com/jmonkey/export/Registry.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** Registry.java 3 Sep 2005 16:01:41 -0000 1.10 --- Registry.java 24 Sep 2005 14:32:44 -0000 1.11 *************** *** 69,72 **** --- 69,74 ---- */ public abstract class Registry { + private static String FS = File.separator; + public static final int TYPE_NONE = 0; // None - not a property public static final int TYPE_STRING_SINGLE = 1; // String *************** *** 126,129 **** --- 128,183 ---- } + public static int javaTypeToType(String type) throws RegistryException + { + if (type.equals("String")) { + return Registry.TYPE_STRING_SINGLE; + } + else if (type.equals("String[]")) { + return Registry.TYPE_STRING_ARRAY; + } + else if (type.equals("Object")) { + return Registry.TYPE_OBJECT_SINGLE; + } + else if (type.equals("Object[]")) { + return Registry.TYPE_OBJECT_ARRAY; + } + else if (type.equals("boolean")) { + return Registry.TYPE_BOOLEAN_SINGLE; + } + else if (type.equals("byte")) { + return Registry.TYPE_BYTE_SINGLE; + } + else if (type.equals("byte[]")) { + return Registry.TYPE_BYTE_ARRAY; + } + else if (type.equals("char")) { + return Registry.TYPE_CHAR_SINGLE; + } + else if (type.equals("char[]")) { + return Registry.TYPE_CHAR_ARRAY; + } + else if (type.equals("short")) { + return Registry.TYPE_SHORT_SINGLE; + } + else if (type.equals("int")) { + return Registry.TYPE_INT_SINGLE; + } + else if (type.equals("int[]")) { + return Registry.TYPE_INT_ARRAY; + } + else if (type.equals("long")) { + return Registry.TYPE_LONG_SINGLE; + } + else if (type.equals("float")) { + return Registry.TYPE_FLOAT_SINGLE; + } + else if (type.equals("double")) { + return Registry.TYPE_DOUBLE_SINGLE; + } + else { + throw new RegistryException("Type not supported (" + type + ")"); + } + } + /** * The directory that resources will be stored in.<BR> *************** *** 132,141 **** public static final File RESOURCE_DIRECTORY = new File(Runtime.ensureDirectory(System.getProperty("user.home") + ! File.separator + ".lexi" + File.separator + ! "export")); public static final File REGISTRY_DIRECTORY = new File(Runtime.ensureDirectory(RESOURCE_DIRECTORY.getAbsolutePath() + ! File.separator + "registry")); protected Registry() { --- 186,194 ---- public static final File RESOURCE_DIRECTORY = new File(Runtime.ensureDirectory(System.getProperty("user.home") + ! FS + ".lexi" + FS + "export")); public static final File REGISTRY_DIRECTORY = new File(Runtime.ensureDirectory(RESOURCE_DIRECTORY.getAbsolutePath() + ! FS + "registry")); protected Registry() { *************** *** 218,226 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract boolean getBoolean(String group, String key, ! boolean defaultValue); /** --- 271,279 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract boolean getBoolean(String group, String key) ! throws RegistryPropertyException; /** *************** *** 229,236 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract byte getByte(String group, String key, byte defaultValue); /** --- 282,290 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract byte getByte(String group, String key) ! throws RegistryPropertyException; /** *************** *** 239,247 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract byte[] getByteArray(String group, String key, ! byte[] defaultValue); /** --- 293,301 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract byte[] getByteArray(String group, String key) ! throws RegistryPropertyException; /** *************** *** 250,257 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract char getChar(String group, String key, char defaultValue); /** --- 304,312 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract char getChar(String group, String key) ! throws RegistryPropertyException; /** *************** *** 260,268 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract char[] getCharArray(String group, String key, ! char[] defaultValue); /** --- 315,323 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract char[] getCharArray(String group, String key) ! throws RegistryPropertyException; /** *************** *** 271,279 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract double getDouble(String group, String key, ! double defaultValue); /** --- 326,334 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract double getDouble(String group, String key) ! throws RegistryPropertyException; /** *************** *** 289,296 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract float getFloat(String group, String key, float defaultValue); /** --- 344,352 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract float getFloat(String group, String key) ! throws RegistryPropertyException; /** *************** *** 306,313 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract int getInteger(String group, String key, int defaultValue); /** --- 362,370 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract int getInteger(String group, String key) ! throws RegistryPropertyException; /** *************** *** 316,324 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract int[] getIntegerArray(String group, String key, ! int[] defaultValue); /** --- 373,381 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract int[] getIntegerArray(String group, String key) ! throws RegistryPropertyException; /** *************** *** 335,342 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract long getLong(String group, String key, long defaultValue); /** --- 392,400 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract long getLong(String group, String key) ! throws RegistryPropertyException; /** *************** *** 345,353 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract Object[] getObject(String group, String key, ! Serializable[] defaultValue); /** --- 403,411 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract Object[] getObjectArray(String group, String key) ! throws RegistryPropertyException; /** *************** *** 356,364 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract Object getObject(String group, String key, ! Serializable defaultValue); /** --- 414,422 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract Object getObject(String group, String key) ! throws RegistryPropertyException; /** *************** *** 367,374 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract short getShort(String group, String key, short defaultValue); /** --- 425,433 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract short getShort(String group, String key) ! throws RegistryPropertyException; /** *************** *** 377,385 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract String getString(String group, String key, ! String defaultValue); /** --- 436,444 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract String getString(String group, String key) ! throws RegistryPropertyException; /** *************** *** 388,396 **** * @param group the name of the group * @param key the name of the key in the group ! * @param defaultValue a default value if the key is not found. ! * @return the value if the key is found, or the default value */ ! public abstract String[] getStringArray(String group, String key, ! String[] defaultValue); /** --- 447,455 ---- * @param group the name of the group * @param key the name of the key in the group ! * @return the value if the key is found ! * @throws RegistryPropertyException if the property is missing. */ ! public abstract String[] getStringArray(String group, String key) ! throws RegistryPropertyException; /** *************** *** 400,405 **** * @param key the name of the key in the group * @return the type of the value */ ! public abstract int getType(String group, String key); /** --- 459,466 ---- * @param key the name of the key in the group * @return the type of the value + * @throws RegistryPropertyException if the property is missing. */ ! public abstract int getType(String group, String key) ! throws RegistryPropertyException; /** *************** *** 421,426 **** */ public abstract void importGroup(String group, RegistryGroup properties); ! /** * Test if the registry is in sync with the stored version. * Calling commit() will sync the registry with the stored --- 482,514 ---- */ public abstract void importGroup(String group, RegistryGroup properties); + + /** + * Initialize a Registry group from an array of Strings. The array + * has dimensions N x 3 where N is the number of properties. Each + * property is defined by the three array row entries, as follows: + * <ul> + * <li><code>props[i][0]</code> gives the property name. + * <li><code>props[i][1]</code> gives the initial value of the + * property, encoded as a String. If there is no 2nd row entry, + * the property is defined with type "String" and an empty value. + * <li><code>props[i][2]</code> gives the Java type of the property. + * If there is no 3rd row entry, the property is assumed to have + * type "String". + * </ul> + * @param group the name of the group + * @param props the property definitions are as described above. + * @throws RegistryException if the property type is not supported, + * the value doesn't match the type, or there is some other + * inconsistency. + */ + public abstract void initGroup(String group, String[][] props); ! /** ! * Test if the registry is currently blank. ! * @return <code>true</code> if the registry contains no groups. ! */ ! public abstract boolean isBlank(); ! ! /** * Test if the registry is in sync with the stored version. * Calling commit() will sync the registry with the stored Index: RegistryImpl.java =================================================================== RCS file: /cvsroot/lexi/lexi/src/com/jmonkey/export/RegistryImpl.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RegistryImpl.java 3 Sep 2005 16:01:42 -0000 1.3 --- RegistryImpl.java 24 Sep 2005 14:32:44 -0000 1.4 *************** *** 344,376 **** } ! private String getBasicProperty(String group, String key, String value, ! int type) { ! if (value == null) { ! RegistryGroup rg = (RegistryGroup) m_groups.get(group); ! if (rg == null) { ! return null; ! } ! String res = rg.getProperty(key); ! if (res != null && rg.getPropertyType(key) != type) { ! throw new RegistryTypeException("property has wrong type", group, ! key, rg.getPropertyType(key), type); ! } ! return res; } ! else { ! ensureGroup(group); ! RegistryGroup rg = (RegistryGroup) m_groups.get(group); ! String res = rg.getProperty(key); ! if (res == null) { ! rg.setProperty(key, value, type); ! res = value; ! } ! else if (rg.getPropertyType(key) != type) { ! throw new RegistryTypeException("property has wrong type", group, ! key, rg.getPropertyType(key), type); ! } ! return res; } } --- 344,362 ---- } ! private String getBasicProperty(String group, String key, int type) { ! RegistryGroup rg = (RegistryGroup) m_groups.get(group); ! if (rg == null) { ! throw new RegistryPropertyException(group, key, "missing registry group"); } ! String res = rg.getProperty(key); ! if (res == null) { ! throw new RegistryPropertyException(group, key, "missing property"); } + if (rg.getPropertyType(key) != type) { + throw new RegistryTypeException("property has wrong type", group, + key, rg.getPropertyType(key), type); + } + return res; } *************** *** 564,583 **** // =============================================================================== ! public String getString(String group, String key, String defaultValue) { ! return getBasicProperty(group, key, defaultValue, TYPE_STRING_SINGLE); } ! public String[] getStringArray(String group, String key, ! String[] defaultValue) { ! String encoded = encode(defaultValue); ! String res = getBasicProperty(group, key, encoded, TYPE_STRING_ARRAY); return (String[]) decode(res); } ! public boolean getBoolean(String group, String key, boolean defaultValue) { ! Code.debug("getBoolean(\"" + group + "\", \"" + key + "\", " + ! defaultValue + ")"); ! String tmp = Boolean.toString(defaultValue); ! String res = getBasicProperty(group, key, tmp, TYPE_BOOLEAN_SINGLE).trim(); if (res.equalsIgnoreCase("true")) { return true; --- 550,565 ---- // =============================================================================== ! public String getString(String group, String key) { ! return getBasicProperty(group, key, TYPE_STRING_SINGLE); } ! public String[] getStringArray(String group, String key) { ! String res = getBasicProperty(group, key, TYPE_STRING_ARRAY); return (String[]) decode(res); } ! public boolean getBoolean(String group, String key) { ! Code.debug("getBoolean(\"" + group + "\", \"" + key + "\")"); ! String res = getBasicProperty(group, key, TYPE_BOOLEAN_SINGLE); if (res.equalsIgnoreCase("true")) { return true; *************** *** 587,597 **** } else { ! throw new RegistryException("malformed boolean value"); } } ! public int getInteger(String group, String key, int defaultValue) { ! String tmp = Integer.toString(defaultValue); ! String res = getBasicProperty(group, key, tmp, TYPE_INT_SINGLE).trim(); try { return Integer.parseInt(res); --- 569,578 ---- } else { ! throw new RegistryPropertyException(group, key, "malformed boolean value"); } } ! public int getInteger(String group, String key) { ! String res = getBasicProperty(group, key, TYPE_INT_SINGLE); try { return Integer.parseInt(res); *************** *** 602,614 **** } ! public int[] getIntegerArray(String group, String key, int[] defaultValue) { ! String encoded = encode(defaultValue); ! String res = getBasicProperty(group, key, encoded, TYPE_INT_ARRAY); return (int[]) decode(res); } ! public long getLong(String group, String key, long defaultValue) { ! String tmp = Long.toString(defaultValue); ! String res = getBasicProperty(group, key, tmp, TYPE_LONG_SINGLE).trim(); try { return Long.parseLong(res); --- 583,593 ---- } ! public int[] getIntegerArray(String group, String key) { ! String res = getBasicProperty(group, key, TYPE_INT_ARRAY); return (int[]) decode(res); } ! public long getLong(String group, String key) { ! String res = getBasicProperty(group, key, TYPE_LONG_SINGLE).trim(); try { return Long.parseLong(res); *************** *** 619,625 **** } ! public byte getByte(String group, String key, byte defaultValue) { ! String tmp = Byte.toString(defaultValue); ! String res = getBasicProperty(group, key, tmp, TYPE_BYTE_SINGLE).trim(); try { return Byte.parseByte(res); --- 598,603 ---- } ! public byte getByte(String group, String key) { ! String res = getBasicProperty(group, key, TYPE_BYTE_SINGLE).trim(); try { return Byte.parseByte(res); *************** *** 630,642 **** } ! public byte[] getByteArray(String group, String key, byte[] defaultValue) { ! String encoded = encode(defaultValue); ! String res = getBasicProperty(group, key, encoded, TYPE_BYTE_ARRAY); return (byte[]) decode(res); } ! public char getChar(String group, String key, char defaultValue) { ! String tmp = Character.toString(defaultValue); ! String res = getBasicProperty(group, key, tmp, TYPE_CHAR_SINGLE).trim(); if (res.length() != 1) { throw new RegistryException("malformed char value"); --- 608,618 ---- } ! public byte[] getByteArray(String group, String key) { ! String res = getBasicProperty(group, key, TYPE_BYTE_ARRAY); return (byte[]) decode(res); } ! public char getChar(String group, String key) { ! String res = getBasicProperty(group, key, TYPE_CHAR_SINGLE).trim(); if (res.length() != 1) { throw new RegistryException("malformed char value"); *************** *** 645,657 **** } ! public char[] getCharArray(String group, String key, char[] defaultValue) { ! String encoded = encode(defaultValue); ! String res = getBasicProperty(group, key, encoded, TYPE_CHAR_ARRAY); return (char[]) decode(res); } ! public double getDouble(String group, String key, double defaultValue) { ! String tmp = Double.toString(defaultValue); ! String res = getBasicProperty(group, key, tmp, TYPE_DOUBLE_SINGLE).trim(); try { return Double.parseDouble(res); --- 621,631 ---- } ! public char[] getCharArray(String group, String key) { ! String res = getBasicProperty(group, key, TYPE_CHAR_ARRAY); return (char[]) decode(res); } ! public double getDouble(String group, String key) { ! String res = getBasicProperty(group, key, TYPE_DOUBLE_SINGLE).trim(); try { return Double.parseDouble(res); *************** *** 662,668 **** } ! public float getFloat(String group, String key, float defaultValue) { ! String tmp = Float.toString(defaultValue); ! String res = getBasicProperty(group, key, tmp, TYPE_FLOAT_SINGLE).trim(); try { return Float.parseFloat(res); --- 636,641 ---- } ! public float getFloat(String group, String key) { ! String res = getBasicProperty(group, key, TYPE_FLOAT_SINGLE).trim(); try { return Float.parseFloat(res); *************** *** 673,692 **** } ! public Object getObject(String group, String key, Serializable defaultValue) { ! String tmp = encode(defaultValue); ! String res = getBasicProperty(group, key, tmp, TYPE_OBJECT_SINGLE); return decode(res); } ! public Object[] getObject(String group, String key, ! Serializable[] defaultValue) { ! String tmp = encode(defaultValue); ! String res = getBasicProperty(group, key, tmp, TYPE_OBJECT_ARRAY); return (Object[]) decode(res); } ! public short getShort(String group, String key, short defaultValue) { ! String tmp = Short.toString(defaultValue); ! String res = getBasicProperty(group, key, tmp, TYPE_SHORT_SINGLE).trim(); try { return Short.parseShort(res); --- 646,661 ---- } ! public Object getObject(String group, String key) { ! String res = getBasicProperty(group, key, TYPE_OBJECT_SINGLE); return decode(res); } ! public Object[] getObjectArray(String group, String key) { ! String res = getBasicProperty(group, key, TYPE_OBJECT_ARRAY); return (Object[]) decode(res); } ! public short getShort(String group, String key) { ! String res = getBasicProperty(group, key, TYPE_SHORT_SINGLE).trim(); try { return Short.parseShort(res); *************** *** 793,796 **** --- 762,769 ---- return 0; } + + public boolean isBlank() { + return m_groups.size() == 0; + } public int size() { *************** *** 840,843 **** --- 813,834 ---- } } + + public void initGroup(String group, String[][] props) { + if (!isGroup(group)) { + RegistryGroup rg = new RegistryGroup(); + for (int i = 0; i < props.length; i++) { + String[] prop = props[i]; + if (prop == null || prop.length == 0) { + continue; + } + String propName = prop[0]; + String propValue = (prop.length > 1) ? prop[1] : ""; + String propType = (prop.length > 2) ? prop[2] : "String"; + rg.setProperty(propName, propValue, Registry.javaTypeToType(propType)); + } + m_groups.put(group, rg); + m_altered = true; + } + } public void replaceGroup(String group, RegistryGroup RegistryGroup) { Index: RegistryPropertyException.java =================================================================== RCS file: /cvsroot/lexi/lexi/src/com/jmonkey/export/RegistryPropertyException.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RegistryPropertyException.java 3 Sep 2005 05:50:19 -0000 1.1 --- RegistryPropertyException.java 24 Sep 2005 14:32:44 -0000 1.2 *************** *** 26,31 **** /** ! * This exception is thrown if a Registry is misused or if it is found ! * to be inconsistent. * * @author Stephen Crawley --- 26,31 ---- /** ! * This exception is thrown if there is a problem with a Registry ! * property; e.g. a requested property is missing. * * @author Stephen Crawley |