Ticket #13956: 13956_v1.patch

File 13956_v1.patch, 9.4 KB (added by anonymous, 9 years ago)
  • src/org/openstreetmap/josm/data/validation/tests/TagChecker.java

     
    6868
    6969    /** Normalized keys: the key should be substituted by the value if the key was not found in presets */
    7070    private static final Map<String, String> harmonizedKeys = new HashMap<>();
    71     /** The spell check preset values */
    72     private static volatile MultiMap<String, String> presetsValueData;
     71    /** The spell check preset values which are not stored in TaggingPresets */
     72    private static volatile MultiMap<String, String> additinalPresetsValueData;
    7373    /** The TagChecker data */
    7474    private static final List<CheckerData> checkerData = new ArrayList<>();
    7575    private static final List<String> ignoreDataStartsWith = new ArrayList<>();
     
    258258
    259259        Collection<TaggingPreset> presets = TaggingPresets.getTaggingPresets();
    260260        if (!presets.isEmpty()) {
    261             presetsValueData = new MultiMap<>();
     261            additinalPresetsValueData = new MultiMap<>();
    262262            for (String a : OsmPrimitive.getUninterestingKeys()) {
    263                 presetsValueData.putVoid(a);
     263                additinalPresetsValueData.putVoid(a);
    264264            }
    265265            // TODO directionKeys are no longer in OsmPrimitive (search pattern is used instead)
    266266            for (String a : Main.pref.getCollection(ValidatorPreference.PREFIX + ".knownkeys",
    267267                    Arrays.asList(new String[]{"is_in", "int_ref", "fixme", "population"}))) {
    268                 presetsValueData.putVoid(a);
     268                additinalPresetsValueData.putVoid(a);
    269269            }
    270270            for (TaggingPreset p : presets) {
    271271                for (TaggingPresetItem i : p.data) {
     
    285285        Collection<String> values = ky.getValues();
    286286        if (ky.key != null && values != null) {
    287287            try {
    288                 presetsValueData.putAll(ky.key, values);
    289288                harmonizedKeys.put(harmonizeKey(ky.key), ky.key);
    290289            } catch (NullPointerException e) {
    291290                Main.error(e, p+": Unable to initialize "+ky+'.');
     
    308307        return false;
    309308    }
    310309
     310    private static Set<String> getPresetValues(String key) {
     311        Set<String> res = TaggingPresets.PRESET_TAG_CACHE.get(key);
     312        if (res != null)
     313            return res;
     314        return additinalPresetsValueData.get(key);
     315    }
     316
    311317    /**
    312318     * Determines if the given key is in internal presets.
    313319     * @param key key
     
    315321     * @since 9023
    316322     */
    317323    public static boolean isKeyInPresets(String key) {
    318         return presetsValueData.get(key) != null;
     324        return getPresetValues(key) != null;
    319325    }
    320326
    321327    /**
     
    326332     * @since 9023
    327333     */
    328334    public static boolean isTagInPresets(String key, String value) {
    329         final Set<String> values = presetsValueData.get(key);
     335        final Set<String> values = getPresetValues(key);
    330336        return values != null && (values.isEmpty() || values.contains(value));
    331337    }
    332338
     
    465471                        .build());
    466472                withErrors.put(p, "HTML");
    467473            }
    468             if (checkValues && key != null && value != null && !value.isEmpty() && presetsValueData != null) {
     474            if (checkValues && key != null && value != null && !value.isEmpty() && additinalPresetsValueData != null) {
    469475                if (!isTagIgnored(key, value)) {
    470476                    if (!isKeyInPresets(key)) {
    471477                        String prettifiedKey = harmonizeKey(key);
     
    491497                    } else if (!isTagInPresets(key, value)) {
    492498                        // try to fix common typos and check again if value is still unknown
    493499                        String fixedValue = harmonizeValue(prop.getValue());
    494                         Map<String, String> possibleValues = getPossibleValues(presetsValueData.get(key));
     500                        Map<String, String> possibleValues = getPossibleValues(getPresetValues(key));
    495501                        if (possibleValues.containsKey(fixedValue)) {
    496502                            final String newKey = possibleValues.get(fixedValue);
    497503                            // misspelled preset value
  • src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionManager.java

     
    1414import java.util.Set;
    1515import java.util.function.Function;
    1616
    17 import org.openstreetmap.josm.Main;
    1817import org.openstreetmap.josm.data.osm.DataSet;
    1918import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2019import org.openstreetmap.josm.data.osm.Relation;
     
    112111    protected MultiMap<String, String> tagCache;
    113112
    114113    /**
    115      * the same as tagCache but for the preset keys and values can be accessed directly
    116      */
    117     protected static final MultiMap<String, String> PRESET_TAG_CACHE = new MultiMap<>();
    118 
    119     /**
    120114     * Cache for tags that have been entered by the user.
    121115     */
    122116    protected static final Set<UserInputTag> USER_INPUT_TAG_CACHE = new LinkedHashSet<>();
     
    215209    }
    216210
    217211    protected static void cachePresetItem(TaggingPreset p, TaggingPresetItem item) {
    218         if (item instanceof KeyedItem) {
    219             KeyedItem ki = (KeyedItem) item;
    220             if (ki.key != null && ki.getValues() != null) {
    221                 try {
    222                     PRESET_TAG_CACHE.putAll(ki.key, ki.getValues());
    223                 } catch (NullPointerException e) {
    224                     Main.error(e, p + ": Unable to cache " + ki);
    225                 }
    226             }
    227         } else if (item instanceof Roles) {
     212        if (item instanceof Roles) {
    228213            Roles r = (Roles) item;
    229214            for (Role i : r.roles) {
    230215                if (i.key != null) {
     
    260245    }
    261246
    262247    protected List<String> getPresetKeys() {
    263         return new ArrayList<>(PRESET_TAG_CACHE.keySet());
     248        return new ArrayList<>(TaggingPresets.PRESET_TAG_CACHE.keySet());
    264249    }
    265250
    266251    protected Collection<String> getUserInputKeys() {
     
    286271    }
    287272
    288273    protected static List<String> getPresetValues(String key) {
    289         return new ArrayList<>(PRESET_TAG_CACHE.getValues(key));
     274        return new ArrayList<>(TaggingPresets.PRESET_TAG_CACHE.getValues(key));
    290275    }
    291276
    292277    protected static Collection<String> getUserInputValues(String key) {
  • src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresets.java

     
    1414import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1515import org.openstreetmap.josm.gui.MenuScroller;
    1616import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
     17import org.openstreetmap.josm.gui.tagging.presets.items.CheckGroup;
     18import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem;
     19import org.openstreetmap.josm.gui.tagging.presets.items.Roles;
     20import org.openstreetmap.josm.tools.MultiMap;
    1721import org.openstreetmap.josm.tools.SubclassFilteredCollection;
    1822
    1923/**
     
    2529    /** The collection of tagging presets */
    2630    private static final Collection<TaggingPreset> taggingPresets = new ArrayList<>();
    2731
     32    /** cache for key/value pairs found in the preset */
     33    public static final MultiMap<String, String> PRESET_TAG_CACHE = new MultiMap<>();
     34
    2835    /** The collection of listeners */
    2936    private static final Collection<TaggingPresetListener> listeners = new ArrayList<>();
    3037
     
    3845    public static void readFromPreferences() {
    3946        taggingPresets.clear();
    4047        taggingPresets.addAll(TaggingPresetReader.readFromPreferences(false, false));
     48        cachePresets(taggingPresets);
    4149    }
    4250
    4351    /**
     
    8795    }
    8896
    8997    /**
     98     * Initialize the cache for presets. This is done only once.
     99     * @param presets Tagging presets to cache
     100     */
     101    private static void cachePresets(Collection<TaggingPreset> presets) {
     102        for (final TaggingPreset p : presets) {
     103            for (TaggingPresetItem item : p.data) {
     104                cachePresetItem(p, item);
     105            }
     106        }
     107    }
     108
     109    private static void cachePresetItem(TaggingPreset p, TaggingPresetItem item) {
     110        if (item instanceof KeyedItem) {
     111            KeyedItem ki = (KeyedItem) item;
     112            if (ki.key != null && ki.getValues() != null) {
     113                try {
     114                    PRESET_TAG_CACHE.putAll(ki.key, ki.getValues());
     115                } catch (NullPointerException e) {
     116                    Main.error(e, p + ": Unable to cache " + ki);
     117                }
     118            }
     119        } else if (item instanceof Roles) {
     120        } else if (item instanceof CheckGroup) {
     121            for (KeyedItem check : ((CheckGroup) item).checks) {
     122                cachePresetItem(p, check);
     123            }
     124        }
     125    }
     126
     127    /**
    90128     * Replies a new collection containing all tagging presets.
    91129     * @return a new collection containing all tagging presets. Empty if presets are not initialized (never null)
    92130     */