01: package org.apache.ojb.broker.ant;
02:
03: /* Copyright 2005 The Apache Software Foundation.
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * 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.2.2 $
33: */
34: public class WriteDtdToFileCommand extends Command {
35: /** The file to output the DTD to. */
36: private File _outputFile;
37:
38: /**
39: * Sets the file to output the DTD to.
40: *
41: * @param outputFile The output file
42: */
43: public void setOutputFile(File outputFile) {
44: _outputFile = outputFile;
45: }
46:
47: /**
48: * {@inheritDoc}
49: */
50: public boolean isRequiringModel() {
51: return true;
52: }
53:
54: /**
55: * {@inheritDoc}
56: */
57: public void execute(Task task, Database dbModel,
58: DescriptorRepository objModel) throws BuildException {
59: if (_outputFile == null) {
60: throw new BuildException("No output file specified");
61: }
62: if (_outputFile.exists() && !_outputFile.canWrite()) {
63: throw new BuildException("Cannot overwrite output file "
64: + _outputFile.getAbsolutePath());
65: }
66:
67: try {
68: FileWriter outputWriter = new FileWriter(_outputFile);
69: DataDtdWriter dtdWriter = new DataDtdWriter();
70: DdlUtilsDataHandling handling = new DdlUtilsDataHandling();
71:
72: handling.setModel(dbModel, objModel);
73: handling.getDataDTD(outputWriter);
74: outputWriter.close();
75: task.log("Written DTD to " + _outputFile.getAbsolutePath(),
76: Project.MSG_INFO);
77: } catch (Exception ex) {
78: throw new BuildException("Failed to write to output file "
79: + _outputFile.getAbsolutePath(), ex);
80: }
81: }
82: }
|