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