001 package org.apache.ojb.broker.ant;
002
003 /* Copyright 2005 The Apache Software Foundation.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 import java.io.File;
019 import java.io.FileWriter;
020
021 import org.apache.ddlutils.io.DataDtdWriter;
022 import org.apache.ddlutils.model.Database;
023 import org.apache.ojb.broker.metadata.DescriptorRepository;
024 import org.apache.tools.ant.BuildException;
025 import org.apache.tools.ant.Project;
026 import org.apache.tools.ant.Task;
027
028 /**
029 * The command for creating a data DTD for a given database model.
030 *
031 * @author Thomas Dudziak
032 * @version $Revision: 1.1 $
033 */
034 public class WriteDtdToFileCommand extends Command
035 {
036 /** The file to output the DTD to. */
037 private File _outputFile;
038
039 /**
040 * Sets the file to output the DTD to.
041 *
042 * @param outputFile The output file
043 */
044 public void setOutputFile(File outputFile)
045 {
046 _outputFile = outputFile;
047 }
048
049 /**
050 * {@inheritDoc}
051 */
052 public boolean isRequiringModel()
053 {
054 return true;
055 }
056
057 /**
058 * {@inheritDoc}
059 */
060 public void execute(Task task, Database dbModel, DescriptorRepository objModel) throws BuildException
061 {
062 if (_outputFile == null)
063 {
064 throw new BuildException("No output file specified");
065 }
066 if (_outputFile.exists() && !_outputFile.canWrite())
067 {
068 throw new BuildException("Cannot overwrite output file "+_outputFile.getAbsolutePath());
069 }
070
071 try
072 {
073 FileWriter outputWriter = new FileWriter(_outputFile);
074 DataDtdWriter dtdWriter = new DataDtdWriter();
075 DdlUtilsDataHandling handling = new DdlUtilsDataHandling();
076
077 handling.setModel(dbModel, objModel);
078 handling.getDataDTD(outputWriter);
079 outputWriter.close();
080 task.log("Written DTD to "+_outputFile.getAbsolutePath(), Project.MSG_INFO);
081 }
082 catch (Exception ex)
083 {
084 throw new BuildException("Failed to write to output file "+_outputFile.getAbsolutePath(), ex);
085 }
086 }
087 }