View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  
18  package com.gridsystems.config.tools;
19  
20  import java.io.File;
21  
22  /**
23   * Verifies that a String is a valid path to a file that exists.
24   *
25   * Verifies that a String is a valid path to a file that exists. Also checks
26   * the file type. Subclasses of this class can refine the filter by
27   * reimplementing the checkPattern(File) method.
28   *
29   * @author SJM
30   */
31  public class FileVerifier extends TextVerifier {
32    /**
33     * Errors detected by this verifier.
34     */
35    public static final String ID_ERROR = "InvalidFile";
36  
37    /**
38     * Allows both files and directories.
39     */
40    public static final int FILES_AND_DIRECTORIES = 0;
41    /**
42     * Allows only files.
43     */
44    public static final int FILES_ONLY = 1;
45    /**
46     * Allows only directories.
47     */
48    public static final int DIRECTORIES_ONLY = 2;
49  
50  
51    /**
52     * Allows paths to existing and non-existing files.
53     */
54    public static final int FILE_ALL = 0;
55  
56    /**
57     * Allows paths to existing files.
58     */
59    public static final int FILE_EXIST = 1;
60  
61    /**
62     * Allows paths to non-existing files.
63     */
64    public static final int FILE_NOT_EXIST = 2;
65  
66    /**
67     * Selection mode.
68     */
69    private int mode;
70  
71    /**
72     * Filename filtering mode.
73     */
74    private int existenceConstraint;
75  
76    /**
77     * Error identifier.
78     */
79    protected String id;
80  
81    /**
82     * Creates an instance.
83     *
84     * @param support The ErrorSupport instance
85     * @param id      The error identifier
86     */
87    public FileVerifier(ErrorSupport support, String id) {
88      this(support, id, FILES_AND_DIRECTORIES, FILE_ALL);
89    }
90  
91    /**
92     * Creates an instance.
93     *
94     * @param support    The Error support instance
95     * @param id         The error identifier
96     * @param mode       The type of files that will be accepted.
97     *                   Valid values are {@link #FILES_AND_DIRECTORIES},
98     *                   {@link #DIRECTORIES_ONLY}, and {@link #FILES_ONLY}.
99     *                   If it is neither of them, it defaults to
100    *                   {@link #FILES_AND_DIRECTORIES}
101    * @param constraint The existence checks that the files must pass to be
102    *                   accepted. It can be {@link #FILE_ALL},
103    *                   {@link #FILE_EXIST} and {@link #FILE_NOT_EXIST}. If it is
104    *                   neither of them, it defaults to {@link #FILE_ALL}
105    */
106   public FileVerifier(ErrorSupport support, String id, int mode, int constraint) {
107     super(support);
108     this.id = id;
109 
110     switch (mode) {
111       case FileVerifier.FILES_AND_DIRECTORIES:
112       case FileVerifier.DIRECTORIES_ONLY:
113       case FileVerifier.FILES_ONLY:
114         this.mode = mode;
115         break;
116       default:
117     }
118 
119     switch (constraint) {
120       case FileVerifier.FILE_ALL:
121       case FileVerifier.FILE_EXIST:
122       case FileVerifier.FILE_NOT_EXIST:
123         this.existenceConstraint = constraint;
124         break;
125       default:
126     }
127   }
128 
129   /**
130    * {@inheritDoc}
131    */
132   public boolean verify(String text) {
133     File file = new File(text);
134 
135     boolean valid;
136     if (file.exists()) {
137       valid = this.existenceConstraint != FILE_NOT_EXIST
138               && checkPattern(file) && checkMode(file);
139     } else {
140       valid = this.existenceConstraint != FILE_EXIST;
141     }
142 
143     updateError(id, !valid);
144     return valid;
145   }
146 
147   /**
148    * @see TextVerifier#cleanErrors()
149    */
150   public void cleanErrors() {
151     updateError(id, false);
152   }
153   /**
154    * Returns true if the file matches the veryfication mode.
155    *
156    * @param f The file to check
157    * @return true if the file matches the current mode
158    */
159   private boolean checkMode(File f) {
160     return mode == FILES_AND_DIRECTORIES
161            || (mode == FILES_ONLY && f.isFile())
162            || f.isDirectory();
163   }
164 
165   /**
166    * Checks the restrictions in the file name/path.
167    *
168    * Checks the restrictions in the file name/path. This basic implementation
169    * allways returns true; subclasses of FileVerifier that need pattern match
170    * can reimplement this method. The files that cause this method to return
171    * false will cause the verification to fail.
172    *
173    * @param file the File object to check.
174    *
175    * @return a boolean that is true if and only if the file matches the desired
176    * pattern/s.
177    */
178   public boolean checkPattern(File file) {
179     return true;
180   }
181 }