java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/FormatterDefinition.java
branchv_0
changeset 238 4a1864c3e867
parent 188 54bacc7ed42b
child 250 aae5009bd0af
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/FormatterDefinition.java	Mon Mar 04 20:15:24 2019 +0100
     1.3 @@ -0,0 +1,114 @@
     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 static info.globalcode.sql.dk.Xmlns.CONFIGURATION;
    1.24 +import info.globalcode.sql.dk.formatting.Formatter;
    1.25 +import info.globalcode.sql.dk.formatting.FormatterContext;
    1.26 +import info.globalcode.sql.dk.formatting.FormatterException;
    1.27 +import java.lang.reflect.Constructor;
    1.28 +import java.lang.reflect.InvocationTargetException;
    1.29 +import javax.xml.bind.annotation.XmlElement;
    1.30 +
    1.31 +/**
    1.32 + * Configured (but not yet instantiated) formatter.
    1.33 + *
    1.34 + * @author Ing. František Kučera (frantovo.cz)
    1.35 + */
    1.36 +public class FormatterDefinition implements NameIdentified {
    1.37 +
    1.38 +	private String name;
    1.39 +	private String className;
    1.40 +	private Properties properties = new Properties();
    1.41 +
    1.42 +	public FormatterDefinition() {
    1.43 +	}
    1.44 +
    1.45 +	public FormatterDefinition(String name, String className) {
    1.46 +		this.name = name;
    1.47 +		this.className = className;
    1.48 +	}
    1.49 +
    1.50 +	public FormatterDefinition(String name, String className, Properties properties) {
    1.51 +		this(name, className);
    1.52 +		this.properties = properties;
    1.53 +	}
    1.54 +
    1.55 +	@XmlElement(name = "name", namespace = CONFIGURATION)
    1.56 +	@Override
    1.57 +	public String getName() {
    1.58 +		return name;
    1.59 +	}
    1.60 +
    1.61 +	public void setName(String name) {
    1.62 +		this.name = name;
    1.63 +	}
    1.64 +
    1.65 +	/**
    1.66 +	 * Filter's class. Must implement the
    1.67 +	 * <code>info.globalcode.sql.dk.formatting.Formatter</code> interface.
    1.68 +	 * Subclassing the
    1.69 +	 * <code>info.globalcode.sql.dk.formatting.AbstractFormatter</code> is strongly recommended.
    1.70 +	 * The constructor must accept one parameter:
    1.71 +	 * <code>info.globalcode.sql.dk.formatting.FormatterContext</code>
    1.72 +	 *
    1.73 +	 * @return fully qualified class name
    1.74 +	 */
    1.75 +	@XmlElement(name = "class", namespace = CONFIGURATION)
    1.76 +	public String getClassName() {
    1.77 +		return className;
    1.78 +	}
    1.79 +
    1.80 +	public void setClassName(String className) {
    1.81 +		this.className = className;
    1.82 +	}
    1.83 +
    1.84 +	@XmlElement(name = "property", namespace = CONFIGURATION)
    1.85 +	public Properties getProperties() {
    1.86 +		return properties;
    1.87 +	}
    1.88 +
    1.89 +	public void setProperties(Properties properties) {
    1.90 +		this.properties = properties;
    1.91 +	}
    1.92 +
    1.93 +	/**
    1.94 +	 * @param context
    1.95 +	 * @return
    1.96 +	 * @throws FormatterException
    1.97 +	 */
    1.98 +	public Formatter getInstance(FormatterContext context) throws FormatterException {
    1.99 +		context.getProperties().setDefaults(properties);
   1.100 +		try {
   1.101 +			Constructor constructor = Class.forName(className).getConstructor(context.getClass());
   1.102 +
   1.103 +			Object instance = constructor.newInstance(context);
   1.104 +			if (instance instanceof Formatter) {
   1.105 +				return (Formatter) instance;
   1.106 +			} else {
   1.107 +				throw new FormatterException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface");
   1.108 +			}
   1.109 +		} catch (ClassNotFoundException e) {
   1.110 +			throw new FormatterException("Formatter class does not exist: " + className, e);
   1.111 +		} catch (NoSuchMethodException e) {
   1.112 +			throw new FormatterException("Formatter class with no valid constructor: " + className, e);
   1.113 +		} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
   1.114 +			throw new FormatterException("Formatter's constructor caused an error: " + className, e);
   1.115 +		}
   1.116 +	}
   1.117 +}