01: package com.canoo.ant.table.test;
02:
03: import java.io.IOException;
04: import java.util.HashMap;
05: import java.util.Iterator;
06: import java.util.LinkedList;
07: import java.util.List;
08: import java.util.Map;
09: import java.util.Properties;
10:
11: import org.apache.log4j.Logger;
12:
13: import com.canoo.ant.table.APropertyTable;
14:
15: public class InMemoryPropertyTable extends APropertyTable {
16:
17: private static final Logger LOG = Logger
18: .getLogger(InMemoryPropertyTable.class);
19: private static Map sTables = new HashMap(); // maps tablename to List of Strings
20:
21: public InMemoryPropertyTable() {
22: }
23:
24: public static void addTo(String tablename, String[] entries) {
25: if (!sTables.containsKey(tablename)) {
26: sTables.put(tablename, new LinkedList());
27: }
28: List rows = (List) sTables.get(tablename);
29: rows.add(entries);
30: }
31:
32: public static void reset() {
33: sTables = new HashMap();
34: }
35:
36: protected List read(final String tablename) throws IOException {
37: List result = new LinkedList();
38: List rows = (List) sTables.get(tablename);
39: if (null == rows) {
40: LOG.error("no such table " + tablename);
41: return result;
42: }
43: Iterator eachrow = rows.iterator();
44: if (!eachrow.hasNext()) {
45: LOG.error("no header line in table " + tablename);
46: return result;
47: }
48: String[] header = (String[]) eachrow.next();
49: while (eachrow.hasNext()) {
50: String[] row = (String[]) eachrow.next();
51: Properties entry = new Properties();
52: for (int i = 0; i < header.length; i++) {
53: entry.setProperty(header[i], row[i]);
54: }
55: result.add(entry);
56: }
57: return result;
58: }
59: }
|