01: package org.drools.brms.server.util;
02:
03: import java.io.BufferedReader;
04: import java.io.InputStream;
05: import java.io.InputStreamReader;
06: import java.lang.reflect.Method;
07: import java.text.DateFormat;
08: import java.util.ArrayList;
09: import java.util.Calendar;
10: import java.util.List;
11: import java.util.StringTokenizer;
12:
13: import org.drools.repository.AssetItem;
14: import org.drools.repository.RulesRepositoryException;
15:
16: public class RowLoader {
17:
18: private String[] headers;
19: List extractors;
20:
21: public String[] getHeaders() {
22: return headers;
23: }
24:
25: public String[] getRow(AssetItem item) {
26: String[] row = new String[headers.length];
27: for (int i = 0; i < row.length; i++) {
28: Method meth = (Method) extractors.get(i);
29: try {
30: Object val = meth.invoke(item, (Object[]) null);
31: if (val instanceof String) {
32: String s = (String) val;
33: if (s.length() > 64) {
34: s = s.substring(0, 61) + "...";
35: }
36: row[i] = s;
37:
38: } else if (val instanceof Calendar) {
39: row[i] = DateFormat.getDateInstance().format(
40: ((Calendar) val).getTime());
41: } else {
42: row[i] = val.toString();
43: }
44: } catch (Exception e) {
45: if (e instanceof RuntimeException)
46: throw (RuntimeException) e;
47: throw new RulesRepositoryException(e);
48: }
49: }
50: return row;
51: }
52:
53: public RowLoader(String resourcename) {
54:
55: InputStream in = RowLoader.class.getResourceAsStream("/"
56: + resourcename + ".properties");
57: BufferedReader reader = new BufferedReader(
58: new InputStreamReader(in));
59: List fields = new ArrayList();
60: extractors = new ArrayList();
61: String line;
62:
63: try {
64:
65: while ((line = reader.readLine()) != null) {
66: if (!line.startsWith("#") && !line.trim().equals("")) {
67: StringTokenizer tok = new StringTokenizer(line, "=");
68: String field = tok.nextToken();
69: String method = tok.nextToken();
70: fields.add(field);
71:
72: final Method meth = AssetItem.class.getMethod(
73: method, new Class[] {});
74:
75: extractors.add(meth);
76:
77: }
78: }
79: } catch (Exception e) {
80: if (e instanceof RuntimeException) {
81: throw (RuntimeException) e;
82: }
83: throw new RulesRepositoryException(e);
84: }
85: headers = (String[]) fields.toArray(new String[fields.size()]);
86: }
87:
88: }
|