19 |
19 |
20 import static info.globalcode.sql.dk.Xmlns.CONFIGURATION; |
20 import static info.globalcode.sql.dk.Xmlns.CONFIGURATION; |
21 import info.globalcode.sql.dk.DKException; |
21 import info.globalcode.sql.dk.DKException; |
22 import info.globalcode.sql.dk.formatting.Formatter; |
22 import info.globalcode.sql.dk.formatting.Formatter; |
23 import info.globalcode.sql.dk.formatting.FormatterContext; |
23 import info.globalcode.sql.dk.formatting.FormatterContext; |
|
24 import info.globalcode.sql.dk.formatting.FormatterException; |
24 import java.lang.reflect.Constructor; |
25 import java.lang.reflect.Constructor; |
25 import java.lang.reflect.InvocationTargetException; |
26 import java.lang.reflect.InvocationTargetException; |
26 import javax.xml.bind.annotation.XmlElement; |
27 import javax.xml.bind.annotation.XmlElement; |
27 |
28 |
28 /** |
29 /** |
74 /** |
75 /** |
75 * @param context |
76 * @param context |
76 * @return |
77 * @return |
77 * @throws DKException |
78 * @throws DKException |
78 */ |
79 */ |
79 public Formatter getInstance(FormatterContext context) throws DKException { |
80 public Formatter getInstance(FormatterContext context) throws FormatterException { |
80 try { |
81 try { |
81 Constructor constructor = Class.forName(className).getConstructor(context.getClass()); |
82 Constructor constructor = Class.forName(className).getConstructor(context.getClass()); |
82 |
83 |
83 Object instance = constructor.newInstance(context); |
84 Object instance = constructor.newInstance(context); |
84 if (instance instanceof Formatter) { |
85 if (instance instanceof Formatter) { |
85 return (Formatter) instance; |
86 return (Formatter) instance; |
86 } else { |
87 } else { |
87 throw new DKException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface"); |
88 throw new FormatterException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface"); |
88 } |
89 } |
89 } catch (ClassNotFoundException e) { |
90 } catch (ClassNotFoundException e) { |
90 throw new DKException("No formatter class with name: " + className, e); |
91 throw new FormatterException("No formatter class with name: " + className, e); |
91 } catch (NoSuchMethodException e) { |
92 } catch (NoSuchMethodException e) { |
92 throw new DKException("Formatter class with no valid constructor: " + className, e); |
93 throw new FormatterException("Formatter class with no valid constructor: " + className, e); |
93 } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { |
94 } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { |
94 throw new DKException("Formatter's constructor caused an error: " + className, e); |
95 throw new FormatterException("Formatter's constructor caused an error: " + className, e); |
95 } |
96 } |
96 } |
97 } |
97 } |
98 } |