3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk.configuration;
20 import static info.globalcode.sql.dk.Xmlns.CONFIGURATION;
21 import info.globalcode.sql.dk.DKException;
22 import info.globalcode.sql.dk.formatting.Formatter;
23 import info.globalcode.sql.dk.formatting.FormatterContext;
24 import info.globalcode.sql.dk.formatting.FormatterException;
25 import java.lang.reflect.Constructor;
26 import java.lang.reflect.InvocationTargetException;
27 import javax.xml.bind.annotation.XmlElement;
30 * Configured (but not yet instantiated) formatter.
32 * @author Ing. František Kučera (frantovo.cz)
34 public class FormatterDefinition implements NameIdentified {
37 private String className;
38 private Properties properties = new Properties();
40 public FormatterDefinition() {
43 public FormatterDefinition(String name, String className) {
45 this.className = className;
48 public FormatterDefinition(String name, String className, Properties properties) {
49 this(name, className);
50 this.properties = properties;
53 @XmlElement(name = "name", namespace = CONFIGURATION)
55 public String getName() {
59 public void setName(String name) {
64 * Filter's class. Must implement the
65 * <code>info.globalcode.sql.dk.formatting.Formatter</code> interface.
67 * <code>info.globalcode.sql.dk.formatting.AbstractFormatter</code> is strongly recommended.
68 * The constructor must accept one parameter:
69 * <code>info.globalcode.sql.dk.formatting.FormatterContext</code>
71 * @return fully qualified class name
73 @XmlElement(name = "class", namespace = CONFIGURATION)
74 public String getClassName() {
78 public void setClassName(String className) {
79 this.className = className;
82 @XmlElement(name = "property", namespace = CONFIGURATION)
83 public Properties getProperties() {
87 public void setProperties(Properties properties) {
88 this.properties = properties;
96 public Formatter getInstance(FormatterContext context) throws FormatterException {
97 context.getProperties().setDefaults(properties);
99 Constructor constructor = Class.forName(className).getConstructor(context.getClass());
101 Object instance = constructor.newInstance(context);
102 if (instance instanceof Formatter) {
103 return (Formatter) instance;
105 throw new FormatterException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface");
107 } catch (ClassNotFoundException e) {
108 throw new FormatterException("Formatter class does not exist: " + className, e);
109 } catch (NoSuchMethodException e) {
110 throw new FormatterException("Formatter class with no valid constructor: " + className, e);
111 } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
112 throw new FormatterException("Formatter's constructor caused an error: " + className, e);