
http://stackoverflow.com/questions/11838674/how-to-read-property-name-with-spaces-in-java
/** Converts unicodes to encoded \uxxxx and writes out any of the
* characters in specialSaveChars with a preceding slash
*/
private String saveConvert(String theString, boolean escapeSpace) {
int len = theString.length()
StringBuffer outBuffer = new StringBuffer(len * 2)
for (int x = 0x <lenx++) {
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()
}
/**
* Convert a nibble to a hex character
*
* @param nibble
*the 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' }
public synchronized Object put(Object key, Object value) {
context.putOrUpdate(key.toString(), value.toString())
return super.put(key, value)
}
public synchronized Object put(Object key, Object value, String line) {
context.putOrUpdate(key.toString(), value.toString(), line)
return super.put(key, value)
}
public synchronized Object remove(Object key) {
context.remove(key.toString())
return super.remove(key)
}
class PropertiesContext {
private List commentOrEntrys = new ArrayList()
public List getCommentOrEntrys() {
return commentOrEntrys
}
public void addCommentLine(String line) {
commentOrEntrys.add(line)
}
public void putOrUpdate(PropertyEntry pe) {
remove(pe.getKey())
commentOrEntrys.add(pe)
}
public void putOrUpdate(String key, String value, String line) {
PropertyEntry pe = new PropertyEntry(key, value, line)
remove(key)
commentOrEntrys.add(pe)
}
public void putOrUpdate(String key, String value) {
PropertyEntry pe = new PropertyEntry(key, value)
int index = remove(key)
commentOrEntrys.add(index,pe)
}
public int remove(String key) {
for (int index = 0index <commentOrEntrys.size()index++) {
Object obj = commentOrEntrys.get(index)
if (obj instanceof PropertyEntry) {
if (obj != null) {
if (key.equals(((PropertyEntry) obj).getKey())) {
commentOrEntrys.remove(obj)
return index
}
}
}
}
return commentOrEntrys.size()
}
class PropertyEntry {
private String key
private String value
private String line
public String getLine() {
return line
}
public void setLine(String line) {
this.line = line
}
public PropertyEntry(String key, String value) {
this.key = key
this.value = value
}
/**
* @param key
* @param value
* @param line
*/
public PropertyEntry(String key, String value, String line) {
this(key, value)
this.line = line
}
public String getKey() {
return key
}
public void setKey(String key) {
this.key = key
}
public String getValue() {
return value
}
public void setValue(String value) {
this.value = value
}
public String toString() {
if (line != null) {
return line
}
if (key != null &&value != null) {
String k = saveConvert(key, true)
String v = saveConvert(value, false)
return k + "=" + v
}
return null
}
}
}
/**
* @param comment
*/
public void addComment(String comment) {
if (comment != null) {
context.addCommentLine("#" + comment)
}
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)