001: package org.andromda.schema2xmi;
002:
003: import org.andromda.core.common.AndroMDALogger;
004: import org.andromda.core.common.XmlObjectFactory;
005: import org.apache.commons.cli.CommandLine;
006: import org.apache.commons.cli.CommandLineParser;
007: import org.apache.commons.cli.HelpFormatter;
008: import org.apache.commons.cli.Option;
009: import org.apache.commons.cli.Options;
010: import org.apache.commons.cli.ParseException;
011: import org.apache.commons.cli.PosixParser;
012:
013: /**
014: * Converts a database schema to an XMI document.
015: *
016: * @author Chad Brandon
017: */
018: public class Schema2XMI {
019: private static Options options;
020:
021: /**
022: * The command to display help
023: */
024: private static final String HELP = "h";
025:
026: /**
027: * The command line argument to specify the XMI version that will be
028: * producted.
029: */
030: private static final String XMI_VERSION = "x";
031:
032: /**
033: * The command line argument to specify the input model file.
034: */
035: private static final String INPUT_MODEL = "i";
036:
037: /**
038: * The command line argument to specify the JDBC driver class
039: */
040: private static final String DRIVER = "d";
041:
042: /**
043: * The command line argument to specify the schema user.
044: */
045: private static final String USER = "u";
046:
047: /**
048: * The command line argument to specify the schema user password.
049: */
050: private static final String PASSWORD = "p";
051:
052: /**
053: * The command line argument to specify the connection URL.
054: */
055: private static final String CONNECTION_URL = "c";
056:
057: /**
058: * The command line argument to specify the transformed output file.
059: */
060: private static final String OUTPUT_MODEL = "o";
061:
062: /**
063: * The command line argument specifying the URI to the type mappings file.
064: */
065: private static final String MAPPINGS = "m";
066:
067: /**
068: * The command line argument specifying the package to which the model
069: * element will be generated.
070: */
071: private static final String PACKAGE = "P";
072:
073: /**
074: * The command line argument specifying the name of the schema where the
075: * table resides.
076: */
077: private static final String SCHEMA = "s";
078:
079: /**
080: * The command line argument specifying the tables names to match on
081: */
082: private static final String TABLE_PATTERN = "t";
083:
084: /**
085: * The command line argument specifying the attribute names pattern to match on.
086: */
087: private static final String COLUMN_PATTERN = "a";
088:
089: /**
090: * The command line argument specifying the class stereotype name.
091: */
092: private static final String CLASS_STEREOTYPES = "C";
093:
094: /**
095: * The command line argument specifying the identifier stereotype name.
096: */
097: private static final String IDENTIFIER_STEREOTYPES = "I";
098:
099: /**
100: * The command line argument specifiying the name of the tagged value to use
101: * for tagged column names.
102: */
103: private static final String TABLE_TAGGEDVALUE = "V";
104:
105: /**
106: * The command line argument specifiying the name of the tagged value to use
107: * for tagged column names.
108: */
109: private static final String COLUMN_TAGGEDVALUE = "v";
110:
111: /**
112: * Configure the CLI options.
113: */
114: static {
115: try {
116: AndroMDALogger.initialize();
117:
118: // turn off validation because of the incorrect parsers
119: // in the JDK
120: XmlObjectFactory.setDefaultValidating(false);
121: } catch (Throwable th) {
122: th.printStackTrace();
123: }
124:
125: options = new Options();
126:
127: Option option = new Option(HELP, false,
128: "Display help information");
129: option.setLongOpt("help");
130: options.addOption(option);
131:
132: option = new Option(XMI_VERSION, true,
133: "Specifies the XMI version that will be produced");
134: option.setLongOpt("xmi");
135: options.addOption(option);
136:
137: option = new Option(INPUT_MODEL, true,
138: "Input model file (to which model elements will be added)");
139: option.setLongOpt("input");
140: options.addOption(option);
141:
142: option = new Option(DRIVER, true, "JDBC driver class");
143: option.setLongOpt("driver");
144: options.addOption(option);
145:
146: option = new Option(CONNECTION_URL, true, "JDBC connection URL");
147: option.setLongOpt("connectionUrl");
148: options.addOption(option);
149:
150: option = new Option(USER, true, "Schema user name");
151: option.setLongOpt("user");
152: options.addOption(option);
153:
154: option = new Option(PASSWORD, true, "Schema user password");
155: option.setLongOpt("password");
156: options.addOption(option);
157:
158: option = new Option(MAPPINGS, true,
159: "The type mappings URI (i.e. file:${basedir}/DataypeMappings.xml)");
160: option.setLongOpt("mappings");
161: options.addOption(option);
162:
163: option = new Option(SCHEMA, true,
164: "The name of the schema where the tables can be found");
165: option.setLongOpt("schema");
166: options.addOption(option);
167:
168: option = new Option(TABLE_PATTERN, true,
169: "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,
174: "The column name pattern of columns to process (regular expression)");
175: option.setLongOpt("columnPattern");
176: options.addOption(option);
177:
178: option = new Option(PACKAGE, true,
179: "The package to output classifiers");
180: option.setLongOpt("package");
181: options.addOption(option);
182:
183: option = new Option(CLASS_STEREOTYPES, true,
184: "Comma seperated list of stereotype names to add to the created class");
185: option.setLongOpt("classStereotypes");
186: options.addOption(option);
187:
188: option = new Option(IDENTIFIER_STEREOTYPES, true,
189: "Comma seperated list of stereotype names to add to any class identifiers");
190: option.setLongOpt("identifierStereotypes");
191: options.addOption(option);
192:
193: option = new Option(TABLE_TAGGEDVALUE, true,
194: "The tagged value to use for storing the table name");
195: option.setLongOpt("tableTaggedValue");
196: options.addOption(option);
197:
198: option = new Option(COLUMN_TAGGEDVALUE, true,
199: "The tagged value to use for storing the column name");
200: option.setLongOpt("columnTaggedValue");
201: options.addOption(option);
202:
203: option = new Option(OUTPUT_MODEL, true,
204: "Output location to which the result of the transformation will be written");
205: option.setLongOpt("output");
206: options.addOption(option);
207: }
208:
209: /**
210: * Display usage information based upon current command-line option
211: * configuration.
212: */
213: public static void displayHelp() {
214: HelpFormatter formatter = new HelpFormatter();
215: formatter.printHelp(Schema2XMI.class.getName()
216: + " [options] ...]]", "\nOptions:", options, "\n");
217: }
218:
219: /**
220: * Parse a string-array of command-line arguments.
221: * <p>
222: * This will parse the arguments against the configured schema2xmi
223: * command-line options, and return a <code>CommandLine</code> object from
224: * which we can retrieve the command like options.
225: * </p>
226: *
227: * @see <a href="http://jakarta.apache.org/commons/cli/">CLI </a>
228: * @param args The command-line arguments to parse.
229: * @return The <code>CommandLine</code> result.
230: * @throws ParseException If an error occurs while parsing the command-line
231: * options.
232: */
233: public CommandLine parseCommands(String[] args)
234: throws ParseException {
235: CommandLineParser parser = new PosixParser();
236: return parser.parse(options, args);
237: }
238:
239: public static void main(String[] args) {
240: Schema2XMI schema2Xmi = new Schema2XMI();
241: try {
242: CommandLine commandLine = schema2Xmi.parseCommands(args);
243: if (commandLine.hasOption(HELP)
244: || !(commandLine.hasOption(OUTPUT_MODEL)
245: && commandLine.hasOption(DRIVER)
246: && commandLine.hasOption(CONNECTION_URL)
247: && commandLine.hasOption(USER) && commandLine
248: .hasOption(PASSWORD))) {
249: Schema2XMI.displayHelp();
250: } else {
251: String inputModel = commandLine
252: .getOptionValue(INPUT_MODEL);
253: SchemaTransformer transformer = new SchemaTransformer(
254: commandLine.getOptionValue(DRIVER), commandLine
255: .getOptionValue(CONNECTION_URL),
256: commandLine.getOptionValue(USER), commandLine
257: .getOptionValue(PASSWORD));
258:
259: // set the extra options
260: transformer.setXmiVersion(commandLine
261: .getOptionValue(XMI_VERSION));
262: transformer.setTypeMappings(commandLine
263: .getOptionValue(MAPPINGS));
264: transformer.setPackageName(commandLine
265: .getOptionValue(PACKAGE));
266: transformer.setSchema(commandLine
267: .getOptionValue(SCHEMA));
268: transformer.setTableNamePattern(commandLine
269: .getOptionValue(TABLE_PATTERN));
270: transformer.setColumnNamePattern(commandLine
271: .getOptionValue(COLUMN_PATTERN));
272: transformer.setClassStereotypes(commandLine
273: .getOptionValue(CLASS_STEREOTYPES));
274: transformer.setIdentifierStereotypes(commandLine
275: .getOptionValue(IDENTIFIER_STEREOTYPES));
276: transformer.setTableTaggedValue(commandLine
277: .getOptionValue(TABLE_TAGGEDVALUE));
278: transformer.setColumnTaggedValue(commandLine
279: .getOptionValue(COLUMN_TAGGEDVALUE));
280:
281: String outputLocation = commandLine
282: .getOptionValue(OUTPUT_MODEL);
283: transformer.transform(inputModel, outputLocation);
284: }
285: } catch (Throwable throwable) {
286: throwable = getRootCause(throwable);
287: throwable.printStackTrace();
288: }
289: }
290:
291: private static Throwable getRootCause(Throwable th) {
292: Throwable cause = th;
293: if (cause.getCause() != null) {
294: cause = cause.getCause();
295: cause = getRootCause(cause);
296: }
297: return cause;
298: }
299: }
|