4
* Copyright (c) 2001-2004 by Andy Balaam and the FreeGuide contributors
6
* freeguide-tv.sourceforge.net
8
* Released under the GNU General Public License
9
* with ABSOLUTELY NO WARRANTY.
11
* See the file COPYING for more information.
14
package freeguide.lib.fgspecific;
17
import java.io.BufferedReader;
19
import java.io.FileReader;
22
* A static class to check configuration options.
25
*@created 28 June 2003
29
public class ConfigGuesser {
32
* Description of the Method
34
*@param group Description of the Parameter
35
*@param entry Description of the Parameter
36
*@param val Description of the Parameter
37
*@return Description of the Return Value
39
public static String checkValue(String group, String entry, Object val) {
41
if( group.equals( "misc" ) ) {
43
if( entry.equals("day_start_time") ) {
44
return checkValidTime( (String)val, "the FreeGuide day start" );
45
} else if( entry.equals( "grabber_start_time" ) ) {
46
return checkValidTime( (String)val, "the grabber start time" );
47
} else if( entry.equals( "days_to_grab" ) ) {
48
return checkNumeric( (String)val, "the number of days to grab");
49
} else if( entry.equals( "working_directory" ) ) {
50
return checkDirWriteable( (File)val, "a working directory");
51
} else if( entry.equals( "install_directory" ) ) {
52
return checkDirWriteable( (File)val, "the install directory");
53
} else if( entry.equals( "region" ) ) {
54
return checkNonEmpty( (String)val, "region" );
55
} else if( entry.equals( "browser" ) ) {
57
} else if( entry.equals( "privacy" ) ) {
63
String msg = "Check asked for on an unknown option \"" + group + "."
66
if( FreeGuide.log != null ) {
67
FreeGuide.log.info( msg );
69
System.err.println( msg );
72
// If it's not one of these, don't check , just say it's good.
79
* Description of the Method
81
*@param time Description of the Parameter
82
*@param option Description of the Parameter
83
*@return Description of the Return Value
85
private static String checkValidTime(String time, String whatFor) {
87
if (time != null && time.length() == 5 && time.charAt(2) == ':') {
90
return "The time you gave for " + whatFor
91
+ " is not in valid hh:mm format.";
96
* Description of the Method
98
*@param number Description of the Parameter
99
*@param option Description of the Parameter
100
*@return Description of the Return Value
102
private static String checkNumeric(String number, String whatFor) {
104
if( number == null ) {
105
return "The option for " + whatFor + " is blank.";
109
Integer.parseInt( number );
110
} catch( NumberFormatException e ) {
112
return "The option for " + whatFor + " is not a valid number.";
120
* Description of the Method
122
*@param number Description of the Parameter
123
*@param option Description of the Parameter
124
*@return Description of the Return Value
126
private static String checkNonEmpty(String number, String whatFor) {
128
if( number == null || number.equals("") ) {
129
return "The option for " + whatFor + " is blank.";
136
* Description of the Method
138
*@param dir Description of the Parameter
139
*@param whatFor Description of the Parameter
140
*@return Description of the Return Value
142
private static String checkDirWriteable(File dir, String whatFor) {
143
PreferencesGroup prefs = new PreferencesGroup();
145
dir = new File(prefs.performSubstitutions(dir.toString()));
153
// Check it exists and you can write a file to it
154
File testFile = new File(dir.getPath() + "test.tmp");
155
testFile.createNewFile();
156
if (dir.exists() && testFile.canWrite()) {
162
return "The directory you chose for " + whatFor
163
+ " can't be created or isn't writeable.";
165
} catch (java.io.IOException e) {
167
return "There was an error trying to create the directory you "
168
+ "chose for " + whatFor + ".";
172
private final static String fs = File.separator;