franta-hg@26: /** franta-hg@26: * SQL-DK franta-hg@26: * Copyright © 2013 František Kučera (frantovo.cz) franta-hg@26: * franta-hg@26: * This program is free software: you can redistribute it and/or modify franta-hg@26: * it under the terms of the GNU General Public License as published by franta-hg@26: * the Free Software Foundation, either version 3 of the License, or franta-hg@26: * (at your option) any later version. franta-hg@26: * franta-hg@26: * This program is distributed in the hope that it will be useful, franta-hg@26: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@26: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@26: * GNU General Public License for more details. franta-hg@26: * franta-hg@26: * You should have received a copy of the GNU General Public License franta-hg@26: * along with this program. If not, see . franta-hg@26: */ franta-hg@26: package info.globalcode.sql.dk.configuration; franta-hg@26: franta-hg@30: import static info.globalcode.sql.dk.Xmlns.CONFIGURATION; franta-hg@26: import info.globalcode.sql.dk.formatting.Formatter; franta-hg@26: import info.globalcode.sql.dk.formatting.FormatterContext; franta-hg@34: import info.globalcode.sql.dk.formatting.FormatterException; franta-hg@26: import java.lang.reflect.Constructor; franta-hg@26: import java.lang.reflect.InvocationTargetException; franta-hg@26: import javax.xml.bind.annotation.XmlElement; franta-hg@26: franta-hg@26: /** franta-hg@155: * Configured (but not yet instantiated) formatter. franta-hg@26: * franta-hg@26: * @author Ing. František Kučera (frantovo.cz) franta-hg@26: */ franta-hg@26: public class FormatterDefinition implements NameIdentified { franta-hg@26: franta-hg@26: private String name; franta-hg@26: private String className; franta-hg@104: private Properties properties = new Properties(); franta-hg@26: franta-hg@29: public FormatterDefinition() { franta-hg@29: } franta-hg@29: franta-hg@29: public FormatterDefinition(String name, String className) { franta-hg@29: this.name = name; franta-hg@29: this.className = className; franta-hg@29: } franta-hg@29: franta-hg@104: public FormatterDefinition(String name, String className, Properties properties) { franta-hg@104: this(name, className); franta-hg@104: this.properties = properties; franta-hg@104: } franta-hg@104: franta-hg@30: @XmlElement(name = "name", namespace = CONFIGURATION) franta-hg@26: @Override franta-hg@26: public String getName() { franta-hg@26: return name; franta-hg@26: } franta-hg@26: franta-hg@26: public void setName(String name) { franta-hg@26: this.name = name; franta-hg@26: } franta-hg@26: franta-hg@26: /** franta-hg@26: * Filter's class. Must implement the franta-hg@26: * info.globalcode.sql.dk.formatting.Formatter interface. franta-hg@26: * Subclassing the franta-hg@26: * info.globalcode.sql.dk.formatting.AbstractFormatter is strongly recommended. franta-hg@26: * The constructor must accept one parameter: franta-hg@26: * info.globalcode.sql.dk.formatting.FormatterContext franta-hg@26: * franta-hg@26: * @return fully qualified class name franta-hg@26: */ franta-hg@30: @XmlElement(name = "class", namespace = CONFIGURATION) franta-hg@26: public String getClassName() { franta-hg@26: return className; franta-hg@26: } franta-hg@26: franta-hg@26: public void setClassName(String className) { franta-hg@26: this.className = className; franta-hg@26: } franta-hg@26: franta-hg@104: @XmlElement(name = "property", namespace = CONFIGURATION) franta-hg@104: public Properties getProperties() { franta-hg@104: return properties; franta-hg@104: } franta-hg@104: franta-hg@104: public void setProperties(Properties properties) { franta-hg@104: this.properties = properties; franta-hg@104: } franta-hg@104: franta-hg@26: /** franta-hg@26: * @param context franta-hg@26: * @return franta-hg@188: * @throws FormatterException franta-hg@26: */ franta-hg@34: public Formatter getInstance(FormatterContext context) throws FormatterException { franta-hg@104: context.getProperties().setDefaults(properties); franta-hg@26: try { franta-hg@26: Constructor constructor = Class.forName(className).getConstructor(context.getClass()); franta-hg@26: franta-hg@26: Object instance = constructor.newInstance(context); franta-hg@26: if (instance instanceof Formatter) { franta-hg@26: return (Formatter) instance; franta-hg@26: } else { franta-hg@34: throw new FormatterException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface"); franta-hg@26: } franta-hg@26: } catch (ClassNotFoundException e) { franta-hg@160: throw new FormatterException("Formatter class does not exist: " + className, e); franta-hg@26: } catch (NoSuchMethodException e) { franta-hg@34: throw new FormatterException("Formatter class with no valid constructor: " + className, e); franta-hg@26: } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { franta-hg@34: throw new FormatterException("Formatter's constructor caused an error: " + className, e); franta-hg@26: } franta-hg@26: } franta-hg@26: }