📄 rtproperties.java
字号:
public PropertyChangeEvent(Object key, Object oldValue, Object newValue) { this.keyObj = key; // may be null this.oldVal = oldValue; // may be null this.newVal = newValue; // may be null } public RTProperties getSource() { return RTProperties.this; } public Object getKey() { return this.keyObj; // may be null } public Object getOldValue() { return this.oldVal; // may be null } public Object getNewValue() { return this.newVal; // may be null } } private java.util.List<PropertyChangeListener> changeListeners = null; /** *** Adds a PropertyChangeListener to this instance *** @param pcl A PropertyChangeListener to add to this instance **/ public void addChangeListener(PropertyChangeListener pcl) { if (this.changeListeners == null) { this.changeListeners = new Vector<PropertyChangeListener>(); } this.changeListeners.add(pcl); } /** *** Removes a PropertyChangeListener from this instance *** @param pcl A PropertyChangeListener to remove from this instance **/ public void removeChangeListener(PropertyChangeListener pcl) { if (this.changeListeners != null) { this.changeListeners.remove(pcl); } } /** *** Fires a PropertyChange event *** @param key The property key which changed *** @param oldVal The old value of the property key which changed **/ protected void firePropertyChanged(Object key, Object oldVal) { if (this.changeListeners != null) { Object newVal = this.getProperties().get(key); RTProperties.PropertyChangeEvent pce = new RTProperties.PropertyChangeEvent(key,oldVal,newVal); for (Iterator i = this.changeListeners.iterator(); i.hasNext();) { ((RTProperties.PropertyChangeListener)i.next()).propertyChange(pce); } } } // ------------------------------------------------------------------------ /** *** Gets the backing properties Map for this instance *** @return The backing properties Map for this instance **/ public Map<Object,Object> getProperties() { if (this.cfgProperties == null) { this.cfgProperties = CreateDefaultMap(); if (this.cfgProperties instanceof OrderedMap) { ((OrderedMap)this.cfgProperties).setIgnoreCase(this.ignoreCase); } } return this.cfgProperties; } /** *** Returns an Iterator over the property keys defined in this RTProperties instance *** @return An Iterator over the property keys defined in this RTProperties instance **/ public Iterator<?> keyIterator() { return this.getPropertyKeys().iterator(); } /** *** Gets a set of property keys defined by this RTProperties instance *** @return A set of property keys defined by this RTProperties instance **/ public Set<?> getPropertyKeys() { return this.getProperties().keySet(); } /** *** Returns a set of property keys defined in this RTProperties instance which start with the specified String *** @return A set of property keys defined in this RTProperties instance which start with the specified String **/ public Set<?> getPropertyKeys(String startsWith) { OrderedSet<String> keys = new OrderedSet<String>(); for (Iterator<?> i = this.keyIterator(); i.hasNext();) { String k = i.next().toString(); if (StringTools.startsWithIgnoreCase(k, startsWith)) { keys.add(k); } } return keys; } /** *** Returns a subset of this RTProperties instance containing key/value pairs which match the *** specified partial key. *** @param keyStartsWith The partial key used to match keys in this instance *** @return The RTProperties subset **/ public RTProperties getSubset(String keyStartsWith) { RTProperties rtp = new RTProperties(); for (Iterator<?> i = this.keyIterator(); i.hasNext();) { Object k = i.next(); if (k instanceof String) { String ks = (String)k; if (StringTools.startsWithIgnoreCase(ks,keyStartsWith)) { String v = this.getString(ks, null); rtp.setProperty(ks, v); } } } return rtp; } /* Extract a Map containing a group of key/values from the runtime config */ /* public Map<String,String> extractMap(String keyEnd, String valEnd) { Map<String,String> m = new OrderedMap<String,String>(); for (Iterator<?> i = this.keyIterator(); i.hasNext();) { String mkKey = i.next().toString(); if (mkKey.endsWith(keyEnd)) { String key = getString(mkKey, null); if (key != null) { // <-- will never be null anyway String mvKey = mkKey.substring(0, mkKey.length() - keyEnd.length()) + valEnd; String val = this.getString(mvKey, ""); m.put(key, val); } } } return m; } */ // ------------------------------------------------------------------------ /** *** Returns true if the specified property key is defined *** @param key A property key *** @return True if the specified property key is defined **/ public boolean hasProperty(Object key) { if (key == null) { return false; } else if (this.getProperties().containsKey(key)) { return true; } else if (this._hasEnvironmentProperty(key.toString())) { return true; } else { return false; } } /** *** Returns the first defined property key in the list *** @param key An array of property keys *** @return the first defined property key in the list **/ public String getFirstDefinedKey(String key[]) { if (key != null) { for (int i = 0; i < key.length; i++) { if (this.hasProperty(key[i])) { return key[i]; } } } return null; } /** *** Returns the specified key, if defined *** @param key The propery key *** @return The property key if defined, or null otherwise **/ public String getFirstDefinedKey(String key) { return this.hasProperty(key)? key : null; } // ------------------------------------------------------------------------ /** *** Sets the value for the specified key *** @param key The property key *** @param value The value to associate with the specified key **/ public void setProperty(Object key, Object value) { if (key != null) { Map<Object,Object> props = this.getProperties(); /* "!<key>" implies removable of <key> from Map (value is ignored) */ String k = (key instanceof String)? (String)key : null; if ((k != null) && !k.equals("") && ("|!^".indexOf(k.charAt(0)) >= 0)) { key = k.substring(1); value = null; } /* encode arrays? */ if ((value != null) && value.getClass().isArray()) { Class arrayClass = value.getClass(); if (arrayClass.getComponentType().isPrimitive()) { value = StringTools.encodeArray(value, ARRAY_DELIM, false); } else { Object a[] = (Object[])value; boolean quote = (a instanceof Number[])? false : true; value = StringTools.encodeArray(a, ARRAY_DELIM, quote); } } else { // } /* add/remove key/value */ if (!(props instanceof Properties) || (key instanceof String)) { Object oldVal = props.get(key); if (value == null) { //Print._println("Removing key: " + key); props.remove(key); } else if ((props instanceof OrderedMap) && key.equals(KEY_NAME)) { //Print._println("Setting name: " + value); ((OrderedMap<Object,Object>)props).put(0, key, value); } else { //Print._println("Setting key: " + key + "=" + value); props.put(key, value); } this.firePropertyChanged(key, oldVal); } else { // Non-String are not supported in the 'Properties' class } } } // ------------------------------------------------------------------------ /** *** Adds the properties in the specified RTProperties instance to this instance *** @param rtp The RTProperties instance from which properties will be copied to this instance *** @return The name of this RTProperties instance **/ public String setProperties(RTProperties rtp) { return this.setProperties(rtp, false); } /** *** Adds the properties in the specified RTProperties instance to this instance *** @param rtp The RTProperties instance from which properties will be copied to this instance *** @param inclName True to set the name of this instace to the instance of the specified RTProperties instance. *** @return The name of this RTProperties instance **/ public String setProperties(RTProperties rtp, boolean inclName) { if (rtp != null) { return this.setProperties(rtp.getProperties(), inclName); } else { return null; } } // ------------------------------------------------------------------------ public String setProperties(URL url) throws IOException { return this.setProperties(url, false); } public String setProperties(URL url, boolean inclName) throws IOException { String name = null; if (url != null) { InputStream uis = url.openStream(); try { name = this.setProperties(uis, inclName); } finally { try { uis.close(); } catch (IOException ioe) {/*ignore*/} } } return name; } // ------------------------------------------------------------------------ public String setProperties(File file) throws IOException { return this.setProperties(file, false); } public String setProperties(File file, boolean inclName) throws IOException { String name = null; if (file != null) { FileInputStream fis = new FileInputStream(file); try { name = this.setProperties(fis, inclName); } finally { try { fis.close(); } catch (IOException ioe) {/*ignore*/} } } return name; } // ------------------------------------------------------------------------ public String setProperties(InputStream in) throws IOException { return this.setProperties(in, false); } public String setProperties(InputStream in, boolean inclName) throws IOException { OrderedProperties props = new OrderedProperties(); if (USE_PROPERTIES_LOADER) { // Warning! '<Properties>.load' uses character set "ISO-8859-1" props.load(in); // "props.put(key,value)" is used for insertion } else { Print.logWarn("Non-standard Properties file loading ..."); RTProperties.loadProperties(props, in); } return this.setProperties(props.getOrderedMap(), inclName); } // ------------------------------------------------------------------------ public String setProperties(Map props) { return this.setProperties(props, false); } public String setProperties(Map props, boolean inclName) { // Note: Does NOT remove old properties (by design) if (props != null) { String n = null; for (Iterator i = props.keySet().iterator(); i.hasNext();) { Object key = i.next(); Object val = props.get(key); if (KEY_NAME.equals(key)) { n = (val != null)? val.toString() : null; if (inclName) { this.setName(n); } } else { this.setProperty(key, val); } } return n; } else { return null; } } // ------------------------------------------------------------------------ public void setPropertySeparatorChar(char propSep) { this.propertySeparator = propSep; } public char getPropertySeparatorChar() { return this.propertySeparator; } // ------------------------------------------------------------------------ public String setProperties(String props) { return this.setProperties(props, false); } public String setProperties(String props, char propSep) { this.setPropertySeparatorChar(propSep); return this.setProperties(props, false); } public String setProperties(String props, boolean inclName) { if (props != null) { char propSep = this.getPropertySeparatorChar(); /* check for prefixing name in string (ie. "[name] key=value") */ String n = null, p = props.trim(); if (p.startsWith("[")) { int x = p.indexOf("]"); if (x > 0) { // found "[name]" n = p.substring(1,x).trim(); p = p.substring(x+1).trim();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -