📄 variablebundle.java
字号:
} else { writeSaveFile.write(rawKey + "=" + escapeWhiteSpace(writableProperties.getProperty(key, ""))); writeSaveFile.newLine(); properties.setProperty(key, writableProperties.getProperty(key, "")); writableProperties.remove(key); } } else { properties.remove(key); } } else { writeSaveFile.write(currentLine); writeSaveFile.newLine(); } currentLine = readSaveFile.readLine(); } // write out the rest of the writableProperties Set<String> propsLeft = writableProperties.stringPropertyNames(); List<String> propsLeftList = new ArrayList<String>(propsLeft); Collections.sort(propsLeftList); for (String nextKey: propsLeftList) { String nextKeyEscaped = escapeWhiteSpace(nextKey); String nextValueEscaped = escapeWhiteSpace(writableProperties.getProperty(nextKey, "")); writeSaveFile.write(nextKeyEscaped + "=" + nextValueEscaped); writeSaveFile.newLine(); properties.setProperty(nextKey, writableProperties.getProperty(nextKey, "")); writableProperties.remove(nextKey); } clearRemoveList(); readSaveFile.close(); writeSaveFile.flush(); writeSaveFile.close(); // if you don't delete the .old file first, then the // rename fails under Windows. String oldSaveName = pSaveFile.getAbsolutePath() + ".old"; File oldSave = new File (oldSaveName); if (oldSave.exists()) oldSave.delete(); String fileName = new String(pSaveFile.getAbsolutePath()); pSaveFile.renameTo(oldSave); outputFile.renameTo(new File(fileName)); } catch (Exception e) { System.out.println(getProperty("VariableBundle.saveError", "Error saving properties file: " + pSaveFile.getName() + ": " + e.getMessage())); e.printStackTrace(System.err); } } } } /* * Converts encoded \uxxxx to unicode chars * and changes special saved chars to their original forms * * ripped directly from java.util.Properties; hope they don't mind. */ private String loadConvert (String theString) { char aChar; int len = theString.length(); StringBuffer outBuffer = new StringBuffer(len); for(int x=0; x<len; ) { aChar = theString.charAt(x++); if (aChar == '\\') { aChar = theString.charAt(x++); if(aChar == 'u') { // Read the xxxx int value=0; for (int i=0; i<4; i++) { aChar = theString.charAt(x++); switch (aChar) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': value = (value << 4) + aChar - '0'; break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': value = (value << 4) + 10 + aChar - 'a'; break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': value = (value << 4) + 10 + aChar - 'A'; break; default: throw new IllegalArgumentException( "Malformed \\uxxxx encoding."); } } outBuffer.append((char)value); } else { if (aChar == 't') aChar = '\t'; else if (aChar == 'r') aChar = '\r'; else if (aChar == 'n') aChar = '\n'; else if (aChar == 'f') aChar = '\f'; outBuffer.append(aChar); } } else outBuffer.append(aChar); } return outBuffer.toString(); } /* * Converts unicodes to encoded \uxxxx * and writes out any of the characters in specialSaveChars * with a preceding slash * * ripped directly from java.util.Properties; hope they don't mind. */ private String saveConvert(String theString, boolean escapeSpace) { int len = theString.length(); StringBuffer outBuffer = new StringBuffer(len*2); for(int x=0; x<len; x++) { char aChar = theString.charAt(x); switch(aChar) { case ' ': if (x == 0 || escapeSpace) outBuffer.append('\\'); outBuffer.append(' '); break; case '\\':outBuffer.append('\\'); outBuffer.append('\\'); break; case '\t':outBuffer.append('\\'); outBuffer.append('t'); break; case '\n':outBuffer.append('\\'); outBuffer.append('n'); break; case '\r':outBuffer.append('\\'); outBuffer.append('r'); break; case '\f':outBuffer.append('\\'); outBuffer.append('f'); break; default: if ((aChar < 0x0020) || (aChar > 0x007e)) { outBuffer.append('\\'); outBuffer.append('u'); outBuffer.append(toHex((aChar >> 12) & 0xF)); outBuffer.append(toHex((aChar >> 8) & 0xF)); outBuffer.append(toHex((aChar >> 4) & 0xF)); outBuffer.append(toHex( aChar & 0xF)); } else { if (specialSaveChars.indexOf(aChar) != -1) outBuffer.append('\\'); outBuffer.append(aChar); } } } return outBuffer.toString(); } /** * Escapes whitespace in a string by putting a '\' in front of each * whitespace character. */ public String escapeWhiteSpace(String sourceString) { /* char[] origString = sourceString.toCharArray(); StringBuffer returnString = new StringBuffer(); for (int i = 0; i < origString.length; i++) { char currentChar = origString[i]; if (Character.isWhitespace(currentChar) || '\\' == currentChar) returnString.append('\\'); returnString.append(currentChar); } return returnString.toString(); */ return saveConvert(sourceString, true); } /** * resolves a whitespace-escaped string. */ public String unEscapeString(String sourceString) { return loadConvert(sourceString); } /** * Clears the removeList. This should generally be called after * you do a writeProperties(); */ public void clearRemoveList() { removeSet.clear(); } /** * This removes the property from the current VariableBundle. */ private void removeProperty(String remProp) { if (remProp != null) { removeSet.add(remProp.intern()); } } /** * Removes a property from the removeList. Only necessary if a property * had been removed since the last save, and now has been set to a new * value. It's probably a good idea, though, to call this method any * time a property has its value set. */ private void unRemoveProperty(String unRemProp) { if (unRemProp != null) { removeSet.remove(unRemProp.intern()); } } /** * Returns true if the property has been removed. */ public boolean propertyIsRemoved(String prop) { if (prop != null) { return removeSet.contains(prop.intern()); } return false; } /** * This notifies all registered listeners for changedValue that its * value has changed. */ public void fireValueChanged(String changedValue) { // only notify each listener once. Set notified = new HashSet(); Vector listeners = (Vector)VCListeners.get(changedValue); if (listeners != null) { Iterator iter = listeners.iterator(); while (iter.hasNext()) { ValueChangeListener vcl = (ValueChangeListener) iter.next(); vcl.valueChanged(changedValue); notified.add(vcl); } } // now add the glob listeners. Enumeration keys = VCGlobListeners.keys(); while (keys.hasMoreElements()) { String currentPattern = (String) keys.nextElement(); if (changedValue.startsWith(currentPattern)) { Vector globListeners = (Vector) VCGlobListeners.get(currentPattern); if (globListeners != null && globListeners.size() > 0) { for (int i = 0; i < globListeners.size(); i++) { ValueChangeListener currentListener = ((ValueChangeListener)globListeners.elementAt(i)); if (!notified.contains(currentListener)) { currentListener.valueChanged(changedValue); notified.add(currentListener); } } } } } } /** * This adds the ValueChangeListener to listen for changes in the * given property. */ public void addValueChangeListener(ValueChangeListener vcl, String property) { if (property.endsWith("*")) { String startProperty = property.substring(0, property.length() - 1); Vector listeners = (Vector)VCGlobListeners.get(startProperty); if (listeners == null) { listeners = new Vector(); listeners.add(vcl); VCGlobListeners.put(startProperty, listeners); } else { if (!listeners.contains(vcl)) listeners.add(vcl); } } else { Vector listeners = (Vector)VCListeners.get(property); if (listeners == null) { listeners = new Vector(); listeners.add(vcl); VCListeners.put(property, listeners); } else { if (!listeners.contains(vcl)) listeners.add(vcl); } } } /** * This removes the given ValueChangeListener for all the values that * it's listening to. */ public void removeValueChangeListener(ValueChangeListener vcl) { Enumeration keys = VCListeners.keys(); Vector currentListenerList; while (keys.hasMoreElements()) { currentListenerList = (Vector)VCListeners.get(keys.nextElement()); while (currentListenerList != null && currentListenerList.contains(vcl)) currentListenerList.remove(vcl); } keys = VCGlobListeners.keys(); while (keys.hasMoreElements()) { currentListenerList = (Vector)VCGlobListeners.get(keys.nextElement()); while (currentListenerList != null && currentListenerList.contains(vcl)) currentListenerList.remove(vcl); } } /** * This removes the given ValueChangeListener from listening on the * given property. */ public void removeValueChangeListener(ValueChangeListener vcl, String property) { Vector currentListenerList; currentListenerList = (Vector)VCListeners.get(property); while (currentListenerList != null && currentListenerList.contains(vcl)) currentListenerList.remove(vcl); currentListenerList = (Vector)VCGlobListeners.get(property); while (currentListenerList != null && currentListenerList.contains(vcl)) currentListenerList.remove(vcl); } /** * Returns all of the ValueChangeListeners registered. */ public Map getAllListeners() { HashMap returnValue = new HashMap(VCListeners); returnValue.putAll(VCGlobListeners); return returnValue; } /** * Returns a formatted message using the given key and the appropriate * objects. If no message corresponding to the given key exists, uses * the key string as the pattern instead. */ public String formatMessage(String key, Object... arguments) { String pattern = getProperty(key, key); return java.text.MessageFormat.format(pattern, arguments); } /** * Convert a nibble to a hex character * @paramnibblethe nibble to convert. */ private static char toHex(int nibble) { return hexDigit[(nibble & 0xF)]; } /** A table of hex digits */ private static final char[] hexDigit = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; //private static final String keyValueSeparators = "=: \t\r\n\f"; //private static final String strictKeyValueSeparators = "=:"; private static final String specialSaveChars = "=: \t\r\n\f#!"; private static final String whiteSpaceChars = " \t\r\n\f"; /** * Returns the current saveFile. */ public File getSaveFile() { return mSaveFile; } /** * Sets the save file. */ public void setSaveFile(File newFile) { mSaveFile = newFile; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -