KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > util > FileUtil


1 /*
2  * Copyright 1999-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
18 package org.apache.lenya.util;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import org.apache.log4j.Category;
30
31 /**
32  * @version $Id: FileUtil.java 56071 2004-10-30 19:59:45Z gregor $
33  */

34 public final class FileUtil {
35     private static Category log = Category.getInstance(FileUtil.class);
36
37     /**
38      * DOCUMENT ME!
39      *
40      * @param args DOCUMENT ME!
41      */

42     public static void main(String JavaDoc[] args) {
43         if (args.length == 0) {
44             System.err.println("Usage: java " + new FileUtil().getClass().getName());
45
46             return;
47         }
48
49         if (args[0].equals("--copy")) {
50             if (args.length != 3) {
51                 System.err.println("Usage: --copy source destination");
52
53                 return;
54             }
55
56             try {
57                 System.err.println("cp " + args[1] + " " + args[2]);
58                 copy(args[1], args[2]);
59             } catch (FileNotFoundException JavaDoc e) {
60                 System.err.println(e);
61             } catch (IOException JavaDoc e) {
62                 System.err.println(e);
63             }
64
65             return;
66         }
67
68         if (args[0].equals("--concatPath")) {
69             // FIXME:
70
File JavaDoc file = org.apache.lenya.util.FileUtil.file(
71                     "/root/temp/jpf-1.9/java/lenya/x/xps/samples/invoices/invoices",
72                     "../addresses/lenya.xml");
73             System.out.println(file.getAbsolutePath());
74         } else {
75         }
76     }
77
78     /**
79      * Copying a file
80      *
81      * @param source_name DOCUMENT ME!
82      * @param destination_name DOCUMENT ME!
83      *
84      * @throws FileNotFoundException DOCUMENT ME!
85      * @throws IOException DOCUMENT ME!
86      */

87     public static void copy(String JavaDoc source_name, String JavaDoc destination_name)
88             throws FileNotFoundException JavaDoc, IOException JavaDoc {
89         InputStream JavaDoc source = new FileInputStream JavaDoc(source_name);
90         File JavaDoc destination_file = new File JavaDoc(destination_name);
91         File JavaDoc parent = new File JavaDoc(destination_file.getParent());
92
93         if (!parent.exists()) {
94             parent.mkdirs();
95             log.debug("Directory has been created: " + parent.getAbsolutePath());
96         }
97
98         OutputStream JavaDoc destination = new FileOutputStream JavaDoc(destination_name);
99         byte[] bytes_buffer = new byte[1024];
100         int bytes_read;
101
102         while ((bytes_read = source.read(bytes_buffer)) >= 0) {
103             destination.write(bytes_buffer, 0, bytes_read);
104         }
105     }
106
107     /**
108      * Copy a single File or a complete Directory including its Contents.
109      *
110      * @param src the source File.
111      * @param dest the destiantion File.
112      *
113      * @throws FileNotFoundException if the source File does not exists.
114      * @throws IOException if an error occures in the io system.
115      */

116     public static void copy(File JavaDoc src, File JavaDoc dest) throws FileNotFoundException JavaDoc, IOException JavaDoc {
117
118         if (src.isFile()) {
119             copySingleFile(src, dest);
120         } else {
121             File JavaDoc[] contents = src.listFiles();
122
123             if (contents == null)
124                 return;
125
126             dest.mkdirs();
127
128             for (int i = 0; i < contents.length; i++) {
129                 String JavaDoc destPath = dest.getAbsolutePath() + File.separator + contents[i].getName();
130                 copy(contents[i], new File JavaDoc(destPath));
131             }
132         }
133     }
134
135     /**
136      * Copy a single File.
137      *
138      * @param src the source File.
139      * @param dest the destiantion File.
140      *
141      * @throws FileNotFoundException if the source File does not exists.
142      * @throws IOException if an error occures in the io system.
143      */

144     protected static void copySingleFile(File JavaDoc src, File JavaDoc dest) throws FileNotFoundException JavaDoc,
145             IOException JavaDoc {
146
147         dest.getParentFile().mkdirs();
148         dest.createNewFile();
149         org.apache.avalon.excalibur.io.FileUtil.copyFile(src, dest);
150     }
151
152     /**
153      * Returns a file by specifying an absolute directory name and a relative file name
154      *
155      * @param absoluteDir DOCUMENT ME!
156      * @param relativeFile DOCUMENT ME!
157      *
158      * @return DOCUMENT ME!
159      */

160     public static File JavaDoc file(String JavaDoc absoluteDir, String JavaDoc relativeFile) {
161         File JavaDoc file = new File JavaDoc(fileName(absoluteDir, relativeFile));
162
163         return file;
164     }
165
166     /**
167      * Returns an absolute file name by specifying an absolute directory name and a relative file
168      * name
169      *
170      * @param absoluteDir DOCUMENT ME!
171      * @param relativeFile DOCUMENT ME!
172      *
173      * @return DOCUMENT ME!
174      */

175     public static String JavaDoc fileName(String JavaDoc absoluteDir, String JavaDoc relativeFile) {
176         String JavaDoc fileName = null;
177         String JavaDoc newAbsoluteDir = null;
178
179         if (!(absoluteDir.charAt(absoluteDir.length() - 1) == '/')) {
180             newAbsoluteDir = absoluteDir + "/";
181         } else {
182             newAbsoluteDir = absoluteDir;
183         }
184
185         if (relativeFile.indexOf("../") == 0) {
186             StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(newAbsoluteDir, "/");
187             newAbsoluteDir = "/";
188
189             int numberOfTokens = token.countTokens();
190
191             for (int i = 0; i < (numberOfTokens - 1); i++) {
192                 newAbsoluteDir = newAbsoluteDir + token.nextToken() + "/";
193             }
194
195             String JavaDoc newRelativeFile = relativeFile.substring(3, relativeFile.length());
196             fileName = fileName(newAbsoluteDir, newRelativeFile);
197         } else if (relativeFile.indexOf("./") == 0) {
198             fileName = newAbsoluteDir + relativeFile.substring(2, relativeFile.length());
199         } else {
200             fileName = newAbsoluteDir + relativeFile;
201         }
202
203         return fileName;
204     }
205
206     /**
207      * Returns an absolute file name by specifying an absolute directory name and a relative file
208      * name
209      *
210      * @param absoluteFile DOCUMENT ME!
211      * @param relativeFile DOCUMENT ME!
212      *
213      * @return DOCUMENT ME!
214      */

215     public static String JavaDoc concat(String JavaDoc absoluteFile, String JavaDoc relativeFile) {
216         File JavaDoc file = new File JavaDoc(absoluteFile);
217
218         if (file.isFile()) {
219             return fileName(file.getParent(), relativeFile);
220         }
221
222         return fileName(absoluteFile, relativeFile);
223     }
224
225     /**
226      * Deletes all dirs up to stop dir or if dirs in hirachy are not empty.
227      *
228      * @param start File to delete the parents of. The File itself is not deleted.
229      * @param stop Stop deleting at this dir. This dir is not deleted.
230      * @throws IllegalArgumentException If stop is not a dir or start is not a descending sibling of
231      * stop dir.
232      */

233     public static void deleteParentDirs(File JavaDoc start, File JavaDoc stop) throws IllegalArgumentException JavaDoc {
234         if (!stop.isDirectory())
235             throw new IllegalArgumentException JavaDoc("Stop dir '" + stop.getAbsolutePath()
236                     + "' is not a directory");
237         if (!start.getAbsolutePath().startsWith(stop.getAbsolutePath()))
238             throw new IllegalArgumentException JavaDoc("Start dir '" + start.getAbsolutePath()
239                     + "' is not a descending sibling of stop directory '" + stop.getAbsolutePath()
240                     + "'.");
241
242         File JavaDoc parent = start.getParentFile();
243
244         while (!parent.equals(stop) && parent.delete())
245             parent = parent.getParentFile();
246     }
247 }
Popular Tags