KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > FileUtil


1 /**
2  *
3  * Copyright 2004-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  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.util;
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.BufferedOutputStream JavaDoc;
22 import java.io.BufferedReader JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.FileReader JavaDoc;
28 import java.io.FilenameFilter JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.LinkedList JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.apache.geronimo.interop.SystemException;
37 import org.apache.geronimo.interop.properties.SystemProperties;
38
39
40 public abstract class FileUtil {
41     // private data
42

43     private static int _tempIndex;
44
45     private static Object JavaDoc _tempIndexLock = new Object JavaDoc();
46
47     // public methods
48

49     public static int compareLines(String JavaDoc file1, String JavaDoc file2) {
50         return compareLines(file1, file2, false);
51     }
52
53     public static int compareLines(String JavaDoc file1, String JavaDoc file2, boolean removeTopLevelComments) {
54         String JavaDoc lines1 = readLines(file1, removeTopLevelComments);
55         String JavaDoc lines2 = readLines(file2, removeTopLevelComments);
56         return lines1.compareTo(lines2);
57     }
58
59     public static void copyDir(String JavaDoc fromDir, String JavaDoc toDir) {
60         copyDir(fromDir, toDir, true);
61     }
62
63     public static void copyDir(String JavaDoc fromDir, String JavaDoc toDir, boolean rec) {
64         File JavaDoc dirFile = new File JavaDoc(fromDir);
65         if (!dirFile.exists()) {
66             return;
67         }
68
69         File JavaDoc toDirFile = new File JavaDoc(toDir);
70         if (!toDirFile.exists()) {
71             toDirFile.mkdir();
72         }
73         String JavaDoc[] fileList = dirFile.list();
74         if (fileList != null) {
75             for (int i = 0; i < fileList.length; i++) {
76                 String JavaDoc name = fileList[i];
77                 String JavaDoc from = fromDir + File.separator + name;
78                 String JavaDoc to = toDir + File.separatorChar + name;
79                 File JavaDoc file = new File JavaDoc(from);
80                 if (file.isDirectory()) {
81                     if (rec) {
82                         copyDir(from, to);
83                     }
84                 } else {
85                     copyFile(from, to);
86                 }
87             }
88         }
89     }
90
91     public static void copyFile(String JavaDoc from, String JavaDoc to) {
92         mkdirs(to);
93         try {
94             InputStream JavaDoc input = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(from));
95             OutputStream JavaDoc output = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(to));
96             int c;
97             while ((c = input.read()) != -1) {
98                 output.write(c);
99             }
100             input.close();
101             output.close();
102         } catch (IOException JavaDoc ex) {
103             throw new SystemException(ex);
104         }
105     }
106
107     public static void copyFiles(String JavaDoc fromDir, String JavaDoc toDir, List JavaDoc files) {
108         for (Iterator JavaDoc i = files.iterator(); i.hasNext();) {
109             String JavaDoc file = (String JavaDoc) i.next();
110             copyFile(fromDir + "/" + file, toDir + "/" + file);
111         }
112     }
113
114     public static void deleteDir(String JavaDoc dir) {
115         File JavaDoc dirFile = new File JavaDoc(dir);
116         if (dirFile.exists()) {
117             deleteFilesInDir(dir);
118             dirFile.delete();
119         }
120     }
121
122     public static void deleteFile(String JavaDoc file) {
123         new File JavaDoc(file).delete();
124     }
125
126     public static void deleteFiles(List JavaDoc files) {
127         for (Iterator JavaDoc i = files.iterator(); i.hasNext();) {
128             String JavaDoc fileName = (String JavaDoc) i.next();
129             File JavaDoc file = new File JavaDoc(fileName);
130             file.delete();
131         }
132     }
133
134     public static void deleteFilesInDir(String JavaDoc dir) {
135         File JavaDoc dirFile = new File JavaDoc(dir);
136         String JavaDoc[] fileList = dirFile.list();
137         if (fileList != null) {
138             for (int i = 0; i < fileList.length; i++) {
139                 String JavaDoc path = dir + File.separator + fileList[i];
140                 File JavaDoc file = new File JavaDoc(path);
141                 if (file.isDirectory()) {
142                     deleteDir(path);
143                 }
144                 file.delete();
145             }
146         }
147     }
148
149     public static String JavaDoc expandHomeRelativePath(String JavaDoc path) {
150         if (path.startsWith("~")) {
151             path = SystemProperties.getHome() + path.substring(1);
152         }
153         return path;
154     }
155
156     public static List JavaDoc findFiles(String JavaDoc baseDir) {
157         return findFiles(baseDir, "", true, true, "");
158     }
159
160     public static List JavaDoc findFiles(String JavaDoc baseDir, String JavaDoc pattern) {
161         return findFiles(baseDir, pattern, true, true, "");
162     }
163
164     public static List JavaDoc findFiles(String JavaDoc baseDir, String JavaDoc pattern, boolean fullPath, boolean recursive) {
165         return findFiles(baseDir, pattern, fullPath, recursive, "");
166     }
167
168     private static List JavaDoc findFiles(String JavaDoc baseDir, String JavaDoc pattern, boolean fullPath, boolean recursive, String JavaDoc relativeBase) {
169         if (pattern.equals("**")) {
170             pattern = ""; // Equivalent to "*"
171
recursive = true;
172         }
173         final String JavaDoc prefix = StringUtil.beforeFirst("*", pattern);
174         final String JavaDoc suffix = StringUtil.afterFirst("*", pattern);
175         final boolean finalRecursive = recursive;
176         FilenameFilter JavaDoc filter = new FilenameFilter JavaDoc() {
177             public boolean accept(File JavaDoc file, String JavaDoc name) {
178                 if (finalRecursive && new File JavaDoc(file.getPath() + File.separator + name).isDirectory()) {
179                     return true;
180                 }
181                 return name.startsWith(prefix) && name.endsWith(suffix);
182             }
183         }
184                 ;
185         List JavaDoc list = new LinkedList JavaDoc();
186         File JavaDoc dirFile = new File JavaDoc(baseDir);
187         String JavaDoc[] files = dirFile.list(filter);
188         if (files != null) {
189             int n = files.length;
190             for (int i = 0; i < n; i++) {
191                 String JavaDoc fileName = files[i];
192                 String JavaDoc fullName = baseDir.length() == 0 ? fileName
193                                   : (baseDir + (fullPath ? File.separatorChar : '/') + fileName);
194                 File JavaDoc file = new File JavaDoc(fullName);
195                 if (file.isDirectory()) {
196                     if (recursive) {
197                         String JavaDoc relativeName = relativeBase.length() == 0 ? fileName
198                                               : (relativeBase + '/' + fileName);
199                         list.addAll(findFiles(fullName, pattern, fullPath,
200                                               recursive, relativeName));
201                     }
202                 } else if (fullPath) {
203                     list.add(fullName);
204                 } else {
205                     String JavaDoc relativeName = relativeBase.length() == 0 ? fileName
206                                           : (relativeBase + '/' + fileName);
207                     list.add(relativeName);
208                 }
209             }
210         }
211         return list;
212     }
213
214     public static void mkdir(String JavaDoc dir) {
215         try {
216             new File JavaDoc(dir).mkdirs();
217         } catch (Exception JavaDoc ex) {
218             throw new SystemException(ex);
219         }
220     }
221
222     public static void mkdirs(String JavaDoc file) {
223         try {
224             file = file.replace('/', File.separatorChar);
225             int pos = file.lastIndexOf(File.separatorChar);
226             if (pos != -1) {
227                 String JavaDoc dir = file.substring(0, pos);
228                 mkdir(dir);
229             }
230         } catch (Exception JavaDoc ex) {
231             throw new SystemException(ex);
232         }
233     }
234
235     public static String JavaDoc newTempDir() {
236         String JavaDoc tempDir = SystemProperties.getTempDir();
237         synchronized (_tempIndexLock) {
238             tempDir += "/" + (++_tempIndex);
239         }
240         tempDir = pretty(tempDir);
241         deleteFilesInDir(tempDir);
242         mkdirs(tempDir + "/x.x");
243         return tempDir;
244     }
245
246     public static String JavaDoc pretty(String JavaDoc file) {
247         try {
248             return new File JavaDoc(file).getCanonicalPath();
249         } catch (Exception JavaDoc ignore) {
250             return file.replace('/', File.separatorChar);
251         }
252     }
253
254     /**
255      * * Read all bytes of a file into an array.
256      */

