java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 16 May 2015 20:25:16 +0200
branchv_0
changeset 188 54bacc7ed42b
parent 160 84ea4a819fb2
permissions -rw-r--r--
jdbc-dk-driver: include main project sources
     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.formatting.Formatter;
    22 import info.globalcode.sql.dk.formatting.FormatterContext;
    23 import info.globalcode.sql.dk.formatting.FormatterException;
    24 import java.lang.reflect.Constructor;
    25 import java.lang.reflect.InvocationTargetException;
    26 import javax.xml.bind.annotation.XmlElement;
    27 
    28 /**
    29  * Configured (but not yet instantiated) formatter.
    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 	private Properties properties = new Properties();
    38 
    39 	public FormatterDefinition() {
    40 	}
    41 
    42 	public FormatterDefinition(String name, String className) {
    43 		this.name = name;
    44 		this.className = className;
    45 	}
    46 
    47 	public FormatterDefinition(String name, String className, Properties properties) {
    48 		this(name, className);
    49 		this.properties = properties;
    50 	}
    51 
    52 	@XmlElement(name = "name", namespace = CONFIGURATION)
    53 	@Override
    54 	public String getName() {
    55 		return name;
    56 	}
    57 
    58 	public void setName(String name) {
    59 		this.name = name;
    60 	}
    61 
    62 	/**
    63 	 * Filter's class. Must implement the
    64 	 * <code>info.globalcode.sql.dk.formatting.Formatter</code> interface.
    65 	 * Subclassing the
    66 	 * <code>info.globalcode.sql.dk.formatting.AbstractFormatter</code> is strongly recommended.
    67 	 * The constructor must accept one parameter:
    68 	 * <code>info.globalcode.sql.dk.formatting.FormatterContext</code>
    69 	 *
    70 	 * @return fully qualified class name
    71 	 */
    72 	@XmlElement(name = "class", namespace = CONFIGURATION)
    73 	public String getClassName() {
    74 		return className;
    75 	}
    76 
    77 	public void setClassName(String className) {
    78 		this.className = className;
    79 	}
    80 
    81 	@XmlElement(name = "property", namespace = CONFIGURATION)
    82 	public Properties getProperties() {
    83 		return properties;
    84 	}
    85 
    86 	public void setProperties(Properties properties) {
    87 		this.properties = properties;
    88 	}
    89 
    90 	/**
    91 	 * @param context
    92 	 * @return
    93 	 * @throws FormatterException
    94 	 */
    95 	public Formatter getInstance(FormatterContext context) throws FormatterException {
    96 		context.getProperties().setDefaults(properties);
    97 		try {
    98 			Constructor constructor = Class.forName(className).getConstructor(context.getClass());
    99 
   100 			Object instance = constructor.newInstance(context);
   101 			if (instance instanceof Formatter) {
   102 				return (Formatter) instance;
   103 			} else {
   104 				throw new FormatterException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface");
   105 			}
   106 		} catch (ClassNotFoundException e) {
   107 			throw new FormatterException("Formatter class does not exist: " + className, e);
   108 		} catch (NoSuchMethodException e) {
   109 			throw new FormatterException("Formatter class with no valid constructor: " + className, e);
   110 		} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
   111 			throw new FormatterException("Formatter's constructor caused an error: " + className, e);
   112 		}
   113 	}
   114 }