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