datasource/Cached_Content_Loader: added caching content loader
[metux-java.git] / util / TextDB.java
blob048fbf90c2e5982016a9d510626a93c1b80a1a6c
2 package org.de.metux.util;
4 import java.util.Properties;
5 import java.util.Hashtable;
6 import java.io.FileNotFoundException;
7 import java.io.BufferedReader;
8 import java.io.FileReader;
9 import java.net.URLDecoder;
12 Note: URLDecoder.decode(String text) is deprecated.
13 Instead .decode(String text, String enc) should be used.
14 Have to think about it carefully.
17 public class TextDB
19 Properties hash = new Properties();
21 public Hashtable getHashtable ()
23 return hash;
26 public Hashtable Load(String filename)
28 Hashtable h = new Hashtable ();
29 if (LoadIntoHashtable(filename,h))
30 return h;
31 else
32 return null;
35 public static boolean LoadIntoHashtable(String filename, Hashtable table)
37 BufferedReader in;
39 try
41 in = new BufferedReader(new FileReader(filename));
43 catch (FileNotFoundException e)
45 return false;
48 try
50 String line;
51 while ((line=in.readLine())!=null)
53 int x;
54 int len;
56 /* strip off comments */
57 if ((x=line.indexOf('#'))>=0)
58 line = line.substring(0,x);
60 line = line.trim();
61 if ((len=line.length())>0)
63 /* -- split into key and value -- */
64 if ((x = line.indexOf(':'))>0)
66 String key;
67 String value;
68 String str;
70 key = (line.substring(0,x)).trim();
71 value = (line.substring(x+1)).trim();
72 value = URLDecoder.decode(value);
74 if (table.containsKey(key))
75 table.put(key,table.get(key)+"\n"+value);
76 else
77 table.put(key,value);
79 else
80 System.err.println ( "CORRUPT LINE "+line );
83 return true;
85 catch(Exception e)
87 System.err.println("File input error");
88 return false;
92 public TextDB ( String filename ) throws FileNotFoundException
94 if (this.hash==null)
95 this.hash = new Properties();
97 LoadIntoHashtable(filename,this.hash);