Changeset 6465 in josm


Ignore:
Timestamp:
2013-12-10T20:40:41+01:00 (10 years ago)
Author:
simon04
Message:

fix #9412 - wrong fix for building=entrance validation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/DeprecatedTags.java

    r6459 r6465  
    55
    66import java.util.Collection;
     7import java.util.LinkedHashMap;
    78import java.util.LinkedList;
    89import java.util.List;
     10import java.util.Map;
    911
    1012import org.openstreetmap.josm.command.ChangePropertyCommand;
     
    137139        checks.add(new DeprecationCheck(2129).
    138140                test("monitoring:river_level").
    139                 alternative("monitoring:water_level"));
     141                changeKey("monitoring:river_level", "monitoring:water_level"));
    140142        // see #9365 - Useless tag layer=0
    141143        checks.add(new UnnecessaryTagCheck(2130).
     
    170172    }
    171173
     174    /**
     175     * Represents on deprecation check consisting of a series of {@code test}s,
     176     * automatic {@code change}s/{@code keyChange}s (fixes for the deprecated tag),
     177     * or a suggestion of tagging {@code alternatives}.
     178     */
    172179    private static class DeprecationCheck {
    173180
     
    175182        protected final List<Tag> test = new LinkedList<Tag>();
    176183        protected final List<Tag> change = new LinkedList<Tag>();
     184        protected final Map<String, String> keyChange = new LinkedHashMap<String, String>();
    177185        protected final List<Tag> alternatives = new LinkedList<Tag>();
    178186
     187        /**
     188         * Creates a new {@code DeprecationCheck}.
     189         * @param code {@link TestError#code}
     190         */
    179191        public DeprecationCheck(int code) {
    180192            this.code = code;
    181193        }
    182194
     195        /**
     196         * Adds a test criterion which matches primitives with tag {@code key=value}.
     197         * @return {@code this}
     198         */
    183199        DeprecationCheck test(String key, String value) {
    184200            test.add(new Tag(key, value));
     
    186202        }
    187203
     204        /**
     205         * Adds a test criterion which matches primitives with key {@code key}.
     206         * @return {@code this}
     207         */
    188208        DeprecationCheck test(String key) {
    189209            return test(key, null);
    190210        }
    191211
     212        /**
     213         * Adds an automatic fix which sets/adds the tag {@code key=value}.
     214         * @return {@code this}
     215         * @see #alternative(String, String)
     216         * @see #alternative(String)
     217         */
    192218        DeprecationCheck add(String key, String value) {
    193219            change.add(new Tag(key, value));
     
    195221        }
    196222
     223        /**
     224         * Adds an automatic fix which removes the key {@code key}.
     225         * @return {@code this}
     226         */
    197227        DeprecationCheck remove(String key) {
    198228            change.add(new Tag(key));
     
    200230        }
    201231
     232        /**
     233         * Adds an automatic fix which changes the key {@code oldKey} to {@code newKey}.
     234         * @return {@code this}
     235         */
     236        DeprecationCheck changeKey(String oldKey, String newKey) {
     237            keyChange.put(oldKey, newKey);
     238            return this;
     239        }
     240
     241        /**
     242         * Adds a test criterion which matches primitives with tag {@code key=value},
     243         * and an automatic fix which removes the key {@code key}.
     244         * Equivalent to {@link #test(String, String)} plus {@link #remove(String)}.
     245         * @return {@code this}
     246         */
    202247        DeprecationCheck testAndRemove(String key, String value) {
    203248            return test(key, value).remove(key);
    204249        }
    205250
     251        /**
     252         * Adds a suggestion to use an alternative tag {@code key=value} instead of the deprecated tag.
     253         * This is used for cases where no automatic fix is sensible.
     254         * @return {@code this}
     255         */
    206256        DeprecationCheck alternative(String key, String value) {
    207257            alternatives.add(new Tag(key, value));
     
    209259        }
    210260
     261        /**
     262         * Adds a suggestion to use an alternative key {@code key} instead of the deprecated tag.
     263         * This is used for cases where no automatic fix is sensible.
     264         * @return {@code this}
     265         */
    211266        DeprecationCheck alternative(String key) {
    212267            return alternative(key, null);
    213268        }
    214269
     270        /**
     271         * Tests whether the {@link OsmPrimitive} contains a deprecated tag which is represented by this {@code DeprecationCheck}.
     272         * @param p the primitive to test
     273         * @return true when the primitive contains a deprecated tag
     274         */
    215275        boolean matchesPrimitive(OsmPrimitive p) {
    216276            for (Tag tag : test) {
     
    225285        }
    226286
     287        /**
     288         * Constructs a fix in terms of a {@link Command} for the {@link OsmPrimitive}.
     289         * @param p the primitive to construct the fix for
     290         * @return the fix
     291         */
    227292        Command fixPrimitive(OsmPrimitive p) {
    228293            Collection<Command> cmds = new LinkedList<Command>();
     
    230295                cmds.add(new ChangePropertyCommand(p, tag.getKey(), tag.getValue()));
    231296            }
    232             if (test.size() == 1 && alternatives.size() == 1) {
    233                 cmds.add(new ChangePropertyKeyCommand(p, test.get(0).getKey(), alternatives.get(0).getKey()));
     297            for (Map.Entry<String, String> i : keyChange.entrySet()) {
     298                cmds.add(new ChangePropertyKeyCommand(p, i.getKey(), i.getValue()));
    234299            }
    235300            return new SequenceCommand(tr("Deprecation fix of {0}", Utils.join(", ", test)), cmds);
    236301        }
    237302
     303        /**
     304         * Constructs a localized description for this deprecation check.
     305         * @return a localized description
     306         */
    238307        String getDescription() {
    239308            if (alternatives.isEmpty())
     
    269338        @Override
    270339        public boolean isFixable() {
    271             return !check.change.isEmpty() || (check.test.size() == 1 && check.alternatives.size() == 1);
     340            return !check.change.isEmpty() || !check.keyChange.isEmpty();
    272341        }
    273342
Note: See TracChangeset for help on using the changeset viewer.