Ignore:
Timestamp:
2015-11-19T00:33:55+01:00 (8 years ago)
Author:
Don-vip
Message:

see #12102 - cleanup TagChecker

  • code refactor/cleanup/simplification
  • clean list of ignored tags by removing tags that have been added to internal presets during past years
  • add unit test to detect these tags if we add new tags to internal presets that are currently being ignored
File:
1 edited

Legend:

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

    r8969 r9023  
    8080    private static final List<String> ignoreDataEquals = new ArrayList<>();
    8181    private static final List<String> ignoreDataEndsWith = new ArrayList<>();
    82     private static final List<IgnoreKeyPair> ignoreDataKeyPair = new ArrayList<>();
     82    private static final List<Tag> ignoreDataTag = new ArrayList<>();
    8383
    8484    /** The preferences prefix */
     
    159159        ignoreDataEquals.clear();
    160160        ignoreDataEndsWith.clear();
    161         ignoreDataKeyPair.clear();
     161        ignoreDataTag.clear();
    162162        harmonizedKeys.clear();
    163163
     
    207207                            break;
    208208                        case "K:":
    209                             IgnoreKeyPair tmp = new IgnoreKeyPair();
    210                             int mid = line.indexOf('=');
    211                             tmp.key = line.substring(0, mid);
    212                             tmp.value = line.substring(mid+1);
    213                             ignoreDataKeyPair.add(tmp);
     209                            ignoreDataTag.add(Tag.ofString(line));
    214210                        }
    215211                    } else if (tagcheckerfile) {
     
    309305
    310306    /**
     307     * Determines if the given key is in internal presets.
     308     * @param key key
     309     * @return {@code true} if the given key is in internal presets
     310     * @since 9023
     311     */
     312    public static boolean isKeyInPresets(String key) {
     313        return presetsValueData.get(key) != null;
     314    }
     315
     316    /**
     317     * Determines if the given tag is in internal presets.
     318     * @param key key
     319     * @param value value
     320     * @return {@code true} if the given tag is in internal presets
     321     * @since 9023
     322     */
     323    public static boolean isTagInPresets(String key, String value) {
     324        final Set<String> values = presetsValueData.get(key);
     325        return values != null && (values.isEmpty() || values.contains(value));
     326    }
     327
     328    /**
     329     * Returns the list of ignored tags.
     330     * @return the list of ignored tags
     331     * @since 9023
     332     */
     333    public static List<Tag> getIgnoredTags() {
     334        return new ArrayList<>(ignoreDataTag);
     335    }
     336
     337    /**
     338     * Determines if the given tag is ignored for checks "key/tag not in presets".
     339     * @param key key
     340     * @param value value
     341     * @return {@code true} if the given tag is ignored
     342     * @since 9023
     343     */
     344    public static boolean isTagIgnored(String key, String value) {
     345        boolean tagInPresets = isTagInPresets(key, value);
     346        boolean ignore = false;
     347
     348        for (String a : ignoreDataStartsWith) {
     349            if (key.startsWith(a)) {
     350                ignore = true;
     351            }
     352        }
     353        for (String a : ignoreDataEquals) {
     354            if (key.equals(a)) {
     355                ignore = true;
     356            }
     357        }
     358        for (String a : ignoreDataEndsWith) {
     359            if (key.endsWith(a)) {
     360                ignore = true;
     361            }
     362        }
     363
     364        if (!tagInPresets) {
     365            for (Tag a : ignoreDataTag) {
     366                if (key.equals(a.getKey()) && value.equals(a.getValue())) {
     367                    ignore = true;
     368                }
     369            }
     370        }
     371        return ignore;
     372    }
     373
     374    /**
    311375     * Checks the primitive tags
    312376     * @param p The primitive to check
     
    373437            }
    374438            if (checkValues && key != null && value != null && !value.isEmpty() && presetsValueData != null) {
    375                 final Set<String> values = presetsValueData.get(key);
    376                 final boolean keyInPresets = values != null;
    377                 final boolean tagInPresets = values != null && (values.isEmpty() || values.contains(prop.getValue()));
    378 
    379                 boolean ignore = false;
    380                 for (String a : ignoreDataStartsWith) {
    381                     if (key.startsWith(a)) {
    382                         ignore = true;
    383                     }
    384                 }
    385                 for (String a : ignoreDataEquals) {
    386                     if (key.equals(a)) {
    387                         ignore = true;
    388                     }
    389                 }
    390                 for (String a : ignoreDataEndsWith) {
    391                     if (key.endsWith(a)) {
    392                         ignore = true;
    393                     }
    394                 }
    395 
    396                 if (!tagInPresets) {
    397                     for (IgnoreKeyPair a : ignoreDataKeyPair) {
    398                         if (key.equals(a.key) && value.equals(a.value)) {
    399                             ignore = true;
    400                         }
    401                     }
    402                 }
    403 
    404                 if (!ignore) {
    405                     if (!keyInPresets) {
     439                if (!isTagIgnored(key, value)) {
     440                    if (!isKeyInPresets(key)) {
    406441                        String prettifiedKey = harmonizeKey(key);
    407442                        String fixedKey = harmonizedKeys.get(prettifiedKey);
     
    420455                            withErrors.put(p, "UPK");
    421456                        }
    422                     } else if (!tagInPresets) {
     457                    } else if (!isTagInPresets(key, value)) {
    423458                        // try to fix common typos and check again if value is still unknown
    424459                        String fixedValue = harmonizeValue(prop.getValue());
    425                         Map<String, String> possibleValues = getPossibleValues(values);
     460                        Map<String, String> possibleValues = getPossibleValues(presetsValueData.get(key));
    426461                        if (possibleValues.containsKey(fixedValue)) {
    427462                            fixedValue = possibleValues.get(fixedValue);
     
    642677
    643678        return false;
    644     }
    645 
    646     protected static class IgnoreKeyPair {
    647         public String key;
    648         public String value;
    649679    }
    650680
Note: See TracChangeset for help on using the changeset viewer.