📄 variablebundle.java
字号:
package net.suberic.util;import java.util.*;import java.io.*;/** * VariableBundle is a combination of a Properties object, a ResourceBundle * object, and (optionally) a second Properties object to act as the 'parent' * properties. This allows both for a single point of reference for * variables, as well as the ability to do hierarchical lookups with the * parent (see getProperty() for an example). * * The order of lookup is as follows: Local properties are checked first, * then parent properties, and then finally (if the value is not found in * any properties) the ResourceBundle is checked. */public class VariableBundle extends Object { private Properties properties; private Properties writableProperties; private Properties temporaryProperties = new Properties(); private ResourceBundle resources; private VariableBundle parentProperties; private File mSaveFile; private Set removeSet = new HashSet(); private Hashtable VCListeners = new Hashtable(); private Hashtable VCGlobListeners = new Hashtable(); public VariableBundle(InputStream propertiesFile, String resourceFile, VariableBundle newParentProperties) { configure(propertiesFile, resourceFile, newParentProperties); } public VariableBundle(File propertiesFile, VariableBundle newParentProperties) throws java.io.FileNotFoundException { FileInputStream fis = new FileInputStream(propertiesFile); configure(fis, null, newParentProperties); try { fis.close(); } catch (java.io.IOException ioe) { } mSaveFile = propertiesFile; } public VariableBundle(InputStream propertiesFile, String resourceFile) { this(propertiesFile, resourceFile, null); } public VariableBundle(InputStream propertiesFile, VariableBundle newParentProperties) { this(propertiesFile, null, newParentProperties); } public VariableBundle(Properties editableProperties, VariableBundle newParentProperties) { writableProperties = editableProperties; parentProperties = newParentProperties; properties = new Properties(); resources = null; } /** * Configures the VariableBundle. */ protected void configure(InputStream propertiesFile, String resourceFile, VariableBundle newParentProperties) { writableProperties = new Properties(); if (resourceFile != null) try { resources = ResourceBundle.getBundle(resourceFile, Locale.getDefault()); } catch (MissingResourceException mre) { System.err.println("Error loading resource " + mre.getClassName() + mre.getKey() + ": trying default locale."); try { resources = ResourceBundle.getBundle(resourceFile, Locale.US); } catch (MissingResourceException mreTwo){ System.err.println("Unable to load default (US) resource bundle; exiting."); System.exit(1); } } else resources=null; properties = new Properties(); if (propertiesFile != null) try { properties.load(propertiesFile); } catch (java.io.IOException ioe) { System.err.println(ioe.getMessage() + ": " + propertiesFile); } List includeStreams = getPropertyAsList("VariableBundle.include", ""); if (includeStreams != null && includeStreams.size() > 0) { for (int i = 0; i < includeStreams.size(); i++) { String current = (String) includeStreams.get(i); try { if (current != null && ! current.equals("")) { java.net.URL url = this.getClass().getResource(current); java.io.InputStream is = url.openStream(); properties.load(is); } } catch (java.io.IOException ioe) { System.err.println("error including file " + current + ": " + ioe.getMessage()); ioe.printStackTrace(); } } } parentProperties = newParentProperties; } public String getProperty(String key, String defaultValue) { String returnValue = ""; if (! propertyIsRemoved(key)) { returnValue = temporaryProperties.getProperty(key, ""); } if (returnValue == "") { if (! propertyIsRemoved(key)) { returnValue = writableProperties.getProperty(key, ""); } if (returnValue == "") { if (! propertyIsRemoved(key)) { returnValue = properties.getProperty(key, ""); } if (returnValue == "") { returnValue=getParentProperty(key, ""); if (returnValue == "") { if (resources != null) try { returnValue = resources.getString(key); } catch (MissingResourceException mre) { returnValue=defaultValue; } else returnValue = defaultValue; } } } } return returnValue; } public String getProperty(String key) throws MissingResourceException { String returnValue; returnValue = getProperty(key, ""); if (returnValue == "") { throw new MissingResourceException(key, "", key); } return returnValue; } private String getParentProperty(String key, String defaultValue) { if (parentProperties==null) { return defaultValue; } else { return parentProperties.getProperty(key, defaultValue); } } public ResourceBundle getResources() { return resources; } public void setResourceBundle (ResourceBundle newResources) { resources = newResources; } public Properties getProperties() { return properties; } /** * Sets the Properties object for this VariableBundle. */ public void setProperties(Properties newProperties) { properties = newProperties; } public VariableBundle getParentProperties() { return parentProperties; } public Properties getWritableProperties() { return writableProperties; } public void setProperty(String propertyName, String propertyValue) { internSetProperty(propertyName, propertyValue, true); } /** * Sets a group of properties at once, not firing any valueChanged events * until all properties are set. */ public void setAllProperties(Properties properties) { for (String propertyName: properties.stringPropertyNames()) { String propertyValue = properties.getProperty(propertyName); internSetProperty(propertyName, propertyValue, false); } for (String propertyName: properties.stringPropertyNames()) { fireValueChanged(propertyName); } } private void internSetProperty(String propertyName, String propertyValue, boolean notify) { temporaryProperties.remove(propertyName); if (propertyValue == null || propertyValue.equalsIgnoreCase("")) { removeProperty(propertyName); writableProperties.remove(propertyName); } else { unRemoveProperty(propertyName); writableProperties.setProperty(propertyName, propertyValue); } if (notify) { fireValueChanged(propertyName); } } /** * sets a property as temporary (so it won't be saved). */ public void setProperty(String propertyName, String propertyValue, boolean temporary) { if (temporary) { temporaryProperties.setProperty(propertyName, propertyValue); fireValueChanged(propertyName); } else { setProperty(propertyName, propertyValue); } } /** * Returns a property which has multiple values separated by a ':' (colon) * as a java.util.Enumeration. */ public Enumeration getPropertyAsEnumeration(String propertyName, String defaultValue) { StringTokenizer tokens = new StringTokenizer(getProperty(propertyName, defaultValue), ":"); return tokens; } /** * Converts a value which has multiple values separated by a ':' (colon) * to a java.util.Vector. */ public static Vector<String> convertToVector(String value) { Vector<String> returnValue = new Vector<String>(); StringTokenizer tokens = new StringTokenizer(value, ":"); while (tokens.hasMoreElements()) returnValue.add((String)tokens.nextElement()); return returnValue; } /** * Converts the given property value to a Vector using the convertToVector * call. */ public Vector<String> getPropertyAsVector(String propertyName, String defaultValue) { return convertToVector(getProperty(propertyName, defaultValue)); } /** * Converts a value which has multiple values separated by a ':' (colon) * to a java.util.List. */ public static List<String> convertToList(String value) { List<String> returnValue = new ArrayList<String>(); StringTokenizer tokens = new StringTokenizer(value, ":"); while (tokens.hasMoreElements()) returnValue.add((String)tokens.nextElement()); return returnValue; } /** * Converts the given property value to a List using the convertToList * call. */ public List<String> getPropertyAsList(String propertyName, String defaultValue) { return convertToList(getProperty(propertyName, defaultValue)); } /** * Converts a List of Strings to a colon-delimited String. */ public static String convertToString(List pValue) { if (pValue == null || pValue.size() == 0) return ""; else { StringBuffer returnBuffer = new StringBuffer(); Iterator it = pValue.iterator(); while (it.hasNext()) { returnBuffer.append((String) it.next()); if (it.hasNext()) { returnBuffer.append(":"); } } return returnBuffer.toString(); } } /** * Returns all property keys in this VariableBundle. */ public Set<String> getPropertyNames() { HashSet<String> returnValue = new HashSet<String>(); returnValue.addAll(temporaryProperties.stringPropertyNames()); returnValue.addAll(writableProperties.stringPropertyNames()); returnValue.addAll(properties.stringPropertyNames()); if (parentProperties != null) returnValue.addAll(parentProperties.getPropertyNames()); if (resources != null) returnValue.addAll(resources.keySet()); return returnValue; } /** * Returns all property keys in this VariableBundle that start with * the given string. */ public Set<String> getPropertyNamesStartingWith(String startsWith) { Set<String> returnValue = new HashSet<String>(); Set<String> allProps = getPropertyNames(); for (String prop: allProps) { if (prop.startsWith(startsWith)) returnValue.add(prop); } return returnValue; } /** * Saves the current properties in the VariableBundle to a file. Note * that this only saves the writableProperties of this particular * VariableBundle--underlying defaults are not written. */ public void saveProperties() { if (mSaveFile != null) { saveProperties(mSaveFile); } } /** * Saves the current properties in the VariableBundle to a file. Note * that this only saves the writableProperties of this particular * VariableBundle--underlying defaults are not written. */ public void saveProperties(File pSaveFile) { if (pSaveFile == null) return; synchronized(this) { if (writableProperties.size() > 0) { File outputFile; String currentLine, key; int equalsLoc; try { if (! pSaveFile.exists()) pSaveFile.createNewFile(); outputFile = pSaveFile.createTempFile(pSaveFile.getName(), ".tmp", pSaveFile.getParentFile()); BufferedReader readSaveFile = new BufferedReader(new FileReader(pSaveFile)); BufferedWriter writeSaveFile = new BufferedWriter(new FileWriter(outputFile)); currentLine = readSaveFile.readLine(); while (currentLine != null) { equalsLoc = currentLine.indexOf('='); if (equalsLoc != -1) { String rawKey = currentLine.substring(0, equalsLoc); key = unEscapeString(rawKey); if (!propertyIsRemoved(key)) { if (writableProperties.getProperty(key, "").equals("")) { writeSaveFile.write(currentLine); writeSaveFile.newLine();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -