java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Dec 2013 18:35:50 +0100
branchv_0
changeset 30 b7ea47b2d4ca
parent 29 d66858b4b563
child 34 9335cf31c0f2
permissions -rw-r--r--
class for xmlns constants
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@26
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@26
     8
 * (at your option) any later version.
franta-hg@26
     9
 *
franta-hg@26
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@26
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@26
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@26
    13
 * GNU General Public License for more details.
franta-hg@26
    14
 *
franta-hg@26
    15
 * You should have received a copy of the GNU General Public License
franta-hg@26
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@26
    17
 */
franta-hg@26
    18
package info.globalcode.sql.dk.configuration;
franta-hg@26
    19
franta-hg@30
    20
import static info.globalcode.sql.dk.Xmlns.CONFIGURATION;
franta-hg@26
    21
import info.globalcode.sql.dk.DKException;
franta-hg@26
    22
import info.globalcode.sql.dk.formatting.Formatter;
franta-hg@26
    23
import info.globalcode.sql.dk.formatting.FormatterContext;
franta-hg@26
    24
import java.lang.reflect.Constructor;
franta-hg@26
    25
import java.lang.reflect.InvocationTargetException;
franta-hg@26
    26
import javax.xml.bind.annotation.XmlElement;
franta-hg@26
    27
franta-hg@26
    28
/**
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@26
    36
franta-hg@29
    37
	public FormatterDefinition() {
franta-hg@29
    38
	}
franta-hg@29
    39
franta-hg@29
    40
	public FormatterDefinition(String name, String className) {
franta-hg@29
    41
		this.name = name;
franta-hg@29
    42
		this.className = className;
franta-hg@29
    43
	}
franta-hg@29
    44
franta-hg@30
    45
	@XmlElement(name = "name", namespace = CONFIGURATION)
franta-hg@26
    46
	@Override
franta-hg@26
    47
	public String getName() {
franta-hg@26
    48
		return name;
franta-hg@26
    49
	}
franta-hg@26
    50
franta-hg@26
    51
	public void setName(String name) {
franta-hg@26
    52
		this.name = name;
franta-hg@26
    53
	}
franta-hg@26
    54
franta-hg@26
    55
	/**
franta-hg@26
    56
	 * Filter's class. Must implement the
franta-hg@26
    57
	 * <code>info.globalcode.sql.dk.formatting.Formatter</code> interface.
franta-hg@26
    58
	 * Subclassing the
franta-hg@26
    59
	 * <code>info.globalcode.sql.dk.formatting.AbstractFormatter</code> is strongly recommended.
franta-hg@26
    60
	 * The constructor must accept one parameter:
franta-hg@26
    61
	 * <code>info.globalcode.sql.dk.formatting.FormatterContext</code>
franta-hg@26
    62
	 *
franta-hg@26
    63
	 * @return fully qualified class name
franta-hg@26
    64
	 */
franta-hg@30
    65
	@XmlElement(name = "class", namespace = CONFIGURATION)
franta-hg@26
    66
	public String getClassName() {
franta-hg@26
    67
		return className;
franta-hg@26
    68
	}
franta-hg@26
    69
franta-hg@26
    70
	public void setClassName(String className) {
franta-hg@26
    71
		this.className = className;
franta-hg@26
    72
	}
franta-hg@26
    73
franta-hg@26
    74
	/**
franta-hg@26
    75
	 * @param context
franta-hg@26
    76
	 * @return
franta-hg@26
    77
	 * @throws DKException
franta-hg@26
    78
	 */
franta-hg@26
    79
	public Formatter getInstance(FormatterContext context) throws DKException {
franta-hg@26
    80
		try {
franta-hg@26
    81
			Constructor constructor = Class.forName(className).getConstructor(context.getClass());
franta-hg@26
    82
franta-hg@26
    83
			Object instance = constructor.newInstance(context);
franta-hg@26
    84
			if (instance instanceof Formatter) {
franta-hg@26
    85
				return (Formatter) instance;
franta-hg@26
    86
			} else {
franta-hg@26
    87
				throw new DKException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface");
franta-hg@26
    88
			}
franta-hg@26
    89
		} catch (ClassNotFoundException e) {
franta-hg@26
    90
			throw new DKException("No formatter class with name: " + className, e);
franta-hg@26
    91
		} catch (NoSuchMethodException e) {
franta-hg@26
    92
			throw new DKException("Formatter class with no valid constructor: " + className, e);
franta-hg@26
    93
		} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
franta-hg@26
    94
			throw new DKException("Formatter's constructor caused an error: " + className, e);
franta-hg@26
    95
		}
franta-hg@26
    96
	}
franta-hg@26
    97
}