257     public static byte[] readBytes(String JavaDoc fileName) {
258         try {
259             ByteArrayOutputStream JavaDoc bytes = new ByteArrayOutputStream JavaDoc();
260             InputStream JavaDoc input = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(fileName));
261             int c;
262             while ((c = input.read()) != -1) {
263                 bytes.write((byte) c);
264             }
265             input.close();
266             return bytes.toByteArray();
267         } catch (IOException JavaDoc ex) {
268             throw new SystemException(ex);
269         }
270     }
271
272     public static String JavaDoc readLines(String JavaDoc fileName) {
273         return readLines(fileName, false);
274     }
275
276     /**
277      * * Read all lines of a file into a string, optionally removing comments.
278      */

279     public static String JavaDoc readLines(String JavaDoc fileName, boolean removeTopLevelComments) {
280         try {
281             StringBuffer JavaDoc code = new StringBuffer JavaDoc();
282             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new FileReader JavaDoc(fileName));
283             String JavaDoc line;
284             while ((line = input.readLine()) != null) {
285                 if (removeTopLevelComments && line.length() >= 3) {
286                     char c1 = line.charAt(1);
287                     char c2 = line.charAt(2);
288                     if (c1 == '*' && c2 == '*') {
289                         continue;
290                     }
291                 }
292                 code.append(line);
293                 code.append('\n');
294             }
295             input.close();
296             return code.toString();
297         } catch (IOException JavaDoc ex) {
298             throw new SystemException(ex);
299         }
300     }
301 }
302
Popular Tags