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