java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Dec 2013 23:31:55 +0100
branchv_0
changeset 34 9335cf31c0f2
parent 30 b7ea47b2d4ca
child 104 245f1b88a3e6
permissions -rw-r--r--
first working version
     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 static info.globalcode.sql.dk.Xmlns.CONFIGURATION;
    21 import info.globalcode.sql.dk.DKException;
    22 import info.globalcode.sql.dk.formatting.Formatter;
    23 import info.globalcode.sql.dk.formatting.FormatterContext;
    24 import info.globalcode.sql.dk.formatting.FormatterException;
    25 import java.lang.reflect.Constructor;
    26 import java.lang.reflect.InvocationTargetException;
    27 import javax.xml.bind.annotation.XmlElement;
    28 
    29 /**
    30  *
    31  * @author Ing. František Kučera (frantovo.cz)
    32  */
    33 public class FormatterDefinition implements NameIdentified {
    34 
    35 	private String name;
    36 	private String className;
    37 
    38 	public FormatterDefinition() {
    39 	}
    40 
    41 	public FormatterDefinition(String name, String className) {
    42 		this.name = name;
    43 		this.className = className;
    44 	}
    45 
    46 	@XmlElement(name = "name", namespace = CONFIGURATION)
    47 	@Override
    48 	public String getName() {
    49 		return name;
    50 	}
    51 
    52 	public void setName(String name) {
    53 		this.name = name;
    54 	}
    55 
    56 	/**
    57 	 * Filter's class. Must implement the
    58 	 * <code>info.globalcode.sql.dk.formatting.Formatter</code> interface.
    59 	 * Subclassing the
    60 	 * <code>info.globalcode.sql.dk.formatting.AbstractFormatter</code> is strongly recommended.
    61 	 * The constructor must accept one parameter:
    62 	 * <code>info.globalcode.sql.dk.formatting.FormatterContext</code>
    63 	 *
    64 	 * @return fully qualified class name
    65 	 */
    66 	@XmlElement(name = "class", namespace = CONFIGURATION)
    67 	public String getClassName() {
    68 		return className;
    69 	}
    70 
    71 	public void setClassName(String className) {
    72 		this.className = className;
    73 	}
    74 
    75 	/**
    76 	 * @param context
    77 	 * @return
    78 	 * @throws DKException
    79 	 */
    80 	public Formatter getInstance(FormatterContext context) throws FormatterException {
    81 		try {
    82 			Constructor constructor = Class.forName(className).getConstructor(context.getClass());
    83 
    84 			Object instance = constructor.newInstance(context);
    85 			if (instance instanceof Formatter) {
    86 				return (Formatter) instance;
    87 			} else {
    88 				throw new FormatterException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface");
    89 			}
    90 		} catch (ClassNotFoundException e) {
    91 			throw new FormatterException("No formatter class with name: " + className, e);
    92 		} catch (NoSuchMethodException e) {
    93 			throw new FormatterException("Formatter class with no valid constructor: " + className, e);
    94 		} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
    95 			throw new FormatterException("Formatter's constructor caused an error: " + className, e);
    96 		}
    97 	}
    98 }