1 22 package org.objectweb.petals.tools.packageplugin.utils; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 27 import org.apache.commons.io.FileUtils; 28 29 34 public final class CopyFileSetUtil { 35 36 private CopyFileSetUtil() { 37 } 39 40 51 public static void copyFileSetToDirectory(File baseDir, File toDir, 52 String includePattern, boolean keepDirectory) throws IOException { 53 copyFileSetToDirectory(baseDir, toDir, includePattern, "", 54 keepDirectory); 55 } 56 57 70 public static void copyFileSetToDirectory(File baseDir, File toDir, 71 String includePattern, String excludePattern, boolean keepDirectory) 72 throws IOException { 73 if (baseDir.exists()) { 74 String dirName = "**"; 75 String excludeDirName = ""; 76 String 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 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 122 private static void recurseDirectory(File baseDir, File dir, File toDir, 123 String includePattern, String excludePattern, boolean keepDirectory) 124 throws IOException { 125 String dirName = "**"; 126 String filePattern = includePattern; 127 String lastDirName = includePattern; 128 String 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 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 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 (toDir 174 + fileDir).mkdir(); 175 FileUtils.copyFileToDirectory(file, new File (toDir 176 + fileDir)); 177 } else { 178 FileUtils.copyFileToDirectory(file, toDir); 179 } 180 } 181 } 182 } 183 184 } 185 | Popular Tags |