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

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

remove broken dead code

  • Property svn:eol-style set to native
File size: 9.8 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 public static Collection<String> ignoredErrors = new TreeSet<String>();
68
69 /**
70 * All available tests
71 * TODO: is there any way to find out automatically all available tests?
72 */
73 @SuppressWarnings("unchecked")
74 public static Class<Test>[] allAvailableTests = new Class[] {
75 DuplicateNode.class, // ID 1 .. 99
76 OverlappingWays.class, // ID 101 .. 199
77 UntaggedNode.class, // ID 201 .. 299
78 UntaggedWay.class, // ID 301 .. 399
79 SelfIntersectingWay.class, // ID 401 .. 499
80 DuplicatedWayNodes.class, // ID 501 .. 599
81 CrossingWays.class, // ID 601 .. 699
82 SimilarNamedWays.class, // ID 701 .. 799
83 NodesWithSameName.class, // ID 801 .. 899
84 Coastlines.class, // ID 901 .. 999
85 WronglyOrderedWays.class, // ID 1001 .. 1099
86 UnclosedWays.class, // ID 1101 .. 1199
87 TagChecker.class, // ID 1201 .. 1299
88 UnconnectedWays.class, // ID 1301 .. 1399
89 DuplicateWay.class, // ID 1401 .. 1499
90 NameMismatch.class, // ID 1501 .. 1599
91 MultipolygonTest.class, // ID 1601 .. 1699
92 RelationChecker.class, // ID 1701 .. 1799
93 TurnrestrictionTest.class, // ID 1801 .. 1899
94 };
95
96 public OsmValidator() {
97 checkValidatorDir();
98 initializeTests(getTests());
99 loadIgnoredErrors(); //FIXME: load only when needed
100 }
101
102 /**
103 * Returns the plugin's directory of the plugin
104 *
105 * @return The directory of the plugin
106 */
107 public static String getValidatorDir()
108 {
109 return Main.pref.getPreferencesDir() + "validator/";
110 }
111
112 /**
113 * Check if plugin directory exists (store ignored errors file)
114 */
115 private void checkValidatorDir() {
116 try {
117 File pathDir = new File(getValidatorDir());
118 if (!pathDir.exists()) {
119 pathDir.mkdirs();
120 }
121 } catch (Exception e){
122 e.printStackTrace();
123 }
124 }
125
126 private void loadIgnoredErrors() {
127 ignoredErrors.clear();
128 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
129 try {
130 final BufferedReader in = new BufferedReader(new FileReader(getValidatorDir() + "ignorederrors"));
131 for (String line = in.readLine(); line != null; line = in.readLine()) {
132 ignoredErrors.add(line);
133 }
134 } catch (final FileNotFoundException e) {
135 // Ignore
136 } catch (final IOException e) {
137 e.printStackTrace();
138 }
139 }
140 }
141
142 public static void addIgnoredError(String s) {
143 ignoredErrors.add(s);
144 }
145
146 public static boolean hasIgnoredError(String s) {
147 return ignoredErrors.contains(s);
148 }
149
150 public static void saveIgnoredErrors() {
151 try {
152 final PrintWriter out = new PrintWriter(new FileWriter(getValidatorDir()
153 + "ignorederrors"), false);
154 for (String e : ignoredErrors) {
155 out.println(e);
156 }
157 out.close();
158 } catch (final IOException e) {
159 e.printStackTrace();
160 }
161 }
162
163 public static void initializeErrorLayer() {
164 if (!Main.pref.getBoolean(ValidatorPreference.PREF_LAYER, true))
165 return;
166 if (errorLayer == null) {
167 errorLayer = new ValidatorLayer();
168 Main.main.addLayer(errorLayer);
169 }
170 }
171
172 /** Gets a map from simple names to all tests. */
173 public static Map<String, Test> getAllTestsMap() {
174 Map<String, Test> tests = new HashMap<String, Test>();
175 for (Class<Test> testClass : getAllAvailableTests()) {
176 try {
177 Test test = testClass.newInstance();
178 tests.put(testClass.getSimpleName(), test);
179 } catch (Exception e) {
180 e.printStackTrace();
181 continue;
182 }
183 }
184 applyPrefs(tests, false);
185 applyPrefs(tests, true);
186 return tests;
187 }
188
189 private static void applyPrefs(Map<String, Test> tests, boolean beforeUpload) {
190 Pattern regexp = Pattern.compile("(\\w+)=(true|false),?");
191 Matcher m = regexp.matcher(Main.pref.get(beforeUpload ? ValidatorPreference.PREF_TESTS_BEFORE_UPLOAD
192 : ValidatorPreference.PREF_TESTS));
193 int pos = 0;
194 while (m.find(pos)) {
195 String testName = m.group(1);
196 Test test = tests.get(testName);
197 if (test != null) {
198 boolean enabled = Boolean.valueOf(m.group(2));
199 if (beforeUpload) {
200 test.testBeforeUpload = enabled;
201 } else {
202 test.enabled = enabled;
203 }
204 }
205 pos = m.end();
206 }
207 }
208
209 public static Collection<Test> getTests() {
210 return getAllTestsMap().values();
211 }
212
213 public static Collection<Test> getEnabledTests(boolean beforeUpload) {
214 Collection<Test> enabledTests = getTests();
215 for (Test t : new ArrayList<Test>(enabledTests)) {
216 if (beforeUpload ? t.testBeforeUpload : t.enabled) {
217 continue;
218 }
219 enabledTests.remove(t);
220 }
221 return enabledTests;
222 }
223
224 /**
225 * Gets the list of all available test classes
226 *
227 * @return An array of the test classes
228 */
229 public static Class<Test>[] getAllAvailableTests() {
230 return allAvailableTests;
231 }
232
233 /**
234 * Initializes all tests
235 * @param allTests The tests to initialize
236 */
237 public static void initializeTests(Collection<Test> allTests) {
238 for (Test test : allTests) {
239 try {
240 if (test.enabled) {
241 test.initialize();
242 }
243 } catch (Exception e) {
244 e.printStackTrace();
245 JOptionPane.showMessageDialog(Main.parent,
246 tr("Error initializing test {0}:\n {1}", test.getClass()
247 .getSimpleName(), e),
248 tr("Error"),
249 JOptionPane.ERROR_MESSAGE);
250 }
251 }
252 }
253
254 /* -------------------------------------------------------------------------- */
255 /* interface LayerChangeListener */
256 /* -------------------------------------------------------------------------- */
257 @Override
258 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
259 }
260
261 @Override
262 public void layerAdded(Layer newLayer) {
263 }
264
265 @Override
266 public void layerRemoved(Layer oldLayer) {
267 if (oldLayer instanceof OsmDataLayer && Main.map.mapView.getActiveLayer() == oldLayer) {
268 Main.map.validatorDialog.tree.setErrorList(new ArrayList<TestError>());
269 }
270 if (oldLayer == errorLayer) {
271 errorLayer = null;
272 return;
273 }
274 if (Main.map.mapView.getLayersOfType(OsmDataLayer.class).isEmpty()) {
275 if (errorLayer != null) {
276 Main.map.mapView.removeLayer(errorLayer);
277 }
278 }
279 }
280}
Note: See TracBrowser for help on using the repository browser.