java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 04 Jan 2014 20:08:56 +0100
branchv_0
changeset 130 8548e21177f9
parent 128 67f5ff139da0
child 155 eb3676c6929b
permissions -rw-r--r--
XML formatter: option for preserve whitespace or indent text with line breaks
     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.TabularPrefetchingFormatter;
    26 import info.globalcode.sql.dk.formatting.TabularWrappingFormatter;
    27 import info.globalcode.sql.dk.formatting.XhtmlFormatter;
    28 import info.globalcode.sql.dk.formatting.XmlFormatter;
    29 import java.util.ArrayList;
    30 import java.util.Collection;
    31 import java.util.Collections;
    32 import java.util.List;
    33 import javax.xml.bind.annotation.XmlElement;
    34 import javax.xml.bind.annotation.XmlRootElement;
    35 import javax.xml.bind.annotation.XmlTransient;
    36 
    37 /**
    38  *
    39  * @author Ing. František Kučera (frantovo.cz)
    40  */
    41 @XmlRootElement(name = "configuration", namespace = CONFIGURATION)
    42 public class Configuration {
    43 
    44 	private List<DatabaseDefinition> databases = new ArrayList<>();
    45 	private List<FormatterDefinition> formatters = new ArrayList<>();
    46 	/**
    47 	 * is used if no formatter is specified on CLI nor in user configuration
    48 	 */
    49 	public static final String DEFAULT_FORMATTER = TabularFormatter.NAME;
    50 	/**
    51 	 * Can be used as default if prefetching is ok – for configuration listings (config is alread in
    52 	 * memory, so this does not matter)
    53 	 */
    54 	public static final String DEFAULT_FORMATTER_PREFETCHING = TabularPrefetchingFormatter.NAME;
    55 	private String defaultFormatter;
    56 	/**
    57 	 * Default list of formatters. Is used if particular name is not found in user configuration.
    58 	 */
    59 	private static final Collection<FormatterDefinition> buildInFormatters;
    60 
    61 	static {
    62 		Collection<FormatterDefinition> l = new ArrayList<>();
    63 		l.add(new FormatterDefinition(SilentFormatter.NAME, SilentFormatter.class.getName()));
    64 		l.add(new FormatterDefinition(SingleValueFormatter.NAME, SingleValueFormatter.class.getName()));
    65 		l.add(new FormatterDefinition(XmlFormatter.NAME, XmlFormatter.class.getName()));
    66 		l.add(new FormatterDefinition(XhtmlFormatter.NAME, XhtmlFormatter.class.getName()));
    67 		l.add(new FormatterDefinition(TabularFormatter.NAME, TabularFormatter.class.getName()));
    68 		l.add(new FormatterDefinition(TabularPrefetchingFormatter.NAME, TabularPrefetchingFormatter.class.getName()));
    69 		l.add(new FormatterDefinition(TabularWrappingFormatter.NAME, TabularWrappingFormatter.class.getName()));
    70 		buildInFormatters = Collections.unmodifiableCollection(l);
    71 	}
    72 
    73 	@XmlElement(name = "database", namespace = CONFIGURATION)
    74 	public List<DatabaseDefinition> getDatabases() {
    75 		return databases;
    76 	}
    77 
    78 	public void setDatabases(List<DatabaseDefinition> databases) {
    79 		this.databases = databases;
    80 	}
    81 
    82 	/**
    83 	 * @throws ConfigurationException if no database with this name is configured
    84 	 */
    85 	public DatabaseDefinition getDatabase(String name) throws ConfigurationException {
    86 		DatabaseDefinition dd = findByName(databases, name);
    87 		if (dd == null) {
    88 			throw new ConfigurationException("Database is not configured: " + name);
    89 		} else {
    90 			return dd;
    91 		}
    92 	}
    93 
    94 	/**
    95 	 * @return only configured formatters
    96 	 * @see #getBuildInFormatters()
    97 	 * @see #getAllFormatters()
    98 	 */
    99 	@XmlElement(name = "formatter", namespace = CONFIGURATION)
   100 	public List<FormatterDefinition> getFormatters() {
   101 		return formatters;
   102 	}
   103 
   104 	public void setFormatters(List<FormatterDefinition> formatters) {
   105 		this.formatters = formatters;
   106 	}
   107 
   108 	/**
   109 	 * @param name name of desired formatter. Looking for this name in user configuration, then in
   110 	 * buil-in formatters. If null, default from configuration or (if not configured) built-in
   111 	 * default is used.
   112 	 * @return formatter definition
   113 	 * @throws ConfigurationException if no formatter with this name was found
   114 	 */
   115 	public FormatterDefinition getFormatter(String name) throws ConfigurationException {
   116 		if (name == null) {
   117 			return defaultFormatter == null ? getFormatter(DEFAULT_FORMATTER) : getFormatter(defaultFormatter);
   118 		} else {
   119 			FormatterDefinition fd = findByName(formatters, name);
   120 			fd = fd == null ? findByName(buildInFormatters, name) : fd;
   121 			if (fd == null) {
   122 				throw new ConfigurationException("Formatter is not configured: " + name);
   123 			} else {
   124 				return fd;
   125 			}
   126 		}
   127 	}
   128 
   129 	/**
   130 	 * @return only built-in formatters
   131 	 * @see #getAllFormatters()
   132 	 * @see #getFormatters()
   133 	 */
   134 	@XmlTransient
   135 	public Collection<FormatterDefinition> getBuildInFormatters() {
   136 		return buildInFormatters;
   137 	}
   138 
   139 	/**
   140 	 * @return built-in + configured formatters
   141 	 * @see #getFormatters()
   142 	 */
   143 	@XmlTransient
   144 	public Collection<FormatterDefinition> getAllFormatters() {
   145 		Collection<FormatterDefinition> allFormatters = new ArrayList<>();
   146 		allFormatters.addAll(buildInFormatters);
   147 		allFormatters.addAll(formatters);
   148 		return allFormatters;
   149 	}
   150 
   151 	/**
   152 	 * @return name of default formatter, is used if name is not specified on CLI
   153 	 */
   154 	@XmlElement(name = "defaultFormatter", namespace = CONFIGURATION)
   155 	public String getDefaultFormatter() {
   156 		return defaultFormatter;
   157 	}
   158 
   159 	public void setDefaultFormatter(String defaultFormatter) {
   160 		this.defaultFormatter = defaultFormatter;
   161 	}
   162 }