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