ValidationConfiguration.java 6.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CityDoctor2 is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.check;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import de.hft.stuttgart.citydoctor2.checks.CheckPrototype;
import de.hft.stuttgart.citydoctor2.checks.Checks;
import de.hft.stuttgart.citydoctor2.parser.ParserConfiguration;
Matthias Betz's avatar
Matthias Betz committed
42
import de.hft.stuttgart.citydoctor2.utils.Localization;
43
44
45
46
47
48
49
50
51
52
53
54
55

/**
 * The validation configuration class represented in the yaml configuration
 * file. Here configuration files can be loaded and written as well as filled
 * with missing data, when the configuration file is not complete.
 * 
 * @author Matthias Betz
 *
 */
public class ValidationConfiguration implements Serializable {

	private static final long serialVersionUID = -8020055032177740646L;

Matthias Betz's avatar
Matthias Betz committed
56
	private static final Logger logger = LogManager.getLogger(ValidationConfiguration.class);
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

	private int numberOfRoundingPlaces = 8;
	private String schematronFilePath = null;

	private boolean xmlValidation = false;
	private boolean useStreaming = false;
	private FilterConfiguration filter;
	private Map<CheckId, CheckConfiguration> checks;

	private ParserConfiguration parserConfig;

	public static ValidationConfiguration loadValidationConfig(String validationFile) throws FileNotFoundException {
		return loadValidationConfig(new FileInputStream(validationFile));
	}

	public static ValidationConfiguration loadValidationConfig(InputStream stream) {
		Yaml yaml = new Yaml(new Constructor(ValidationConfiguration.class));
		ValidationConfiguration config = yaml.load(stream);
		config.validateConfiguration();
		return config;
	}

	public static ValidationConfiguration loadStandardValidationConfig() {
		ValidationConfiguration config = new ValidationConfiguration();
		config.checks = new EnumMap<>(CheckId.class);
		for (CheckPrototype c : Checks.getCheckPrototypes()) {
			CheckConfiguration cConfig = new CheckConfiguration();
			cConfig.setEnabled(true);
			if (!c.getDefaultParameter().isEmpty()) {
				Map<String, String> paramMap = new HashMap<>();
				for (DefaultParameter param : c.getDefaultParameter()) {
					paramMap.put(param.getName(), param.getDefaultValue());
				}
				cConfig.setParameters(paramMap);
			}
			config.checks.put(c.getCheckId(), cConfig);
		}
		return config;
	}

	public void saveAs(File f) throws IOException {
		DumperOptions options = new DumperOptions();
		options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
		Yaml yaml = new Yaml(options);
		try (BufferedWriter bw = new BufferedWriter(new FileWriter(f))) {
			yaml.dump(this, bw);
		}
	}

	public boolean isXmlValidation() {
		return xmlValidation;
	}

	public void setXmlValidation(boolean xmlValidation) {
		this.xmlValidation = xmlValidation;
	}

	public String getSchematronFilePath() {
		return schematronFilePath;
	}

	public void setSchematronFilePath(String schematronFilePath) {
		this.schematronFilePath = schematronFilePath;
	}

	public boolean isUseStreaming() {
		return useStreaming;
	}

	public void setUseStreaming(boolean useStreaming) {
		this.useStreaming = useStreaming;
	}

	public FilterConfiguration getFilter() {
		return filter;
	}

	public void setFilter(FilterConfiguration filter) {
		this.filter = filter;
	}

	public Map<CheckId, CheckConfiguration> getChecks() {
		if (checks == null) {
			checks = new EnumMap<>(CheckId.class);
		}
		return checks;
	}

	public void setChecks(Map<CheckId, CheckConfiguration> checks) {
		this.checks = checks;
	}

	public void validateConfiguration() {
		for (CheckPrototype c : Checks.getCheckPrototypes()) {
			// enable all checks that are missing in the validation config
			checks.computeIfAbsent(c.getCheckId(), id -> {
				CheckConfiguration cConfig = new CheckConfiguration();
				cConfig.setEnabled(true);
				return cConfig;
			});
		}
		for (java.util.Map.Entry<CheckId, CheckConfiguration> e : checks.entrySet()) {
			if (!e.getValue().isEnabled()) {
				continue;
			}
			CheckPrototype c = Checks.getCheckPrototypeForId(e.getKey());
			for (CheckId dep : c.getDependencies()) {
				CheckConfiguration checkConfig = checks.get(dep);
				if (!checkConfig.isEnabled()) {
					checkConfig.setEnabled(true);
Matthias Betz's avatar
Matthias Betz committed
167
					logger.warn(Localization.getText("ValidationConfiguration.reenable"), dep, c.getCheckId());
168
169
170
171
172
173
174
175
				}
			}
			insertMissingParametersWithDefaultParameters(e, c);
		}
		if (schematronFilePath != null) {
			File f = new File(schematronFilePath);
			if (!f.exists() || !f.isFile()) {
				schematronFilePath = null;
Matthias Betz's avatar
Matthias Betz committed
176
				logger.warn(Localization.getText("ValidationConfiguration.missingSchematron"), f.getAbsolutePath());
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
			}
		}
	}

	private void insertMissingParametersWithDefaultParameters(java.util.Map.Entry<CheckId, CheckConfiguration> e,
			CheckPrototype c) {
		for (DefaultParameter param : c.getDefaultParameter()) {
			String configuredValue = e.getValue().getParameters().get(param.getName());
			if (configuredValue == null) {
				e.getValue().getParameters().put(param.getName(), param.getDefaultValue());
			}
		}
	}

	public int getNumberOfRoundingPlaces() {
		return numberOfRoundingPlaces;
	}

	public void setNumberOfRoundingPlaces(int numberOfRoundingPlaces) {
		this.numberOfRoundingPlaces = numberOfRoundingPlaces;
	}

	public ParserConfiguration getParserConfiguration() {
		if (parserConfig == null) {
			parserConfig = new ParserConfiguration(numberOfRoundingPlaces, xmlValidation);
		}
		return parserConfig;
	}
	
	public void setParserConfiguration(ParserConfiguration parserConfig) {
		this.parserConfig = parserConfig;
	}

}