1 package org.andromda.schema2xmi;
2
3 import org.andromda.core.common.AndroMDALogger;
4 import org.andromda.core.common.XmlObjectFactory;
5 import org.apache.commons.cli.CommandLine;
6 import org.apache.commons.cli.CommandLineParser;
7 import org.apache.commons.cli.HelpFormatter;
8 import org.apache.commons.cli.Option;
9 import org.apache.commons.cli.Options;
10 import org.apache.commons.cli.ParseException;
11 import org.apache.commons.cli.PosixParser;
12
13
14 /***
15 * Converts a database schema to an XMI document.
16 *
17 * @author Chad Brandon
18 */
19 public class Schema2XMI
20 {
21 private static Options options;
22
23 /***
24 * The command to display help
25 */
26 private static final String HELP = "h";
27
28 /***
29 * The command line argument to specify the XMI version that will be
30 * producted.
31 */
32 private static final String XMI_VERSION = "x";
33
34 /***
35 * The command line argument to specify the input model file.
36 */
37 private static final String INPUT_MODEL = "i";
38
39 /***
40 * The command line argument to specify the JDBC driver class
41 */
42 private static final String DRIVER = "d";
43
44 /***
45 * The command line argument to specify the schema user.
46 */
47 private static final String USER = "u";
48
49 /***
50 * The command line argument to specify the schema user password.
51 */
52 private static final String PASSWORD = "p";
53
54 /***
55 * The command line argument to specify the connection URL.
56 */
57 private static final String CONNECTION_URL = "c";
58
59 /***
60 * The command line argument to specify the transformed output file.
61 */
62 private static final String OUTPUT_MODEL = "o";
63
64 /***
65 * The command line argument specifying the URI to the type mappings file.
66 */
67 private static final String MAPPINGS = "m";
68
69 /***
70 * The command line argument specifying the package to which the model
71 * element will be generated.
72 */
73 private static final String PACKAGE = "P";
74
75 /***
76 * The command line argument specifying the name of the schema where the
77 * table resides.
78 */
79 private static final String SCHEMA = "s";
80
81 /***
82 * The command line argument specifying the tables names to match on
83 */
84 private static final String TABLE_PATTERN = "t";
85
86 /***
87 * The command line argument specifying the attribute names pattern to match on.
88 */
89 private static final String COLUMN_PATTERN = "a";
90
91 /***
92 * The command line argument specifying the class stereotype name.
93 */
94 private static final String CLASS_STEREOTYPES = "C";
95
96 /***
97 * The command line argument specifying the identifier stereotype name.
98 */
99 private static final String IDENTIFIER_STEREOTYPES = "I";
100
101 /***
102 * The command line argument specifiying the name of the tagged value to use
103 * for tagged column names.
104 */
105 private static final String TABLE_TAGGEDVALUE = "V";
106
107 /***
108 * The command line argument specifiying the name of the tagged value to use
109 * for tagged column names.
110 */
111 private static final String COLUMN_TAGGEDVALUE = "v";
112
113 /***
114 * Configure the CLI options.
115 */
116 static
117 {
118 try
119 {
120 AndroMDALogger.initialize();
121
122
123
124 XmlObjectFactory.setDefaultValidating(false);
125 }
126 catch (Throwable th)
127 {
128 th.printStackTrace();
129 }
130
131 options = new Options();
132
133 Option option = new Option(HELP, false, "Display help information");
134 option.setLongOpt("help");
135 options.addOption(option);
136
137 option = new Option(XMI_VERSION, true, "Specifies the XMI version that will be produced");
138 option.setLongOpt("xmi");
139 options.addOption(option);
140
141 option = new Option(INPUT_MODEL, true, "Input model file (to which model elements will be added)");
142 option.setLongOpt("input");
143 options.addOption(option);
144
145 option = new Option(DRIVER, true, "JDBC driver class");
146 option.setLongOpt("driver");
147 options.addOption(option);
148
149 option = new Option(CONNECTION_URL, true, "JDBC connection URL");
150 option.setLongOpt("connectionUrl");
151 options.addOption(option);
152
153 option = new Option(USER, true, "Schema user name");
154 option.setLongOpt("user");
155 options.addOption(option);
156
157 option = new Option(PASSWORD, true, "Schema user password");
158 option.setLongOpt("password");
159 options.addOption(option);
160
161 option = new Option(MAPPINGS, true, "The type mappings URI (i.e. file:${basedir}/DataypeMappings.xml)");
162 option.setLongOpt("mappings");
163 options.addOption(option);
164
165 option = new Option(SCHEMA, true, "The name of the schema where the tables can be found");
166 option.setLongOpt("schema");
167 options.addOption(option);
168
169 option = new Option(TABLE_PATTERN, true, "The table name pattern of tables to process (regular expression)");
170 option.setLongOpt("tablePattern");
171 options.addOption(option);
172
173 option = new Option(COLUMN_PATTERN, true, "The column name pattern of columns to process (regular expression)");
174 option.setLongOpt("columnPattern");
175 options.addOption(option);
176
177 option = new Option(PACKAGE, true, "The package to output classifiers");
178 option.setLongOpt("package");
179 options.addOption(option);
180
181 option =
182 new Option(CLASS_STEREOTYPES, true, "Comma seperated list of stereotype names to add to the created class");
183 option.setLongOpt("classStereotypes");
184 options.addOption(option);
185
186 option =
187 new Option(
188 IDENTIFIER_STEREOTYPES, true, "Comma seperated list of stereotype names to add to any class identifiers");
189 option.setLongOpt("identifierStereotypes");
190 options.addOption(option);
191
192 option = new Option(TABLE_TAGGEDVALUE, true, "The tagged value to use for storing the table name");
193 option.setLongOpt("tableTaggedValue");
194 options.addOption(option);
195
196 option = new Option(COLUMN_TAGGEDVALUE, true, "The tagged value to use for storing the column name");
197 option.setLongOpt("columnTaggedValue");
198 options.addOption(option);
199
200 option =
201 new Option(OUTPUT_MODEL, true, "Output location to which the result of the transformation will be written");
202 option.setLongOpt("output");
203 options.addOption(option);
204 }
205
206 /***
207 * Display usage information based upon current command-line option
208 * configuration.
209 */
210 public static void displayHelp()
211 {
212 HelpFormatter formatter = new HelpFormatter();
213 formatter.printHelp(Schema2XMI.class.getName() + " [options] ...]]", "\nOptions:", options, "\n");
214 }
215
216 /***
217 * Parse a string-array of command-line arguments.
218 * <p>
219 * This will parse the arguments against the configured schema2xmi
220 * command-line options, and return a <code>CommandLine</code> object from
221 * which we can retrieve the command like options.
222 * </p>
223 *
224 * @see <a href="http://jakarta.apache.org/commons/cli/">CLI </a>
225 * @param args The command-line arguments to parse.
226 * @return The <code>CommandLine</code> result.
227 * @throws ParseException If an error occurs while parsing the command-line
228 * options.
229 */
230 public CommandLine parseCommands(String[] args)
231 throws ParseException
232 {
233 CommandLineParser parser = new PosixParser();
234 return parser.parse(options, args);
235 }
236
237 public static void main(String[] args)
238 {
239 Schema2XMI schema2Xmi = new Schema2XMI();
240 try
241 {
242 CommandLine commandLine = schema2Xmi.parseCommands(args);
243 if (
244 commandLine.hasOption(HELP) ||
245 !(
246 commandLine.hasOption(OUTPUT_MODEL) && commandLine.hasOption(DRIVER) &&
247 commandLine.hasOption(CONNECTION_URL) && commandLine.hasOption(USER) &&
248 commandLine.hasOption(PASSWORD)
249 ))
250 {
251 Schema2XMI.displayHelp();
252 }
253 else
254 {
255 String inputModel = commandLine.getOptionValue(INPUT_MODEL);
256 SchemaTransformer transformer =
257 new SchemaTransformer(
258 commandLine.getOptionValue(DRIVER),
259 commandLine.getOptionValue(CONNECTION_URL),
260 commandLine.getOptionValue(USER),
261 commandLine.getOptionValue(PASSWORD));
262
263
264 transformer.setXmiVersion(commandLine.getOptionValue(XMI_VERSION));
265 transformer.setTypeMappings(commandLine.getOptionValue(MAPPINGS));
266 transformer.setPackageName(commandLine.getOptionValue(PACKAGE));
267 transformer.setSchema(commandLine.getOptionValue(SCHEMA));
268 transformer.setTableNamePattern(commandLine.getOptionValue(TABLE_PATTERN));
269 transformer.setColumnNamePattern(commandLine.getOptionValue(COLUMN_PATTERN));
270 transformer.setClassStereotypes(commandLine.getOptionValue(CLASS_STEREOTYPES));
271 transformer.setIdentifierStereotypes(commandLine.getOptionValue(IDENTIFIER_STEREOTYPES));
272 transformer.setTableTaggedValue(commandLine.getOptionValue(TABLE_TAGGEDVALUE));
273 transformer.setColumnTaggedValue(commandLine.getOptionValue(COLUMN_TAGGEDVALUE));
274
275 String outputLocation = commandLine.getOptionValue(OUTPUT_MODEL);
276 transformer.transform(inputModel, outputLocation);
277 }
278 }
279 catch (Throwable throwable)
280 {
281 throwable = getRootCause(throwable);
282 throwable.printStackTrace();
283 }
284 }
285
286 private static Throwable getRootCause(Throwable th)
287 {
288 Throwable cause = th;
289 if (cause.getCause() != null)
290 {
291 cause = cause.getCause();
292 cause = getRootCause(cause);
293 }
294 return cause;
295 }
296 }