package org.jzonic.jlo.formatter;
import org.jzonic.jlo.error.ErrorHandler;
import java.lang.reflect.Constructor;
/**
* The <code>FormatterFactory</code> creates a formatter by a given
* name. If the name does not contain any package informations such
* as "com.foo.MyFormatter" then it takes "org.jlo.formatter." as
* prefix.
*
* @author Andreas Mecky
* @author Terry Dye
* @version 1.0
*/
public class FormatterFactory {
/**
* The private constructor that cannot be used.
*/
private FormatterFactory() {
}
/**
* This method instantiates the formatter with the
* given name.
*
* @param name The name of the formatter.
*/
public static Formatter getFormatter(String name,String configurationName) {
// check if the name denotes a package and class
if ( name.indexOf(".") == -1 ) {
// no package so we the formatter is inside this package
name = "org.jzonic.jlo.formatter."+name;
}
try {
// now we instantiate the formatter
Class c = Class.forName(name);
Constructor con = c.getDeclaredConstructor(new Class[]{String.class});
Formatter fmt = (Formatter)con.newInstance(new Object[]{new String(configurationName)});
return fmt;
} catch (Exception e) {
// something went wrong and we send it to the ErrorHandler
ErrorHandler.reportError("Exception while instantiating the formatter: " + name, e);
return new SimpleFormatter("Default");
}
}
}