两个类

1package com.censoft.util; 2 3import java.util.Properties; 4 5import java.io.*; 6import java.util.ArrayList; 7import java.util.Iterator; 8import java.util.List; 9 10public class SafeProperties extends Properties { 11 private static final long serialVersionUID = 5011694856722313621L; 12 13 private static final String keyValueSeparators = "=: \t\r\n\f"; 14 15 private static final String strictKeyValueSeparators = "=:"; 16 17 private static final String specialSaveChars = "=: \t\r\n\f#!"; 18 19 private static final String whiteSpaceChars = " \t\r\n\f"; 20 21 private PropertiesContext context = new PropertiesContext(); 22 23 public PropertiesContext getContext() { 24 return context; 25 } 26 27 public synchronized void load(InputStream inStream) throws IOException { 28 29 BufferedReader in; 30 31 in = new BufferedReader(new InputStreamReader(inStream, "8859_1")); 32 while (true) { 33 String line = in.readLine(); 34 String intactLine = line; 35 if (line == null) 36 return; 37 38 if (line.length() > 0) { 39 40 int len = line.length(); 41 int keyStart; 42 for (keyStart = 0; keyStart < len; keyStart++) 43 if (whiteSpaceChars.indexOf(line.charAt(keyStart)) == -1) 44 break; 45 46 if (keyStart == len) 47 continue; 48 49 char firstChar = line.charAt(keyStart); 50 51 if ((firstChar != '#') && (firstChar != '!')) { 52 while (continueLine(line)) { 53 String nextLine = in.readLine(); 54 intactLine = intactLine + "\n" + nextLine; 55 if (nextLine == null) 56 nextLine = ""; 57 String loppedLine = line.substring(0, len - 1); 58 int startIndex; 59 for (startIndex = 0; startIndex < nextLine.length(); startIndex++) 60 if (whiteSpaceChars.indexOf(nextLine.charAt(startIndex)) == -1) 61 break; 62 nextLine = nextLine.substring(startIndex, nextLine.length()); 63 line = new String(loppedLine + nextLine); 64 len = line.length(); 65 } 66 67 int separatorIndex; 68 for (separatorIndex = keyStart; separatorIndex < len; separatorIndex++) { 69 char currentChar = line.charAt(separatorIndex); 70 if (currentChar == '\\') 71 separatorIndex++; 72 else if (keyValueSeparators.indexOf(currentChar) != -1) 73 break; 74 } 75 76 int valueIndex; 77 for (valueIndex = separatorIndex; valueIndex < len; valueIndex++) 78 if (whiteSpaceChars.indexOf(line.charAt(valueIndex)) == -1) 79 break; 80 81 if (valueIndex < len) 82 if (strictKeyValueSeparators.indexOf(line.charAt(valueIndex)) != -1) 83 valueIndex++; 84 85 while (valueIndex < len) { 86 if (whiteSpaceChars.indexOf(line.charAt(valueIndex)) == -1) 87 break; 88 valueIndex++; 89 } 90 String key = line.substring(keyStart, separatorIndex); 91 String value = (separatorIndex < len) ? line.substring(valueIndex, len) : ""; 92 93 key = loadConvert(key); 94 value = loadConvert(value); 95 put(key, value, intactLine); 96 } else { 97 context.addCommentLine(intactLine); 98 } 99 } else { 100 context.addCommentLine(intactLine); 101 } 102 } 103 } 104 105 106 private String loadConvert(String theString) { 107 char aChar; 108 int len = theString.length(); 109 StringBuffer outBuffer = new StringBuffer(len); 110 111 for (int x = 0; x < len;) { 112 aChar = theString.charAt(x++); 113 if (aChar == '\\') { 114 aChar = theString.charAt(x++); 115 if (aChar == 'u') { 116 // Read the xxxx 117 int value = 0; 118 for (int i = 0; i < 4; i++) { 119 aChar = theString.charAt(x++); 120 switch (aChar) { 121 case '0': 122 case '1': 123 case '2': 124 case '3': 125 case '4': 126 case '5': 127 case '6': 128 case '7': 129 case '8': 130 case '9': 131 value = (value << 4) + aChar - '0'; 132 break; 133 case 'a': 134 case 'b': 135 case 'c': 136 case 'd': 137 case 'e': 138 case 'f': 139 value = (value << 4) + 10 + aChar - 'a'; 140 break; 141 case 'A': 142 case 'B': 143 case 'C': 144 case 'D': 145 case 'E': 146 case 'F': 147 value = (value << 4) + 10 + aChar - 'A'; 148 break; 149 default: 150 throw new IllegalArgumentException("Malformed \\uxxxx encoding."); 151 } 152 } 153 outBuffer.append((char) value); 154 } else { 155 if (aChar == 't') 156 outBuffer.append('\t'); /* ibm@7211 */ 157 158 else if (aChar == 'r') 159 outBuffer.append('\r'); /* ibm@7211 */ 160 else if (aChar == 'n') { 161 /* 162 * ibm@8897 do not convert a \n to a line.separator 163 * because on some platforms line.separator is a String 164 * of "\r\n". When a Properties class is saved as a file 165 * (store()) and then restored (load()) the restored 166 * input MUST be the same as the output (so that 167 * Properties.equals() works). 168 * 169 */ 170 outBuffer.append('\n'); /* ibm@8897 ibm@7211 */ 171 } else if (aChar == 'f') 172 outBuffer.append('\f'); /* ibm@7211 */ 173 else 174 /* ibm@7211 */ 175 outBuffer.append(aChar); /* ibm@7211 */ 176 } 177 } else 178 outBuffer.append(aChar); 179 } 180 return outBuffer.toString(); 181 } 182 183 public synchronized void store(OutputStream out, String header) throws IOException { 184 BufferedWriter awriter; 185 awriter = new BufferedWriter(new OutputStreamWriter(out, "8859_1")); 186 if (header != null) 187 writeln(awriter, "#" + header); 188 List<Object> entrys = context.getCommentOrEntrys(); 189 for (Iterator<Object> iter = entrys.iterator(); 190 iter.hasNext();) { 191 Object obj = iter.next(); 192 if (obj.toString() != null) { 193 writeln(awriter, obj.toString()); 194 } 195 } 196 awriter.flush(); 197 } 198 199 private static void writeln(BufferedWriter bw, String s) throws IOException { 200 bw.write(s); 201 bw.newLine(); 202 } 203 204 private boolean continueLine(String line) { 205 int slashCount = 0; 206 int index = line.length() - 1; 207 while ((index >= 0) && (line.charAt(index--) == '\\')) 208 slashCount++; 209 return (slashCount % 2 == 1); 210 } 211 212 private String saveConvert(String theString, boolean escapeSpace) { 213 int len = theString.length(); 214 StringBuffer outBuffer = new StringBuffer(len * 2); 215 216 for (int x = 0; x < len; x++) { 217 char aChar = theString.charAt(x); 218 switch (aChar) { 219 case ' ': 220 if (x == 0 || escapeSpace) 221 outBuffer.append('\\'); 222 223 outBuffer.append(' '); 224 break; 225 case '\\': 226 outBuffer.append('\\'); 227 outBuffer.append('\\'); 228 break; 229 case '\t': 230 outBuffer.append('\\'); 231 outBuffer.append('t'); 232 break; 233 case '\n': 234 outBuffer.append('\\'); 235 outBuffer.append('n'); 236 break; 237 case '\r': 238 outBuffer.append('\\'); 239 outBuffer.append('r'); 240 break; 241 case '\f': 242 outBuffer.append('\\'); 243 outBuffer.append('f'); 244 break; 245 default: 246 if ((aChar < 0x0020) || (aChar > 0x007e)) { 247 outBuffer.append('\\'); 248 outBuffer.append('u'); 249 outBuffer.append(toHex((aChar >> 12) & 0xF)); 250 outBuffer.append(toHex((aChar >> 8) & 0xF)); 251 outBuffer.append(toHex((aChar >> 4) & 0xF)); 252 outBuffer.append(toHex(aChar & 0xF)); 253 } else { 254 if (specialSaveChars.indexOf(aChar) != -1) 255 outBuffer.append('\\'); 256 outBuffer.append(aChar); 257 } 258 } 259 } 260 return outBuffer.toString(); 261 } 262 263 /** 264 * Convert a nibble to a hex character 265 * @param nibble the nibble to convert. 266 */ 267 private static char toHex(int nibble) { 268 return hexDigit[(nibble & 0xF)]; 269 } 270 271 /** A table of hex digits */ 272 private static final char[] hexDigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 273 'F' }; 274 275 public synchronized Object put(Object key, Object value) { 276 context.putOrUpdate(key.toString(), value.toString()); 277 return super.put(key, value); 278 } 279 280 public synchronized Object put(Object key, Object value, String line) { 281 context.putOrUpdate(key.toString(), value.toString(), line); 282 return super.put(key, value); 283 } 284 285 286 public synchronized Object remove(Object key) { 287 context.remove(key.toString()); 288 return super.remove(key); 289 } 290 291 class PropertiesContext { 292 private List<Object> commentOrEntrys = new ArrayList<Object>(); 293 294 public List<Object> getCommentOrEntrys() { 295 return commentOrEntrys; 296 } 297 298 public void addCommentLine(String line) { 299 commentOrEntrys.add(line); 300 } 301 302 public void putOrUpdate(PropertyEntry pe) { 303 remove(pe.getKey()); 304 commentOrEntrys.add(pe); 305 } 306 307 public void putOrUpdate(String key, String value, String line) { 308 PropertyEntry pe = new PropertyEntry(key, value, line); 309 remove(key); 310 commentOrEntrys.add(pe); 311 } 312 313 public void putOrUpdate(String key, String value) { 314 PropertyEntry pe = new PropertyEntry(key, value); 315 int index = remove(key); 316 commentOrEntrys.add(index,pe); 317 } 318 319 public int remove(String key) { 320 for (int index = 0; index < commentOrEntrys.size(); index++) { 321 Object obj = commentOrEntrys.get(index); 322 if (obj instanceof PropertyEntry) { 323 if (obj != null) { 324 if (key.equals(((PropertyEntry) obj).getKey())) { 325 commentOrEntrys.remove(obj); 326 return index; 327 } 328 } 329 } 330 } 331 return commentOrEntrys.size(); 332 } 333 334 class PropertyEntry { 335 private String key; 336 337 private String value; 338 339 private String line; 340 341 public String getLine() { 342 return line; 343 } 344 345 public void setLine(String line) { 346 this.line = line; 347 } 348 349 public PropertyEntry(String key, String value) { 350 this.key = key; 351 this.value = value; 352 } 353 354 /** 355 * @param key 356 * @param value 357 * @param line 358 */ 359 public PropertyEntry(String key, String value, String line) { 360 this(key, value); 361 this.line = line; 362 } 363 364 public String getKey() { 365 return key; 366 } 367 368 public void setKey(String key) { 369 this.key = key; 370 } 371 372 public String getValue() { 373 return value; 374 } 375 376 public void setValue(String value) { 377 this.value = value; 378 } 379 380 public String toString() { 381 if (line != null) { 382 return line; 383 } 384 if (key != null && value != null) { 385 String k = saveConvert(key, true); 386 String v = saveConvert(value, false); 387 return k + "=" + v; 388 } 389 return null; 390 } 391 } 392 } 393 394 395 public void addComment(String comment) { 396 if (comment != null) { 397 context.addCommentLine("#" + comment); 398 } 399 } 400 401}
View Code

1package com.censoft.util; 2 3import java.io.File; 4import java.io.FileInputStream; 5import java.io.FileOutputStream; 6import java.io.InputStream; 7import java.io.InputStreamReader; 8import java.io.OutputStream; 9import java.io.Reader; 10 11public class PropertyUtils { 12 13 public static final String FILENAME ="solr.properties"; 14 15 /** 16 * 根据KEY,读取文件对应的值 * 17 * filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties * 18 */ 19 public static String getProp(String filename, String key) { 20 try { 21 SafeProperties safeProperty = new SafeProperties(); 22 String filepath = PropertyUtils.class.getClassLoader().getResource("/").getPath() + filename; 23 24 File file = new File(filepath); 25 InputStream in = new FileInputStream(file); 26 safeProperty.load(in); 27 in.close(); 28 String value = safeProperty.getProperty(key); 29 return value; 30 } catch (Exception e){ 31 e.printStackTrace(); 32 return null; 33 } 34 } 35 36 /** 37 * @param filename 文件名称 38 * @param key 键 39 * @param value 对应键需要修改的值 40 */ 41 public static void setProp(String filename, String key, String value) { 42 try { 43 SafeProperties safeProperty = new SafeProperties(); 44 String filepath = PropertyUtils.class.getClassLoader().getResource("/").getPath() + filename; 45 File file = new File(filepath); 46 InputStream in = new FileInputStream(file); 47 safeProperty.load(in); // 一定要在修改值之前关闭fis 48 in.close(); 49 OutputStream fos = new FileOutputStream(file); 50 safeProperty.setProperty(key, value); // 保存,并加入注释 51 safeProperty.store(fos, null); 52 fos.flush(); 53 fos.close(); 54 } catch (Exception e) { 55 e.printStackTrace(); 56 } 57 } 58 59 /** 60 * 根据KEY,读取文件对应的值 * 61 * filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties * 62 */ 63 public static String getProp(String key) { 64 try { 65 SafeProperties safeProperty = new SafeProperties(); 66 String filepath = PropertyUtils.class.getClassLoader().getResource("/").getPath() + FILENAME; 67 68 filepath = filepath.replace("classes/", ""); 69 Reader inStream = new InputStreamReader(new FileInputStream(filepath), "UTF-8"); 70 safeProperty.load(inStream); 71 72 /* File file = new File(filepath); 73 InputStream in = new FileInputStream(file); 74 safeProperty.load(in);*/ 75 inStream.close(); 76 String value = safeProperty.getProperty(key); 77 return value; 78 } catch (Exception e){ 79 e.printStackTrace(); 80 return null; 81 } 82 } 83 84 /** 85 * @param filename 文件名称 86 * @param key 键 87 * @param value 对应键需要修改的值 88 */ 89 public static void setProp( String key, String value) { 90 try { 91 SafeProperties safeProperty = new SafeProperties(); 92 String filepath = PropertyUtils.class.getClassLoader().getResource("/").getPath() + FILENAME; 93 filepath = filepath.replace("classes/", ""); 94 File file = new File(filepath); 95 InputStream in = new FileInputStream(file); 96 safeProperty.load(in); // 一定要在修改值之前关闭fis 97 in.close(); 98 OutputStream fos = new FileOutputStream(file); 99 safeProperty.setProperty(key, value); // 保存,并加入注释 100 safeProperty.store(fos, null); 101 fos.flush(); 102 fos.close(); 103 } catch (Exception e) { 104 e.printStackTrace(); 105 } 106 } 107}
View Code