1 | |
package org.apache.ojb.broker.metadata.torque; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import org.apache.ojb.broker.metadata.DescriptorRepository; |
19 | |
import org.apache.ojb.broker.metadata.RepositoryPersistor; |
20 | |
|
21 | |
import java.io.FileWriter; |
22 | |
import java.io.IOException; |
23 | |
import java.util.Iterator; |
24 | |
|
25 | |
public class TorqueRepositoryGenerator |
26 | |
{ |
27 | |
|
28 | |
private TorqueTablePreprocessor tablePreprocessor; |
29 | |
private TorqueTableGenerator tableGenerator; |
30 | |
|
31 | |
public TorqueRepositoryGenerator(String xmlInputFile, boolean ignoreAutoIncrement) throws Exception |
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 xmlOutputFile, String databaseName, String indexTablespaceName) throws Exception |
42 | |
{ |
43 | |
Iterator tableDescriptorIterators = this.tablePreprocessor.getStandardTables().values().iterator(); |
44 | |
StringBuffer buffer = new StringBuffer(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 databaseName, String indexTablespaceName, StringBuffer 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 xmlOutputFile, StringBuffer buffer) throws IOException |
70 | |
{ |
71 | |
FileWriter writer = new FileWriter(xmlOutputFile); |
72 | |
writer.write(buffer.toString()); |
73 | |
writer.flush(); |
74 | |
writer.close(); |
75 | |
} |
76 | |
|
77 | |
public static void main(String args[]) throws Exception |
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 | |
} |