1 /*
2 * PROJECT : DAR Runtime and Tools
3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org
4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE
5 * Version 2.1, February 1999
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 package org.enableit.db.darrt.ant;
22
23 import java.io.File;
24 import java.util.StringTokenizer;
25
26 import org.apache.log4j.Category;
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.Project;
29 import org.enableit.db.beans.Driver;
30 import org.enableit.db.beans.Provider;
31 import org.enableit.db.darrt.SchemaExporter;
32
33
34 /***
35 * Ant Task to export a database schema as an XML file.
36 *
37 * <p>ASA interpretation of DatabaseMetaData object
38 * SCHEMAS = userids and groups</p>
39 *
40 * @version __VERSION__
41 *
42 * @author __AUTHOR__
43 */
44 public class SchemaExporterTask extends AbstractSchemaTask {
45 /***
46 * The Log4J <code>Category</code> doing the logging.
47 * Same <code>Category</code> is used throughout the package.
48 */
49 protected static Category logger = Category.getInstance(SchemaExporterTask.class);
50
51 /*
52 * Constant Properties
53 */
54
55 /***
56 * A valid output mode.
57 */
58 public static final int OM_SINGLE_FILE = 1;
59
60 /***
61 * A valid output mode.
62 */
63 public static final int OM_FILE_PER_TABLE = 10;
64
65 /***
66 * A valid output mode.
67 */
68 public static final int OM_SINGLE_FILE_AND_FILE_PER_TABLE = 11;
69
70 /*
71 * Properties
72 */
73 private String tableNamePattern;
74 private String catalog;
75 private String outputFile;
76 private String outputDir;
77 private boolean debug = false;
78
79 /*
80 * DB Connection stuff
81 */
82 private String schema;
83 private String[] tableTypes;
84
85 /***
86 * The output mode.
87 */
88 private int outputMode = 1;
89
90 /*
91 * Constructors
92 */
93
94 /***
95 * Default Constructor
96 */
97 public SchemaExporterTask() {
98 }
99
100 /*
101 * Methods
102 */
103
104 /***
105 * Sets the target (output) directory & file in one
106 * @deprecated The outputDir and outputFile attributes give better control.
107 */
108 public void setOutput(String output) {
109 output = output.replace('/', File.separatorChar).replace('//',
110 File.separatorChar);
111 outputDir = output.substring(0, output.lastIndexOf(File.separator));
112 outputFile = output.substring(output.lastIndexOf(File.separator) + 1);
113 }
114
115 /***
116 * Sets the target (output) file.
117 */
118 public void setOutputFile(String outputFile) {
119 this.outputFile = outputFile;
120 }
121
122 /***
123 * Sets the target (output) directory.
124 */
125 public void setOutputDir(String outputDir) {
126 outputDir = outputDir.replace('/', File.separatorChar).replace('//',
127 File.separatorChar);
128 this.outputDir = outputDir;
129 }
130
131 /***
132 * Set the table name pattern.
133 */
134 public void setTableNamePattern(String tableNamePattern) {
135 this.tableNamePattern = tableNamePattern;
136 }
137
138 /***
139 * Set the schema name.
140 */
141 public void setSchema(String schema) {
142 this.schema = schema;
143 }
144
145 /***
146 * Set the table types to export.
147 */
148 public void setTableTypes(String tableTypes) {
149 //ArrayList list = new ArrayList() ;
150 StringTokenizer st = new StringTokenizer(tableTypes, ",");
151
152 this.tableTypes = new String[st.countTokens()];
153
154 int i = 0;
155
156 while (st.hasMoreTokens()) {
157 this.tableTypes[i++] = st.nextToken().trim();
158 }
159 }
160
161 /***
162 * Set the JDBC catalogue name (Sybase and Microsoft userids are more likely
163 * to know this as database name)
164 */
165 public void setCatalog(String catalog) {
166 this.catalog = catalog;
167 }
168
169 /***
170 * Sets the output type.
171 *
172 * @see #
173 */
174 public void setOutputMode(int mode) {
175 this.outputMode = mode;
176 }
177
178 /***
179 * Run the schema export.
180 */
181 public void execute() {
182 try {
183 log("Exporting schema...");
184 log("\t... from datasource " + url);
185
186 SchemaExporter se = new SchemaExporter();
187
188 if (tableNamePattern != null) {
189 se.setTablePattern(tableNamePattern);
190 }
191
192 if (schema != null) {
193 se.setSchemaName(schema);
194 }
195
196 se.setTableTypes(tableTypes);
197 se.setDebug(debug);
198
199 log("Output dir set to: " + outputDir, Project.MSG_VERBOSE);
200 se.setOperDir(new File(outputDir));
201
202 log("Output file name set to: " + outputFile, Project.MSG_VERBOSE);
203 se.setFileName(outputFile);
204
205 Provider provider = new Provider();
206 Driver d = new Driver();
207
208 d.setClassName(driver);
209 provider.setDriver(d);
210 provider.setUrl(url);
211 provider.setUsername(userid);
212 provider.setPassword(password);
213
214 se.exportToFile(provider, outputMode);
215 } catch (Exception e) {
216 //if (e.getMessage()==null || e.getMessage().length()==0) {
217 e.printStackTrace(System.err);
218
219 //}
220 throw new BuildException(e.getMessage());
221 }
222 }
223
224 /***
225 * Returns the directory name classes should be stored in based on the
226 * Java package name received
227 */
228 private String getPackageDir(String javaPackage) {
229 StringBuffer dir = new StringBuffer();
230 StringTokenizer st = new StringTokenizer(javaPackage, ".");
231
232 while (st.hasMoreTokens()) {
233 dir.append(st.nextToken());
234 dir.append("/");
235 }
236
237 // remove final '/' and return
238 return dir.substring(0, dir.length() - 1);
239 }
240
241 /***
242 * Set the debug flag.
243 */
244 public void setDebug(boolean debug) {
245 this.debug = debug;
246 }
247 }
This page was automatically generated by Maven