    001    package ifs.util;
    002
    003    import java.util.*;
    004
    005    /** Data properties.
    006     *
    007     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
    008     * @version 1.0
    009     */
    010    public class DataProperties extends Properties {
    011        private boolean iSaveDefaults = false;
    012
    013        /** Constructor */
    014        public DataProperties() {
    015            super();
    016        }
    017
    018        /** Constructor
    019         * @param defaults default properties
    020         */
    021        public DataProperties(Properties defaults) {
    022            super(defaults);
    023            iSaveDefaults = getPropertyBoolean("General.SaveDefaultProperties", false);
    024        }
    025
    026        /** Constructor
    027         * @param properties default properties
    028         */
    029        public DataProperties(Dictionary properties) {
    030            super();
    031            for (Enumeration e=properties.keys();e.hasMoreElements();) {
    032                Object key = e.nextElement();
    033                Object value = properties.get(key);
    034                setProperty(key.toString(), value.toString());
    035            }
    036            iSaveDefaults = getPropertyBoolean("General.SaveDefaultProperties", false);
    037        }
    038
    039        /** Returns string property
    040         * @param key key
    041         * @param defaultValue default value to be returned when such property is not present
    042         */
    043        public String getProperty(String key, String defaultValue) {
    044            if (!iSaveDefaults || containsPropery(key)) return super.getProperty(key, defaultValue);
    045            if (defaultValue!=null) setProperty(key, defaultValue);
    046            return defaultValue;
    047        }
    048
    049        /** Sets string property
    050         * @param key key
    051         * @param value value
    052         */
    053        public Object setProperty(String key, String value) {
    054            Object ret = super.setProperty(key, value);
    055            if ("General.SaveDefaultProperties".equals(key))
    056                iSaveDefaults = getPropertyBoolean("General.SaveDefaultProperties", false);
    057            return ret;
    058        }
    059
    060        /** Returns int property
    061         * @param key key
    062         * @param defaultValue default value to be returned when such property is not present
    063         */
    064        public int getPropertyInt(String key, int defaultValue) {
    065            try {
    066                if (containsPropery(key)) return Integer.parseInt(getProperty(key));
    067                if (iSaveDefaults) setProperty(key, String.valueOf(defaultValue));
    068                return defaultValue;
    069            } catch (NumberFormatException nfe) {
    070                if (iSaveDefaults) setProperty(key, String.valueOf(defaultValue));
    071                return defaultValue;
    072            }
    073        }
    074
    075        /** Returns long property
    076         * @param key key
    077         * @param defaultValue default value to be returned when such property is not present
    078         */
    079        public long getPropertyLong(String key, long defaultValue) {
    080            try {
    081                if (containsPropery(key)) return Long.parseLong(getProperty(key));
    082                if (iSaveDefaults) setProperty(key, String.valueOf(defaultValue));
    083                return defaultValue;
    084            } catch (NumberFormatException nfe) {
    085                if (iSaveDefaults) setProperty(key, String.valueOf(defaultValue));
    086                return defaultValue;
    087            }
    088        }
    089
    090        /** Returns true if there is such property
    091         * @param key key
    092         */
    093        public boolean containsPropery(String key) {
    094            return getProperty(key)!=null;
    095        }
    096
    097        /** Returns boolean property
    098         * @param key key
    099         * @param defaultValue default value to be returned when such property is not present
    100         */
    101        public boolean getPropertyBoolean(String key, boolean defaultValue) {
    102            try {
    103                if (containsPropery(key)) return getProperty(key).equalsIgnoreCase("true");
    104                if (iSaveDefaults) setProperty(key, (defaultValue?"true":"false"));
    105                return defaultValue;
    106            } catch (Exception nfe) {
    107                if (iSaveDefaults) setProperty(key, (defaultValue?"true":"false"));
    108                return defaultValue;
    109            }
    110        }
    111
    112        /** Returns double property
    113         * @param key key
    114         * @param defaultValue default value to be returned when such property is not present
    115         */
    116        public double getPropertyDouble(String key, double defaultValue) {
    117            try {
    118                if (containsPropery(key)) return Double.parseDouble(getProperty(key));
    119                if (iSaveDefaults) setProperty(key, String.valueOf(defaultValue));
    120                return defaultValue;
    121            } catch (NumberFormatException nfe) {
    122                if (iSaveDefaults) setProperty(key, String.valueOf(defaultValue));
    123                return defaultValue;
    124            }
    125        }
    126
    127        /** Returns float property
    128         * @param key key
    129         * @param defaultValue default value to be returned when such property is not present
    130         */
    131        public float getPropertyFloat(String key, float defaultValue) {
    132            try {
    133                if (containsPropery(key)) return Float.parseFloat(getProperty(key));
    134                if (iSaveDefaults) setProperty(key, String.valueOf(defaultValue));
    135                return defaultValue;
    136            } catch (NumberFormatException nfe) {
    137                if (iSaveDefaults) setProperty(key, String.valueOf(defaultValue));
    138                return defaultValue;
    139            }
    140        }
    141
    142        /** Returns properties as dictionary.
    143         */
    144        public Dictionary toDict() {
    145            Hashtable ret = new Hashtable();
    146            for (Enumeration e=propertyNames();e.hasMoreElements();) {
    147                String key = (String)e.nextElement();
    148                String prop = getProperty(key);
    149                if (key!=null && prop!=null) ret.put(key,prop);
    150            }
    151            return ret;
    152        }
    153
    154        private void expand(String key) {
    155            String value = getProperty(key);
    156            if (value==null) return;
    157            int done = -1, idx = -1;
    158            while ((idx=value.indexOf('%', done+1))>=0) {
    159                int idx2 = value.indexOf('%', idx+1);
    160                if (idx2<0) return;
    161                String subString = value.substring(idx+1,idx2);
    162                if (containsPropery(subString))
    163                    value = value.substring(0,idx)+getProperty(subString)+value.substring(idx2+1);
    164                else
    165                    done=idx;
    166            }
    167        }
    168
    169        /** Loads properties from an input stream*/
    170        public void load(java.io.InputStream inputStream) throws java.io.IOException {
    171            super.load(inputStream);
    172            for (Enumeration e=keys();e.hasMoreElements();) {
    173                expand((String)e.nextElement());
    174            }
    175            iSaveDefaults = getPropertyBoolean("General.SaveDefaultProperties", false);
    176        }
    177    }