1 20 21 package fr.dyade.aaa.util; 22 23 import java.io.*; 24 import java.util.Hashtable ; 25 26 public class SimplePropertiesReader { 27 private Hashtable propertiesTable; 28 private File file; 29 30 public SimplePropertiesReader(File file) { 31 this.file = file; 32 propertiesTable = new Hashtable (); 33 putProperties(); 34 } 35 36 public SimplePropertiesReader(String file) { 37 this.file = new File(file); 38 propertiesTable = new Hashtable (); 39 putProperties(); 40 } 41 42 private String buildString(String line, int index) { 43 StringBuffer buf = new StringBuffer (); 44 StringBuffer fbuf = new StringBuffer (); 45 for (int i=index; i<line.length(); i++) { 46 char c = line.charAt(i); 47 if (c=='\\') buf.append('\\'); 48 buf.append(c); 49 } 50 for (int i=0; i<buf.length(); i++) { 51 char c = buf.charAt(i); 52 if (c==' ' || c=='\t') continue; 53 for (int j=i;j<buf.length();j++) 54 fbuf.append(buf.charAt(j)); 55 break; 56 } 57 return fbuf.toString(); 58 } 59 60 private void putProperties() { 61 if (file.exists()) { 62 try { 63 FileInputStream fileInputStream = new FileInputStream(file); 64 BufferedReader in = new BufferedReader(new InputStreamReader(fileInputStream)); 65 while (true) { 66 String line = in.readLine(); 68 if (line == null) { 69 fileInputStream.close(); 70 return; 71 } 72 73 if (line.length() > 0) { 74 char firstChar = line.charAt(0); 75 if (firstChar == '#') continue; 76 int i = line.indexOf(' '); 77 if (i<0) { 78 i = line.indexOf('\t'); 79 if (i<0) continue; 80 } 81 String propName = line.substring(0,i); 82 String propValue = buildString(line,i); 83 try { 84 propertiesTable.put(propName,propValue); 85 } catch (NullPointerException e) {} 86 } 87 } 88 } catch (IOException e) { 89 e.printStackTrace(); 90 } 91 } 92 } 93 94 public String getProperty(String prop) { 95 return (String ) propertiesTable.get(prop); 96 } 97 98 public String toString() { 99 return propertiesTable.toString(); 100 } 101 } 102 | Popular Tags |