01: package org.jzonic.jlo.formatter;
02:
03: import org.jzonic.jlo.error.ErrorHandler;
04:
05: import java.lang.reflect.Constructor;
06:
07: /**
08: * The <code>FormatterFactory</code> creates a formatter by a given
09: * name. If the name does not contain any package informations such
10: * as "com.foo.MyFormatter" then it takes "org.jlo.formatter." as
11: * prefix.
12: *
13: * @author Andreas Mecky
14: * @author Terry Dye
15: * @version 1.0
16: */
17:
18: public class FormatterFactory {
19:
20: /**
21: * The private constructor that cannot be used.
22: */
23: private FormatterFactory() {
24: }
25:
26: /**
27: * This method instantiates the formatter with the
28: * given name.
29: *
30: * @param name The name of the formatter.
31: */
32: public static Formatter getFormatter(String name,
33: String configurationName) {
34: // check if the name denotes a package and class
35: if (name.indexOf(".") == -1) {
36: // no package so we the formatter is inside this package
37: name = "org.jzonic.jlo.formatter." + name;
38: }
39: try {
40: // now we instantiate the formatter
41: Class c = Class.forName(name);
42: Constructor con = c
43: .getDeclaredConstructor(new Class[] { String.class });
44: Formatter fmt = (Formatter) con
45: .newInstance(new Object[] { new String(
46: configurationName) });
47: return fmt;
48: } catch (Exception e) {
49: // something went wrong and we send it to the ErrorHandler
50: ErrorHandler.reportError(
51: "Exception while instantiating the formatter: "
52: + name, e);
53: return new SimpleFormatter("Default");
54: }
55: }
56: }
|