java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Configuration.java
transform the record formatter into the recfile formatter
still a human-readable format and very similar
but also machine-readable – can be processed in GNU Recutils and Relational pipes
/**
* 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 <http://www.gnu.org/licenses/>.
*/
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.RecfileFormatter;
import info.globalcode.sql.dk.formatting.SilentFormatter;
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<DatabaseDefinition> databases = new ArrayList<>();
private List<FormatterDefinition> 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<FormatterDefinition> buildInFormatters;
static {
Collection<FormatterDefinition> 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(RecfileFormatter.NAME, RecfileFormatter.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<DatabaseDefinition> getDatabases() {
return databases;
}
public void setDatabases(List<DatabaseDefinition> 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<FormatterDefinition> getFormatters() {
return formatters;
}
public void setFormatters(List<FormatterDefinition> 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<FormatterDefinition> getBuildInFormatters() {
return buildInFormatters;
}
/**
* @return built-in + configured formatters
* @see #getFormatters()
*/
@XmlTransient
public Collection<FormatterDefinition> getAllFormatters() {
Collection<FormatterDefinition> 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;
}
}