KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > lib > TKIniFile


1 /*
2  * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/lib/TKIniFile.java,v 1.8 2002/01/25 11:43:25 alex Exp $
3  *
4  */

5 /**
6  * Fuer JDK "UNTER" 1.1
7  */

8 package com.teamkonzept.lib;
9
10 import java.io.*;
11 import java.util.*;
12 import java.net.*;
13 import org.apache.log4j.Category;
14
15 public class TKIniFile implements ConfigurationErrorCodes
16 {
17     private static Category cat = Category.getInstance(TKIniFile.class);
18
19     String JavaDoc theIniFile;
20     String JavaDoc emptyPat = " ";
21     String JavaDoc tabPat = " ";
22
23     /**
24      * Konstruktor
25      * Der Pfad und Filename wird uebergeben und global zur Verfuegung gestellt
26      */

27     public TKIniFile(String JavaDoc theIniFile) {
28         this.theIniFile = theIniFile;
29     }
30
31     /**
32      * returns an InputStream to the PropertyFile
33      * change this method to use ResourceBundle.loadBundle()
34      */

35     InputStream openInputStream() throws TKException
36     {
37       try
38       {
39         URL url = getClass().getResource(theIniFile);
40         if (url == null)
41         {
42             cat.error("TKUploadField.ini is missing !");
43             throw new TKConfigurationException("TKUploadField.ini fehlt",NO_PROPERTY_FILE,HIGH_SEVERITY,false, null);
44         }
45         return url.openStream();
46       }
47       catch (IOException e)
48       {
49           cat.error("Can't locate ResourceFile " + theIniFile, e);
50       }
51       return null;
52     }
53
54     /**
55      * VORSICHT URALT:
56      * inifile OHNE Section!!!!!!!!!!!!!!!!
57      *
58      * Das Inifile:
59      * ============
60      * key1=val1
61      * key2=val2
62      *
63      * @param String key, der zugehoerige Wert wird zurueckgegeben
64      * @return den Wert
65      */

66     public String JavaDoc getValue(String JavaDoc key) throws TKException
67     {
68
69         try {
70             // FileInputStream inIniFile = new FileInputStream(theIniFile);
71
Properties props = new Properties(); // empty list
72
props.load(openInputStream());
73             String JavaDoc value = props.getProperty(key);
74             return value;
75         }
76         catch (IOException e) {
77             cat.error("Can't open Inifile: ", e);
78             return "";
79         }
80     }
81
82     /**
83      * VORSICHT URALT:
84      * inifile OHNE Section!!!!!!!!!!!!!!!!
85      *
86      * Das Inifile:
87      * ============
88      * key1=val1
89      * key2=val2
90      *
91      *
92      * List das Inifile ein
93      *
94      * @return Hashtable mit allen key-value-Paaren aus dem inifile.
95      */

96     public TKHashtable getAll() throws TKException
97     {
98
99         TKHashtable myIniHash = new TKHashtable();
100         try {
101             // FileInputStream inIniFile = new FileInputStream(theIniFile);
102
Properties props = new Properties(); // empty list
103
props.load(openInputStream());
104
105             Enumeration enumObject = props.propertyNames();
106             while(enumObject.hasMoreElements()) {
107                 String JavaDoc key = enumObject.nextElement().toString();
108                 String JavaDoc val = props.getProperty(key);
109                 myIniHash.put(key,val);
110             }
111
112             return myIniHash;
113         }
114         catch (IOException e) {
115             cat.error("Can't open Inifile: ", e);
116             myIniHash = null;
117             return myIniHash;
118         }
119
120     }
121
122     //***********************************************************************
123
//----------------------- NEU -------------------------------------------
124
//***********************************************************************
125
/**
126      * Alle Sections werden ausgelesen
127      *
128      * Aufbau eines iniFiles:
129      * ======================
130      * #KOMMENTAR
131      *
132      * [SECTION 1]
133      * key_1=val_1
134      * key_2=val_2
135      * ....
136      * key_n=val_n
137      *
138      * ...
139      *
140      * [SECTION N]
141      * key_1=val_1
142      * key_2=val_2
143      * ....
144      * key_n=val_n
145      *
146      * [END]
147      *
148      * @return verschachtelte Hashtable:
149      * key=SECTION, val= key/value-Paare der SECTION
150
151      */

152
153     public TKHashtable getAllSections() throws TKException
154     {
155         try {
156             String JavaDoc sectionPat = "[";
157             String JavaDoc comentPat = "#";
158             String JavaDoc splitPat = "=";
159             String JavaDoc merke = "";
160             String JavaDoc sectionKey = "";
161             TKHashtable lineHash = null;
162
163             TKHashtable sectionsHash = new TKHashtable();
164
165             // FileInputStream fileInputStream = new FileInputStream(theIniFile );
166
BufferedReader dataInputStream = new BufferedReader(new InputStreamReader(openInputStream()));
167             String JavaDoc line;
168
169             while( (line = dataInputStream.readLine()) != null) {
170                 String JavaDoc key= "";
171                 String JavaDoc value = "";
172
173                 if( (line.indexOf(sectionPat) > -1) && (!line.equals(""))) {
174                     if( merke.equals("") ){
175                         merke = line;
176                         sectionKey = line;
177                     }
178                     else {
179                         merke = sectionKey;
180
181                         if(! line.equals("[END]") )
182                             sectionKey = line;
183
184                         //---- [SECTIONS] -> key/val-Paare ----//
185
if(line.equals("[END]") )
186                             sectionsHash.put(sectionKey.substring(1, sectionKey.length()-1), lineHash);
187                         else
188                             sectionsHash.put(merke.substring(1, merke.length()-1), lineHash);
189
190                     }
191
192                     if(! line.equals("[END]") )
193                         lineHash = new TKHashtable();
194
195                 }
196                 //---- line key=val ----//
197
else {
198                     line = cutEmptyStrings(line);
199                     if( (!line.equals("")) && (!line.startsWith(comentPat)) ){
200                         int idx = line.indexOf(splitPat);
201                         if( idx > -1 ){
202                             key = line.substring( 0, idx );
203                             value = line.substring( idx+1, line.length() );
204                             lineHash.put(key,value);
205                         }
206                     }
207                 }
208             }
209
210             //TKHttp.out().println("<br><b>sectionsHash: </b>"+sectionsHash);
211
return sectionsHash;
212
213         }
214         catch (IOException e) {
215             cat.error("Can't open Inifile: ", e);
216             return null;
217         }
218
219     }
220
221     //***********************************************************************
222
/**
223      * Beginnen eingelesene Zeilen mit eine Leerzeichen oder einem Tab,
224      * so werden diese entfernt
225      *
226      * @param String line, eine Zeile des Inifiles
227      *
228      * @return eine Zeile des Inifiles, die nicht mit einem Leerzeichen oder eine tab beginnt
229      */

230     public String JavaDoc cutEmptyStrings (String JavaDoc line) {
231
232         if( !line.equals("") ) {
233
234             while( line.startsWith(emptyPat) || line.startsWith(tabPat) ){
235
236                 if(line.startsWith(emptyPat) )
237                     line = emptyString(line);
238
239                 if(line.startsWith(tabPat) )
240                     line = tabString(line);
241
242             }
243         }
244         return line;
245     }
246
247     //***********************************************************************
248
/**
249      * Beginnt eine eingelesene Zeile mit eine Leerzeichen,
250      * so werden diese entfernt
251      *
252      * @param String line, eine Zeile des Inifiles
253      *
254      * @return eine Zeile des Inifiles, die nicht mit einem Leerzeichen beginnt
255      */

256     public String JavaDoc emptyString (String JavaDoc line) {
257
258         while( line.startsWith(emptyPat) )
259             line = line.substring(1, line.length());
260
261
262         return line;
263     }
264
265     //***********************************************************************
266
/**
267      * Beginnt eine eingelesene Zeile mit einem Tab,
268      * so werden diese entfernt
269      *
270      * @param String line, eine Zeile des Inifiles
271      *
272      * @return eine Zeile des Inifiles, die nicht mit einem Tab beginnt
273      */

274     public String JavaDoc tabString (String JavaDoc line) {
275
276         while( line.startsWith(tabPat))
277              line = line.substring(1, line.length());
278
279         return line;
280     }
281
282     //***********************************************************************
283
/**
284      * Die key/val-paare einer bestimten Section werden ausgelesen
285      * Inifile-Objekt wird erzeugt
286      *
287      * @param String section, eine [SECTION]
288      * @return verschachtelte Hashtable:
289      */

290     public TKHashtable getOneSection(String JavaDoc section) throws TKException
291     {
292
293         TKHashtable iniHash = getAllSections();
294         TKHashtable contentHash = (TKHashtable) iniHash.get(section);
295         return contentHash;
296
297     }
298
299     //***********************************************************************
300
/**
301      * Die key/val-paare einer bestimten Section werden ausgelesen
302      * Es wird kein Inifile-Objekt erzeugt
303      *
304      * @param String section, eine [SECTION]
305      * @param TKHashtable iniHash, Hashtable mit allen sections
306      *
307      * @return Hashtable mit den key/val-paaren einer Section
308      */

309     public TKHashtable getOneSection(String JavaDoc section, TKHashtable iniHash) {
310
311         TKHashtable contentHash = (TKHashtable) iniHash.get(section);
312         return contentHash;
313
314     }
315
316     //***********************************************************************
317
/**
318      * Die Values einer section werden gelesen
319      * Inifile-Objekt wird erzeugt
320      *
321      * @param String section, eine [SECTION]
322      *
323      * @return Vector mit allen Values einer Section
324      */

325     public TKVector getValsFromOneSection(String JavaDoc section) throws TKException
326     {
327         TKHashtable iniHash = getAllSections();
328         return getKeysFromOneSection(section, iniHash);
329
330     }
331
332     //***********************************************************************
333
/**
334      * Die Keys einer section werden gelesen
335      * Inifile-Objekt wird erzeugt
336      *
337      * @param String section, eine [SECTION]
338      *
339      * @return Vector mit allen Keys einer Section
340      */

341     public TKVector getValsFromOneSection(String JavaDoc section, TKHashtable iniHash) {
342
343         TKVector valVector = new TKVector();
344         TKHashtable contentHash = (TKHashtable) iniHash.get(section);
345
346         Enumeration contentEnum = contentHash.keys();
347         while( contentEnum.hasMoreElements() ) {
348             String JavaDoc contentKey = (String JavaDoc) contentEnum.nextElement();
349             String JavaDoc contentVal = (String JavaDoc) contentHash.get(contentKey);
350             valVector.addElement( contentVal );
351         }
352
353         return valVector;
354     }
355
356     //***********************************************************************
357
/**
358      * Die Keys einer section werden gelesen
359      * Es wird kein Inifile-Objekt erzeugt
360      *
361      * @param String section, eine [SECTION]
362      * @param TKHashtable iniHash, Hashtable mit allen Sections
363      *
364      * @return Vector mit allen Keys einer Section
365      */

366
367     public TKVector getKeysFromOneSection(String JavaDoc section) throws TKException
368     {
369         TKHashtable iniHash = getAllSections();
370         return getKeysFromOneSection(section, iniHash);
371
372     }
373
374
375     //***********************************************************************
376
/**
377      * Die Keys einer section werden gelesen
378      * Es wird kein Inifile-Objekt erzeugt
379      *
380      * @param String section, eine [SECTION]
381      * @param TKHashtable iniHash, Hashtable mit allen Sections
382      *
383      * @return Vector mit allen Keys einer Section
384      */

385
386     public TKVector getKeysFromOneSection(String JavaDoc section, TKHashtable iniHash) {
387
388         TKVector keyVector = new TKVector();
389         TKHashtable contentHash = (TKHashtable) iniHash.get(section);
390
391         Enumeration contentEnum = contentHash.keys();
392         while( contentEnum.hasMoreElements() ) {
393             String JavaDoc contentKey = (String JavaDoc) contentEnum.nextElement();
394             keyVector.addElement( contentKey );
395         }
396
397         return keyVector;
398     }
399 }
400
401
Popular Tags