KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > packageplugin > utils > CopyFileSetUtil


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: CopyFileSet.java 15:05:18 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.tools.packageplugin.utils;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import org.apache.commons.io.FileUtils;
28
29 /**
30  * Copy a fileset to a directory
31  *
32  * @author ddesjardins - eBMWebsourcing
33  */

34 public final class CopyFileSetUtil {
35
36     private CopyFileSetUtil() {
37         // Do nothing
38
}
39
40     /**
41      * Copy a fileset
42      *
43      * @param baseDir
44      * base directory
45      * @param toDir
46      * directory to copy to
47      * @param includePattern
48      * file pattern
49      * @throws IOException
50      */

51     public static void copyFileSetToDirectory(File JavaDoc baseDir, File JavaDoc toDir,
52         String JavaDoc includePattern, boolean keepDirectory) throws IOException JavaDoc {
53         copyFileSetToDirectory(baseDir, toDir, includePattern, "",
54             keepDirectory);
55     }
56
57     /**
58      * Copy a fileset
59      *
60      * @param baseDir
61      * base directory
62      * @param toDir
63      * directory to copy to
64      * @param includePattern
65      * file pattern to include
66      * @param excludePattern
67      * file pattern to exclude
68      * @throws IOException
69      */

70     public static void copyFileSetToDirectory(File JavaDoc baseDir, File JavaDoc toDir,
71         String JavaDoc includePattern, String JavaDoc excludePattern, boolean keepDirectory)
72         throws IOException JavaDoc {
73         if (baseDir.exists()) {
74             String JavaDoc dirName = "**";
75             String JavaDoc excludeDirName = "";
76             String JavaDoc filePattern = includePattern;
77             if (includePattern.startsWith("**")) {
78                 if (includePattern.indexOf(File.separator) > -1) {
79                     dirName = includePattern.substring(0, includePattern
80                         .indexOf(File.separator));
81                 } else {
82                     dirName = "";
83                 }
84                 filePattern = includePattern.substring(3);
85             } else {
86                 dirName = "";
87             }
88             if (!"".equals(excludePattern)
89                 && excludePattern.indexOf(File.separator) > -1) {
90                 excludeDirName = excludePattern.substring(0, excludePattern
91                     .indexOf(File.separator));
92             } else {
93                 excludeDirName = excludePattern;
94             }
95             for (File JavaDoc file : baseDir.listFiles()) {
96                 if (file.isDirectory()
97                     && ("**".equals(dirName) || file.getName().equals(dirName))
98                     && (!file.getName().equals(excludeDirName))) {
99                     recurseDirectory(baseDir, file, toDir, includePattern,
100                         excludePattern, keepDirectory);
101                 } else if (file.isFile()
102                     && file.getName().contains(filePattern.replace("*", ""))) {
103                     FileUtils.copyFileToDirectory(file, toDir);
104                 }
105             }
106         }
107     }
108
109     /**
110      * Recurse a directory
111      *
112      * @param dir
113      * directory to recurse
114      * @param toDir
115      * directory to copy to
116      * @param includePattern
117      * include pattern
118      * @param excludePattern
119      * exclude pattern
120      * @throws IOException
121      */

122     private static void recurseDirectory(File JavaDoc baseDir, File JavaDoc dir, File JavaDoc toDir,
123         String JavaDoc includePattern, String JavaDoc excludePattern, boolean keepDirectory)
124         throws IOException JavaDoc {
125         String JavaDoc dirName = "**";
126         String JavaDoc filePattern = includePattern;
127         String JavaDoc lastDirName = includePattern;
128         String JavaDoc excludeDirName = "";
129         if (includePattern.startsWith("**")) {
130             dirName = includePattern.substring(0, includePattern
131                 .indexOf(File.separator));
132             filePattern = includePattern.substring(3);
133             if (filePattern.indexOf(File.separator) > -1) {
134                 lastDirName = filePattern.substring(0, filePattern
135                     .indexOf(File.separator));
136             }
137         } else {
138             if (includePattern.indexOf(File.separator) > -1) {
139                 dirName = includePattern.substring(0, includePattern
140                     .indexOf(File.separator));
141                 filePattern = includePattern.substring(dirName.length() + 1);
142             }
143         }
144         if (!"".equals(excludePattern)
145             && excludePattern.indexOf(File.separator) > -1) {
146             excludeDirName = excludePattern.substring(0, excludePattern
147                 .indexOf(File.separator));
148         } else {
149             excludeDirName = excludePattern;
150         }
151         if (dir.getName().equals(lastDirName)) {
152             filePattern = filePattern.substring(lastDirName.length() + 1);
153             includePattern = filePattern;
154             if (includePattern.indexOf(File.separator) > -1) {
155                 dirName = includePattern.substring(0, includePattern
156                     .indexOf(File.separator));
157                 filePattern = includePattern.substring(dirName.length() + 1);
158             }
159         }
160         for (File JavaDoc file : dir.listFiles()) {
161             if (file.isDirectory()
162                 && ("**".equals(dirName) || file.getName().equals(dirName))
163                 && (!file.getName().equals(excludeDirName))) {
164                 recurseDirectory(baseDir, file, toDir, includePattern,
165                     excludePattern, keepDirectory);
166             } else if (file.isFile()
167                 && file.getName().contains(filePattern.replace("*", ""))) {
168                 String JavaDoc fileDir = file.getAbsolutePath().substring(
169                     baseDir.getAbsolutePath().length());
170                 fileDir = fileDir.substring(0, fileDir.length()
171                     - file.getName().length());
172                 if (keepDirectory) {
173                     new File JavaDoc(toDir
174                         + fileDir).mkdir();
175                     FileUtils.copyFileToDirectory(file, new File JavaDoc(toDir
176                         + fileDir));
177                 } else {
178                     FileUtils.copyFileToDirectory(file, toDir);
179                 }
180             }
181         }
182     }
183
184 }
185
Popular Tags