1
2
3
4
5
6
7
8
9
10 package org.mule.tools.anttasks;
11
12 import org.apache.tools.ant.BuildException;
13 import org.apache.tools.ant.Location;
14 import org.apache.tools.ant.Task;
15
16 import java.io.File;
17 import java.text.MessageFormat;
18
19
20
21
22 public class FileChecker extends Task
23 {
24 private Location location;
25
26
27
28
29 public FileChecker(Location location)
30 {
31 this.location = location;
32 }
33
34
35
36
37 public void checkFile(File file, String parameterName, boolean isDirectory, boolean needWrite) throws BuildException
38 {
39 if (file == null)
40 {
41 throw new BuildException(MessageFormat.format("No {0} specified", parameterName), getLocation());
42 }
43 if (!file.exists())
44 {
45 throw new BuildException(
46 MessageFormat.format("{0} \"{1}\" does not exist", parameterName, file),
47 getLocation());
48 }
49 if (!file.canRead())
50 {
51 throw new BuildException(
52 MessageFormat.format("{0} \"{1}\" is not readable", parameterName, file),
53 getLocation());
54 }
55 if (needWrite && !file.canWrite())
56 {
57 throw new BuildException(
58 MessageFormat.format("{0} \"{1}\" is not writable", parameterName, file),
59 getLocation());
60 }
61 if (isDirectory)
62 {
63 if (!file.isDirectory())
64 {
65 throw new BuildException(
66 MessageFormat.format("{0} \"{1}\" is not a directory", parameterName, file),
67 getLocation());
68 }
69 }
70 else
71 {
72 if (file.isDirectory())
73 {
74 throw new BuildException(
75 MessageFormat.format("{0} \"{1}\" is a directory", parameterName, file),
76 getLocation());
77 }
78 }
79 }
80 }
81