Coverage Report - org.apache.ojb.broker.metadata.torque.TorqueRepositoryGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
TorqueRepositoryGenerator
N/A
N/A
1.8
 
 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;
 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  
 }