View Javadoc

1   package org.apache.ojb.broker.ant;
2   
3   /* Copyright 2005 The Apache Software Foundation.
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  import java.io.File;
19  import java.io.FileWriter;
20  
21  import org.apache.ddlutils.io.DataDtdWriter;
22  import org.apache.ddlutils.model.Database;
23  import org.apache.ojb.broker.metadata.DescriptorRepository;
24  import org.apache.tools.ant.BuildException;
25  import org.apache.tools.ant.Project;
26  import org.apache.tools.ant.Task;
27  
28  /**
29   * The command for creating a data DTD for a given database model.
30   * 
31   * @author Thomas Dudziak
32   * @version $Revision: 1.1 $
33   */
34  public class WriteDtdToFileCommand extends Command
35  {
36      /** The file to output the DTD to. */
37      private File _outputFile;
38  
39      /**
40       * Sets the file to output the DTD to.
41       * 
42       * @param outputFile The output file
43       */
44      public void setOutputFile(File outputFile)
45      {
46          _outputFile = outputFile;
47      }
48  
49      /**
50       * {@inheritDoc}
51       */
52      public boolean isRequiringModel()
53      {
54          return true;
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      public void execute(Task task, Database dbModel, DescriptorRepository objModel) throws BuildException
61      {
62          if (_outputFile == null)
63          {
64              throw new BuildException("No output file specified");
65          }
66          if (_outputFile.exists() && !_outputFile.canWrite())
67          {
68              throw new BuildException("Cannot overwrite output file "+_outputFile.getAbsolutePath());
69          }
70  
71          try
72          {
73              FileWriter           outputWriter = new FileWriter(_outputFile);
74              DataDtdWriter        dtdWriter    = new DataDtdWriter();
75              DdlUtilsDataHandling handling     = new DdlUtilsDataHandling();
76  
77              handling.setModel(dbModel, objModel);
78              handling.getDataDTD(outputWriter);
79              outputWriter.close();
80              task.log("Written DTD to "+_outputFile.getAbsolutePath(), Project.MSG_INFO);
81          }
82          catch (Exception ex)
83          {
84              throw new BuildException("Failed to write to output file "+_outputFile.getAbsolutePath(), ex);
85          }
86      }
87  }