* @param file
* @throws ConfigurationManagerException */
public synchronized Configuration load(File file,String configName,ConfigurationParser parser) throws ConfigurationManagerException {
if (file == null) {
ErrorReporter.getErrorHandler().reportError("The file is NULL");
throw new ConfigurationManagerException("The file is NULL");
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// set the validation flag (true if it should be validated)
dbf.setValidating(validate);
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
ErrorReporter.getErrorHandler().reportError("The parser cannot create a new document builder",pce);
throw new ConfigurationManagerException("The parser cannot create a new document builder: "
+ pce.getMessage());
}
// if we have to validate then we need to define an error handler here
if (validate) {
try {
// Set an ErrorHandler before parsing
OutputStreamWriter errorWriter =
new OutputStreamWriter(System.err, "UTF-8");
db.setErrorHandler(
new ConfigErrorHandler(new PrintWriter(errorWriter, true)));
} catch (Exception e) {
ErrorReporter.getErrorHandler().reportError("The parser cannot set up the error handler");
throw new ConfigurationManagerException("The parser cannot set up the error handler");
}
}
Document doc = null;
try {
FileInputStream fis = new FileInputStream(file);
InputSource ins = new InputSource(fis);
BufferedInputStream buffy = new BufferedInputStream(fis);
ins = new InputSource(buffy);
doc = db.parse(file);
} catch (SAXException se) {
ErrorReporter.getErrorHandler().reportError("The parser cannot parse the file",se);
throw new ConfigurationManagerException(
"The parser cannot parse the file: " + se.getMessage());
} catch (IOException ioe) {
ErrorReporter.getErrorHandler().reportError("The parser cannot parse the file",ioe);
throw new ConfigurationManagerException(
"The parser cannot open the file: " + ioe.getMessage());
}
Configuration config = parser.parse(doc, configName);
try {
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.FileReader(file));