KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > util > SimplePropertiesReader


1 /*
2  * Copyright (C) 1996 - 2000 BULL
3  * Copyright (C) 1996 - 2000 INRIA
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 package fr.dyade.aaa.util;
22
23 import java.io.*;
24 import java.util.Hashtable JavaDoc;
25
26 public class SimplePropertiesReader {
27   private Hashtable JavaDoc propertiesTable;
28   private File file;
29
30   public SimplePropertiesReader(File file) {
31     this.file = file;
32     propertiesTable = new Hashtable JavaDoc();
33     putProperties();
34   }
35
36   public SimplePropertiesReader(String JavaDoc file) {
37     this.file = new File(file);
38     propertiesTable = new Hashtable JavaDoc();
39     putProperties();
40   }
41
42   private String JavaDoc buildString(String JavaDoc line, int index) {
43     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
44     StringBuffer JavaDoc fbuf = new StringBuffer JavaDoc();
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           // Get next line
67
String JavaDoc 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 JavaDoc propName = line.substring(0,i);
82             String JavaDoc propValue = buildString(line,i);
83             try {
84               propertiesTable.put(propName,propValue);
85             } catch (NullPointerException JavaDoc e) {}
86           }
87         }
88       } catch (IOException e) {
89         e.printStackTrace();
90       }
91     }
92   }
93
94   public String JavaDoc getProperty(String JavaDoc prop) {
95     return (String JavaDoc) propertiesTable.get(prop);
96   }
97
98   public String JavaDoc toString() {
99     return propertiesTable.toString();
100   }
101 }
102
Popular Tags