1 | |
package liquibase.sql.visitor; |
2 | |
|
3 | |
import java.util.HashMap; |
4 | |
import java.util.Map; |
5 | |
|
6 | |
public class SqlVisitorFactory { |
7 | |
|
8 | |
@SuppressWarnings("unchecked") |
9 | |
private final Map<String, Class> tagToClassMap; |
10 | |
|
11 | 0 | private static final SqlVisitorFactory instance = new SqlVisitorFactory(); |
12 | |
|
13 | |
@SuppressWarnings("unchecked") |
14 | 0 | private SqlVisitorFactory() { |
15 | 0 | tagToClassMap = new HashMap<String, Class>(); |
16 | 0 | Class[] visitors = new Class[] { PrependSqlVisitor.class, AppendSqlVisitor.class, |
17 | |
RegExpReplaceSqlVisitor.class, ReplaceSqlVisitor.class, }; |
18 | |
|
19 | |
try { |
20 | 0 | for (Class<SqlVisitor> visitorClass : visitors) { |
21 | 0 | SqlVisitor visitor = visitorClass.newInstance(); |
22 | 0 | tagToClassMap.put(visitor.getName(), visitorClass); |
23 | |
} |
24 | 0 | } catch (Exception e) { |
25 | 0 | throw new RuntimeException(e); |
26 | 0 | } |
27 | 0 | } |
28 | |
|
29 | |
public static SqlVisitorFactory getInstance() { |
30 | 0 | return instance; |
31 | |
} |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public SqlVisitor create(String tagName) { |
37 | 0 | Class<?> aClass = tagToClassMap.get(tagName); |
38 | 0 | if (aClass == null) { |
39 | 0 | throw new RuntimeException("Unknown tag: " + tagName); |
40 | |
} |
41 | |
try { |
42 | 0 | return (SqlVisitor) aClass.newInstance(); |
43 | 0 | } catch (Exception e) { |
44 | 0 | throw new RuntimeException(e); |
45 | |
} |
46 | |
} |
47 | |
|
48 | |
} |