1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.addam.ddlgen;
20
21 import org.apache.tools.ant.BuildException;
22
23 import java.io.*;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.ResourceBundle;
27 import java.util.regex.Pattern;
28
29 /**
30 * Used to define what the output files written by a generator, allows include/exclude for specific
31 * objects, and can generate a master script based on the objects generated.
32 * Date: Nov 11, 2004
33 */
34 public class GeneratorSpec implements Filter {
35
36 public void setType(String type) {
37 this.type = type;
38 }
39
40 public void setVendor(String vendor) {
41 this.vendor = vendor;
42 }
43
44 public void setMasterScript(String file) {
45 this.masterScript = file;
46 }
47
48 public void setPrefix(String prefix) {
49 if (prefix == null) prefix = "";
50 this.prefix = prefix;
51 }
52
53 public void setExtension(String extension) {
54 if (extension == null) extension = "";
55 this.extension = extension;
56 }
57
58 public void setIncludes(String includes) {
59 String[] includeArray = includes.split(",");
60 for (String anIncludeArray : includeArray) {
61 Table table = new Table();
62 table.setName(anIncludeArray);
63 addConfiguredInclude(table);
64 }
65 }
66
67 public void setIncludesPattern(String pattern) {
68 includesPattern = Pattern.compile(pattern);
69 }
70
71 public void setIncludesFile(File includesFile) {
72 try {
73 BufferedReader reader = new BufferedReader(new FileReader(includesFile));
74 for (String line = reader.readLine(); line != null; line = reader.readLine()) {
75 Table table = new Table();
76 table.setName(line);
77 addConfiguredInclude(table);
78 }
79 } catch (FileNotFoundException e) {
80 throw new BuildException("file not found:" + includesFile.getAbsolutePath());
81 } catch (IOException e) {
82 throw new BuildException("error reading file:" + includesFile.getAbsolutePath());
83 }
84 }
85
86 public void setExcludes(String excludes) {
87 String[] excludeArray = excludes.split(",");
88 for (String anExcludeArray : excludeArray) {
89 Table table = new Table();
90 table.setName(anExcludeArray);
91 addConfiguredExclude(table);
92 }
93 }
94
95 public void setExcludesPattern(String pattern) {
96 excludesPattern = Pattern.compile(pattern);
97 }
98
99 public void setExcludesFile(File excludesFile) {
100 try {
101 BufferedReader reader = new BufferedReader(new FileReader(excludesFile));
102 for (String line = reader.readLine(); line != null;) {
103 Table table = new Table();
104 table.setName(line);
105 addConfiguredInclude(table);
106 }
107 } catch (FileNotFoundException e) {
108 throw new BuildException("file not found:" + excludesFile.getAbsolutePath());
109 } catch (IOException e) {
110 throw new BuildException("error reading file:" + excludesFile.getAbsolutePath());
111 }
112 }
113
114 public void setDir(String dir) {
115 this.dir = dir;
116 }
117
118 public void addConfiguredInclude(Table table) {
119 includedTables.add(table.getName().trim());
120 }
121
122 public void addConfiguredExclude(Table table) {
123 excludedTables.add(table.getName().trim());
124 }
125
126 public boolean includes(String objectName) {
127 boolean includes = true;
128 boolean excludes = false;
129 String normalizedTableName = objectName.trim();
130 if (!includedTables.isEmpty()) {
131 includes = includedTables.contains(normalizedTableName);
132 }
133 if (!includes && includesPattern != null) {
134 includes = includesPattern.matcher(objectName).matches();
135 }
136 if (!excludedTables.isEmpty()) {
137 excludes = excludedTables.contains(normalizedTableName);
138 }
139 if (!excludes && excludesPattern != null) {
140 excludes = excludesPattern.matcher(objectName).matches();
141 }
142 return includes && !excludes;
143 }
144
145 public String getVendor() {
146 return vendor;
147 }
148
149 public Generator getGenerator() throws Exception {
150 String classname;
151 try {
152 ResourceBundle bundle = ResourceBundle.getBundle("net.sourceforge.addam.ddlgen.RegisteredGenerators");
153 classname = bundle.getString(type.toLowerCase());
154 } catch (Exception e) {
155 throw new BuildException("invalid type: " + type);
156 }
157 return (Generator)Class.forName(classname).newInstance();
158 }
159
160 public FileWriter getMasterScriptWriter() throws IOException {
161 File scriptDir = new File(dir);
162 if (masterScriptWriter == null && masterScript != null) {
163 if (!scriptDir.exists()) {
164 scriptDir.mkdirs();
165 }
166 masterScriptWriter = new FileWriter(new File(scriptDir,masterScript));
167 }
168 return masterScriptWriter;
169 }
170
171 public String getFileName(String tableName) {
172
173 String suffix = extension;
174 if (suffix.length() > 0 && !suffix.startsWith(".")) {
175 suffix = "." + suffix;
176 }
177 if (tableName == null) tableName = "";
178 return prefix + tableName + suffix;
179 }
180
181 public File getFile(String tableName) {
182 return new File(dir, getFileName(tableName));
183 }
184
185 public FileWriter getFileWriter(String object) throws IOException {
186
187 String suffix = extension;
188 if (suffix.length() > 0 && !suffix.startsWith(".")) {
189 suffix = "." + suffix;
190 }
191 File file = new File(dir, prefix + object + suffix);
192 File dir = file.getParentFile();
193 if (!dir.exists()) dir.mkdirs();
194 return new FileWriter(file);
195 }
196
197 private String dir;
198 private String type;
199 private String prefix = "";
200 private String extension = "";
201 private String vendor;
202 private String masterScript;
203 private FileWriter masterScriptWriter = null;
204 private final List<String> includedTables = new ArrayList<String>();
205 private final List<String> excludedTables = new ArrayList<String>();
206 private Pattern includesPattern;
207 private Pattern excludesPattern;
208 }