001    package org.apache.torque.task;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.BufferedWriter;
023    import java.io.FileWriter;
024    
025    import org.apache.tools.ant.BuildException;
026    import org.apache.tools.ant.Project;
027    import org.apache.tools.ant.Task;
028    
029    import org.apache.torque.engine.database.model.Database;
030    import org.apache.torque.engine.database.transform.SQLToAppData;
031    
032    /**
033     * An ant task for creating an xml schema from an sql schema
034     *
035     * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
036     * @author <a href="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
037     * @version $Id: TorqueSQLTransformTask.java,v 1.1 2007-10-21 07:57:26 abyrne Exp $
038     */
039    public class TorqueSQLTransformTask extends Task
040    {
041        /** SQL input file. */
042        private String inputFile;
043    
044        /** XML descriptor output file. */
045        private String outputFile;
046    
047        /**
048         * Get the current input file
049         *
050         * @return the input file
051         */
052        public String getInputFile()
053        {
054            return inputFile;
055        }
056    
057        /**
058         * Set the sql input file.  This file must exist
059         *
060         * @param v the input file
061         */
062        public void setInputFile(String v)
063        {
064            inputFile = v;
065        }
066    
067        /**
068         * Get the current output file.
069         *
070         * @return the output file
071         */
072        public String getOutputFile()
073        {
074            return outputFile;
075        }
076    
077        /**
078         * Set the current output file.  If the file does not
079         * exist it will be created.  If the file exists all
080         * it's contents will be replaced.
081         *
082         * @param v the output file
083         */
084        public void setOutputFile (String v)
085        {
086            outputFile = v;
087        }
088    
089        /**
090         * Execute the task.
091         *
092         * @throws BuildException Any exceptions caught during procssing will be
093         *         rethrown wrapped into a BuildException
094         */
095        public void execute() throws BuildException
096        {
097            try
098            {
099                log("Parsing SQL Schema", Project.MSG_INFO);
100    
101                SQLToAppData sqlParser = new SQLToAppData(inputFile);
102                Database app = sqlParser.execute();
103    
104                log("Preparing to write xml schema", Project.MSG_INFO);
105                FileWriter fr = new FileWriter(outputFile);
106                BufferedWriter br = new BufferedWriter (fr);
107    
108                br.write(app.toString());
109    
110                log("Writing xml schema", Project.MSG_INFO);
111    
112                br.flush();
113                br.close();
114            }
115            catch (Exception e)
116            {
117                throw new BuildException(e);
118            }
119        }
120    }