1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.validation;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GraphicsEnvironment;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FileNotFoundException;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.PrintWriter;
|
---|
11 | import java.nio.charset.StandardCharsets;
|
---|
12 | import java.nio.file.Files;
|
---|
13 | import java.nio.file.Path;
|
---|
14 | import java.nio.file.Paths;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.Arrays;
|
---|
17 | import java.util.Collection;
|
---|
18 | import java.util.Collections;
|
---|
19 | import java.util.HashMap;
|
---|
20 | import java.util.Map;
|
---|
21 | import java.util.SortedMap;
|
---|
22 | import java.util.TreeMap;
|
---|
23 | import java.util.TreeSet;
|
---|
24 |
|
---|
25 | import javax.swing.JOptionPane;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.data.validation.tests.Addresses;
|
---|
29 | import org.openstreetmap.josm.data.validation.tests.ApiCapabilitiesTest;
|
---|
30 | import org.openstreetmap.josm.data.validation.tests.BarriersEntrances;
|
---|
31 | import org.openstreetmap.josm.data.validation.tests.Coastlines;
|
---|
32 | import org.openstreetmap.josm.data.validation.tests.ConditionalKeys;
|
---|
33 | import org.openstreetmap.josm.data.validation.tests.CrossingWays;
|
---|
34 | import org.openstreetmap.josm.data.validation.tests.DuplicateNode;
|
---|
35 | import org.openstreetmap.josm.data.validation.tests.DuplicateRelation;
|
---|
36 | import org.openstreetmap.josm.data.validation.tests.DuplicateWay;
|
---|
37 | import org.openstreetmap.josm.data.validation.tests.DuplicatedWayNodes;
|
---|
38 | import org.openstreetmap.josm.data.validation.tests.Highways;
|
---|
39 | import org.openstreetmap.josm.data.validation.tests.InternetTags;
|
---|
40 | import org.openstreetmap.josm.data.validation.tests.Lanes;
|
---|
41 | import org.openstreetmap.josm.data.validation.tests.LongSegment;
|
---|
42 | import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
|
---|
43 | import org.openstreetmap.josm.data.validation.tests.MultipolygonTest;
|
---|
44 | import org.openstreetmap.josm.data.validation.tests.NameMismatch;
|
---|
45 | import org.openstreetmap.josm.data.validation.tests.OpeningHourTest;
|
---|
46 | import org.openstreetmap.josm.data.validation.tests.OverlappingWays;
|
---|
47 | import org.openstreetmap.josm.data.validation.tests.PowerLines;
|
---|
48 | import org.openstreetmap.josm.data.validation.tests.PublicTransportRouteTest;
|
---|
49 | import org.openstreetmap.josm.data.validation.tests.RelationChecker;
|
---|
50 | import org.openstreetmap.josm.data.validation.tests.SelfIntersectingWay;
|
---|
51 | import org.openstreetmap.josm.data.validation.tests.SimilarNamedWays;
|
---|
52 | import org.openstreetmap.josm.data.validation.tests.TagChecker;
|
---|
53 | import org.openstreetmap.josm.data.validation.tests.TurnrestrictionTest;
|
---|
54 | import org.openstreetmap.josm.data.validation.tests.UnclosedWays;
|
---|
55 | import org.openstreetmap.josm.data.validation.tests.UnconnectedWays;
|
---|
56 | import org.openstreetmap.josm.data.validation.tests.UntaggedNode;
|
---|
57 | import org.openstreetmap.josm.data.validation.tests.UntaggedWay;
|
---|
58 | import org.openstreetmap.josm.data.validation.tests.WayConnectedToArea;
|
---|
59 | import org.openstreetmap.josm.data.validation.tests.WronglyOrderedWays;
|
---|
60 | import org.openstreetmap.josm.gui.layer.ValidatorLayer;
|
---|
61 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
|
---|
62 | import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference;
|
---|
63 | import org.openstreetmap.josm.tools.Logging;
|
---|
64 | import org.openstreetmap.josm.tools.Utils;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * A OSM data validator.
|
---|
68 | *
|
---|
69 | * @author Francisco R. Santos <frsantos@gmail.com>
|
---|
70 | */
|
---|
71 | public final class OsmValidator {
|
---|
72 |
|
---|
73 | private OsmValidator() {
|
---|
74 | // Hide default constructor for utilities classes
|
---|
75 | }
|
---|
76 |
|
---|
77 | private static volatile ValidatorLayer errorLayer;
|
---|
78 |
|
---|
79 | /** Grid detail, multiplier of east,north values for valuable cell sizing */
|
---|
80 | private static double griddetail;
|
---|
81 |
|
---|
82 | private static final Collection<String> ignoredErrors = new TreeSet<>();
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * All registered tests
|
---|
86 | */
|
---|
87 | private static final Collection<Class<? extends Test>> allTests = new ArrayList<>();
|
---|
88 | private static final Map<String, Test> allTestsMap = new HashMap<>();
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * All available tests in core
|
---|
92 | */
|
---|
93 | @SuppressWarnings("unchecked")
|
---|
94 | private static final Class<Test>[] CORE_TEST_CLASSES = new Class[] {
|
---|
95 | /* FIXME - unique error numbers for tests aren't properly unique - ignoring will not work as expected */
|
---|
96 | DuplicateNode.class, // ID 1 .. 99
|
---|
97 | OverlappingWays.class, // ID 101 .. 199
|
---|
98 | UntaggedNode.class, // ID 201 .. 299
|
---|
99 | UntaggedWay.class, // ID 301 .. 399
|
---|
100 | SelfIntersectingWay.class, // ID 401 .. 499
|
---|
101 | DuplicatedWayNodes.class, // ID 501 .. 599
|
---|
102 | CrossingWays.Ways.class, // ID 601 .. 699
|
---|
103 | CrossingWays.Boundaries.class, // ID 601 .. 699
|
---|
104 | CrossingWays.Barrier.class, // ID 601 .. 699
|
---|
105 | CrossingWays.SelfCrossing.class, // ID 601 .. 699
|
---|
106 | SimilarNamedWays.class, // ID 701 .. 799
|
---|
107 | Coastlines.class, // ID 901 .. 999
|
---|
108 | WronglyOrderedWays.class, // ID 1001 .. 1099
|
---|
109 | UnclosedWays.class, // ID 1101 .. 1199
|
---|
110 | TagChecker.class, // ID 1201 .. 1299
|
---|
111 | UnconnectedWays.UnconnectedHighways.class, // ID 1301 .. 1399
|
---|
112 | UnconnectedWays.UnconnectedRailways.class, // ID 1301 .. 1399
|
---|
113 | UnconnectedWays.UnconnectedWaterways.class, // ID 1301 .. 1399
|
---|
114 | UnconnectedWays.UnconnectedNaturalOrLanduse.class, // ID 1301 .. 1399
|
---|
115 | UnconnectedWays.UnconnectedPower.class, // ID 1301 .. 1399
|
---|
116 | DuplicateWay.class, // ID 1401 .. 1499
|
---|
117 | NameMismatch.class, // ID 1501 .. 1599
|
---|
118 | MultipolygonTest.class, // ID 1601 .. 1699
|
---|
119 | RelationChecker.class, // ID 1701 .. 1799
|
---|
120 | TurnrestrictionTest.class, // ID 1801 .. 1899
|
---|
121 | DuplicateRelation.class, // ID 1901 .. 1999
|
---|
122 | WayConnectedToArea.class, // ID 2301 .. 2399
|
---|
123 | PowerLines.class, // ID 2501 .. 2599
|
---|
124 | Addresses.class, // ID 2601 .. 2699
|
---|
125 | Highways.class, // ID 2701 .. 2799
|
---|
126 | BarriersEntrances.class, // ID 2801 .. 2899
|
---|
127 | OpeningHourTest.class, // 2901 .. 2999
|
---|
128 | MapCSSTagChecker.class, // 3000 .. 3099
|
---|
129 | Lanes.class, // 3100 .. 3199
|
---|
130 | ConditionalKeys.class, // 3200 .. 3299
|
---|
131 | InternetTags.class, // 3300 .. 3399
|
---|
132 | ApiCapabilitiesTest.class, // 3400 .. 3499
|
---|
133 | LongSegment.class, // 3500 .. 3599
|
---|
134 | PublicTransportRouteTest.class, // 3600 .. 3699
|
---|
135 | };
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Adds a test to the list of available tests
|
---|
139 | * @param testClass The test class
|
---|
140 | */
|
---|
141 | public static void addTest(Class<? extends Test> testClass) {
|
---|
142 | allTests.add(testClass);
|
---|
143 | try {
|
---|
144 | allTestsMap.put(testClass.getName(), testClass.getConstructor().newInstance());
|
---|
145 | } catch (ReflectiveOperationException e) {
|
---|
146 | Logging.error(e);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | static {
|
---|
151 | for (Class<? extends Test> testClass : CORE_TEST_CLASSES) {
|
---|
152 | addTest(testClass);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Initializes {@code OsmValidator}.
|
---|
158 | */
|
---|
159 | public static void initialize() {
|
---|
160 | checkValidatorDir();
|
---|
161 | initializeGridDetail();
|
---|
162 | loadIgnoredErrors(); //FIXME: load only when needed
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Returns the validator directory.
|
---|
167 | *
|
---|
168 | * @return The validator directory
|
---|
169 | */
|
---|
170 | public static String getValidatorDir() {
|
---|
171 | return new File(Main.pref.getUserDataDirectory(), "validator").getAbsolutePath();
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Check if validator directory exists (store ignored errors file)
|
---|
176 | */
|
---|
177 | private static void checkValidatorDir() {
|
---|
178 | File pathDir = new File(getValidatorDir());
|
---|
179 | if (!pathDir.exists()) {
|
---|
180 | Utils.mkDirs(pathDir);
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | private static void loadIgnoredErrors() {
|
---|
185 | ignoredErrors.clear();
|
---|
186 | if (ValidatorPreference.PREF_USE_IGNORE.get()) {
|
---|
187 | Path path = Paths.get(getValidatorDir()).resolve("ignorederrors");
|
---|
188 | if (path.toFile().exists()) {
|
---|
189 | try {
|
---|
190 | ignoredErrors.addAll(Files.readAllLines(path, StandardCharsets.UTF_8));
|
---|
191 | } catch (final FileNotFoundException e) {
|
---|
192 | Logging.debug(Logging.getErrorMessage(e));
|
---|
193 | } catch (final IOException e) {
|
---|
194 | Logging.error(e);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Adds an ignored error
|
---|
202 | * @param s The ignore group / sub group name
|
---|
203 | * @see TestError#getIgnoreGroup()
|
---|
204 | * @see TestError#getIgnoreSubGroup()
|
---|
205 | */
|
---|
206 | public static void addIgnoredError(String s) {
|
---|
207 | ignoredErrors.add(s);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Check if a error should be ignored
|
---|
212 | * @param s The ignore group / sub group name
|
---|
213 | * @return <code>true</code> to ignore that error
|
---|
214 | */
|
---|
215 | public static boolean hasIgnoredError(String s) {
|
---|
216 | return ignoredErrors.contains(s);
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Saves the names of the ignored errors to a file
|
---|
221 | */
|
---|
222 | public static void saveIgnoredErrors() {
|
---|
223 | try (PrintWriter out = new PrintWriter(new File(getValidatorDir(), "ignorederrors"), StandardCharsets.UTF_8.name())) {
|
---|
224 | for (String e : ignoredErrors) {
|
---|
225 | out.println(e);
|
---|
226 | }
|
---|
227 | } catch (IOException e) {
|
---|
228 | Logging.error(e);
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Initializes error layer.
|
---|
234 | */
|
---|
235 | public static synchronized void initializeErrorLayer() {
|
---|
236 | if (!ValidatorPreference.PREF_LAYER.get())
|
---|
237 | return;
|
---|
238 | if (errorLayer == null) {
|
---|
239 | errorLayer = new ValidatorLayer();
|
---|
240 | Main.getLayerManager().addLayer(errorLayer);
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Resets error layer.
|
---|
246 | * @since 11852
|
---|
247 | */
|
---|
248 | public static synchronized void resetErrorLayer() {
|
---|
249 | errorLayer = null;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Gets a map from simple names to all tests.
|
---|
254 | * @return A map of all tests, indexed and sorted by the name of their Java class
|
---|
255 | */
|
---|
256 | public static SortedMap<String, Test> getAllTestsMap() {
|
---|
257 | applyPrefs(allTestsMap, false);
|
---|
258 | applyPrefs(allTestsMap, true);
|
---|
259 | return new TreeMap<>(allTestsMap);
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Returns the instance of the given test class.
|
---|
264 | * @param <T> testClass type
|
---|
265 | * @param testClass The class of test to retrieve
|
---|
266 | * @return the instance of the given test class, if any, or {@code null}
|
---|
267 | * @since 6670
|
---|
268 | */
|
---|
269 | @SuppressWarnings("unchecked")
|
---|
270 | public static <T extends Test> T getTest(Class<T> testClass) {
|
---|
271 | if (testClass == null) {
|
---|
272 | return null;
|
---|
273 | }
|
---|
274 | return (T) allTestsMap.get(testClass.getName());
|
---|
275 | }
|
---|
276 |
|
---|
277 | private static void applyPrefs(Map<String, Test> tests, boolean beforeUpload) {
|
---|
278 | for (String testName : Main.pref.getCollection(beforeUpload
|
---|
279 | ? ValidatorPreference.PREF_SKIP_TESTS_BEFORE_UPLOAD : ValidatorPreference.PREF_SKIP_TESTS)) {
|
---|
280 | Test test = tests.get(testName);
|
---|
281 | if (test != null) {
|
---|
282 | if (beforeUpload) {
|
---|
283 | test.testBeforeUpload = false;
|
---|
284 | } else {
|
---|
285 | test.enabled = false;
|
---|
286 | }
|
---|
287 | }
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Gets all tests that are possible
|
---|
293 | * @return The tests
|
---|
294 | */
|
---|
295 | public static Collection<Test> getTests() {
|
---|
296 | return getAllTestsMap().values();
|
---|
297 | }
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Gets all tests that are run
|
---|
301 | * @param beforeUpload To get the ones that are run before upload
|
---|
302 | * @return The tests
|
---|
303 | */
|
---|
304 | public static Collection<Test> getEnabledTests(boolean beforeUpload) {
|
---|
305 | Collection<Test> enabledTests = getTests();
|
---|
306 | for (Test t : new ArrayList<>(enabledTests)) {
|
---|
307 | if (beforeUpload ? t.testBeforeUpload : t.enabled) {
|
---|
308 | continue;
|
---|
309 | }
|
---|
310 | enabledTests.remove(t);
|
---|
311 | }
|
---|
312 | return enabledTests;
|
---|
313 | }
|
---|
314 |
|
---|
315 | /**
|
---|
316 | * Gets the list of all available test classes
|
---|
317 | *
|
---|
318 | * @return A collection of the test classes
|
---|
319 | */
|
---|
320 | public static Collection<Class<? extends Test>> getAllAvailableTestClasses() {
|
---|
321 | return Collections.unmodifiableCollection(allTests);
|
---|
322 | }
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Initialize grid details based on current projection system. Values based on
|
---|
326 | * the original value fixed for EPSG:4326 (10000) using heuristics (that is, test&error
|
---|
327 | * until most bugs were discovered while keeping the processing time reasonable)
|
---|
328 | */
|
---|
329 | public static void initializeGridDetail() {
|
---|
330 | String code = Main.getProjection().toCode();
|
---|
331 | if (Arrays.asList(ProjectionPreference.wgs84.allCodes()).contains(code)) {
|
---|
332 | OsmValidator.griddetail = 10_000;
|
---|
333 | } else if (Arrays.asList(ProjectionPreference.mercator.allCodes()).contains(code)) {
|
---|
334 | OsmValidator.griddetail = 0.01;
|
---|
335 | } else if (Arrays.asList(ProjectionPreference.lambert.allCodes()).contains(code)) {
|
---|
336 | OsmValidator.griddetail = 0.1;
|
---|
337 | } else {
|
---|
338 | OsmValidator.griddetail = 1.0;
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Returns grid detail, multiplier of east,north values for valuable cell sizing
|
---|
344 | * @return grid detail
|
---|
345 | * @since 11852
|
---|
346 | */
|
---|
347 | public static double getGridDetail() {
|
---|
348 | return griddetail;
|
---|
349 | }
|
---|
350 |
|
---|
351 | private static boolean testsInitialized;
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Initializes all tests if this operations hasn't been performed already.
|
---|
355 | */
|
---|
356 | public static synchronized void initializeTests() {
|
---|
357 | if (!testsInitialized) {
|
---|
358 | Logging.debug("Initializing validator tests");
|
---|
359 | final long startTime = System.currentTimeMillis();
|
---|
360 | initializeTests(getTests());
|
---|
361 | testsInitialized = true;
|
---|
362 | if (Logging.isDebugEnabled()) {
|
---|
363 | final long elapsedTime = System.currentTimeMillis() - startTime;
|
---|
364 | Logging.debug("Initializing validator tests completed in {0}", Utils.getDurationString(elapsedTime));
|
---|
365 | }
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Initializes all tests
|
---|
371 | * @param allTests The tests to initialize
|
---|
372 | */
|
---|
373 | public static void initializeTests(Collection<? extends Test> allTests) {
|
---|
374 | for (Test test : allTests) {
|
---|
375 | try {
|
---|
376 | if (test.enabled) {
|
---|
377 | test.initialize();
|
---|
378 | }
|
---|
379 | } catch (Exception e) { // NOPMD
|
---|
380 | Logging.error(e);
|
---|
381 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
382 | JOptionPane.showMessageDialog(Main.parent,
|
---|
383 | tr("Error initializing test {0}:\n {1}", test.getClass().getSimpleName(), e),
|
---|
384 | tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
385 | }
|
---|
386 | }
|
---|
387 | }
|
---|
388 | }
|
---|
389 |
|
---|
390 | }
|
---|