View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.util.file;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Vector;
22  import java.io.FileReader;
23  import java.io.BufferedReader;
24  import java.io.FileNotFoundException;
25  import java.io.IOException;
26  import java.io.FileOutputStream;
27  import java.io.File;
28  
29  /***
30  * Task to merge files to create the database script
31  *
32  * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
33  * @version $Id: MergeFiles.java,v 1.4 2004/02/23 03:17:53 jford Exp $
34  */
35  public class MergeFiles
36  {
37      protected static List files;
38      protected static String dest_file = null;
39  
40      public static boolean verbose = false;
41  
42      protected static ArrayList baseArray = new ArrayList(1024);
43      protected static String lineSeparator = System.getProperty("line.separator", "\r\n");
44  
45      public static void main(String[] args) throws Exception
46      {
47          MergeFiles main=new MergeFiles();
48  
49          try
50          {
51              if(args.length < 2)
52              {
53                  System.out.println("Usage: java MergeFiles [scratch/drop] c:/temp/all.sql c:/temp/dbpsml.sql c:/temp/populate.sql .... c:/temp/File(n)");
54                  System.out.println("Usage: If scratch is specified then all sql statements starting with DROP will be overlooked.");
55                  System.out.println("Usage: If drop is specified then only sql statements starting with DROP will be added.");
56                  System.out.println("Usage: All the files listed after c:/temp/all.sql will be added to c:/temp/all.sql");
57                  throw new Exception("Incorrect number of arguments supplied");
58              }
59              int file_index = 0;
60              boolean db_from_scratch = false;
61              boolean db_drop = false;
62  
63              if (args[0].equals("scratch"))
64              {
65                  file_index = 1;
66                  db_from_scratch = true;
67              }
68              else if (args[0].equals("drop"))
69              {
70                  file_index = 1;
71                  db_drop = true;                
72              }
73  
74              files = new Vector(args.length - 1 - file_index);
75              dest_file = args[file_index];
76              for (int index = (file_index + 1); index < args.length; index++)
77              {
78                  files.add(args[index]);
79              }
80  
81              for (int index = 0; index < files.size(); index++)
82              {
83                  BufferedReader reader =  new BufferedReader(new FileReader((String)files.get(index)));
84                  String line = null;
85                  int idx = 0;
86                  while((line = reader.readLine()) != null)
87                  {   if (line.startsWith("#"))
88                      {
89                          continue;
90                      }
91                      if (db_from_scratch)
92                      {
93                          if (!(line.startsWith("DROP") || 
94                                line.startsWith("drop") || 
95                                line.startsWith("Drop")))
96                          {
97                              baseArray.add(idx, line);
98                              idx++;
99                          }
100                     }
101                     else if (db_drop)
102                     {
103                         if ( (line.startsWith("DROP") || 
104                               line.startsWith("drop") || 
105                               line.startsWith("Drop")))
106                         {
107                             baseArray.add(idx, line);
108                             idx++;
109                         }                        
110                     }
111                     else
112                     {
113                         baseArray.add(idx, line);
114                         idx++;
115                     }
116                     if(verbose)
117                         System.out.println("While reading baseArray["+idx+"] = " + line);
118                 }
119                 reader.close();
120             }
121             if(verbose)
122                 System.out.println("\nMerge Files\n");
123 
124             baseArray.add("commit;");
125             baseArray.trimToSize();
126 
127             main.writeToFile();
128 
129         }
130         catch(FileNotFoundException ex)
131         {
132             System.err.println(ex.getMessage());
133         }
134         catch(IOException ex)
135         {
136             System.err.println(ex.getMessage());
137         }
138         catch(SecurityException ex)
139         {
140             System.err.println(ex.getMessage());
141         }
142     }
143 
144     public void writeToFile() throws FileNotFoundException, IOException
145     {
146         FileOutputStream writer = null;
147         try
148         {
149             writer = new FileOutputStream(dest_file);
150         }
151         catch (FileNotFoundException ex)
152         {
153             File file = new File(dest_file);
154             writer = new FileOutputStream(file.getPath(), false);
155         }
156         writer.flush();
157         for (int i = 0; i < baseArray.size(); i++)
158         {
159             if(verbose)
160                 System.out.println("While writing baseArray["+i+"] = " + baseArray.get(i));
161             writer.write(((String)baseArray.get(i)).getBytes());
162             writer.write(lineSeparator.getBytes());
163             writer.flush();
164         }
165         writer.close();
166 
167     }
168 
169 
170 }
171