001: /*
002: * Copyright 2006 Le Duc Bao, Ralf Joachim
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.castor.ddlgen;
017:
018: import java.io.FileOutputStream;
019: import java.io.PrintWriter;
020: import java.util.Properties;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.castor.mapping.MappingUnmarshaller;
025: import org.exolab.castor.mapping.Mapping;
026: import org.exolab.castor.util.CommandLineOptions;
027:
028: /**
029: * Main Program.
030: *
031: * @author <a href="mailto:leducbao AT gmail DOT com">Le Duc Bao</a>
032: * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
033: * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
034: * @since 1.1
035: */
036: public final class Main {
037: //--------------------------------------------------------------------------
038:
039: /** The <a href="http://jakarta.apache.org/commons/logging/">Jakarta Commons
040: * Logging </a> instance used for all logging. */
041: private static final Log LOG = LogFactory.getLog(Main.class);
042:
043: //--------------------------------------------------------------------------
044:
045: /**
046: * @param args options
047: */
048: public static void main(final String[] args) {
049: CommandLineOptions allOptions = new CommandLineOptions();
050:
051: allOptions.addFlag("m", "mapping.xml", "input mapping file.");
052: allOptions.addFlag("c", "ddl.properties",
053: "configuration file.", true);
054: allOptions.addFlag("d", "mysql.properties",
055: "specific database configuration file.", true);
056: allOptions.addFlag("e", "MySQL",
057: "database engine, for example MySQL, Oracle", true);
058: allOptions.addFlag("o", "output.sql", "output ddl file", true);
059: allOptions.addFlag("h", "", "Displays this help screen.", true);
060:
061: // Process the specified command line options.
062: Properties options = allOptions.getOptions(args);
063:
064: String mappingName = options.getProperty("m");
065: String ddlName = options.getProperty("o");
066: String globalConfig = options.getProperty("c");
067: String specificConfig = options.getProperty("d");
068: String engine = options.getProperty("e");
069:
070: if (options.getProperty("h") != null || mappingName == null) {
071: PrintWriter pw = new PrintWriter(System.out, true);
072: allOptions.printHelp(pw);
073: pw.flush();
074: System.exit(0);
075: }
076:
077: // Verify and adjust output file.
078: if (ddlName == null) {
079: ddlName = mappingName.replaceAll(".xml", ".sql");
080: }
081:
082: LOG.info("mapping file: " + mappingName);
083: LOG.info("ddl output file: " + ddlName);
084: LOG.info("global configuration file: " + globalConfig);
085: LOG.info("specific database configuration file: "
086: + specificConfig);
087: LOG.info("database: " + engine);
088:
089: // Create generator and generate ddl.
090: try {
091: Generator generator = GeneratorFactory.createDDLGenerator(
092: engine, globalConfig, specificConfig);
093:
094: Mapping mapping = new Mapping();
095: mapping.loadMapping(mappingName);
096: new MappingUnmarshaller().loadMappingOnly(mapping);
097:
098: generator.setMapping(mapping);
099: generator.generateDDL(new FileOutputStream(ddlName));
100: } catch (Exception ex) {
101: LOG.error("Error: " + ex.getMessage(), ex);
102: }
103: }
104:
105: //--------------------------------------------------------------------------
106:
107: /**
108: * Hide utility class constructor.
109: */
110: private Main() {
111: super ();
112: }
113:
114: //--------------------------------------------------------------------------
115: }
|