01: package com.canoo.ant.table;
02:
03: import com.canoo.ant.filter.ITableFilter;
04:
05: import java.io.File;
06: import java.util.Properties;
07:
08: public class TableFactory {
09: public static final String KEY_TABLE = "table";
10: public static final String KEY_NAME = "name";
11: public static final String KEY_TABLE_CLASS = "tableClass";
12: public static final String KEY_CONTAINER = "container";
13: public static final String KEY_FOREIGN_TABLE = "foreignTable";
14: public static final String KEY_FOREIGN_NAME = "foreignName";
15: public static final String KEY_FILTER_CLASS = "filterClass";
16: public static final String KEY_PREFIX = "prefix";
17:
18: private static final String TABLE_PACKAGE = "com.canoo.ant.table.";
19: private static final String TABLE_SUFFIX = "PropertyTable";
20: private static final String FILTER_PACKAGE = "com.canoo.ant.filter.";
21: private static final String FILTER_SUFFIX = "Filter";
22: private static final String DEFAULT_FILTER = "FirstEquals";
23:
24: public static void initOrDefault(IPropertyTable table,
25: ITableFilter filter, Properties colInfo,
26: final File container, String key) {
27: // foreign Name defaults to the current name since it is usual to name
28: // columns identically that are used for the "join"
29: String foreignName = colInfo.getProperty(KEY_FOREIGN_NAME, key);
30: // if foreignName is of format table.col, use table as default
31: String foreignTable = null;
32: if (foreignName != null) {
33: int delimPos = foreignName.indexOf(".");
34: if (delimPos > -1) {
35: foreignTable = foreignName.substring(0, delimPos);
36: }
37: }
38: foreignTable = colInfo.getProperty(KEY_FOREIGN_TABLE,
39: foreignTable);
40: table.setPrefix(colInfo.getProperty(KEY_PREFIX, null));
41: table.setContainer(container);
42: table.setTable(foreignTable);
43: filter.setForeignName(foreignName);
44: table.setFilter(filter);
45: }
46:
47: public static IPropertyTable createTable(Properties colInfo,
48: String defaultTableClass) throws InstantiationException,
49: IllegalAccessException, ClassNotFoundException {
50: String tableClass = colInfo.getProperty(KEY_TABLE_CLASS,
51: defaultTableClass);
52: if (!isFullyQualified(tableClass)) {
53: tableClass = TABLE_PACKAGE + tableClass + TABLE_SUFFIX;
54: }
55: return (IPropertyTable) Class.forName(tableClass).newInstance();
56: }
57:
58: public static ITableFilter createFilter(Properties colInfo)
59: throws InstantiationException, IllegalAccessException,
60: ClassNotFoundException {
61: String filterClass = colInfo.getProperty(KEY_FILTER_CLASS,
62: DEFAULT_FILTER);
63: if (!isFullyQualified(filterClass)) {
64: filterClass = FILTER_PACKAGE + filterClass + FILTER_SUFFIX;
65: }
66: return (ITableFilter) Class.forName(filterClass).newInstance();
67: }
68:
69: private static boolean isFullyQualified(String possibleClassName) {
70: return possibleClassName.indexOf(".") > -1;
71: }
72: }
|