java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 10 Jan 2014 23:21:28 +0100
branchv_0
changeset 155 eb3676c6929b
parent 104 245f1b88a3e6
child 160 84ea4a819fb2
permissions -rw-r--r--
more JavaDoc
     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  * Configured (but not yet instantiated) formatter.
    31  *
    32  * @author Ing. František Kučera (frantovo.cz)
    33  */
    34 public class FormatterDefinition implements NameIdentified {
    35 
    36 	private String name;
    37 	private String className;
    38 	private Properties properties = new Properties();
    39 
    40 	public FormatterDefinition() {
    41 	}
    42 
    43 	public FormatterDefinition(String name, String className) {
    44 		this.name = name;
    45 		this.className = className;
    46 	}
    47 
    48 	public FormatterDefinition(String name, String className, Properties properties) {
    49 		this(name, className);
    50 		this.properties = properties;
    51 	}
    52 
    53 	@XmlElement(name = "name", namespace = CONFIGURATION)
    54 	@Override
    55 	public String getName() {
    56 		return name;
    57 	}
    58 
    59 	public void setName(String name) {
    60 		this.name = name;
    61 	}
    62 
    63 	/**
    64 	 * Filter's class. Must implement the
    65 	 * <code>info.globalcode.sql.dk.formatting.Formatter</code> interface.
    66 	 * Subclassing the
    67 	 * <code>info.globalcode.sql.dk.formatting.AbstractFormatter</code> is strongly recommended.
    68 	 * The constructor must accept one parameter:
    69 	 * <code>info.globalcode.sql.dk.formatting.FormatterContext</code>
    70 	 *
    71 	 * @return fully qualified class name
    72 	 */
    73 	@XmlElement(name = "class", namespace = CONFIGURATION)
    74 	public String getClassName() {
    75 		return className;
    76 	}
    77 
    78 	public void setClassName(String className) {
    79 		this.className = className;
    80 	}
    81 
    82 	@XmlElement(name = "property", namespace = CONFIGURATION)
    83 	public Properties getProperties() {
    84 		return properties;
    85 	}
    86 
    87 	public void setProperties(Properties properties) {
    88 		this.properties = properties;
    89 	}
    90 
    91 	/**
    92 	 * @param context
    93 	 * @return
    94 	 * @throws DKException
    95 	 */
    96 	public Formatter getInstance(FormatterContext context) throws FormatterException {
    97 		context.getProperties().setDefaults(properties);
    98 		try {
    99 			Constructor constructor = Class.forName(className).getConstructor(context.getClass());
   100 
   101 			Object instance = constructor.newInstance(context);
   102 			if (instance instanceof Formatter) {
   103 				return (Formatter) instance;
   104 			} else {
   105 				throw new FormatterException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface");
   106 			}
   107 		} catch (ClassNotFoundException e) {
   108 			throw new FormatterException("No formatter class with name: " + className, e);
   109 		} catch (NoSuchMethodException e) {
   110 			throw new FormatterException("Formatter class with no valid constructor: " + className, e);
   111 		} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
   112 			throw new FormatterException("Formatter's constructor caused an error: " + className, e);
   113 		}
   114 	}
   115 }