1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java Sat Dec 21 20:38:50 2013 +0100
1.3 @@ -0,0 +1,90 @@
1.4 +/**
1.5 + * SQL-DK
1.6 + * Copyright © 2013 František Kučera (frantovo.cz)
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +package info.globalcode.sql.dk.configuration;
1.22 +
1.23 +import info.globalcode.sql.dk.DKException;
1.24 +import info.globalcode.sql.dk.formatting.Formatter;
1.25 +import info.globalcode.sql.dk.formatting.FormatterContext;
1.26 +import java.lang.reflect.Constructor;
1.27 +import java.lang.reflect.InvocationTargetException;
1.28 +import javax.xml.bind.annotation.XmlElement;
1.29 +import javax.xml.bind.annotation.XmlTransient;
1.30 +
1.31 +/**
1.32 + *
1.33 + * @author Ing. František Kučera (frantovo.cz)
1.34 + */
1.35 +public class FormatterDefinition implements NameIdentified {
1.36 +
1.37 + private String name;
1.38 + private String className;
1.39 +
1.40 + @XmlElement(name = "name")
1.41 + @Override
1.42 + public String getName() {
1.43 + return name;
1.44 + }
1.45 +
1.46 + public void setName(String name) {
1.47 + this.name = name;
1.48 + }
1.49 +
1.50 + /**
1.51 + * Filter's class. Must implement the
1.52 + * <code>info.globalcode.sql.dk.formatting.Formatter</code> interface.
1.53 + * Subclassing the
1.54 + * <code>info.globalcode.sql.dk.formatting.AbstractFormatter</code> is strongly recommended.
1.55 + * The constructor must accept one parameter:
1.56 + * <code>info.globalcode.sql.dk.formatting.FormatterContext</code>
1.57 + *
1.58 + * @return fully qualified class name
1.59 + */
1.60 + @XmlElement(name = "class")
1.61 + public String getClassName() {
1.62 + return className;
1.63 + }
1.64 +
1.65 + public void setClassName(String className) {
1.66 + this.className = className;
1.67 + }
1.68 +
1.69 + /**
1.70 + * @param context
1.71 + * @return
1.72 + * @throws DKException
1.73 + */
1.74 + @XmlTransient
1.75 + public Formatter getInstance(FormatterContext context) throws DKException {
1.76 + try {
1.77 + Constructor constructor = Class.forName(className).getConstructor(context.getClass());
1.78 +
1.79 + Object instance = constructor.newInstance(context);
1.80 + if (instance instanceof Formatter) {
1.81 + return (Formatter) instance;
1.82 + } else {
1.83 + throw new DKException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface");
1.84 + }
1.85 + } catch (ClassNotFoundException e) {
1.86 + throw new DKException("No formatter class with name: " + className, e);
1.87 + } catch (NoSuchMethodException e) {
1.88 + throw new DKException("Formatter class with no valid constructor: " + className, e);
1.89 + } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
1.90 + throw new DKException("Formatter's constructor caused an error: " + className, e);
1.91 + }
1.92 + }
1.93 +}