3 * Copyright © 2013 František Kučera (frantovo.cz)
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.
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.
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/>.
18 package info.globalcode.sql.dk.configuration;
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;
39 * @author Ing. František Kučera (frantovo.cz)
41 @XmlRootElement(name = "configuration", namespace = CONFIGURATION)
42 public class Configuration {
44 private List<DatabaseDefinition> databases = new ArrayList<>();
45 private List<FormatterDefinition> formatters = new ArrayList<>();
47 * is used if no formatter is specified on CLI nor in user configuration
49 public static final String DEFAULT_FORMATTER = TabularFormatter.NAME;
51 * Can be used as default if prefetching is ok – for configuration listings (config is alread in
52 * memory, so this does not matter)
54 public static final String DEFAULT_FORMATTER_PREFETCHING = TabularPrefetchingFormatter.NAME;
55 private String defaultFormatter;
57 * Default list of formatters. Is used if particular name is not found in user configuration.
59 private static final Collection<FormatterDefinition> buildInFormatters;
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);
73 @XmlElement(name = "database", namespace = CONFIGURATION)
74 public List<DatabaseDefinition> getDatabases() {
78 public void setDatabases(List<DatabaseDefinition> databases) {
79 this.databases = databases;
83 * @throws ConfigurationException if no database with this name is configured
85 public DatabaseDefinition getDatabase(String name) throws ConfigurationException {
86 DatabaseDefinition dd = findByName(databases, name);
88 throw new ConfigurationException("Database is not configured: " + name);
95 * @return only configured formatters
96 * @see #getBuildInFormatters()
97 * @see #getAllFormatters()
99 @XmlElement(name = "formatter", namespace = CONFIGURATION)
100 public List<FormatterDefinition> getFormatters() {
104 public void setFormatters(List<FormatterDefinition> formatters) {
105 this.formatters = formatters;
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
112 * @return formatter definition
113 * @throws ConfigurationException if no formatter with this name was found
115 public FormatterDefinition getFormatter(String name) throws ConfigurationException {
117 return defaultFormatter == null ? getFormatter(DEFAULT_FORMATTER) : getFormatter(defaultFormatter);
119 FormatterDefinition fd = findByName(formatters, name);
120 fd = fd == null ? findByName(buildInFormatters, name) : fd;
122 throw new ConfigurationException("Formatter is not configured: " + name);
130 * @return only built-in formatters
131 * @see #getAllFormatters()
132 * @see #getFormatters()
135 public Collection<FormatterDefinition> getBuildInFormatters() {
136 return buildInFormatters;
140 * @return built-in + configured formatters
141 * @see #getFormatters()
144 public Collection<FormatterDefinition> getAllFormatters() {
145 Collection<FormatterDefinition> allFormatters = new ArrayList<>();
146 allFormatters.addAll(buildInFormatters);
147 allFormatters.addAll(formatters);
148 return allFormatters;
152 * @return name of default formatter, is used if name is not specified on CLI
154 @XmlElement(name = "defaultFormatter", namespace = CONFIGURATION)
155 public String getDefaultFormatter() {
156 return defaultFormatter;
159 public void setDefaultFormatter(String defaultFormatter) {
160 this.defaultFormatter = defaultFormatter;