KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > formatter > FormatterFactory


1 package org.jzonic.jlo.formatter;
2
3 import org.jzonic.jlo.error.ErrorHandler;
4
5 import java.lang.reflect.Constructor JavaDoc;
6 /**
7  * The <code>FormatterFactory</code> creates a formatter by a given
8  * name. If the name does not contain any package informations such
9  * as "com.foo.MyFormatter" then it takes "org.jlo.formatter." as
10  * prefix.
11  *
12  * @author Andreas Mecky
13  * @author Terry Dye
14  * @version 1.0
15  */

16
17 public class FormatterFactory {
18
19     /**
20      * The private constructor that cannot be used.
21      */

22     private FormatterFactory() {
23     }
24
25     /**
26      * This method instantiates the formatter with the
27      * given name.
28      *
29      * @param name The name of the formatter.
30      */

31   public static Formatter getFormatter(String JavaDoc name,String JavaDoc configurationName) {
32     // check if the name denotes a package and class
33
if ( name.indexOf(".") == -1 ) {
34     // no package so we the formatter is inside this package
35
name = "org.jzonic.jlo.formatter."+name;
36     }
37     try {
38     // now we instantiate the formatter
39
Class JavaDoc c = Class.forName(name);
40         Constructor JavaDoc con = c.getDeclaredConstructor(new Class JavaDoc[]{String JavaDoc.class});
41         Formatter fmt = (Formatter)con.newInstance(new Object JavaDoc[]{new String JavaDoc(configurationName)});
42         return fmt;
43     } catch (Exception JavaDoc e) {
44     // something went wrong and we send it to the ErrorHandler
45
ErrorHandler.reportError("Exception while instantiating the formatter: " + name, e);
46         return new SimpleFormatter("Default");
47     }
48   }
49 }
Popular Tags