01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.vfny.geoserver.form.data;
06:
07: import org.apache.struts.action.ActionErrors;
08: import org.apache.struts.action.ActionMessage;
09: import java.io.File;
10: import java.io.FileInputStream;
11: import java.io.IOException;
12:
13: /**
14: * Utility class for common operations used by forms.
15: */
16: public class FormUtils {
17: public static ActionErrors checkFileExistsAndCanRead(File file,
18: ActionErrors errors) {
19: if (!file.exists()) {
20: String actionKey = "error.file.NotExists";
21: Object[] params = new Object[] { file };
22: errors.add("URL", new ActionMessage(actionKey, params));
23:
24: return errors;
25: }
26:
27: //check if we can read it. For some reason file.canRead() doesn't work
28: try {
29: FileInputStream in = new FileInputStream(file);
30: in.read();
31: in.close();
32: } catch (IOException ioe) {
33: String actionKey = "error.file.CantRead";
34: Object[] params = new Object[] { file };
35: errors.add("URL", new ActionMessage(actionKey, params));
36: }
37:
38: return errors;
39: }
40: }
|