[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[3669] | 2 | package org.openstreetmap.josm.data.validation;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[11594] | 6 | import java.awt.GraphicsEnvironment;
|
---|
[3669] | 7 | import java.io.File;
|
---|
| 8 | import java.io.FileNotFoundException;
|
---|
| 9 | import java.io.IOException;
|
---|
[7082] | 10 | import java.nio.charset.StandardCharsets;
|
---|
[7315] | 11 | import java.nio.file.Files;
|
---|
| 12 | import java.nio.file.Path;
|
---|
| 13 | import java.nio.file.Paths;
|
---|
[3669] | 14 | import java.util.ArrayList;
|
---|
[5548] | 15 | import java.util.Arrays;
|
---|
[3669] | 16 | import java.util.Collection;
|
---|
[10297] | 17 | import java.util.Collections;
|
---|
[12667] | 18 | import java.util.EnumMap;
|
---|
[14828] | 19 | import java.util.Enumeration;
|
---|
[3669] | 20 | import java.util.HashMap;
|
---|
[14828] | 21 | import java.util.Iterator;
|
---|
[12667] | 22 | import java.util.List;
|
---|
[3669] | 23 | import java.util.Map;
|
---|
[14828] | 24 | import java.util.Map.Entry;
|
---|
[6691] | 25 | import java.util.SortedMap;
|
---|
| 26 | import java.util.TreeMap;
|
---|
[3669] | 27 | import java.util.TreeSet;
|
---|
[12667] | 28 | import java.util.function.Predicate;
|
---|
[14851] | 29 | import java.util.regex.Pattern;
|
---|
[12667] | 30 | import java.util.stream.Collectors;
|
---|
[3669] | 31 |
|
---|
| 32 | import javax.swing.JOptionPane;
|
---|
[14828] | 33 | import javax.swing.JTree;
|
---|
| 34 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
| 35 | import javax.swing.tree.TreeModel;
|
---|
| 36 | import javax.swing.tree.TreeNode;
|
---|
[3669] | 37 |
|
---|
[12649] | 38 | import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
|
---|
[14120] | 39 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
[5644] | 40 | import org.openstreetmap.josm.data.validation.tests.Addresses;
|
---|
[7574] | 41 | import org.openstreetmap.josm.data.validation.tests.ApiCapabilitiesTest;
|
---|
[6192] | 42 | import org.openstreetmap.josm.data.validation.tests.BarriersEntrances;
|
---|
[3669] | 43 | import org.openstreetmap.josm.data.validation.tests.Coastlines;
|
---|
[6605] | 44 | import org.openstreetmap.josm.data.validation.tests.ConditionalKeys;
|
---|
[16295] | 45 | import org.openstreetmap.josm.data.validation.tests.ConnectivityRelations;
|
---|
[3669] | 46 | import org.openstreetmap.josm.data.validation.tests.CrossingWays;
|
---|
[19062] | 47 | import org.openstreetmap.josm.data.validation.tests.CycleDetector;
|
---|
[17349] | 48 | import org.openstreetmap.josm.data.validation.tests.DirectionNodes;
|
---|
[3669] | 49 | import org.openstreetmap.josm.data.validation.tests.DuplicateNode;
|
---|
[4126] | 50 | import org.openstreetmap.josm.data.validation.tests.DuplicateRelation;
|
---|
[3669] | 51 | import org.openstreetmap.josm.data.validation.tests.DuplicateWay;
|
---|
| 52 | import org.openstreetmap.josm.data.validation.tests.DuplicatedWayNodes;
|
---|
[5902] | 53 | import org.openstreetmap.josm.data.validation.tests.Highways;
|
---|
[7489] | 54 | import org.openstreetmap.josm.data.validation.tests.InternetTags;
|
---|
[6592] | 55 | import org.openstreetmap.josm.data.validation.tests.Lanes;
|
---|
[8320] | 56 | import org.openstreetmap.josm.data.validation.tests.LongSegment;
|
---|
[6506] | 57 | import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
|
---|
[3669] | 58 | import org.openstreetmap.josm.data.validation.tests.MultipolygonTest;
|
---|
| 59 | import org.openstreetmap.josm.data.validation.tests.NameMismatch;
|
---|
[6370] | 60 | import org.openstreetmap.josm.data.validation.tests.OpeningHourTest;
|
---|
[3669] | 61 | import org.openstreetmap.josm.data.validation.tests.OverlappingWays;
|
---|
[5300] | 62 | import org.openstreetmap.josm.data.validation.tests.PowerLines;
|
---|
[9933] | 63 | import org.openstreetmap.josm.data.validation.tests.PublicTransportRouteTest;
|
---|
[3669] | 64 | import org.openstreetmap.josm.data.validation.tests.RelationChecker;
|
---|
[13670] | 65 | import org.openstreetmap.josm.data.validation.tests.RightAngleBuildingTest;
|
---|
[3669] | 66 | import org.openstreetmap.josm.data.validation.tests.SelfIntersectingWay;
|
---|
[15405] | 67 | import org.openstreetmap.josm.data.validation.tests.SharpAngles;
|
---|
[3669] | 68 | import org.openstreetmap.josm.data.validation.tests.SimilarNamedWays;
|
---|
| 69 | import org.openstreetmap.josm.data.validation.tests.TagChecker;
|
---|
| 70 | import org.openstreetmap.josm.data.validation.tests.TurnrestrictionTest;
|
---|
| 71 | import org.openstreetmap.josm.data.validation.tests.UnclosedWays;
|
---|
| 72 | import org.openstreetmap.josm.data.validation.tests.UnconnectedWays;
|
---|
| 73 | import org.openstreetmap.josm.data.validation.tests.UntaggedNode;
|
---|
| 74 | import org.openstreetmap.josm.data.validation.tests.UntaggedWay;
|
---|
[4682] | 75 | import org.openstreetmap.josm.data.validation.tests.WayConnectedToArea;
|
---|
[3669] | 76 | import org.openstreetmap.josm.data.validation.tests.WronglyOrderedWays;
|
---|
[12636] | 77 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[4126] | 78 | import org.openstreetmap.josm.gui.layer.ValidatorLayer;
|
---|
[5548] | 79 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
|
---|
[13647] | 80 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[12846] | 81 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
[12667] | 82 | import org.openstreetmap.josm.tools.AlphanumComparator;
|
---|
[12620] | 83 | import org.openstreetmap.josm.tools.Logging;
|
---|
[15755] | 84 | import org.openstreetmap.josm.tools.Stopwatch;
|
---|
[18208] | 85 | import org.openstreetmap.josm.tools.Utils;
|
---|
[3669] | 86 |
|
---|
| 87 | /**
|
---|
[6472] | 88 | * A OSM data validator.
|
---|
[3669] | 89 | *
|
---|
[6830] | 90 | * @author Francisco R. Santos <frsantos@gmail.com>
|
---|
[3669] | 91 | */
|
---|
[10759] | 92 | public final class OsmValidator {
|
---|
[3669] | 93 |
|
---|
[10759] | 94 | private OsmValidator() {
|
---|
| 95 | // Hide default constructor for utilities classes
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[11852] | 98 | private static volatile ValidatorLayer errorLayer;
|
---|
[3669] | 99 |
|
---|
[3946] | 100 | /** Grid detail, multiplier of east,north values for valuable cell sizing */
|
---|
[11852] | 101 | private static double griddetail;
|
---|
[3946] | 102 |
|
---|
[14828] | 103 | private static final SortedMap<String, String> ignoredErrors = new TreeMap<>();
|
---|
[3669] | 104 | /**
|
---|
[10297] | 105 | * All registered tests
|
---|
[3669] | 106 | */
|
---|
[12542] | 107 | private static final Collection<Class<? extends Test>> allTests = new ArrayList<>();
|
---|
| 108 | private static final Map<String, Test> allTestsMap = new HashMap<>();
|
---|
[10297] | 109 |
|
---|
| 110 | /**
|
---|
| 111 | * All available tests in core
|
---|
| 112 | */
|
---|
[3669] | 113 | @SuppressWarnings("unchecked")
|
---|
[15588] | 114 | private static final Class<Test>[] CORE_TEST_CLASSES = new Class[] {// NOPMD
|
---|
[6593] | 115 | /* FIXME - unique error numbers for tests aren't properly unique - ignoring will not work as expected */
|
---|
[18636] | 116 | /* Error codes are class.getName().hashCode() + "_" + oldCode. There should almost never be a collision. */
|
---|
[4126] | 117 | DuplicateNode.class, // ID 1 .. 99
|
---|
| 118 | OverlappingWays.class, // ID 101 .. 199
|
---|
| 119 | UntaggedNode.class, // ID 201 .. 299
|
---|
| 120 | UntaggedWay.class, // ID 301 .. 399
|
---|
| 121 | SelfIntersectingWay.class, // ID 401 .. 499
|
---|
| 122 | DuplicatedWayNodes.class, // ID 501 .. 599
|
---|
[6581] | 123 | CrossingWays.Ways.class, // ID 601 .. 699
|
---|
| 124 | CrossingWays.Boundaries.class, // ID 601 .. 699
|
---|
[11136] | 125 | CrossingWays.SelfCrossing.class, // ID 601 .. 699
|
---|
[4126] | 126 | SimilarNamedWays.class, // ID 701 .. 799
|
---|
| 127 | Coastlines.class, // ID 901 .. 999
|
---|
| 128 | WronglyOrderedWays.class, // ID 1001 .. 1099
|
---|
| 129 | UnclosedWays.class, // ID 1101 .. 1199
|
---|
| 130 | TagChecker.class, // ID 1201 .. 1299
|
---|
[6515] | 131 | UnconnectedWays.UnconnectedHighways.class, // ID 1301 .. 1399
|
---|
| 132 | UnconnectedWays.UnconnectedRailways.class, // ID 1301 .. 1399
|
---|
| 133 | UnconnectedWays.UnconnectedWaterways.class, // ID 1301 .. 1399
|
---|
| 134 | UnconnectedWays.UnconnectedNaturalOrLanduse.class, // ID 1301 .. 1399
|
---|
| 135 | UnconnectedWays.UnconnectedPower.class, // ID 1301 .. 1399
|
---|
[4126] | 136 | DuplicateWay.class, // ID 1401 .. 1499
|
---|
| 137 | NameMismatch.class, // ID 1501 .. 1599
|
---|
| 138 | MultipolygonTest.class, // ID 1601 .. 1699
|
---|
| 139 | RelationChecker.class, // ID 1701 .. 1799
|
---|
| 140 | TurnrestrictionTest.class, // ID 1801 .. 1899
|
---|
| 141 | DuplicateRelation.class, // ID 1901 .. 1999
|
---|
[4682] | 142 | WayConnectedToArea.class, // ID 2301 .. 2399
|
---|
[5300] | 143 | PowerLines.class, // ID 2501 .. 2599
|
---|
[5644] | 144 | Addresses.class, // ID 2601 .. 2699
|
---|
[5902] | 145 | Highways.class, // ID 2701 .. 2799
|
---|
[6192] | 146 | BarriersEntrances.class, // ID 2801 .. 2899
|
---|
[6506] | 147 | OpeningHourTest.class, // 2901 .. 2999
|
---|
| 148 | MapCSSTagChecker.class, // 3000 .. 3099
|
---|
[6592] | 149 | Lanes.class, // 3100 .. 3199
|
---|
[6605] | 150 | ConditionalKeys.class, // 3200 .. 3299
|
---|
[7489] | 151 | InternetTags.class, // 3300 .. 3399
|
---|
[7574] | 152 | ApiCapabilitiesTest.class, // 3400 .. 3499
|
---|
[8320] | 153 | LongSegment.class, // 3500 .. 3599
|
---|
[9933] | 154 | PublicTransportRouteTest.class, // 3600 .. 3699
|
---|
[18637] | 155 | // 3700 .. 3799 is automatically removed since it clashed with pt_assistant.
|
---|
[15405] | 156 | SharpAngles.class, // 3800 .. 3899
|
---|
[16295] | 157 | ConnectivityRelations.class, // 3900 .. 3999
|
---|
[19062] | 158 | DirectionNodes.class, // 4000 .. 4099
|
---|
[18637] | 159 | RightAngleBuildingTest.class, // 4100 .. 4199
|
---|
[19062] | 160 | CycleDetector.class, // 4200 .. 4299
|
---|
[3669] | 161 | };
|
---|
[6830] | 162 |
|
---|
[12390] | 163 | /**
|
---|
| 164 | * Adds a test to the list of available tests
|
---|
| 165 | * @param testClass The test class
|
---|
| 166 | */
|
---|
[10297] | 167 | public static void addTest(Class<? extends Test> testClass) {
|
---|
[12542] | 168 | allTests.add(testClass);
|
---|
[10297] | 169 | try {
|
---|
[12542] | 170 | allTestsMap.put(testClass.getName(), testClass.getConstructor().newInstance());
|
---|
[10297] | 171 | } catch (ReflectiveOperationException e) {
|
---|
[12620] | 172 | Logging.error(e);
|
---|
[10297] | 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[15603] | 176 | /**
|
---|
| 177 | * Removes a test from the list of available tests. This will not remove
|
---|
| 178 | * core tests.
|
---|
| 179 | *
|
---|
| 180 | * @param testClass The test class
|
---|
| 181 | * @return {@code true} if the test was removed (see {@link Collection#remove})
|
---|
| 182 | * @since 15603
|
---|
| 183 | */
|
---|
| 184 | public static boolean removeTest(Class<? extends Test> testClass) {
|
---|
| 185 | boolean removed = false;
|
---|
| 186 | if (!Arrays.asList(CORE_TEST_CLASSES).contains(testClass)) {
|
---|
| 187 | removed = allTests.remove(testClass);
|
---|
| 188 | allTestsMap.remove(testClass.getName());
|
---|
| 189 | }
|
---|
| 190 | return removed;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[6472] | 193 | static {
|
---|
[10297] | 194 | for (Class<? extends Test> testClass : CORE_TEST_CLASSES) {
|
---|
| 195 | addTest(testClass);
|
---|
[6472] | 196 | }
|
---|
| 197 | }
|
---|
[3669] | 198 |
|
---|
[5902] | 199 | /**
|
---|
[10759] | 200 | * Initializes {@code OsmValidator}.
|
---|
[5902] | 201 | */
|
---|
[10759] | 202 | public static void initialize() {
|
---|
[3946] | 203 | initializeGridDetail();
|
---|
[14828] | 204 | loadIgnoredErrors();
|
---|
[3669] | 205 | }
|
---|
| 206 |
|
---|
| 207 | /**
|
---|
[7335] | 208 | * Returns the validator directory.
|
---|
[3707] | 209 | *
|
---|
[7335] | 210 | * @return The validator directory
|
---|
[3707] | 211 | */
|
---|
[6472] | 212 | public static String getValidatorDir() {
|
---|
[13647] | 213 | File dir = new File(Config.getDirs().getUserDataDirectory(true), "validator");
|
---|
| 214 | try {
|
---|
| 215 | return dir.getAbsolutePath();
|
---|
| 216 | } catch (SecurityException e) {
|
---|
| 217 | Logging.log(Logging.LEVEL_ERROR, null, e);
|
---|
| 218 | return dir.getPath();
|
---|
| 219 | }
|
---|
[3707] | 220 | }
|
---|
| 221 |
|
---|
[8870] | 222 | private static void loadIgnoredErrors() {
|
---|
[12542] | 223 | ignoredErrors.clear();
|
---|
[18636] | 224 | if (Boolean.TRUE.equals(ValidatorPrefHelper.PREF_USE_IGNORE.get())) {
|
---|
[14828] | 225 | Config.getPref().getListOfMaps(ValidatorPrefHelper.PREF_IGNORELIST).forEach(ignoredErrors::putAll);
|
---|
[7834] | 226 | Path path = Paths.get(getValidatorDir()).resolve("ignorederrors");
|
---|
[13647] | 227 | try {
|
---|
| 228 | if (path.toFile().exists()) {
|
---|
| 229 | try {
|
---|
[18636] | 230 | TreeSet<String> treeSet = new TreeSet<>(Files.readAllLines(path, StandardCharsets.UTF_8));
|
---|
[14828] | 231 | treeSet.forEach(ignore -> ignoredErrors.putIfAbsent(ignore, ""));
|
---|
[16296] | 232 | removeLegacyEntries(true);
|
---|
[14828] | 233 |
|
---|
| 234 | saveIgnoredErrors();
|
---|
| 235 | Files.deleteIfExists(path);
|
---|
| 236 |
|
---|
[13647] | 237 | } catch (FileNotFoundException e) {
|
---|
| 238 | Logging.debug(Logging.getErrorMessage(e));
|
---|
| 239 | } catch (IOException e) {
|
---|
| 240 | Logging.error(e);
|
---|
| 241 | }
|
---|
[3669] | 242 | }
|
---|
[13647] | 243 | } catch (SecurityException e) {
|
---|
| 244 | Logging.log(Logging.LEVEL_ERROR, "Unable to load ignored errors", e);
|
---|
[3669] | 245 | }
|
---|
[16296] | 246 | removeLegacyEntries(Config.getPref().get(ValidatorPrefHelper.PREF_IGNORELIST_FORMAT).isEmpty());
|
---|
[3669] | 247 | }
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[16296] | 250 | private static void removeLegacyEntries(boolean force) {
|
---|
| 251 | // see #19053:
|
---|
[18636] | 252 | boolean wasChanged = removeLegacyEntry(force, true, "3000");
|
---|
| 253 | // see #18230 (pt_assistant, RightAngleBuildingTest)
|
---|
| 254 | wasChanged |= removeLegacyEntry(force, false, "3701");
|
---|
| 255 |
|
---|
| 256 | if (wasChanged) {
|
---|
| 257 | saveIgnoredErrors();
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | private static boolean removeLegacyEntry(boolean force, boolean keep, String prefix) {
|
---|
[16296] | 262 | boolean wasChanged = false;
|
---|
| 263 | if (force) {
|
---|
| 264 | Iterator<Entry<String, String>> iter = ignoredErrors.entrySet().iterator();
|
---|
| 265 | while (iter.hasNext()) {
|
---|
| 266 | Entry<String, String> entry = iter.next();
|
---|
[18636] | 267 | if (entry.getKey().startsWith(prefix + "_")) {
|
---|
[16296] | 268 | Logging.warn(tr("Cannot handle ignore list entry {0}", entry));
|
---|
| 269 | iter.remove();
|
---|
| 270 | wasChanged = true;
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
[18636] | 274 | String legacyEntry = ignoredErrors.remove(prefix);
|
---|
| 275 | if (keep && legacyEntry != null) {
|
---|
[16296] | 276 | if (!legacyEntry.isEmpty()) {
|
---|
[18636] | 277 | addIgnoredError(prefix + "_" + legacyEntry, legacyEntry);
|
---|
[16296] | 278 | }
|
---|
| 279 | wasChanged = true;
|
---|
| 280 | }
|
---|
[18636] | 281 | return wasChanged;
|
---|
[16296] | 282 | }
|
---|
| 283 |
|
---|
[12390] | 284 | /**
|
---|
| 285 | * Adds an ignored error
|
---|
| 286 | * @param s The ignore group / sub group name
|
---|
| 287 | * @see TestError#getIgnoreGroup()
|
---|
| 288 | * @see TestError#getIgnoreSubGroup()
|
---|
| 289 | */
|
---|
[3669] | 290 | public static void addIgnoredError(String s) {
|
---|
[14828] | 291 | addIgnoredError(s, "");
|
---|
[3669] | 292 | }
|
---|
| 293 |
|
---|
[12390] | 294 | /**
|
---|
[14828] | 295 | * Adds an ignored error
|
---|
| 296 | * @param s The ignore group / sub group name
|
---|
| 297 | * @param description What the error actually is
|
---|
| 298 | * @see TestError#getIgnoreGroup()
|
---|
| 299 | * @see TestError#getIgnoreSubGroup()
|
---|
| 300 | */
|
---|
| 301 | public static void addIgnoredError(String s, String description) {
|
---|
| 302 | if (description == null) description = "";
|
---|
| 303 | ignoredErrors.put(s, description);
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | /**
|
---|
[15459] | 307 | * Make sure that we don't keep single entries for a "group ignore".
|
---|
[14828] | 308 | */
|
---|
[15460] | 309 | static void cleanupIgnoredErrors() {
|
---|
[14828] | 310 | if (ignoredErrors.size() > 1) {
|
---|
| 311 | List<String> toRemove = new ArrayList<>();
|
---|
| 312 |
|
---|
| 313 | Iterator<Entry<String, String>> iter = ignoredErrors.entrySet().iterator();
|
---|
[15459] | 314 | String lastKey = iter.next().getKey();
|
---|
[14828] | 315 | while (iter.hasNext()) {
|
---|
[15459] | 316 | String currKey = iter.next().getKey();
|
---|
| 317 | if (currKey.startsWith(lastKey) && sameCode(currKey, lastKey)) {
|
---|
| 318 | toRemove.add(currKey);
|
---|
[14828] | 319 | } else {
|
---|
[15459] | 320 | lastKey = currKey;
|
---|
[14828] | 321 | }
|
---|
| 322 | }
|
---|
| 323 | toRemove.forEach(ignoredErrors::remove);
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | Map<String, String> tmap = buildIgnore(buildJTreeList());
|
---|
[14851] | 327 | if (!tmap.isEmpty()) {
|
---|
[14828] | 328 | ignoredErrors.clear();
|
---|
| 329 | ignoredErrors.putAll(tmap);
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[15459] | 333 | private static boolean sameCode(String key1, String key2) {
|
---|
| 334 | return extractCodeFromIgnoreKey(key1).equals(extractCodeFromIgnoreKey(key2));
|
---|
| 335 | }
|
---|
| 336 |
|
---|
[14828] | 337 | /**
|
---|
[15459] | 338 | * Extract the leading digits building the code for the error key.
|
---|
| 339 | * @param key the error key
|
---|
| 340 | * @return the leading digits
|
---|
| 341 | */
|
---|
| 342 | private static String extractCodeFromIgnoreKey(String key) {
|
---|
| 343 | int lenCode = 0;
|
---|
| 344 |
|
---|
| 345 | for (int i = 0; i < key.length(); i++) {
|
---|
| 346 | if (key.charAt(i) >= '0' && key.charAt(i) <= '9') {
|
---|
| 347 | lenCode++;
|
---|
| 348 | } else {
|
---|
| 349 | break;
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
| 352 | return key.substring(0, lenCode);
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | /**
|
---|
[12390] | 356 | * Check if a error should be ignored
|
---|
| 357 | * @param s The ignore group / sub group name
|
---|
| 358 | * @return <code>true</code> to ignore that error
|
---|
| 359 | */
|
---|
[3669] | 360 | public static boolean hasIgnoredError(String s) {
|
---|
[14828] | 361 | return ignoredErrors.containsKey(s);
|
---|
[3669] | 362 | }
|
---|
| 363 |
|
---|
[12390] | 364 | /**
|
---|
[14828] | 365 | * Get the list of all ignored errors
|
---|
[14852] | 366 | * @return The <code>Collection<String></code> of errors that are ignored
|
---|
[12390] | 367 | */
|
---|
[14828] | 368 | public static SortedMap<String, String> getIgnoredErrors() {
|
---|
| 369 | return ignoredErrors;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | /**
|
---|
| 373 | * Build a JTree with a list
|
---|
[14851] | 374 | * @return <type>list as a {@code JTree}
|
---|
[14828] | 375 | */
|
---|
| 376 | public static JTree buildJTreeList() {
|
---|
| 377 | DefaultMutableTreeNode root = new DefaultMutableTreeNode(tr("Ignore list"));
|
---|
[17787] | 378 | final Pattern elemId1Pattern = Pattern.compile(":([rwn])_");
|
---|
[14851] | 379 | final Pattern elemId2Pattern = Pattern.compile("^[0-9]+$");
|
---|
[14828] | 380 | for (Entry<String, String> e: ignoredErrors.entrySet()) {
|
---|
| 381 | String key = e.getKey();
|
---|
[15459] | 382 | // key starts with a code, it maybe followed by a string (eg. a MapCSS rule) and
|
---|
| 383 | // optionally with a list of one or more OSM element IDs
|
---|
| 384 | String description = e.getValue();
|
---|
| 385 |
|
---|
| 386 | ArrayList<String> ignoredElementList = new ArrayList<>();
|
---|
[16643] | 387 | String[] osmobjects = elemId1Pattern.split(key, -1);
|
---|
[14828] | 388 | for (int i = 1; i < osmobjects.length; i++) {
|
---|
| 389 | String osmid = osmobjects[i];
|
---|
[14851] | 390 | if (elemId2Pattern.matcher(osmid).matches()) {
|
---|
[14828] | 391 | osmid = '_' + osmid;
|
---|
| 392 | int index = key.indexOf(osmid);
|
---|
| 393 | if (index < key.lastIndexOf(']')) continue;
|
---|
| 394 | char type = key.charAt(index - 1);
|
---|
[15459] | 395 | ignoredElementList.add(type + osmid);
|
---|
[14828] | 396 | }
|
---|
| 397 | }
|
---|
[15459] | 398 | for (String osmignore : ignoredElementList) {
|
---|
[14828] | 399 | key = key.replace(':' + osmignore, "");
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | DefaultMutableTreeNode trunk;
|
---|
| 403 | DefaultMutableTreeNode branch;
|
---|
| 404 |
|
---|
[18211] | 405 | if (!Utils.isEmpty(description)) {
|
---|
[15459] | 406 | trunk = inTree(root, description);
|
---|
[14828] | 407 | branch = inTree(trunk, key);
|
---|
| 408 | trunk.add(branch);
|
---|
| 409 | } else {
|
---|
| 410 | trunk = inTree(root, key);
|
---|
| 411 | branch = trunk;
|
---|
| 412 | }
|
---|
[15459] | 413 | if (!ignoredElementList.isEmpty()) {
|
---|
| 414 | String item;
|
---|
| 415 | if (ignoredElementList.size() == 1) {
|
---|
| 416 | item = ignoredElementList.iterator().next();
|
---|
| 417 | } else {
|
---|
| 418 | // combination of two or more objects, keep them together
|
---|
| 419 | item = ignoredElementList.toString(); // [ID1, ID2, ..., IDn]
|
---|
| 420 | }
|
---|
| 421 | branch.add(new DefaultMutableTreeNode(item));
|
---|
| 422 | }
|
---|
[14828] | 423 | root.add(trunk);
|
---|
| 424 | }
|
---|
| 425 | return new JTree(root);
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | private static DefaultMutableTreeNode inTree(DefaultMutableTreeNode root, String name) {
|
---|
| 429 | @SuppressWarnings("unchecked")
|
---|
| 430 | Enumeration<TreeNode> trunks = root.children();
|
---|
| 431 | while (trunks.hasMoreElements()) {
|
---|
| 432 | TreeNode ttrunk = trunks.nextElement();
|
---|
| 433 | if (ttrunk instanceof DefaultMutableTreeNode) {
|
---|
| 434 | DefaultMutableTreeNode trunk = (DefaultMutableTreeNode) ttrunk;
|
---|
| 435 | if (name.equals(trunk.getUserObject())) {
|
---|
| 436 | return trunk;
|
---|
| 437 | }
|
---|
| 438 | }
|
---|
| 439 | }
|
---|
| 440 | return new DefaultMutableTreeNode(name);
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | /**
|
---|
| 444 | * Build a {@code HashMap} from a tree of ignored errors
|
---|
| 445 | * @param tree The JTree of ignored errors
|
---|
| 446 | * @return A {@code HashMap} of the ignored errors for comparison
|
---|
| 447 | */
|
---|
| 448 | public static Map<String, String> buildIgnore(JTree tree) {
|
---|
| 449 | TreeModel model = tree.getModel();
|
---|
| 450 | DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
|
---|
| 451 | return buildIgnore(model, root);
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | private static Map<String, String> buildIgnore(TreeModel model, DefaultMutableTreeNode node) {
|
---|
| 455 | HashMap<String, String> rHashMap = new HashMap<>();
|
---|
| 456 |
|
---|
| 457 | for (int i = 0; i < model.getChildCount(node); i++) {
|
---|
| 458 | DefaultMutableTreeNode child = (DefaultMutableTreeNode) model.getChild(node, i);
|
---|
| 459 | if (model.getChildCount(child) == 0) {
|
---|
[15459] | 460 | // create an entry for the error list
|
---|
| 461 | String key = node.getUserObject().toString();
|
---|
| 462 | String description;
|
---|
| 463 |
|
---|
| 464 | if (!model.getRoot().equals(node)) {
|
---|
| 465 | description = ((DefaultMutableTreeNode) node.getParent()).getUserObject().toString();
|
---|
| 466 | } else {
|
---|
| 467 | description = key; // we get here when reading old file ignorederrors
|
---|
[14828] | 468 | }
|
---|
[15459] | 469 | if (tr("Ignore list").equals(description))
|
---|
| 470 | description = "";
|
---|
[18649] | 471 | if (!key.matches("^-?[0-9]+(_.*|$)")) {
|
---|
[15459] | 472 | description = key;
|
---|
| 473 | key = "";
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | String item = child.getUserObject().toString();
|
---|
| 477 | String entry = null;
|
---|
[17787] | 478 | if (item.matches("^\\[([rwn])_.*")) {
|
---|
[15459] | 479 | // list of elements (produced with list.toString() method)
|
---|
| 480 | entry = key + ":" + item.substring(1, item.lastIndexOf(']')).replace(", ", ":");
|
---|
[17787] | 481 | } else if (item.matches("^([rwn])_.*")) {
|
---|
[15459] | 482 | // single element
|
---|
| 483 | entry = key + ":" + item;
|
---|
[18649] | 484 | } else if (item.matches("^-?[0-9]+(_.*|)$")) {
|
---|
[15459] | 485 | // no element ids
|
---|
| 486 | entry = item;
|
---|
| 487 | }
|
---|
| 488 | if (entry != null) {
|
---|
| 489 | rHashMap.put(entry, description);
|
---|
| 490 | } else {
|
---|
| 491 | Logging.warn("ignored unexpected item in validator ignore list management dialog:'" + item + "'");
|
---|
| 492 | }
|
---|
[14828] | 493 | } else {
|
---|
| 494 | rHashMap.putAll(buildIgnore(model, child));
|
---|
| 495 | }
|
---|
| 496 | }
|
---|
| 497 | return rHashMap;
|
---|
| 498 | }
|
---|
| 499 |
|
---|
| 500 | /**
|
---|
| 501 | * Reset the error list by deleting {@code validator.ignorelist}
|
---|
| 502 | */
|
---|
| 503 | public static void resetErrorList() {
|
---|
| 504 | saveIgnoredErrors();
|
---|
| 505 | Config.getPref().putListOfMaps(ValidatorPrefHelper.PREF_IGNORELIST, null);
|
---|
| 506 | OsmValidator.initialize();
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | /**
|
---|
| 510 | * Saves the names of the ignored errors to a preference
|
---|
| 511 | */
|
---|
[3669] | 512 | public static void saveIgnoredErrors() {
|
---|
[14828] | 513 | List<Map<String, String>> list = new ArrayList<>();
|
---|
| 514 | cleanupIgnoredErrors();
|
---|
[16248] | 515 | ignoredErrors.remove("3000"); // see #19053
|
---|
[18636] | 516 | ignoredErrors.remove("3701"); // see #18230
|
---|
[14828] | 517 | list.add(ignoredErrors);
|
---|
| 518 | int i = 0;
|
---|
| 519 | while (i < list.size()) {
|
---|
[18208] | 520 | if (Utils.isEmpty(list.get(i))) {
|
---|
[14828] | 521 | list.remove(i);
|
---|
| 522 | continue;
|
---|
[3671] | 523 | }
|
---|
[14828] | 524 | i++;
|
---|
[3669] | 525 | }
|
---|
[14828] | 526 | if (list.isEmpty()) list = null;
|
---|
| 527 | Config.getPref().putListOfMaps(ValidatorPrefHelper.PREF_IGNORELIST, list);
|
---|
[16296] | 528 | Config.getPref().put(ValidatorPrefHelper.PREF_IGNORELIST_FORMAT, "2");
|
---|
[3669] | 529 | }
|
---|
| 530 |
|
---|
[11852] | 531 | /**
|
---|
| 532 | * Initializes error layer.
|
---|
| 533 | */
|
---|
[10214] | 534 | public static synchronized void initializeErrorLayer() {
|
---|
[16091] | 535 | if (errorLayer == null && Boolean.TRUE.equals(ValidatorPrefHelper.PREF_LAYER.get())) {
|
---|
[3669] | 536 | errorLayer = new ValidatorLayer();
|
---|
[12636] | 537 | MainApplication.getLayerManager().addLayer(errorLayer);
|
---|
[3669] | 538 | }
|
---|
| 539 | }
|
---|
| 540 |
|
---|
[6529] | 541 | /**
|
---|
[11852] | 542 | * Resets error layer.
|
---|
| 543 | * @since 11852
|
---|
| 544 | */
|
---|
| 545 | public static synchronized void resetErrorLayer() {
|
---|
| 546 | errorLayer = null;
|
---|
| 547 | }
|
---|
| 548 |
|
---|
| 549 | /**
|
---|
[6529] | 550 | * Gets a map from simple names to all tests.
|
---|
[6691] | 551 | * @return A map of all tests, indexed and sorted by the name of their Java class
|
---|
[6472] | 552 | */
|
---|
[6691] | 553 | public static SortedMap<String, Test> getAllTestsMap() {
|
---|
[12542] | 554 | applyPrefs(allTestsMap, false);
|
---|
| 555 | applyPrefs(allTestsMap, true);
|
---|
| 556 | return new TreeMap<>(allTestsMap);
|
---|
[3669] | 557 | }
|
---|
[6830] | 558 |
|
---|
[6670] | 559 | /**
|
---|
| 560 | * Returns the instance of the given test class.
|
---|
[9246] | 561 | * @param <T> testClass type
|
---|
[6670] | 562 | * @param testClass The class of test to retrieve
|
---|
| 563 | * @return the instance of the given test class, if any, or {@code null}
|
---|
| 564 | * @since 6670
|
---|
| 565 | */
|
---|
| 566 | @SuppressWarnings("unchecked")
|
---|
| 567 | public static <T extends Test> T getTest(Class<T> testClass) {
|
---|
| 568 | if (testClass == null) {
|
---|
| 569 | return null;
|
---|
| 570 | }
|
---|
[12542] | 571 | return (T) allTestsMap.get(testClass.getName());
|
---|
[6670] | 572 | }
|
---|
[3669] | 573 |
|
---|
| 574 | private static void applyPrefs(Map<String, Test> tests, boolean beforeUpload) {
|
---|
[12846] | 575 | for (String testName : Config.getPref().getList(beforeUpload
|
---|
[12649] | 576 | ? ValidatorPrefHelper.PREF_SKIP_TESTS_BEFORE_UPLOAD : ValidatorPrefHelper.PREF_SKIP_TESTS)) {
|
---|
[3669] | 577 | Test test = tests.get(testName);
|
---|
| 578 | if (test != null) {
|
---|
| 579 | if (beforeUpload) {
|
---|
[6593] | 580 | test.testBeforeUpload = false;
|
---|
[3669] | 581 | } else {
|
---|
[6593] | 582 | test.enabled = false;
|
---|
[3669] | 583 | }
|
---|
| 584 | }
|
---|
| 585 | }
|
---|
| 586 | }
|
---|
| 587 |
|
---|
[12390] | 588 | /**
|
---|
| 589 | * Gets all tests that are possible
|
---|
| 590 | * @return The tests
|
---|
| 591 | */
|
---|
[3669] | 592 | public static Collection<Test> getTests() {
|
---|
| 593 | return getAllTestsMap().values();
|
---|
| 594 | }
|
---|
| 595 |
|
---|
[12390] | 596 | /**
|
---|
| 597 | * Gets all tests that are run
|
---|
| 598 | * @param beforeUpload To get the ones that are run before upload
|
---|
| 599 | * @return The tests
|
---|
| 600 | */
|
---|
[3669] | 601 | public static Collection<Test> getEnabledTests(boolean beforeUpload) {
|
---|
| 602 | Collection<Test> enabledTests = getTests();
|
---|
[7005] | 603 | for (Test t : new ArrayList<>(enabledTests)) {
|
---|
[3671] | 604 | if (beforeUpload ? t.testBeforeUpload : t.enabled) {
|
---|
[3669] | 605 | continue;
|
---|
[3671] | 606 | }
|
---|
[3669] | 607 | enabledTests.remove(t);
|
---|
| 608 | }
|
---|
| 609 | return enabledTests;
|
---|
| 610 | }
|
---|
| 611 |
|
---|
| 612 | /**
|
---|
| 613 | * Gets the list of all available test classes
|
---|
| 614 | *
|
---|
[10297] | 615 | * @return A collection of the test classes
|
---|
[3669] | 616 | */
|
---|
[10297] | 617 | public static Collection<Class<? extends Test>> getAllAvailableTestClasses() {
|
---|
[12542] | 618 | return Collections.unmodifiableCollection(allTests);
|
---|
[3669] | 619 | }
|
---|
| 620 |
|
---|
| 621 | /**
|
---|
[18636] | 622 | * Initialize grid details based on the current projection system. Values based on
|
---|
[6830] | 623 | * the original value fixed for EPSG:4326 (10000) using heuristics (that is, test&error
|
---|
[3946] | 624 | * until most bugs were discovered while keeping the processing time reasonable)
|
---|
| 625 | */
|
---|
[10765] | 626 | public static void initializeGridDetail() {
|
---|
[14120] | 627 | String code = ProjectionRegistry.getProjection().toCode();
|
---|
[5548] | 628 | if (Arrays.asList(ProjectionPreference.wgs84.allCodes()).contains(code)) {
|
---|
[11100] | 629 | OsmValidator.griddetail = 10_000;
|
---|
[5548] | 630 | } else if (Arrays.asList(ProjectionPreference.mercator.allCodes()).contains(code)) {
|
---|
[3946] | 631 | OsmValidator.griddetail = 0.01;
|
---|
[5548] | 632 | } else if (Arrays.asList(ProjectionPreference.lambert.allCodes()).contains(code)) {
|
---|
[3946] | 633 | OsmValidator.griddetail = 0.1;
|
---|
[5547] | 634 | } else {
|
---|
| 635 | OsmValidator.griddetail = 1.0;
|
---|
[3946] | 636 | }
|
---|
| 637 | }
|
---|
| 638 |
|
---|
[11852] | 639 | /**
|
---|
| 640 | * Returns grid detail, multiplier of east,north values for valuable cell sizing
|
---|
| 641 | * @return grid detail
|
---|
| 642 | * @since 11852
|
---|
| 643 | */
|
---|
| 644 | public static double getGridDetail() {
|
---|
| 645 | return griddetail;
|
---|
| 646 | }
|
---|
| 647 |
|
---|
[8840] | 648 | private static boolean testsInitialized;
|
---|
[6779] | 649 |
|
---|
[3946] | 650 | /**
|
---|
[18636] | 651 | * Initializes all tests if this operation hasn't been performed already.
|
---|
[6779] | 652 | */
|
---|
| 653 | public static synchronized void initializeTests() {
|
---|
| 654 | if (!testsInitialized) {
|
---|
[16069] | 655 | final String message = "Initializing validator tests";
|
---|
| 656 | Logging.debug(message);
|
---|
[15755] | 657 | final Stopwatch stopwatch = Stopwatch.createStarted();
|
---|
[6779] | 658 | initializeTests(getTests());
|
---|
| 659 | testsInitialized = true;
|
---|
[16069] | 660 | Logging.debug(stopwatch.toString("Initializing validator tests"));
|
---|
[6779] | 661 | }
|
---|
| 662 | }
|
---|
| 663 |
|
---|
| 664 | /**
|
---|
[3669] | 665 | * Initializes all tests
|
---|
| 666 | * @param allTests The tests to initialize
|
---|
| 667 | */
|
---|
[6670] | 668 | public static void initializeTests(Collection<? extends Test> allTests) {
|
---|
[3669] | 669 | for (Test test : allTests) {
|
---|
| 670 | try {
|
---|
| 671 | if (test.enabled) {
|
---|
| 672 | test.initialize();
|
---|
| 673 | }
|
---|
[11746] | 674 | } catch (Exception e) { // NOPMD
|
---|
[13647] | 675 | String message = tr("Error initializing test {0}:\n {1}", test.getClass().getSimpleName(), e);
|
---|
| 676 | Logging.error(message);
|
---|
[11594] | 677 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
[13672] | 678 | GuiHelper.runInEDT(() ->
|
---|
[14153] | 679 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), message, tr("Error"), JOptionPane.ERROR_MESSAGE)
|
---|
[13672] | 680 | );
|
---|
[11594] | 681 | }
|
---|
[3669] | 682 | }
|
---|
| 683 | }
|
---|
| 684 | }
|
---|
| 685 |
|
---|
[12667] | 686 | /**
|
---|
| 687 | * Groups the given collection of errors by severity, then message, then description.
|
---|
| 688 | * @param errors list of errors to group
|
---|
| 689 | * @param filterToUse optional filter
|
---|
| 690 | * @return collection of errors grouped by severity, then message, then description
|
---|
| 691 | * @since 12667
|
---|
| 692 | */
|
---|
| 693 | public static Map<Severity, Map<String, Map<String, List<TestError>>>> getErrorsBySeverityMessageDescription(
|
---|
| 694 | Collection<TestError> errors, Predicate<? super TestError> filterToUse) {
|
---|
| 695 | return errors.stream().filter(filterToUse).collect(
|
---|
| 696 | Collectors.groupingBy(TestError::getSeverity, () -> new EnumMap<>(Severity.class),
|
---|
| 697 | Collectors.groupingBy(TestError::getMessage, () -> new TreeMap<>(AlphanumComparator.getInstance()),
|
---|
| 698 | Collectors.groupingBy(e -> e.getDescription() == null ? "" : e.getDescription(),
|
---|
| 699 | () -> new TreeMap<>(AlphanumComparator.getInstance()),
|
---|
| 700 | Collectors.toList()
|
---|
| 701 | ))));
|
---|
| 702 | }
|
---|
[15459] | 703 |
|
---|
| 704 | /**
|
---|
| 705 | * For unit tests
|
---|
| 706 | */
|
---|
[15460] | 707 | static void clearIgnoredErrors() {
|
---|
[15459] | 708 | ignoredErrors.clear();
|
---|
| 709 | }
|
---|
[3669] | 710 | }
|
---|