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