01: package org.objectweb.celtix.tools.utils;
02:
03: import java.io.File;
04: import java.io.FileWriter;
05: import java.io.IOException;
06: import java.io.Writer;
07: import java.util.logging.Logger;
08:
09: import org.objectweb.celtix.common.i18n.Message;
10: import org.objectweb.celtix.common.logging.LogUtils;
11: import org.objectweb.celtix.tools.common.ToolException;
12:
13: public class FileWriterUtil {
14: private static final Logger LOG = LogUtils
15: .getL7dLogger(FileWriterUtil.class);
16: private final File target;
17:
18: public FileWriterUtil(String targetDir) throws ToolException {
19: target = new File(targetDir);
20: if (!(target.exists()) || !(target.isDirectory())) {
21: Message msg = new Message("DIRECTORY_NOT_EXIST", LOG,
22: target);
23: throw new ToolException(msg);
24: }
25: }
26:
27: public Writer getWriter(String packageName, String fileName)
28: throws IOException {
29: File dir = buildDir(packageName);
30: File fn = new File(dir, fileName);
31: if (fn.exists() && !fn.delete()) {
32: throw new IOException(fn
33: + ": Can't delete previous version");
34: }
35: return new FileWriter(fn);
36: }
37:
38: public boolean isCollision(String packageName, String fileName)
39: throws ToolException {
40: File dir = buildDir(packageName);
41: return fileExist(dir, fileName);
42: }
43:
44: private File buildDir(String packageName) {
45: File dir;
46: if (packageName == null) {
47: dir = target;
48: } else {
49: dir = new File(target, toDir(packageName));
50: }
51: if (!dir.exists()) {
52: dir.mkdirs();
53: }
54: return dir;
55: }
56:
57: private boolean fileExist(File dir, String fileName) {
58: return new File(dir, fileName).exists();
59: }
60:
61: private String toDir(String packageName) {
62: return packageName.replace('.', File.separatorChar);
63: }
64:
65: }
|