}
private DataModel readDataModel(DirectoryEnvironment environment, String identifier, boolean useMagicNumber)
throws Exception {
p("readDataModel", identifier, environment.getName());
DataModel m = null;
XFile file = makeXFile(environment, identifier);
if (!file.exists()) {
throw new Exception("Can't load '" + identifier + "' file not found: " + file.getAbsolutePath());
}
BufferedInputStream fileIn = new BufferedInputStream(new FileInputStream(file));
PushbackInputStream in = null;
if (useMagicNumber) {
in = new MagicNumberInputStream(fileIn);
} else {
in = new PushbackInputStream(fileIn);
}
TextToken className = TextToken.nextToken(in);
if (className.isString()) {
Class cl = Class.forName(className.getString());
m = (DataModel) cl.newInstance();
m.readDataFrom(in);
} else {
throw new Exception("Expecting String literal (class name) at beginning of file but have: " + className);
}
if (useMagicNumber) {
MagicNumberInputStream ms = (MagicNumberInputStream) in;
while (!ms.hasMagic()) {
if (-1 == ms.read()) {
// no magic number found
break;
}
}
if (!ms.checkMagic()) {
in.close();
throw new MagicNumberException("Wrong magic number in: " + file.getAbsolutePath());
}
}
in.close();
m.setInstanceName(identifier);
return m;
}