1 | /* |
2 | * Copyright 2005-2006 The RbUtils Project |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | * |
16 | */ |
17 | |
18 | // $Id: ValidateFile.java,v 1.2 2006/09/16 14:39:46 redfish Exp $ |
19 | |
20 | package org.ktc.rbutils.api.file; |
21 | |
22 | import java.io.File; |
23 | import java.io.FileNotFoundException; |
24 | import org.apache.commons.lang.NullArgumentException; |
25 | import org.apache.commons.lang.Validate; |
26 | |
27 | /** |
28 | * Assists in validating <code>File</code> arguments. |
29 | * @see org.apache.commons.lang.Validate |
30 | * @since RbUtils 0.7.2 |
31 | * @version $Revision: 1.2 $ |
32 | * @author redfish |
33 | */ |
34 | public class ValidateFile extends Validate { |
35 | |
36 | /** Part of the displayed message when a <code>File</code> does not exist. */ |
37 | private static final String NOT_EXIST_MSG = " does not exist"; |
38 | /** TODO Javadoc. */ |
39 | private static final String FILE_MSG = "file"; |
40 | |
41 | /** Dummy constructor. This <b>MUST NOT</b> be instanciated. */ |
42 | protected ValidateFile() { |
43 | // prevents calls from subclass |
44 | throw new UnsupportedOperationException(); |
45 | } |
46 | |
47 | /** |
48 | * Validate an argument, throwing <code>Exception</code> if the <code>File</code> argument |
49 | * is not a directory. |
50 | * @param directory the object to check if not a directory. |
51 | * @throws NullArgumentException if <code>directory</code> is <code>null</code>. |
52 | * @throws FileNotFoundException if <code>directory</code> does not exist. |
53 | * @throws NotDirectoryException if <code>directory</code> is not a directory. |
54 | */ |
55 | public static void isDirectory(final File directory) throws FileNotFoundException { |
56 | if (directory == null) { |
57 | throw new NullArgumentException("directory"); |
58 | } |
59 | else if (!directory.exists()) { |
60 | throw new FileNotFoundException(directory.getAbsolutePath() + NOT_EXIST_MSG); |
61 | } |
62 | else if (!directory.isDirectory()) { |
63 | throw new NotDirectoryException(directory); |
64 | } |
65 | } |
66 | |
67 | // /** |
68 | // * Validate an argument, throwing <code>Exception</code> if the <code>File</code> argument |
69 | // * is not an existing directory. |
70 | // * @param directory the object to check if not an existing directory |
71 | // * @throws NullArgumentException if <code>directory</code> is <code>null</code> |
72 | // * @throws FileNotFoundException if <code>directory</code> does not exist |
73 | // * @throws NotDirectoryException if <code>directory</code> is not a directory |
74 | // */ |
75 | // public static void directoryExists(final File directory) throws FileNotFoundException { |
76 | // isDirectory(directory); |
77 | // if (!directory.exists()) { |
78 | // throw new FileNotFoundException(directory.getAbsolutePath() + NOT_EXIST_MSG); |
79 | // } |
80 | // } |
81 | |
82 | /** |
83 | * Validate an argument, throwing <code>Exception</code> if the <code>File</code> argument |
84 | * does not exist ant is not a file (file as in a file system). |
85 | * @param file the object to check if not a file. |
86 | * @throws NullArgumentException if <code>file</code> is <code>null</code>. |
87 | * @throws FileNotFoundException if <code>file</code> does not exist. |
88 | * @throws NotFileException if <code>file</code> is not a file. |
89 | */ |
90 | public static void isFile(final File file) throws FileNotFoundException { |
91 | if (file == null) { |
92 | throw new NullArgumentException(FILE_MSG); |
93 | } |
94 | else if (!file.exists()) { |
95 | throw new FileNotFoundException(file.getAbsolutePath() + NOT_EXIST_MSG); |
96 | } |
97 | else if (!file.isFile()) { |
98 | throw new NotFileException(file); |
99 | } |
100 | } |
101 | |
102 | /** |
103 | * Validate an argument, throwing <code>NullArgumentException</code> if the <code>File</code> |
104 | * is <code>null</code>. |
105 | * @param file the file to check if <code>null</code> |
106 | * @throws NullArgumentException if <code>file</code> is <code>null</code> |
107 | */ |
108 | public static void notNull(final File file) { |
109 | if (file == null) { |
110 | throw new NullArgumentException(FILE_MSG); |
111 | } |
112 | } |
113 | |
114 | // /** |
115 | // * Validate an argument, throwing <code>Exception</code> if the <code>File</code> argument |
116 | // * is not is not an existing file (file as in a file system). |
117 | // * @param file the object to check if not a file |
118 | // * @throws NullArgumentException if <code>file</code> is <code>null</code> |
119 | // * @throws NotDirectoryException if <code>file</code> is not a file |
120 | // * @throws FileNotFoundException if <code>file</code> does not exist |
121 | // */ |
122 | // public static void fileExists(final File file) throws FileNotFoundException { |
123 | // isFile(file); |
124 | // if (!file.exists()) { |
125 | // throw new FileNotFoundException(file.getAbsolutePath() + NOT_EXIST_MSG); |
126 | // } |
127 | // } |
128 | |
129 | } |