diff -r 7e08730da258 -r 4a1864c3e867 java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Configuration.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Configuration.java Mon Mar 04 20:15:24 2019 +0100 @@ -0,0 +1,173 @@ +/** + * SQL-DK + * Copyright © 2013 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package info.globalcode.sql.dk.configuration; + +import static info.globalcode.sql.dk.Xmlns.CONFIGURATION; +import static info.globalcode.sql.dk.Functions.findByName; +import info.globalcode.sql.dk.formatting.BarChartFormatter; +import info.globalcode.sql.dk.formatting.SilentFormatter; +import info.globalcode.sql.dk.formatting.SingleRecordFormatter; +import info.globalcode.sql.dk.formatting.SingleValueFormatter; +import info.globalcode.sql.dk.formatting.TabularFormatter; +import info.globalcode.sql.dk.formatting.TabularPrefetchingFormatter; +import info.globalcode.sql.dk.formatting.TabularWrappingFormatter; +import info.globalcode.sql.dk.formatting.TeXFormatter; +import info.globalcode.sql.dk.formatting.XhtmlFormatter; +import info.globalcode.sql.dk.formatting.XmlFormatter; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlTransient; + +/** + * Object representation of user configuration loaded from XML. + * + * @author Ing. František Kučera (frantovo.cz) + */ +@XmlRootElement(name = "configuration", namespace = CONFIGURATION) +public class Configuration { + + private List databases = new ArrayList<>(); + private List formatters = new ArrayList<>(); + /** + * is used if no formatter is specified on CLI nor in user configuration + */ + public static final String DEFAULT_FORMATTER = TabularFormatter.NAME; + /** + * Can be used as default if prefetching is ok – for configuration listings (config is alread in + * memory, so this does not matter) + */ + public static final String DEFAULT_FORMATTER_PREFETCHING = TabularPrefetchingFormatter.NAME; + private String defaultFormatter; + /** + * Default list of formatters. Is used if particular name is not found in user configuration. + */ + private static final Collection buildInFormatters; + + static { + Collection l = new ArrayList<>(); + l.add(new FormatterDefinition(SilentFormatter.NAME, SilentFormatter.class.getName())); + l.add(new FormatterDefinition(SingleValueFormatter.NAME, SingleValueFormatter.class.getName())); + l.add(new FormatterDefinition(SingleRecordFormatter.NAME, SingleRecordFormatter.class.getName())); + l.add(new FormatterDefinition(XmlFormatter.NAME, XmlFormatter.class.getName())); + l.add(new FormatterDefinition(XhtmlFormatter.NAME, XhtmlFormatter.class.getName())); + l.add(new FormatterDefinition(TabularFormatter.NAME, TabularFormatter.class.getName())); + l.add(new FormatterDefinition(TabularPrefetchingFormatter.NAME, TabularPrefetchingFormatter.class.getName())); + l.add(new FormatterDefinition(TabularWrappingFormatter.NAME, TabularWrappingFormatter.class.getName())); + l.add(new FormatterDefinition(TeXFormatter.NAME, TeXFormatter.class.getName())); + //l.add(new FormatterDefinition(DsvFormatter.NAME, DsvFormatter.class.getName())); + //l.add(new FormatterDefinition(SystemCommandExecutor.NAME, SystemCommandExecutor.class.getName())); + l.add(new FormatterDefinition(BarChartFormatter.NAME, BarChartFormatter.class.getName())); + buildInFormatters = Collections.unmodifiableCollection(l); + } + + @XmlElement(name = "database", namespace = CONFIGURATION) + public List getDatabases() { + return databases; + } + + public void setDatabases(List databases) { + this.databases = databases; + } + + /** + * @param name + * @return + * @throws ConfigurationException if no database with this name is configured + */ + public DatabaseDefinition getDatabase(String name) throws ConfigurationException { + DatabaseDefinition dd = findByName(databases, name); + if (dd == null) { + throw new ConfigurationException("Database is not configured: " + name); + } else { + return dd; + } + } + + /** + * @return only configured formatters + * @see #getBuildInFormatters() + * @see #getAllFormatters() + */ + @XmlElement(name = "formatter", namespace = CONFIGURATION) + public List getFormatters() { + return formatters; + } + + public void setFormatters(List formatters) { + this.formatters = formatters; + } + + /** + * @param name name of desired formatter. Looking for this name in user configuration, then in + * buil-in formatters. If null, default from configuration or (if not configured) built-in + * default is used. + * @return formatter definition + * @throws ConfigurationException if no formatter with this name was found + */ + public FormatterDefinition getFormatter(String name) throws ConfigurationException { + if (name == null) { + return defaultFormatter == null ? getFormatter(DEFAULT_FORMATTER) : getFormatter(defaultFormatter); + } else { + FormatterDefinition fd = findByName(formatters, name); + fd = fd == null ? findByName(buildInFormatters, name) : fd; + if (fd == null) { + throw new ConfigurationException("Formatter is not configured: " + name); + } else { + return fd; + } + } + } + + /** + * @return only built-in formatters + * @see #getAllFormatters() + * @see #getFormatters() + */ + @XmlTransient + public Collection getBuildInFormatters() { + return buildInFormatters; + } + + /** + * @return built-in + configured formatters + * @see #getFormatters() + */ + @XmlTransient + public Collection getAllFormatters() { + Collection allFormatters = new ArrayList<>(); + allFormatters.addAll(buildInFormatters); + allFormatters.addAll(formatters); + return allFormatters; + } + + /** + * @return name of default formatter, is used if name is not specified on CLI + */ + @XmlElement(name = "defaultFormatter", namespace = CONFIGURATION) + public String getDefaultFormatter() { + return defaultFormatter; + } + + public void setDefaultFormatter(String defaultFormatter) { + this.defaultFormatter = defaultFormatter; + } +}