1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.data.validation;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FileNotFoundException;
|
---|
9 | import java.io.FileReader;
|
---|
10 | import java.io.FileWriter;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.io.PrintWriter;
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.Arrays;
|
---|
15 | import java.util.Collection;
|
---|
16 | import java.util.HashMap;
|
---|
17 | import java.util.Map;
|
---|
18 | import java.util.SortedMap;
|
---|
19 | import java.util.TreeMap;
|
---|
20 | import java.util.TreeSet;
|
---|
21 |
|
---|
22 | import javax.swing.JOptionPane;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
25 | import org.openstreetmap.josm.actions.ValidateAction;
|
---|
26 | import org.openstreetmap.josm.data.validation.tests.Addresses;
|
---|
27 | import org.openstreetmap.josm.data.validation.tests.BarriersEntrances;
|
---|
28 | import org.openstreetmap.josm.data.validation.tests.Coastlines;
|
---|
29 | import org.openstreetmap.josm.data.validation.tests.ConditionalKeys;
|
---|
30 | import org.openstreetmap.josm.data.validation.tests.CrossingWays;
|
---|
31 | import org.openstreetmap.josm.data.validation.tests.DuplicateNode;
|
---|
32 | import org.openstreetmap.josm.data.validation.tests.DuplicateRelation;
|
---|
33 | import org.openstreetmap.josm.data.validation.tests.DuplicateWay;
|
---|
34 | import org.openstreetmap.josm.data.validation.tests.DuplicatedWayNodes;
|
---|
35 | import org.openstreetmap.josm.data.validation.tests.Highways;
|
---|
36 | import org.openstreetmap.josm.data.validation.tests.Lanes;
|
---|
37 | import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
|
---|
38 | import org.openstreetmap.josm.data.validation.tests.MultipolygonTest;
|
---|
39 | import org.openstreetmap.josm.data.validation.tests.NameMismatch;
|
---|
40 | import org.openstreetmap.josm.data.validation.tests.OpeningHourTest;
|
---|
41 | import org.openstreetmap.josm.data.validation.tests.OverlappingWays;
|
---|
42 | import org.openstreetmap.josm.data.validation.tests.PowerLines;
|
---|
43 | import org.openstreetmap.josm.data.validation.tests.RelationChecker;
|
---|
44 | import org.openstreetmap.josm.data.validation.tests.SelfIntersectingWay;
|
---|
45 | import org.openstreetmap.josm.data.validation.tests.SimilarNamedWays;
|
---|
46 | import org.openstreetmap.josm.data.validation.tests.TagChecker;
|
---|
47 | import org.openstreetmap.josm.data.validation.tests.TurnrestrictionTest;
|
---|
48 | import org.openstreetmap.josm.data.validation.tests.UnclosedWays;
|
---|
49 | import org.openstreetmap.josm.data.validation.tests.UnconnectedWays;
|
---|
50 | import org.openstreetmap.josm.data.validation.tests.UntaggedNode;
|
---|
51 | import org.openstreetmap.josm.data.validation.tests.UntaggedWay;
|
---|
52 | import org.openstreetmap.josm.data.validation.tests.WayConnectedToArea;
|
---|
53 | import org.openstreetmap.josm.data.validation.tests.WronglyOrderedWays;
|
---|
54 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
55 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
56 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
57 | import org.openstreetmap.josm.gui.layer.ValidatorLayer;
|
---|
58 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
|
---|
59 | import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference;
|
---|
60 | import org.openstreetmap.josm.tools.Utils;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * A OSM data validator.
|
---|
64 | *
|
---|
65 | * @author Francisco R. Santos <frsantos@gmail.com>
|
---|
66 | */
|
---|
67 | public class OsmValidator implements LayerChangeListener {
|
---|
68 |
|
---|
69 | public static ValidatorLayer errorLayer = null;
|
---|
70 |
|
---|
71 | /** The validate action */
|
---|
72 | public ValidateAction validateAction = new ValidateAction();
|
---|
73 |
|
---|
74 | /** Grid detail, multiplier of east,north values for valuable cell sizing */
|
---|
75 | public static double griddetail;
|
---|
76 |
|
---|
77 | public static final Collection<String> ignoredErrors = new TreeSet<String>();
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * All available tests
|
---|
81 | * TODO: is there any way to find out automatically all available tests?
|
---|
82 | */
|
---|
83 | @SuppressWarnings("unchecked")
|
---|
84 | private static final Class<Test>[] allAvailableTests = new Class[] {
|
---|
85 | /* FIXME - unique error numbers for tests aren't properly unique - ignoring will not work as expected */
|
---|
86 | DuplicateNode.class, // ID 1 .. 99
|
---|
87 | OverlappingWays.class, // ID 101 .. 199
|
---|
88 | UntaggedNode.class, // ID 201 .. 299
|
---|
89 | UntaggedWay.class, // ID 301 .. 399
|
---|
90 | SelfIntersectingWay.class, // ID 401 .. 499
|
---|
91 | DuplicatedWayNodes.class, // ID 501 .. 599
|
---|
92 | CrossingWays.Ways.class, // ID 601 .. 699
|
---|
93 | CrossingWays.Boundaries.class, // ID 601 .. 699
|
---|
94 | CrossingWays.Barrier.class, // ID 601 .. 699
|
---|
95 | SimilarNamedWays.class, // ID 701 .. 799
|
---|
96 | Coastlines.class, // ID 901 .. 999
|
---|
97 | WronglyOrderedWays.class, // ID 1001 .. 1099
|
---|
98 | UnclosedWays.class, // ID 1101 .. 1199
|
---|
99 | TagChecker.class, // ID 1201 .. 1299
|
---|
100 | UnconnectedWays.UnconnectedHighways.class, // ID 1301 .. 1399
|
---|
101 | UnconnectedWays.UnconnectedRailways.class, // ID 1301 .. 1399
|
---|
102 | UnconnectedWays.UnconnectedWaterways.class, // ID 1301 .. 1399
|
---|
103 | UnconnectedWays.UnconnectedNaturalOrLanduse.class, // ID 1301 .. 1399
|
---|
104 | UnconnectedWays.UnconnectedPower.class, // ID 1301 .. 1399
|
---|
105 | DuplicateWay.class, // ID 1401 .. 1499
|
---|
106 | NameMismatch.class, // ID 1501 .. 1599
|
---|
107 | MultipolygonTest.class, // ID 1601 .. 1699
|
---|
108 | RelationChecker.class, // ID 1701 .. 1799
|
---|
109 | TurnrestrictionTest.class, // ID 1801 .. 1899
|
---|
110 | DuplicateRelation.class, // ID 1901 .. 1999
|
---|
111 | WayConnectedToArea.class, // ID 2301 .. 2399
|
---|
112 | PowerLines.class, // ID 2501 .. 2599
|
---|
113 | Addresses.class, // ID 2601 .. 2699
|
---|
114 | Highways.class, // ID 2701 .. 2799
|
---|
115 | BarriersEntrances.class, // ID 2801 .. 2899
|
---|
116 | OpeningHourTest.class, // 2901 .. 2999
|
---|
117 | MapCSSTagChecker.class, // 3000 .. 3099
|
---|
118 | Lanes.class, // 3100 .. 3199
|
---|
119 | ConditionalKeys.class, // 3200 .. 3299
|
---|
120 | };
|
---|
121 |
|
---|
122 | private static Map<String, Test> allTestsMap;
|
---|
123 | static {
|
---|
124 | allTestsMap = new HashMap<String, Test>();
|
---|
125 | for (Class<Test> testClass : allAvailableTests) {
|
---|
126 | try {
|
---|
127 | allTestsMap.put(testClass.getName(), testClass.newInstance());
|
---|
128 | } catch (Exception e) {
|
---|
129 | Main.error(e);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Constructs a new {@code OsmValidator}.
|
---|
136 | */
|
---|
137 | public OsmValidator() {
|
---|
138 | checkValidatorDir();
|
---|
139 | initializeGridDetail();
|
---|
140 | initializeTests(getTests());
|
---|
141 | loadIgnoredErrors(); //FIXME: load only when needed
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Returns the plugin's directory of the plugin
|
---|
146 | *
|
---|
147 | * @return The directory of the plugin
|
---|
148 | */
|
---|
149 | public static String getValidatorDir() {
|
---|
150 | return Main.pref.getPreferencesDir() + "validator/";
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Check if plugin directory exists (store ignored errors file)
|
---|
155 | */
|
---|
156 | private void checkValidatorDir() {
|
---|
157 | try {
|
---|
158 | File pathDir = new File(getValidatorDir());
|
---|
159 | if (!pathDir.exists()) {
|
---|
160 | pathDir.mkdirs();
|
---|
161 | }
|
---|
162 | } catch (Exception e){
|
---|
163 | Main.error(e);
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | private void loadIgnoredErrors() {
|
---|
168 | ignoredErrors.clear();
|
---|
169 | if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
|
---|
170 | BufferedReader in = null;
|
---|
171 | try {
|
---|
172 | in = new BufferedReader(new FileReader(getValidatorDir() + "ignorederrors"));
|
---|
173 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
174 | ignoredErrors.add(line);
|
---|
175 | }
|
---|
176 | } catch (final FileNotFoundException e) {
|
---|
177 | Main.debug(Main.getErrorMessage(e));
|
---|
178 | } catch (final IOException e) {
|
---|
179 | Main.error(e);
|
---|
180 | } finally {
|
---|
181 | Utils.close(in);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | public static void addIgnoredError(String s) {
|
---|
187 | ignoredErrors.add(s);
|
---|
188 | }
|
---|
189 |
|
---|
190 | public static boolean hasIgnoredError(String s) {
|
---|
191 | return ignoredErrors.contains(s);
|
---|
192 | }
|
---|
193 |
|
---|
194 | public static void saveIgnoredErrors() {
|
---|
195 | PrintWriter out = null;
|
---|
196 | try {
|
---|
197 | out = new PrintWriter(new FileWriter(getValidatorDir() + "ignorederrors"), false);
|
---|
198 | for (String e : ignoredErrors) {
|
---|
199 | out.println(e);
|
---|
200 | }
|
---|
201 | } catch (IOException e) {
|
---|
202 | Main.error(e);
|
---|
203 | } finally {
|
---|
204 | Utils.close(out);
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | public static void initializeErrorLayer() {
|
---|
209 | if (!Main.pref.getBoolean(ValidatorPreference.PREF_LAYER, true))
|
---|
210 | return;
|
---|
211 | if (errorLayer == null) {
|
---|
212 | errorLayer = new ValidatorLayer();
|
---|
213 | Main.main.addLayer(errorLayer);
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Gets a map from simple names to all tests.
|
---|
219 | * @return A map of all tests, indexed and sorted by the name of their Java class
|
---|
220 | */
|
---|
221 | public static SortedMap<String, Test> getAllTestsMap() {
|
---|
222 | applyPrefs(allTestsMap, false);
|
---|
223 | applyPrefs(allTestsMap, true);
|
---|
224 | return new TreeMap<String, Test>(allTestsMap);
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Returns the instance of the given test class.
|
---|
229 | * @param testClass The class of test to retrieve
|
---|
230 | * @return the instance of the given test class, if any, or {@code null}
|
---|
231 | * @since 6670
|
---|
232 | */
|
---|
233 | @SuppressWarnings("unchecked")
|
---|
234 | public static <T extends Test> T getTest(Class<T> testClass) {
|
---|
235 | if (testClass == null) {
|
---|
236 | return null;
|
---|
237 | }
|
---|
238 | return (T) allTestsMap.get(testClass.getName());
|
---|
239 | }
|
---|
240 |
|
---|
241 | private static void applyPrefs(Map<String, Test> tests, boolean beforeUpload) {
|
---|
242 | for(String testName : Main.pref.getCollection(beforeUpload
|
---|
243 | ? ValidatorPreference.PREF_SKIP_TESTS_BEFORE_UPLOAD : ValidatorPreference.PREF_SKIP_TESTS)) {
|
---|
244 | Test test = tests.get(testName);
|
---|
245 | if (test != null) {
|
---|
246 | if (beforeUpload) {
|
---|
247 | test.testBeforeUpload = false;
|
---|
248 | } else {
|
---|
249 | test.enabled = false;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | public static Collection<Test> getTests() {
|
---|
256 | return getAllTestsMap().values();
|
---|
257 | }
|
---|
258 |
|
---|
259 | public static Collection<Test> getEnabledTests(boolean beforeUpload) {
|
---|
260 | Collection<Test> enabledTests = getTests();
|
---|
261 | for (Test t : new ArrayList<Test>(enabledTests)) {
|
---|
262 | if (beforeUpload ? t.testBeforeUpload : t.enabled) {
|
---|
263 | continue;
|
---|
264 | }
|
---|
265 | enabledTests.remove(t);
|
---|
266 | }
|
---|
267 | return enabledTests;
|
---|
268 | }
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Gets the list of all available test classes
|
---|
272 | *
|
---|
273 | * @return An array of the test classes
|
---|
274 | */
|
---|
275 | public static Class<Test>[] getAllAvailableTests() {
|
---|
276 | return Arrays.copyOf(allAvailableTests, allAvailableTests.length);
|
---|
277 | }
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Initialize grid details based on current projection system. Values based on
|
---|
281 | * the original value fixed for EPSG:4326 (10000) using heuristics (that is, test&error
|
---|
282 | * until most bugs were discovered while keeping the processing time reasonable)
|
---|
283 | */
|
---|
284 | public void initializeGridDetail() {
|
---|
285 | String code = Main.getProjection().toCode();
|
---|
286 | if (Arrays.asList(ProjectionPreference.wgs84.allCodes()).contains(code)) {
|
---|
287 | OsmValidator.griddetail = 10000;
|
---|
288 | } else if (Arrays.asList(ProjectionPreference.mercator.allCodes()).contains(code)) {
|
---|
289 | OsmValidator.griddetail = 0.01;
|
---|
290 | } else if (Arrays.asList(ProjectionPreference.lambert.allCodes()).contains(code)) {
|
---|
291 | OsmValidator.griddetail = 0.1;
|
---|
292 | } else {
|
---|
293 | OsmValidator.griddetail = 1.0;
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Initializes all tests
|
---|
299 | * @param allTests The tests to initialize
|
---|
300 | */
|
---|
301 | public static void initializeTests(Collection<? extends Test> allTests) {
|
---|
302 | for (Test test : allTests) {
|
---|
303 | try {
|
---|
304 | if (test.enabled) {
|
---|
305 | test.initialize();
|
---|
306 | }
|
---|
307 | } catch (Exception e) {
|
---|
308 | Main.error(e);
|
---|
309 | JOptionPane.showMessageDialog(Main.parent,
|
---|
310 | tr("Error initializing test {0}:\n {1}", test.getClass()
|
---|
311 | .getSimpleName(), e),
|
---|
312 | tr("Error"),
|
---|
313 | JOptionPane.ERROR_MESSAGE);
|
---|
314 | }
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | /* -------------------------------------------------------------------------- */
|
---|
319 | /* interface LayerChangeListener */
|
---|
320 | /* -------------------------------------------------------------------------- */
|
---|
321 | @Override
|
---|
322 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
323 | }
|
---|
324 |
|
---|
325 | @Override
|
---|
326 | public void layerAdded(Layer newLayer) {
|
---|
327 | }
|
---|
328 |
|
---|
329 | @Override
|
---|
330 | public void layerRemoved(Layer oldLayer) {
|
---|
331 | if (oldLayer == errorLayer) {
|
---|
332 | errorLayer = null;
|
---|
333 | return;
|
---|
334 | }
|
---|
335 | if (Main.map.mapView.getLayersOfType(OsmDataLayer.class).isEmpty()) {
|
---|
336 | if (errorLayer != null) {
|
---|
337 | Main.main.removeLayer(errorLayer);
|
---|
338 | }
|
---|
339 | }
|
---|
340 | }
|
---|
341 | }
|
---|