avoid NullPointerException (value = null) while duplicating to java.util.Properties
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.DsvFormatter;
23 import info.globalcode.sql.dk.formatting.SilentFormatter;
24 import info.globalcode.sql.dk.formatting.SingleValueFormatter;
25 import info.globalcode.sql.dk.formatting.TabularFormatter;
26 import info.globalcode.sql.dk.formatting.TabularPrefetchingFormatter;
27 import info.globalcode.sql.dk.formatting.TabularWrappingFormatter;
28 import info.globalcode.sql.dk.formatting.TeXFormatter;
29 import info.globalcode.sql.dk.formatting.XhtmlFormatter;
30 import info.globalcode.sql.dk.formatting.XmlFormatter;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.List;
35 import javax.xml.bind.annotation.XmlElement;
36 import javax.xml.bind.annotation.XmlRootElement;
37 import javax.xml.bind.annotation.XmlTransient;
40 * Object representation of user configuration loaded from XML.
42 * @author Ing. František Kučera (frantovo.cz)
44 @XmlRootElement(name = "configuration", namespace = CONFIGURATION)
45 public class Configuration {
47 private List<DatabaseDefinition> databases = new ArrayList<>();
48 private List<FormatterDefinition> formatters = new ArrayList<>();
50 * is used if no formatter is specified on CLI nor in user configuration
52 public static final String DEFAULT_FORMATTER = TabularFormatter.NAME;
54 * Can be used as default if prefetching is ok – for configuration listings (config is alread in
55 * memory, so this does not matter)
57 public static final String DEFAULT_FORMATTER_PREFETCHING = TabularPrefetchingFormatter.NAME;
58 private String defaultFormatter;
60 * Default list of formatters. Is used if particular name is not found in user configuration.
62 private static final Collection<FormatterDefinition> buildInFormatters;
65 Collection<FormatterDefinition> l = new ArrayList<>();
66 l.add(new FormatterDefinition(SilentFormatter.NAME, SilentFormatter.class.getName()));
67 l.add(new FormatterDefinition(SingleValueFormatter.NAME, SingleValueFormatter.class.getName()));
68 l.add(new FormatterDefinition(XmlFormatter.NAME, XmlFormatter.class.getName()));
69 l.add(new FormatterDefinition(XhtmlFormatter.NAME, XhtmlFormatter.class.getName()));
70 l.add(new FormatterDefinition(TabularFormatter.NAME, TabularFormatter.class.getName()));
71 l.add(new FormatterDefinition(TabularPrefetchingFormatter.NAME, TabularPrefetchingFormatter.class.getName()));
72 l.add(new FormatterDefinition(TabularWrappingFormatter.NAME, TabularWrappingFormatter.class.getName()));
73 l.add(new FormatterDefinition(TeXFormatter.NAME, TeXFormatter.class.getName()));
74 l.add(new FormatterDefinition(DsvFormatter.NAME, DsvFormatter.class.getName()));
75 buildInFormatters = Collections.unmodifiableCollection(l);
78 @XmlElement(name = "database", namespace = CONFIGURATION)
79 public List<DatabaseDefinition> getDatabases() {
83 public void setDatabases(List<DatabaseDefinition> databases) {
84 this.databases = databases;
88 * @throws ConfigurationException if no database with this name is configured
90 public DatabaseDefinition getDatabase(String name) throws ConfigurationException {
91 DatabaseDefinition dd = findByName(databases, name);
93 throw new ConfigurationException("Database is not configured: " + name);
100 * @return only configured formatters
101 * @see #getBuildInFormatters()
102 * @see #getAllFormatters()
104 @XmlElement(name = "formatter", namespace = CONFIGURATION)
105 public List<FormatterDefinition> getFormatters() {
109 public void setFormatters(List<FormatterDefinition> formatters) {
110 this.formatters = formatters;
114 * @param name name of desired formatter. Looking for this name in user configuration, then in
115 * buil-in formatters. If null, default from configuration or (if not configured) built-in
117 * @return formatter definition
118 * @throws ConfigurationException if no formatter with this name was found
120 public FormatterDefinition getFormatter(String name) throws ConfigurationException {
122 return defaultFormatter == null ? getFormatter(DEFAULT_FORMATTER) : getFormatter(defaultFormatter);
124 FormatterDefinition fd = findByName(formatters, name);
125 fd = fd == null ? findByName(buildInFormatters, name) : fd;
127 throw new ConfigurationException("Formatter is not configured: " + name);
135 * @return only built-in formatters
136 * @see #getAllFormatters()
137 * @see #getFormatters()
140 public Collection<FormatterDefinition> getBuildInFormatters() {
141 return buildInFormatters;
145 * @return built-in + configured formatters
146 * @see #getFormatters()
149 public Collection<FormatterDefinition> getAllFormatters() {
150 Collection<FormatterDefinition> allFormatters = new ArrayList<>();
151 allFormatters.addAll(buildInFormatters);
152 allFormatters.addAll(formatters);
153 return allFormatters;
157 * @return name of default formatter, is used if name is not specified on CLI
159 @XmlElement(name = "defaultFormatter", namespace = CONFIGURATION)
160 public String getDefaultFormatter() {
161 return defaultFormatter;
164 public void setDefaultFormatter(String defaultFormatter) {
165 this.defaultFormatter = defaultFormatter;