ValidationConfiguration.java 7.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
/*-
 *  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;
38
39
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Representer;
40
41
42
43

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
44
import de.hft.stuttgart.citydoctor2.utils.Localization;
45
46
47
48
49
50
51
52
53
54
55
56
57

/**
 * 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
58
	private static final Logger logger = LogManager.getLogger(ValidationConfiguration.class);
59
60

	private int numberOfRoundingPlaces = 8;
61
62
	private double minVertexDistance = 0.0001;

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
	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);
104
		options.setPrettyFlow(true);
Matthias Betz's avatar
Matthias Betz committed
105
		Representer rep = new ValidationConfigurationRepresenter();
106
107
		rep.addClassTag(ValidationConfiguration.class, Tag.MAP);
		Yaml yaml = new Yaml(rep, options);
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
		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;
	}

156
157
158
159
160
161
162
	/**
	 * Validates the configuration, adds all missing checks as enabled, reenables
	 * checks that are necessary to perform other checks
	 * 
	 * @return true if it reenabled disabled checks, false otherwise
	 */
	public boolean validateConfiguration() {
163
164
165
166
167
168
169
170
		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;
			});
		}
171
		boolean reenabledChecks = reenableNecessaryChecks();
Matthias Betz's avatar
Matthias Betz committed
172
173
174
175
176
177
178
179
180
		if (schematronFilePath != null && !schematronFilePath.isEmpty()) {
			File f = new File(schematronFilePath);
			if (!f.exists() || !f.isFile()) {
				schematronFilePath = null;
				if (logger.isWarnEnabled()) {
					logger.warn(Localization.getText("ValidationConfiguration.missingSchematron"), f.getAbsolutePath());
				}
			}
		}
181
		return reenabledChecks;
Matthias Betz's avatar
Matthias Betz committed
182
183
	}

184
185
	private boolean reenableNecessaryChecks() {
		boolean reenabledChecks = false;
186
187
188
189
190
191
192
193
194
		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);
195
					reenabledChecks = true;
Matthias Betz's avatar
Matthias Betz committed
196
197
198
					if (logger.isWarnEnabled()) {
						logger.warn(Localization.getText("ValidationConfiguration.reenable"), dep, c.getCheckId());
					}
199
200
201
202
				}
			}
			insertMissingParametersWithDefaultParameters(e, c);
		}
203
		return reenabledChecks;
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
	}

	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;
	}
230
231

	public void setParserConfig(ParserConfiguration parserConfig) {
232
233
234
		this.parserConfig = parserConfig;
	}

235
236
237
238
239
240
241
	public void setMinVertexDistance(double minVertexDistance) {
		this.minVertexDistance = minVertexDistance;
	}

	public double getMinVertexDistance() {
		return minVertexDistance;
	}
242
}