Java Reference
In-Depth Information
if (field.getType() == String.class) {
field.set(this, value);
} else if (field.getType() == List.class) {
List<String> list = (List<String>) field.get(this);
list.add(value);
} else {
int x = Integer.parseInt(value);
field.set(this, x);
}
}
}
The
load
and
loadLine
methods of the
SpiderOptions
class are used to
load data from a spider configuration file. The
load
method works by reading a simple
configuration file that consists of name value pairs. For each name that is found in the file,
the
loadLine
method uses Java reflection to determine how to set the variable in a
SpiderOptions
object. This allows us to quickly add new variables by adding them
to the
SpiderOptions
object. No additional parsing code is needed for any new prop-
erty.
Reading a Configuration File
The
load
method reads the spider configuration file. For each line encountered by the
load method, the
loadLine
method is called. The
load
method begins by opening the
file and creating a
BufferedReader
. The
BufferedReader
will allow us to read
the file on a line-by-line basis.
FileReader f = new FileReader(new File(inputFile));
BufferedReader r = new BufferedReader(f);
Next, a single line is read from the file. If the line is
null
, then the program is done
reading the file.
String line;
while ((line = r.readLine()) != null) {
The line that was read is passed on to the
parseLine
method. Any exceptions that
can occur are thrown as a
SpiderException
.
try {
parseLine(line);
} catch (IllegalArgumentException e) {
throw (new SpiderException(e));
} catch (SecurityException e) {
throw (new SpiderException(e));
} catch (IllegalAccessException e) {
throw (new SpiderException(e));
} catch (NoSuchFieldException e) {
throw (new SpiderException(e));