📄 rtproperties.java
字号:
} else { // missing name terminating ']' p = p.substring(1).trim(); // just skip first '[' } } /* parse and set properties */ Map propMap = StringTools.parseProperties(p, propSep); if (n == null) { n = this.setProperties(propMap, inclName); } else { this.setProperties(propMap, false); if (inclName) { this.setName(n); } } /* return name, if any */ return n; } else { return null; } } // ------------------------------------------------------------------------ public void removeProperty(Object key) { if (key != null) { Map props = this.getProperties(); if (!(props instanceof Properties) || (key instanceof String)) { Object oldVal = props.get(key); props.remove(key); this.firePropertyChanged(key, oldVal); } } } public void clearProperties() { this.getProperties().clear(); this.firePropertyChanged(null, null); } public void resetProperties(Map props) { this.clearProperties(); this.setProperties(props, true); } // ------------------------------------------------------------------------ public String insertKeyValues(String text) { return this._insertKeyValues(null, text, KEY_START_DELIMITER, KEY_END_DELIMITER); } public String insertKeyValues(String text, String startDelim, String endDelim) { return this._insertKeyValues(null, text, startDelim, endDelim); } public String _insertKeyValues(Object key, String text) { return this._insertKeyValues(key, text, KEY_START_DELIMITER, KEY_END_DELIMITER); } public String _insertKeyValues(final Object mainKey, String text, String startDelim, String endDelim) { if (text != null) { //Print.logInfo("Inserting local keyvalues: " + text); // replacment call-back StringTools.ReplacementMap rm = new StringTools.ReplacementMap() { private Set<Object> thisKeySet = new HashSet<Object>(); private Set<Object> fullKeySet = new HashSet<Object>(); public String get(String k) { if (k == null) { // a bit of a hack here to tell this map to reset the cached keys fullKeySet.addAll(thisKeySet); if (mainKey != null) { fullKeySet.add(mainKey); } thisKeySet.clear(); return null; } else if (fullKeySet.contains(k)) { //Print.logInfo("Key already processed: " + k); return null; } else { //Print.logInfo("Processing key: " + k); thisKeySet.add(k); Object obj = RTProperties.this._getProperty(k, null); return (obj != null)? obj.toString() : null; } } }; // iterate until the string doesn't change String s_old = text; for (int i = 0; i < RTProperties.KEY_MAX_RECURSION; i++) { rm.get(null); // hack to reset the cached keys String s_new = StringTools.insertKeyValues(s_old, startDelim, endDelim, rm, false); //Print.logInfo("New String: " + s_new); if (s_new.equals(s_old)) { return s_new; } s_old = s_new; } return s_old; } else { return text; // return null } } // ------------------------------------------------------------------------ public void setKeyReplacementMode(int mode) { this.keyReplacementMode = mode; } private Object _replaceKeyValues(Object key, Object obj) { if (this.keyReplacementMode == KEY_REPLACEMENT_NONE) { //Print.logInfo("No replacement to be performed: " + obj); return obj; } else if ((obj == null) || !(obj instanceof String)) { //Print.logInfo("Returning non-String object as-is: " + obj); return obj; } else if (this.keyReplacementMode == KEY_REPLACEMENT_LOCAL) { //Print.logInfo("Replacing local keys: " + obj); return this._insertKeyValues(key,(String)obj); } else { //Print.logInfo("Replacing global keys: " + obj); return RTConfig._insertKeyValues(key,(String)obj); } } private Object _getProperty(Object key, Object dft, Class dftClass, boolean replaceKeys) { Object value = this.getProperties().get(key); if (value == null) { if (key != null) { String env = this._getEnvironmentProperty(key.toString()); if (env != null) { return replaceKeys? this._replaceKeyValues(key,env) : env; } } return replaceKeys? this._replaceKeyValues(key,dft) : dft; // no value, return default } else if ((dft == null) && (dftClass == null)) { return replaceKeys? this._replaceKeyValues(key,value) : value; // return as-is } else { // convert 'value' to same type (class) as 'dft' (if specified) Class c = (dftClass != null)? dftClass : dft.getClass(); try { return convertToType(replaceKeys? this._replaceKeyValues(key,value) : value, c); } catch (Throwable t) { return replaceKeys? this._replaceKeyValues(key,dft) : dft; // inconvertable, return as-is } } } public Object _getProperty(Object key, Object dft) { return this._getProperty(key, dft, null, false); } public Object getProperty(Object key, Object dft) { return this._getProperty(key, dft, null, true); } protected static Object convertToType(Object val, Class<?> type) throws Throwable { if ((type == null) || (val == null)) { // not converted return val; } else if (type.isAssignableFrom(val.getClass())) { // already converted return val; } else if (type == String.class) { // convert to String return val.toString(); } else { // IE: // new File(String.class) // new Long(String.class) try { Constructor meth = type.getConstructor(new Class[] { type }); return meth.newInstance(new Object[] { val }); } catch (Throwable t1) { try { Constructor meth = type.getConstructor(new Class[] { String.class }); return meth.newInstance(new Object[] { val.toString() }); } catch (Throwable t2) { Print.logError("Can't convert value to " + type.getName() + ": " + val); throw t2; // inconvertable } } } } // ------------------------------------------------------------------------ public String getString(String key) { return this.getString(key, null); } public String getString(String key[], String dft) { return this.getString(this.getFirstDefinedKey(key), dft); } public String getString(String key, String dft) { Object val = this._getProperty(key, dft, String.class, true); if (val == null) { return null; } else if (val.equals(RTKey.NULL_VALUE)) { return null; } else { return val.toString(); } } public void setString(String property, String value) { this.setProperty(property, value); } // ------------------------------------------------------------------------ public String[] getStringArray(String key) { return this.getStringArray(key, null); } public String[] getStringArray(String key, String dft[]) { String val = this.getString(key, null); if (val == null) { return dft; } else { String va[] = StringTools.parseArray(val); // TODO: check for RTKey.NULL_VALUE in string array return va; } } public void setStringArray(String key, String val[]) { this.setStringArray(key, val, true); } public void setStringArray(String key, String val[], boolean alwaysQuote) { String valStr = StringTools.encodeArray(val, ARRAY_DELIM, alwaysQuote); this.setString(key, valStr); } public void setProperty(String key, String val[]) { this.setStringArray(key, val, true); } // ------------------------------------------------------------------------ public File getFile(String key) { return this.getFile(key, null); } // do not include the following method, otherwise "getFile(file, null)" would be ambiguous //public File getFile(String key, String dft) public File getFile(String key, File dft) { Object val = this._getProperty(key, null, null, true); if (val == null) { return dft; } else if (val instanceof File) { return (File)val; } else { return new File(val.toString()); } } public void setFile(String key, File value) { this.setProperty(key, value); } // ------------------------------------------------------------------------ public double getDouble(String key) { return this.getDouble(key, 0.0); } public double getDouble(String key[], double dft) { return this.getDouble(this.getFirstDefinedKey(key), dft); } public double getDouble(String key, double dft) { Object val = this._getProperty(key, null, null, true); if (val == null) { return dft; } else if (val instanceof Number) { return ((Number)val).doubleValue(); } else { return StringTools.parseDouble(val.toString(), dft); } } public double[] getDoubleArray(String key, double dft[]) { String val[] = this.getStringArray(key, null); if (val == null) { return dft; } else { double n[] = new double[val.length]; for (int i = 0; i < val.length; i++) { n[i] = StringTools.parseDouble(val[i], 0.0); } return n; } } public void setDouble(String key, double value) { this.setProperty(key, value); } public void setDoubleArray(String key, double value[]) { this.setProperty(key, value); } public void setProperty(String key, double value) { this.setProperty(key, new Double(value)); } // ------------------------------------------------------------------------ public float getFloat(String key) { return this.getFloat(key, 0.0F); } public float getFloat(String key[], float dft) { return this.getFloat(this.getFirstDefinedKey(key), dft); } public float getFloat(String key, float dft) { Object val = this._getProperty(key, null, null, true); if (val == null) { return dft; } else if (val instanceof Number) { return ((Number)val).floatValue(); } else { return StringTools.parseFloat(val.toString(), dft); } } public float[] getFloatArray(String key, float dft[]) { String val[] = this.getStringArray(key, null); if (val == null) { return dft; } else { float n[] = new float[val.length]; for (int i = 0; i < val.length; i++) { n[i] = StringTools.parseFloat(val[i], 0.0F); } return n; } } public void setFloat(String key, float value) { this.setProperty(key, value); } public void setFloatArray(String key, float value[]) { this.setProperty(key, value); } public void setProperty(String key, float value) { this.setProperty(key, new Float(value)); } // ------------------------------------------------------------------------ public long getLong(String key) { return this.getLong(key, 0L); } public long getLong(String key[], long dft) { return this.getLong(this.getFirstDefinedKey(key), dft); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -