source: josm/trunk/src/org/openstreetmap/josm/data/validation/OsmValidator.java@ 4048

Last change on this file since 4048 was 3946, checked in by stoecker, 13 years ago

readd stuff which should not have been removed

  • Property svn:eol-style set to native
File size: 10.6 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.data.validation;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.BufferedReader;
7import java.io.File;
8import java.io.FileNotFoundException;
9import java.io.FileReader;
10import java.io.FileWriter;
11import java.io.IOException;
12import java.io.PrintWriter;
13import java.util.ArrayList;
14import java.util.Collection;
15import java.util.HashMap;
16import java.util.Map;
17import java.util.TreeSet;
18import java.util.regex.Matcher;
19import java.util.regex.Pattern;
20
21import javax.swing.JOptionPane;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.actions.ValidateAction;
25import org.openstreetmap.josm.actions.upload.ValidateUploadHook;
26import org.openstreetmap.josm.data.projection.Epsg4326;
27import org.openstreetmap.josm.data.projection.Lambert;
28import org.openstreetmap.josm.data.projection.Mercator;
29import org.openstreetmap.josm.data.validation.tests.Coastlines;
30import org.openstreetmap.josm.data.validation.tests.CrossingWays;
31import org.openstreetmap.josm.data.validation.tests.DuplicateNode;
32import org.openstreetmap.josm.data.validation.tests.DuplicateWay;
33import org.openstreetmap.josm.data.validation.tests.DuplicatedWayNodes;
34import org.openstreetmap.josm.data.validation.tests.MultipolygonTest;
35import org.openstreetmap.josm.data.validation.tests.NameMismatch;
36import org.openstreetmap.josm.data.validation.tests.NodesWithSameName;
37import org.openstreetmap.josm.data.validation.tests.OverlappingWays;
38import org.openstreetmap.josm.data.validation.tests.RelationChecker;
39import org.openstreetmap.josm.data.validation.tests.SelfIntersectingWay;
40import org.openstreetmap.josm.data.validation.tests.SimilarNamedWays;
41import org.openstreetmap.josm.data.validation.tests.TagChecker;
42import org.openstreetmap.josm.data.validation.tests.TurnrestrictionTest;
43import org.openstreetmap.josm.data.validation.tests.UnclosedWays;
44import org.openstreetmap.josm.data.validation.tests.UnconnectedWays;
45import org.openstreetmap.josm.data.validation.tests.UntaggedNode;
46import org.openstreetmap.josm.data.validation.tests.UntaggedWay;
47import org.openstreetmap.josm.data.validation.tests.WronglyOrderedWays;
48import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
49import org.openstreetmap.josm.gui.layer.ValidatorLayer;
50import org.openstreetmap.josm.gui.layer.Layer;
51import org.openstreetmap.josm.gui.layer.OsmDataLayer;
52import org.openstreetmap.josm.gui.preferences.ValidatorPreference;
53
54/**
55 *
56 * A OSM data validator
57 *
58 * @author Francisco R. Santos <frsantos@gmail.com>
59 */
60public class OsmValidator implements LayerChangeListener {
61
62 public static ValidatorLayer errorLayer = null;
63
64 /** The validate action */
65 public ValidateAction validateAction = new ValidateAction();
66
67 /** Grid detail, multiplier of east,north values for valuable cell sizing */
68 public static double griddetail;
69
70 public static Collection<String> ignoredErrors = new TreeSet<String>();
71
72 /**
73 * All available tests
74 * TODO: is there any way to find out automatically all available tests?
75 */
76 @SuppressWarnings("unchecked")
77 public static Class<Test>[] allAvailableTests = new Class[] {
78 DuplicateNode.class, // ID 1 .. 99
79 OverlappingWays.class, // ID 101 .. 199
80 UntaggedNode.class, // ID 201 .. 299
81 UntaggedWay.class, // ID 301 .. 399
82 SelfIntersectingWay.class, // ID 401 .. 499
83 DuplicatedWayNodes.class, // ID 501 .. 599
84 CrossingWays.class, // ID 601 .. 699
85 SimilarNamedWays.class, // ID 701 .. 799
86 NodesWithSameName.class, // ID 801 .. 899
87 Coastlines.class, // ID 901 .. 999
88 WronglyOrderedWays.class, // ID 1001 .. 1099
89 UnclosedWays.class, // ID 1101 .. 1199
90 TagChecker.class, // ID 1201 .. 1299
91 UnconnectedWays.class, // ID 1301 .. 1399
92 DuplicateWay.class, // ID 1401 .. 1499
93 NameMismatch.class, // ID 1501 .. 1599
94 MultipolygonTest.class, // ID 1601 .. 1699
95 RelationChecker.class, // ID 1701 .. 1799
96 TurnrestrictionTest.class, // ID 1801 .. 1899
97 };
98
99 public OsmValidator() {
100 checkValidatorDir();
101 initializeGridDetail();
102 initializeTests(getTests());
103 loadIgnoredErrors(); //FIXME: load only when needed
104 }
105
106 /**
107 * Returns the plugin's directory of the plugin
108 *
109 * @return The directory of the plugin
110 */
111 public static String getValidatorDir()
112 {
113 return Main.pref.getPreferencesDir() + "validator/";
114 }
115
116 /**
117 * Check if plugin directory exists (store ignored errors file)
118 */
119 private void checkValidatorDir() {
120 try {
121 File pathDir = new File(getValidatorDir());
122 if (!pathDir.exists()) {
123 pathDir.mkdirs();
124 }
125 } catch (Exception e){
126 e.printStackTrace();
127 }
128 }
129
130 private void loadIgnoredErrors() {
131 ignoredErrors.clear();
132 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
133 try {
134 final BufferedReader in = new BufferedReader(new FileReader(getValidatorDir() + "ignorederrors"));
135 for (String line = in.readLine(); line != null; line = in.readLine()) {
136 ignoredErrors.add(line);
137 }
138 } catch (final FileNotFoundException e) {
139 // Ignore
140 } catch (final IOException e) {
141 e.printStackTrace();
142 }
143 }
144 }
145
146 public static void addIgnoredError(String s) {
147 ignoredErrors.add(s);
148 }
149
150 public static boolean hasIgnoredError(String s) {
151 return ignoredErrors.contains(s);
152 }
153
154 public static void saveIgnoredErrors() {
155 try {
156 final PrintWriter out = new PrintWriter(new FileWriter(getValidatorDir() + "ignorederrors"), false);
157 for (String e : ignoredErrors) {
158 out.println(e);
159 }
160 out.close();
161 } catch (final IOException e) {
162 e.printStackTrace();
163 }
164 }
165
166 public static void initializeErrorLayer() {
167 if (!Main.pref.getBoolean(ValidatorPreference.PREF_LAYER, true))
168 return;
169 if (errorLayer == null) {
170 errorLayer = new ValidatorLayer();
171 Main.main.addLayer(errorLayer);
172 }
173 }
174
175 /** Gets a map from simple names to all tests. */
176 public static Map<String, Test> getAllTestsMap() {
177 Map<String, Test> tests = new HashMap<String, Test>();
178 for (Class<Test> testClass : getAllAvailableTests()) {
179 try {
180 Test test = testClass.newInstance();
181 tests.put(testClass.getSimpleName(), test);
182 } catch (Exception e) {
183 e.printStackTrace();
184 continue;
185 }
186 }
187 applyPrefs(tests, false);
188 applyPrefs(tests, true);
189 return tests;
190 }
191
192 private static void applyPrefs(Map<String, Test> tests, boolean beforeUpload) {
193 Pattern regexp = Pattern.compile("(\\w+)=(true|false),?");
194 Matcher m = regexp.matcher(Main.pref.get(beforeUpload ? ValidatorPreference.PREF_TESTS_BEFORE_UPLOAD
195 : ValidatorPreference.PREF_TESTS));
196 int pos = 0;
197 while (m.find(pos)) {
198 String testName = m.group(1);
199 Test test = tests.get(testName);
200 if (test != null) {
201 boolean enabled = Boolean.valueOf(m.group(2));
202 if (beforeUpload) {
203 test.testBeforeUpload = enabled;
204 } else {
205 test.enabled = enabled;
206 }
207 }
208 pos = m.end();
209 }
210 }
211
212 public static Collection<Test> getTests() {
213 return getAllTestsMap().values();
214 }
215
216 public static Collection<Test> getEnabledTests(boolean beforeUpload) {
217 Collection<Test> enabledTests = getTests();
218 for (Test t : new ArrayList<Test>(enabledTests)) {
219 if (beforeUpload ? t.testBeforeUpload : t.enabled) {
220 continue;
221 }
222 enabledTests.remove(t);
223 }
224 return enabledTests;
225 }
226
227 /**
228 * Gets the list of all available test classes
229 *
230 * @return An array of the test classes
231 */
232 public static Class<Test>[] getAllAvailableTests() {
233 return allAvailableTests;
234 }
235
236 /**
237 * Initialize grid details based on current projection system. Values based on
238 * the original value fixed for EPSG:4326 (10000) using heuristics (that is, test&error
239 * until most bugs were discovered while keeping the processing time reasonable)
240 */
241 public void initializeGridDetail() {
242 if (Main.proj.toString().equals(new Epsg4326().toString())) {
243 OsmValidator.griddetail = 10000;
244 } else if (Main.proj.toString().equals(new Mercator().toString())) {
245 OsmValidator.griddetail = 0.01;
246 } else if (Main.proj.toString().equals(new Lambert().toString())) {
247 OsmValidator.griddetail = 0.1;
248 }
249 }
250
251 /**
252 * Initializes all tests
253 * @param allTests The tests to initialize
254 */
255 public static void initializeTests(Collection<Test> allTests) {
256 for (Test test : allTests) {
257 try {
258 if (test.enabled) {
259 test.initialize();
260 }
261 } catch (Exception e) {
262 e.printStackTrace();
263 JOptionPane.showMessageDialog(Main.parent,
264 tr("Error initializing test {0}:\n {1}", test.getClass()
265 .getSimpleName(), e),
266 tr("Error"),
267 JOptionPane.ERROR_MESSAGE);
268 }
269 }
270 }
271
272 /* -------------------------------------------------------------------------- */
273 /* interface LayerChangeListener */
274 /* -------------------------------------------------------------------------- */
275 @Override
276 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
277 }
278
279 @Override
280 public void layerAdded(Layer newLayer) {
281 }
282
283 @Override
284 public void layerRemoved(Layer oldLayer) {
285 if (oldLayer instanceof OsmDataLayer && Main.map.mapView.getActiveLayer() == oldLayer) {
286 Main.map.validatorDialog.tree.setErrorList(new ArrayList<TestError>());
287 }
288 if (oldLayer == errorLayer) {
289 errorLayer = null;
290 return;
291 }
292 if (Main.map.mapView.getLayersOfType(OsmDataLayer.class).isEmpty()) {
293 if (errorLayer != null) {
294 Main.map.mapView.removeLayer(errorLayer);
295 }
296 }
297 }
298}
Note: See TracBrowser for help on using the repository browser.