| | 1 | package org.openstreetmap.josm.plugins.validator.tests; |
| | 2 | |
| | 3 | import static org.openstreetmap.josm.tools.I18n.tr; |
| | 4 | |
| | 5 | import java.util.Collection; |
| | 6 | import java.util.HashSet; |
| | 7 | |
| | 8 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 9 | import org.openstreetmap.josm.plugins.validator.Severity; |
| | 10 | import org.openstreetmap.josm.plugins.validator.Test; |
| | 11 | import org.openstreetmap.josm.plugins.validator.TestError; |
| | 12 | |
| | 13 | /** |
| | 14 | * Check for name not equal to any name:* |
| | 15 | * <p> |
| | 16 | * This test finds multilingual objects whose 'name' attribute is not |
| | 17 | * equal to any 'name:*' attribute. For example, a node with |
| | 18 | * name=Europe, name:de=Europa should have name:en=Europe to avoid |
| | 19 | * triggering this test. |
| | 20 | * |
| | 21 | * @author Skela |
| | 22 | */ |
| | 23 | public class NameMismatch extends Test { |
| | 24 | protected static final int NAME_MISMATCH = 1401; |
| | 25 | |
| | 26 | public NameMismatch() { |
| | 27 | super(tr("Name not equal to any name:*"), |
| | 28 | tr("This test finds multilingual objects whose 'name' attribute is not equal to any 'name:*' attribute.")); |
| | 29 | } |
| | 30 | |
| | 31 | /** |
| | 32 | * Check a primitive for a name mismatch. |
| | 33 | * |
| | 34 | * @param p The primitive to be tested |
| | 35 | */ |
| | 36 | private void check(OsmPrimitive p) { |
| | 37 | HashSet<String> names = new HashSet<String>(); |
| | 38 | |
| | 39 | for (String key : p.keySet()) { |
| | 40 | if (key.startsWith("name:")) { |
| | 41 | String name_s = p.get(key); |
| | 42 | if (name_s != null) { |
| | 43 | names.add(name_s); |
| | 44 | } |
| | 45 | } |
| | 46 | } |
| | 47 | |
| | 48 | if (names.isEmpty()) return; |
| | 49 | |
| | 50 | String name = p.get("name"); |
| | 51 | |
| | 52 | if (name == null || names.contains(name)) return; |
| | 53 | |
| | 54 | errors.add(new TestError(this, Severity.OTHER, |
| | 55 | tr("name not equal to any name:*"), |
| | 56 | NAME_MISMATCH, p)); |
| | 57 | } |
| | 58 | |
| | 59 | /** |
| | 60 | * Checks a name mismatch in all primitives. |
| | 61 | * |
| | 62 | * @param selection The primitives to be tested |
| | 63 | */ |
| | 64 | @Override public void visit(Collection<OsmPrimitive> selection) |
| | 65 | { |
| | 66 | for (OsmPrimitive p : selection) { |
| | 67 | if(!p.isDeleted() && !p.incomplete) |
| | 68 | check(p); |
| | 69 | } |
| | 70 | } |
| | 71 | } |