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