View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/xmldb/FileSplitter.java,v 1.9 2004/10/03 18:53:22 hkollmann Exp $
3    * $Revision: 1.9 $
4    * $Date: 2004/10/03 18:53:22 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.xmldb;
25  
26  import java.io.*;
27  
28  
29  
30  /***
31   * DOCUMENT ME!
32   *
33   * @version $Revision: 1.9 $
34   * @author $author$
35   */
36  public class FileSplitter {
37     File           fDestDir;
38     File           fSource;
39  
40     /***
41      * Creates a new FileSplitter object.
42      *
43      * @param s DOCUMENT ME!
44      * @param d DOCUMENT ME!
45      */
46     public FileSplitter(File s,
47                         File d) {
48        this.fSource  = s;
49        this.fDestDir = d;
50  
51        testFiles();
52     }
53  
54  
55     /***
56      * Creates a new FileSplitter object.
57      *
58      * @param sourceFile DOCUMENT ME!
59      * @param destDir DOCUMENT ME!
60      */
61     public FileSplitter(String sourceFile,
62                         String destDir) {
63        this.fSource  = new File(sourceFile);
64        this.fDestDir = new File(destDir);
65  
66        testFiles();
67     }
68  
69     /***
70      * DOCUMENT ME!
71      *
72      * @param args DOCUMENT ME!
73      */
74     public static void main(String[] args) {
75        if (args.length != 2) {
76           System.out.println("usage: java FileSplitter sourceFile destinationDirectory\n\nexample:\njava FileSplitter source.jsp c://tomcat//webapps//your_app//");
77           System.exit(1);
78        }
79  
80        new FileSplitter(args[0], args[1]).splitFile();
81     }
82  
83  
84     /***
85      * DOCUMENT ME!
86      */
87     public void splitFile() {
88        try {
89           BufferedReader in       = new BufferedReader(new FileReader(fSource));
90           BufferedWriter out      = null;
91           String         line     = null;
92           String         fileName = null;
93  
94           while ((line = in.readLine()) != null) {
95              if (line.startsWith("//--file")) {
96                 int preBegin = line.indexOf('\"');
97                 int postEnd = line.indexOf('\"', preBegin + 1);
98  
99                 fileName = line.substring(preBegin + 1, postEnd);
100 
101                System.out.println("current file:" + fileName);
102 
103                if (out != null) {
104                   out.close();
105                }
106 
107                out = new BufferedWriter(new FileWriter(new File(fDestDir,
108                                                                 fileName)));
109             } else {
110                if (out != null) {
111                   out.write(line);
112                   out.newLine();
113                }
114             }
115          }
116 
117          if (out != null) {
118             out.close();
119          }
120       } catch (IOException ioe) {
121          System.out.println("ERROR: " + ioe.toString());
122       }
123    }
124 
125 
126    private void testFiles() {
127       // preparing INPUT
128       if (!fSource.exists()) {
129          System.out.println("ERROR: source file not found");
130          System.exit(1);
131       }
132 
133       if (!fSource.canRead()) {
134          System.out.println("ERROR: source file not readable for this process");
135          System.exit(1);
136       }
137 
138       // preparing OUTPUT
139       if (!fDestDir.exists()) {
140          System.out.println("ERROR: destination directory not found");
141          System.exit(1);
142       }
143 
144       if (!fDestDir.isDirectory()) {
145          System.out.println("ERROR: destination is not a directory");
146          System.exit(1);
147       }
148 
149       if (!fDestDir.canWrite()) {
150          System.out.println("ERROR: destination directory not writable for this process");
151          System.exit(1);
152       }
153    }
154 }