java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/FormatterDefinition.java
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, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package info.globalcode.sql.dk.configuration;
19 import static info.globalcode.sql.dk.Xmlns.CONFIGURATION;
20 import info.globalcode.sql.dk.formatting.Formatter;
21 import info.globalcode.sql.dk.formatting.FormatterContext;
22 import info.globalcode.sql.dk.formatting.FormatterException;
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.InvocationTargetException;
25 import javax.xml.bind.annotation.XmlElement;
28 * Configured (but not yet instantiated) formatter.
30 * @author Ing. František Kučera (frantovo.cz)
32 public class FormatterDefinition implements NameIdentified {
35 private String className;
36 private Properties properties = new Properties();
38 public FormatterDefinition() {
41 public FormatterDefinition(String name, String className) {
43 this.className = className;
46 public FormatterDefinition(String name, String className, Properties properties) {
47 this(name, className);
48 this.properties = properties;
51 @XmlElement(name = "name", namespace = CONFIGURATION)
53 public String getName() {
57 public void setName(String name) {
62 * Filter's class. Must implement the
63 * <code>info.globalcode.sql.dk.formatting.Formatter</code> interface.
65 * <code>info.globalcode.sql.dk.formatting.AbstractFormatter</code> is strongly recommended.
66 * The constructor must accept one parameter:
67 * <code>info.globalcode.sql.dk.formatting.FormatterContext</code>
69 * @return fully qualified class name
71 @XmlElement(name = "class", namespace = CONFIGURATION)
72 public String getClassName() {
76 public void setClassName(String className) {
77 this.className = className;
80 @XmlElement(name = "property", namespace = CONFIGURATION)
81 public Properties getProperties() {
85 public void setProperties(Properties properties) {
86 this.properties = properties;
92 * @throws FormatterException
94 public Formatter getInstance(FormatterContext context) throws FormatterException {
95 context.getProperties().setDefaults(properties);
97 Constructor constructor = Class.forName(className).getConstructor(context.getClass());
99 Object instance = constructor.newInstance(context);
100 if (instance instanceof Formatter) {
101 return (Formatter) instance;
103 throw new FormatterException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface");
105 } catch (ClassNotFoundException e) {
106 throw new FormatterException("Formatter class does not exist: " + className, e);
107 } catch (NoSuchMethodException e) {
108 throw new FormatterException("Formatter class with no valid constructor: " + className, e);
109 } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
110 throw new FormatterException("Formatter's constructor caused an error: " + className, e);