1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
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 }