java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/FormatterDefinition.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 238 4a1864c3e867
permissions -rw-r--r--
fix license version: GNU GPLv3
     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, version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 package info.globalcode.sql.dk.configuration;
    18 
    19 import static info.globalcode.sql.dk.Xmlns.CONFIGURATION;
    20 import info.globalcode.sql.dk.formatting.Formatter;
    21 import info.globalcode.sql.dk.formatting.FormatterContext;
    22 import info.globalcode.sql.dk.formatting.FormatterException;
    23 import java.lang.reflect.Constructor;
    24 import java.lang.reflect.InvocationTargetException;
    25 import javax.xml.bind.annotation.XmlElement;
    26 
    27 /**
    28  * Configured (but not yet instantiated) formatter.
    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 	private Properties properties = new Properties();
    37 
    38 	public FormatterDefinition() {
    39 	}
    40 
    41 	public FormatterDefinition(String name, String className) {
    42 		this.name = name;
    43 		this.className = className;
    44 	}
    45 
    46 	public FormatterDefinition(String name, String className, Properties properties) {
    47 		this(name, className);
    48 		this.properties = properties;
    49 	}
    50 
    51 	@XmlElement(name = "name", namespace = CONFIGURATION)
    52 	@Override
    53 	public String getName() {
    54 		return name;
    55 	}
    56 
    57 	public void setName(String name) {
    58 		this.name = name;
    59 	}
    60 
    61 	/**
    62 	 * Filter's class. Must implement the
    63 	 * <code>info.globalcode.sql.dk.formatting.Formatter</code> interface.
    64 	 * Subclassing the
    65 	 * <code>info.globalcode.sql.dk.formatting.AbstractFormatter</code> is strongly recommended.
    66 	 * The constructor must accept one parameter:
    67 	 * <code>info.globalcode.sql.dk.formatting.FormatterContext</code>
    68 	 *
    69 	 * @return fully qualified class name
    70 	 */
    71 	@XmlElement(name = "class", namespace = CONFIGURATION)
    72 	public String getClassName() {
    73 		return className;
    74 	}
    75 
    76 	public void setClassName(String className) {
    77 		this.className = className;
    78 	}
    79 
    80 	@XmlElement(name = "property", namespace = CONFIGURATION)
    81 	public Properties getProperties() {
    82 		return properties;
    83 	}
    84 
    85 	public void setProperties(Properties properties) {
    86 		this.properties = properties;
    87 	}
    88 
    89 	/**
    90 	 * @param context
    91 	 * @return
    92 	 * @throws FormatterException
    93 	 */
    94 	public Formatter getInstance(FormatterContext context) throws FormatterException {
    95 		context.getProperties().setDefaults(properties);
    96 		try {
    97 			Constructor constructor = Class.forName(className).getConstructor(context.getClass());
    98 
    99 			Object instance = constructor.newInstance(context);
   100 			if (instance instanceof Formatter) {
   101 				return (Formatter) instance;
   102 			} else {
   103 				throw new FormatterException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface");
   104 			}
   105 		} catch (ClassNotFoundException e) {
   106 			throw new FormatterException("Formatter class does not exist: " + className, e);
   107 		} catch (NoSuchMethodException e) {
   108 			throw new FormatterException("Formatter class with no valid constructor: " + className, e);
   109 		} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
   110 			throw new FormatterException("Formatter's constructor caused an error: " + className, e);
   111 		}
   112 	}
   113 }