Added README file
[metux-java.git] / util / TextTable.java
blobaa9180f6d6afc402aff9414860cce46014783afa
2 package org.de.metux.util;
4 import java.util.Hashtable;
5 import java.util.Properties;
6 import java.io.*;
7 import java.util.Enumeration;
9 public class TextTable
11 public static Hashtable Load(String filename,String pkey)
13 Hashtable h = new Hashtable ();
14 if (LoadIntoHashtable(filename,h, pkey))
15 return h;
16 else
17 return null;
20 public static boolean LoadIntoHashtable(String filename, Hashtable table, String pkey)
22 BufferedReader in;
24 try
26 in = new BufferedReader(new FileReader(filename));
28 catch (FileNotFoundException e)
30 return false;
33 try
35 String line;
36 Hashtable record = new Properties();
37 int linecnt = 0;
39 while ((line=in.readLine())!=null)
41 int x;
42 int len;
44 /* strip off comments */
45 if ((x=line.indexOf('#'))>=0)
46 line = line.substring(0,x);
48 line = line.trim();
49 if ((len=line.length())>0)
51 /* -- split into key and value -- */
52 if ((x = line.indexOf(':'))>0)
54 String key;
55 String value;
56 String str;
58 key = (line.substring(0,x)).trim();
59 value = (line.substring(x+1)).trim();
61 if (record.containsKey(key))
62 record.put(key,record.get(key)+"\n"+value);
63 else
64 record.put(key,value);
66 else if ((line.indexOf("--")==0)&&(!record.isEmpty()))
68 String k = (String)record.get(pkey);
69 if (k.length()>0)
70 table.put(k,record);
71 else
72 table.put(record,record);
74 record = new Hashtable();
76 else
77 System.err.println(
78 "CORRUPT LINE ("+
79 filename+":"+linecnt+"): "+line);
81 linecnt++;
83 return true;
85 catch(IOException e)
87 System.err.println("File input error");
88 return false;
92 public static String DumpTable ( Hashtable table )
94 if (table==null)
95 return "<NULL>";
97 String res = "";
98 for (Enumeration e = table.keys(); e.hasMoreElements(); )
100 String key = (String)e.nextElement();
101 res += "#PKEY: "+key+"\n";
102 Hashtable record = (Hashtable)table.get(key);
103 for (Enumeration re = record.keys(); re.hasMoreElements(); )
105 String rk = (String)re.nextElement();
106 res += rk+": "+record.get(rk)+"\n";
108 res += "-- \n";
110 return res;