java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/FormatterDefinition.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 238 4a1864c3e867
permissions -rw-r--r--
fix license version: GNU GPLv3
franta-hg@26
     1
/**
franta-hg@26
     2
 * SQL-DK
franta-hg@26
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@26
     4
 *
franta-hg@26
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@26
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@250
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@26
     8
 *
franta-hg@26
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@26
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@26
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@26
    12
 * GNU General Public License for more details.
franta-hg@26
    13
 *
franta-hg@26
    14
 * You should have received a copy of the GNU General Public License
franta-hg@26
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@26
    16
 */
franta-hg@26
    17
package info.globalcode.sql.dk.configuration;
franta-hg@26
    18
franta-hg@30
    19
import static info.globalcode.sql.dk.Xmlns.CONFIGURATION;
franta-hg@26
    20
import info.globalcode.sql.dk.formatting.Formatter;
franta-hg@26
    21
import info.globalcode.sql.dk.formatting.FormatterContext;
franta-hg@34
    22
import info.globalcode.sql.dk.formatting.FormatterException;
franta-hg@26
    23
import java.lang.reflect.Constructor;
franta-hg@26
    24
import java.lang.reflect.InvocationTargetException;
franta-hg@26
    25
import javax.xml.bind.annotation.XmlElement;
franta-hg@26
    26
franta-hg@26
    27
/**
franta-hg@155
    28
 * Configured (but not yet instantiated) formatter.
franta-hg@26
    29
 *
franta-hg@26
    30
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@26
    31
 */
franta-hg@26
    32
public class FormatterDefinition implements NameIdentified {
franta-hg@26
    33
franta-hg@26
    34
	private String name;
franta-hg@26
    35
	private String className;
franta-hg@104
    36
	private Properties properties = new Properties();
franta-hg@26
    37
franta-hg@29
    38
	public FormatterDefinition() {
franta-hg@29
    39
	}
franta-hg@29
    40
franta-hg@29
    41
	public FormatterDefinition(String name, String className) {
franta-hg@29
    42
		this.name = name;
franta-hg@29
    43
		this.className = className;
franta-hg@29
    44
	}
franta-hg@29
    45
franta-hg@104
    46
	public FormatterDefinition(String name, String className, Properties properties) {
franta-hg@104
    47
		this(name, className);
franta-hg@104
    48
		this.properties = properties;
franta-hg@104
    49
	}
franta-hg@104
    50
franta-hg@30
    51
	@XmlElement(name = "name", namespace = CONFIGURATION)
franta-hg@26
    52
	@Override
franta-hg@26
    53
	public String getName() {
franta-hg@26
    54
		return name;
franta-hg@26
    55
	}
franta-hg@26
    56
franta-hg@26
    57
	public void setName(String name) {
franta-hg@26
    58
		this.name = name;
franta-hg@26
    59
	}
franta-hg@26
    60
franta-hg@26
    61
	/**
franta-hg@26
    62
	 * Filter's class. Must implement the
franta-hg@26
    63
	 * <code>info.globalcode.sql.dk.formatting.Formatter</code> interface.
franta-hg@26
    64
	 * Subclassing the
franta-hg@26
    65
	 * <code>info.globalcode.sql.dk.formatting.AbstractFormatter</code> is strongly recommended.
franta-hg@26
    66
	 * The constructor must accept one parameter:
franta-hg@26
    67
	 * <code>info.globalcode.sql.dk.formatting.FormatterContext</code>
franta-hg@26
    68
	 *
franta-hg@26
    69
	 * @return fully qualified class name
franta-hg@26
    70
	 */
franta-hg@30
    71
	@XmlElement(name = "class", namespace = CONFIGURATION)
franta-hg@26
    72
	public String getClassName() {
franta-hg@26
    73
		return className;
franta-hg@26
    74
	}
franta-hg@26
    75
franta-hg@26
    76
	public void setClassName(String className) {
franta-hg@26
    77
		this.className = className;
franta-hg@26
    78
	}
franta-hg@26
    79
franta-hg@104
    80
	@XmlElement(name = "property", namespace = CONFIGURATION)
franta-hg@104
    81
	public Properties getProperties() {
franta-hg@104
    82
		return properties;
franta-hg@104
    83
	}
franta-hg@104
    84
franta-hg@104
    85
	public void setProperties(Properties properties) {
franta-hg@104
    86
		this.properties = properties;
franta-hg@104
    87
	}
franta-hg@104
    88
franta-hg@26
    89
	/**
franta-hg@26
    90
	 * @param context
franta-hg@26
    91
	 * @return
franta-hg@188
    92
	 * @throws FormatterException
franta-hg@26
    93
	 */
franta-hg@34
    94
	public Formatter getInstance(FormatterContext context) throws FormatterException {
franta-hg@104
    95
		context.getProperties().setDefaults(properties);
franta-hg@26
    96
		try {
franta-hg@26
    97
			Constructor constructor = Class.forName(className).getConstructor(context.getClass());
franta-hg@26
    98
franta-hg@26
    99
			Object instance = constructor.newInstance(context);
franta-hg@26
   100
			if (instance instanceof Formatter) {
franta-hg@26
   101
				return (Formatter) instance;
franta-hg@26
   102
			} else {
franta-hg@34
   103
				throw new FormatterException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface");
franta-hg@26
   104
			}
franta-hg@26
   105
		} catch (ClassNotFoundException e) {
franta-hg@160
   106
			throw new FormatterException("Formatter class does not exist: " + className, e);
franta-hg@26
   107
		} catch (NoSuchMethodException e) {
franta-hg@34
   108
			throw new FormatterException("Formatter class with no valid constructor: " + className, e);
franta-hg@26
   109
		} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
franta-hg@34
   110
			throw new FormatterException("Formatter's constructor caused an error: " + className, e);
franta-hg@26
   111
		}
franta-hg@26
   112
	}
franta-hg@26
   113
}