2 * Copyright 2004-2008 the Seasar Foundation and the Others.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
\r
13 * either express or implied. See the License for the specific language
\r
14 * governing permissions and limitations under the License.
\r
16 package org.seasar.extension.jdbc.gen.internal.util;
\r
18 import java.io.BufferedInputStream;
\r
19 import java.io.BufferedOutputStream;
\r
20 import java.io.File;
\r
21 import java.io.FilenameFilter;
\r
22 import java.io.IOException;
\r
23 import java.util.Arrays;
\r
24 import java.util.Comparator;
\r
26 import org.seasar.framework.exception.IORuntimeException;
\r
27 import org.seasar.framework.util.FileInputStreamUtil;
\r
28 import org.seasar.framework.util.FileOutputStreamUtil;
\r
29 import org.seasar.framework.util.InputStreamUtil;
\r
30 import org.seasar.framework.util.OutputStreamUtil;
\r
33 * {@link File}に関するユーティリティクラスです。
\r
37 public class FileUtil {
\r
42 protected FileUtil() {
\r
55 public static void copyDirectory(File srcDir, File destDir,
\r
56 FilenameFilter filter) {
\r
57 if (!srcDir.isDirectory()) {
\r
58 throw new IllegalArgumentException("srcDir");
\r
60 if (!getCanonicalPath(srcDir).equals(getCanonicalPath(srcDir))
\r
61 && getCanonicalPath(srcDir)
\r
62 .startsWith(getCanonicalPath(srcDir))) {
\r
63 throw new IllegalArgumentException("destDir");
\r
65 copyDir(srcDir, destDir, filter);
\r
69 * ディレクトリを再帰的にコピーします。
\r
78 protected static void copyDir(File srcDir, File destDir,
\r
79 FilenameFilter filter) {
\r
81 File[] srcFiles = srcDir.listFiles(filter);
\r
82 for (File src : srcFiles) {
\r
83 File dest = new File(destDir, src.getName());
\r
84 if (src.isDirectory()) {
\r
85 copyDir(src, dest, filter);
\r
87 copyFile(src, dest);
\r
100 protected static void copyFile(File src, File dest) {
\r
101 BufferedInputStream in = null;
\r
102 BufferedOutputStream out = null;
\r
104 in = new BufferedInputStream(FileInputStreamUtil.create(src));
\r
105 out = new BufferedOutputStream(FileOutputStreamUtil.create(dest));
\r
106 byte[] buf = new byte[1024];
\r
108 while (-1 < (length = in.read(buf))) {
\r
109 out.write(buf, 0, length);
\r
112 } catch (IOException e) {
\r
113 throw new IORuntimeException(e);
\r
115 InputStreamUtil.close(in);
\r
116 OutputStreamUtil.close(out);
\r
126 public static void deleteDirectory(File dir) {
\r
127 if (!dir.isDirectory()) {
\r
128 throw new IllegalArgumentException("dir");
\r
130 if (!dir.exists()) {
\r
131 throw new IllegalArgumentException("dir");
\r
137 * ディレクトリを再帰的に削除します。
\r
142 protected static void deleteDir(File dir) {
\r
143 for (File file : dir.listFiles()) {
\r
144 if (file.isDirectory()) {
\r
155 * この抽象パス名の正規のパス名文字列を返します。
\r
159 * @return この抽象パス名と同じファイルまたはディレクトリを示す正規パス名文字列
\r
161 public static String getCanonicalPath(File file) {
\r
163 return file.getCanonicalPath();
\r
164 } catch (IOException e) {
\r
165 throw new IORuntimeException(e);
\r
170 * この抽象パス名の正規の形式を返します。
\r
174 * @return この抽象パス名と同じファイルまたはディレクトリを示す正規の形式
\r
176 public static File getCanonicalFile(File file) {
\r
178 return file.getCanonicalFile();
\r
179 } catch (IOException e) {
\r
180 throw new IORuntimeException(e);
\r
189 * @param comparator
\r
192 public static void traverseDirectory(File dir, FilenameFilter filter,
\r
193 Comparator<File> comparator, FileHandler handler) {
\r
194 if (!dir.exists()) {
\r
197 File[] files = dir.listFiles(filter);
\r
198 if (files == null) {
\r
201 Arrays.sort(files, comparator);
\r
202 for (File file : files) {
\r
203 if (file.isDirectory()) {
\r
204 traverseDirectory(file, filter, comparator, handler);
\r
206 handler.handle(file);
\r
215 * @param packageName
\r
217 * @param shortClassName
\r
221 public static File createJavaFile(File baseDir, String packageName,
\r
222 String shortClassName) {
\r
224 if (packageName == null) {
\r
225 packageDir = baseDir;
\r
227 packageDir = new File(baseDir, packageName.replace('.',
\r
228 File.separatorChar));
\r
230 return new File(packageDir, shortClassName + ".java");
\r
234 * ファイルを扱うインタフェースです・
\r
238 public interface FileHandler {
\r
245 void handle(File file);
\r