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 473814 2006-11-11 22:30:30Z tv $
038: */
039: public class TorqueSQLTransformTask extends Task {
040: /** SQL input file. */
041: private String inputFile;
042:
043: /** XML descriptor output file. */
044: private String outputFile;
045:
046: /**
047: * Get the current input file
048: *
049: * @return the input file
050: */
051: public String getInputFile() {
052: return inputFile;
053: }
054:
055: /**
056: * Set the sql input file. This file must exist
057: *
058: * @param v the input file
059: */
060: public void setInputFile(String v) {
061: inputFile = v;
062: }
063:
064: /**
065: * Get the current output file.
066: *
067: * @return the output file
068: */
069: public String getOutputFile() {
070: return outputFile;
071: }
072:
073: /**
074: * Set the current output file. If the file does not
075: * exist it will be created. If the file exists all
076: * it's contents will be replaced.
077: *
078: * @param v the output file
079: */
080: public void setOutputFile(String v) {
081: outputFile = v;
082: }
083:
084: /**
085: * Execute the task.
086: *
087: * @throws BuildException Any exceptions caught during procssing will be
088: * rethrown wrapped into a BuildException
089: */
090: public void execute() throws BuildException {
091: try {
092: log("Parsing SQL Schema", Project.MSG_INFO);
093:
094: SQLToAppData sqlParser = new SQLToAppData(inputFile);
095: Database app = sqlParser.execute();
096:
097: log("Preparing to write xml schema", Project.MSG_INFO);
098: FileWriter fr = new FileWriter(outputFile);
099: BufferedWriter br = new BufferedWriter(fr);
100:
101: br.write(app.toString());
102:
103: log("Writing xml schema", Project.MSG_INFO);
104:
105: br.flush();
106: br.close();
107: } catch (Exception e) {
108: throw new BuildException(e);
109: }
110: }
111: }
|