java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 24 Dec 2013 01:20:57 +0100
branchv_0
changeset 48 28735e71a1da
parent 32 5e412dbd9362
child 60 d4e88172a363
permissions -rw-r--r--
print CLI options problems if any
franta-hg@20
     1
/**
franta-hg@20
     2
 * SQL-DK
franta-hg@20
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@20
     4
 *
franta-hg@20
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@20
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@20
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@20
     8
 * (at your option) any later version.
franta-hg@20
     9
 *
franta-hg@20
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@20
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@20
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@20
    13
 * GNU General Public License for more details.
franta-hg@20
    14
 *
franta-hg@20
    15
 * You should have received a copy of the GNU General Public License
franta-hg@20
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@20
    17
 */
franta-hg@26
    18
package info.globalcode.sql.dk.configuration;
franta-hg@26
    19
franta-hg@30
    20
import static info.globalcode.sql.dk.Xmlns.CONFIGURATION;
franta-hg@29
    21
import static info.globalcode.sql.dk.Functions.findByName;
franta-hg@29
    22
import info.globalcode.sql.dk.formatting.SilentFormatter;
franta-hg@32
    23
import info.globalcode.sql.dk.formatting.TabularFormatter;
franta-hg@29
    24
import info.globalcode.sql.dk.formatting.XmlFormatter;
franta-hg@26
    25
import java.util.ArrayList;
franta-hg@29
    26
import java.util.Collection;
franta-hg@29
    27
import java.util.Collections;
franta-hg@26
    28
import java.util.List;
franta-hg@29
    29
import javax.xml.bind.annotation.XmlElement;
franta-hg@26
    30
import javax.xml.bind.annotation.XmlRootElement;
franta-hg@20
    31
franta-hg@20
    32
/**
franta-hg@20
    33
 *
franta-hg@20
    34
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@20
    35
 */
franta-hg@30
    36
@XmlRootElement(name = "configuration", namespace = CONFIGURATION)
franta-hg@20
    37
public class Configuration {
franta-hg@26
    38
franta-hg@26
    39
	private List<DatabaseDefinition> databases = new ArrayList<>();
franta-hg@26
    40
	private List<FormatterDefinition> formatters = new ArrayList<>();
franta-hg@32
    41
	/**
franta-hg@32
    42
	 * is used if no formatter is specified on CLI nor in user configuration
franta-hg@32
    43
	 */
franta-hg@32
    44
	public static final String DEFAULT_FORMATTER = TabularFormatter.NAME;
franta-hg@29
    45
	private String defaultFormatter;
franta-hg@29
    46
	/**
franta-hg@29
    47
	 * Default list of formatters. Is used if particular name is not found in user configuration.
franta-hg@29
    48
	 */
franta-hg@29
    49
	private static final Collection<FormatterDefinition> buildInFormatters;
franta-hg@26
    50
franta-hg@29
    51
	static {
franta-hg@29
    52
		Collection<FormatterDefinition> l = new ArrayList<>();
franta-hg@29
    53
		l.add(new FormatterDefinition(SilentFormatter.NAME, SilentFormatter.class.getName()));
franta-hg@29
    54
		l.add(new FormatterDefinition(XmlFormatter.NAME, XmlFormatter.class.getName()));
franta-hg@32
    55
		l.add(new FormatterDefinition(TabularFormatter.NAME, TabularFormatter.class.getName()));
franta-hg@29
    56
		buildInFormatters = Collections.unmodifiableCollection(l);
franta-hg@29
    57
	}
franta-hg@29
    58
franta-hg@30
    59
	@XmlElement(name = "database", namespace = CONFIGURATION)
franta-hg@26
    60
	public List<DatabaseDefinition> getDatabases() {
franta-hg@26
    61
		return databases;
franta-hg@26
    62
	}
franta-hg@26
    63
franta-hg@26
    64
	public void setDatabases(List<DatabaseDefinition> databases) {
franta-hg@26
    65
		this.databases = databases;
franta-hg@26
    66
	}
franta-hg@26
    67
franta-hg@29
    68
	public DatabaseDefinition getDatabase(String name) {
franta-hg@29
    69
		return findByName(databases, name);
franta-hg@29
    70
	}
franta-hg@29
    71
franta-hg@30
    72
	@XmlElement(name = "formatter", namespace = CONFIGURATION)
franta-hg@26
    73
	public List<FormatterDefinition> getFormatters() {
franta-hg@26
    74
		return formatters;
franta-hg@26
    75
	}
franta-hg@26
    76
franta-hg@26
    77
	public void setFormatters(List<FormatterDefinition> formatters) {
franta-hg@26
    78
		this.formatters = formatters;
franta-hg@26
    79
	}
franta-hg@29
    80
franta-hg@32
    81
	/**
franta-hg@32
    82
	 * @param name name of desired formatter. Looking for this name in user configuration, then in
franta-hg@32
    83
	 * buil-in formatters. If null, default from configuration or (if not configured) built-in
franta-hg@32
    84
	 * default is used.
franta-hg@32
    85
	 * @return formatter definition or null if none for this name is found
franta-hg@32
    86
	 */
franta-hg@29
    87
	public FormatterDefinition getFormatter(String name) {
franta-hg@32
    88
		if (name == null) {
franta-hg@32
    89
			if (defaultFormatter == null) {
franta-hg@32
    90
				return getFormatter(DEFAULT_FORMATTER);
franta-hg@32
    91
			} else {
franta-hg@32
    92
				return getFormatter(defaultFormatter);
franta-hg@32
    93
			}
franta-hg@32
    94
		} else {
franta-hg@32
    95
			FormatterDefinition fd = findByName(formatters, name);
franta-hg@32
    96
			return fd == null ? findByName(buildInFormatters, name) : fd;
franta-hg@32
    97
		}
franta-hg@29
    98
	}
franta-hg@29
    99
franta-hg@29
   100
	/**
franta-hg@29
   101
	 * @return name of default formatter, is used if name is not specified on CLI
franta-hg@29
   102
	 */
franta-hg@30
   103
	@XmlElement(name = "defaultFormatter", namespace = CONFIGURATION)
franta-hg@29
   104
	public String getDefaultFormatter() {
franta-hg@29
   105
		return defaultFormatter;
franta-hg@29
   106
	}
franta-hg@29
   107
franta-hg@29
   108
	public void setDefaultFormatter(String defaultFormatter) {
franta-hg@29
   109
		this.defaultFormatter = defaultFormatter;
franta-hg@29
   110
	}
franta-hg@20
   111
}