Ticket #3412: Validator-NameMismatch.patch

File Validator-NameMismatch.patch, 3.5 KB (added by skela, 16 years ago)

Patch to implement the “name != any of name:*” check

  • plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/NameMismatch.java

     
     1package org.openstreetmap.josm.plugins.validator.tests;
     2
     3import static org.openstreetmap.josm.tools.I18n.tr;
     4
     5import java.util.Collection;
     6import java.util.HashSet;
     7
     8import org.openstreetmap.josm.data.osm.OsmPrimitive;
     9import org.openstreetmap.josm.plugins.validator.Severity;
     10import org.openstreetmap.josm.plugins.validator.Test;
     11import 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 */
     23public 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}
  • plugins/validator/src/org/openstreetmap/josm/plugins/validator/OSMValidatorPlugin.java

    import org.openstreetmap.josm.plugins.va  
    3939import org.openstreetmap.josm.plugins.validator.tests.CrossingWays;
    4040import org.openstreetmap.josm.plugins.validator.tests.DuplicateNode;
    4141import org.openstreetmap.josm.plugins.validator.tests.DuplicatedWayNodes;
     42import org.openstreetmap.josm.plugins.validator.tests.NameMismatch;
    4243import org.openstreetmap.josm.plugins.validator.tests.NodesWithSameName;
    4344import org.openstreetmap.josm.plugins.validator.tests.OverlappingWays;
    4445import org.openstreetmap.josm.plugins.validator.tests.SelfIntersectingWay;
    public class OSMValidatorPlugin extends  
    9697            UnclosedWays.class, // ID 1101 .. 1199
    9798            TagChecker.class, // ID 1201 .. 1299
    9899            UnconnectedWays.class, // ID 1301 .. 1399
     100            NameMismatch.class, // ID  1401 ..  1499
    99101    };
    100102
    101103    /**