KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > torque > TorqueRepositoryGenerator


1 package org.apache.ojb.broker.metadata.torque;
2
3 /* Copyright 2002-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 org.apache.ojb.broker.metadata.DescriptorRepository;
19 import org.apache.ojb.broker.metadata.RepositoryPersistor;
20
21 import java.io.FileWriter JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 public class TorqueRepositoryGenerator
26 {
27
28     private TorqueTablePreprocessor tablePreprocessor;
29     private TorqueTableGenerator tableGenerator;
30
31     public TorqueRepositoryGenerator(String JavaDoc xmlInputFile, boolean ignoreAutoIncrement) throws Exception JavaDoc
32     {
33         RepositoryPersistor persistor = new RepositoryPersistor();
34         DescriptorRepository repository = persistor.readDescriptorRepository(xmlInputFile);
35
36         this.tablePreprocessor = new TorqueTablePreprocessor(repository);
37         this.tablePreprocessor.buildStandardTables();
38         this.tableGenerator = new TorqueTableGenerator(repository, ignoreAutoIncrement);
39     }
40
41     public void generateTorqueRepository(String JavaDoc xmlOutputFile, String JavaDoc databaseName, String JavaDoc indexTablespaceName) throws Exception JavaDoc
42     {
43         Iterator JavaDoc tableDescriptorIterators = this.tablePreprocessor.getStandardTables().values().iterator();
44         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(4096);
45
46         generateDatabaseHeader(databaseName, indexTablespaceName, buffer);
47
48         while (tableDescriptorIterators.hasNext())
49         {
50             this.tableGenerator.generateStandardTable((TableDescriptor) tableDescriptorIterators.next(), buffer, indexTablespaceName);
51         }
52
53         this.tableGenerator.generateMappingTables(buffer, indexTablespaceName);
54         buffer.append("</database>\n");
55         generateOutputFile(xmlOutputFile, buffer);
56     }
57
58     private void generateDatabaseHeader(String JavaDoc databaseName, String JavaDoc indexTablespaceName, StringBuffer JavaDoc buffer)
59     {
60         buffer.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\" ?>\n");
61         buffer.append("<!DOCTYPE database>\n\n");
62         buffer.append("<database name=\"");
63         buffer.append(databaseName);
64         buffer.append("\" indexTablespace=\"");
65         buffer.append(indexTablespaceName);
66         buffer.append("\">\n");
67     }
68
69     private void generateOutputFile(String JavaDoc xmlOutputFile, StringBuffer JavaDoc buffer) throws IOException JavaDoc
70     {
71         FileWriter JavaDoc writer = new FileWriter JavaDoc(xmlOutputFile);
72         writer.write(buffer.toString());
73         writer.flush();
74         writer.close();
75     }
76
77     public static void main(String JavaDoc args[]) throws Exception JavaDoc
78     {
79         if (args.length >= 4 && args.length <= 5)
80         {
81             boolean ignoreAutoIncrement = (args.length == 5 && args[4].equalsIgnoreCase("true"));
82             TorqueRepositoryGenerator generator = new TorqueRepositoryGenerator(args[0], ignoreAutoIncrement);
83             generator.generateTorqueRepository(args[1], args[2], args[3]);
84         }
85         else
86         {
87             System.out.println("[TorqueRepositoryGenerator] Usage: inputFile outputFile databaseName indexTablespaceName (ignoreAutoIncrement)");
88         }
89     }
90
91 }
92
Popular Tags