Ticket #13956: 13956_v5.patch

File 13956_v5.patch, 14.4 KB (added by GerdP, 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> additionalPresetsValueData;
    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            additionalPresetsValueData = new MultiMap<>();
    262262            for (String a : OsmPrimitive.getUninterestingKeys()) {
    263                 presetsValueData.putVoid(a);
     263                additionalPresetsValueData.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                additionalPresetsValueData.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.getPresetValues(key);
     312        if (res != null)
     313            return res;
     314        return additionalPresetsValueData.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 && !isTagIgnored(key, value)) {
     474            if (checkValues && key != null && value != null && !value.isEmpty() && additionalPresetsValueData != null
     475                    && !isTagIgnored(key, value)) {
    469476                if (!isKeyInPresets(key)) {
    470477                    String prettifiedKey = harmonizeKey(key);
    471478                    String fixedKey = harmonizedKeys.get(prettifiedKey);
     
    490497                } else if (!isTagInPresets(key, value)) {
    491498                    // try to fix common typos and check again if value is still unknown
    492499                    String fixedValue = harmonizeValue(prop.getValue());
    493                     Map<String, String> possibleValues = getPossibleValues(presetsValueData.get(key));
     500                    Map<String, String> possibleValues = getPossibleValues(getPresetValues(key));
    494501                    if (possibleValues.containsKey(fixedValue)) {
    495502                        final String newKey = possibleValues.get(fixedValue);
    496503                        // 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;
     
    2928import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
    3029import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
    3130import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
    32 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItem;
    3331import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
    34 import org.openstreetmap.josm.gui.tagging.presets.items.CheckGroup;
    35 import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem;
    36 import org.openstreetmap.josm.gui.tagging.presets.items.Roles;
    3732import org.openstreetmap.josm.gui.tagging.presets.items.Roles.Role;
    3833import org.openstreetmap.josm.tools.CheckParameterUtil;
    3934import org.openstreetmap.josm.tools.MultiMap;
     
    203198    }
    204199
    205200    /**
    206      * Initialize the cache for presets. This is done only once.
    207      * @param presets Tagging presets to cache
    208      */
    209     public static void cachePresets(Collection<TaggingPreset> presets) {
    210         for (final TaggingPreset p : presets) {
    211             for (TaggingPresetItem item : p.data) {
    212                 cachePresetItem(p, item);
    213             }
    214         }
    215     }
    216 
    217     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) {
    228             Roles r = (Roles) item;
    229             for (Role i : r.roles) {
    230                 if (i.key != null) {
    231                     PRESET_ROLE_CACHE.add(i.key);
    232                 }
    233             }
    234         } else if (item instanceof CheckGroup) {
    235             for (KeyedItem check : ((CheckGroup) item).checks) {
    236                 cachePresetItem(p, check);
    237             }
    238         }
    239     }
    240 
    241     /**
    242201     * Remembers user input for the given key/value.
    243202     * @param key Tag key
    244203     * @param value Tag value
     
    259218        return new ArrayList<>(getTagCache().keySet());
    260219    }
    261220
    262     protected List<String> getPresetKeys() {
    263         return new ArrayList<>(PRESET_TAG_CACHE.keySet());
    264     }
    265 
    266221    protected Collection<String> getUserInputKeys() {
    267222        List<String> keys = new ArrayList<>();
    268223        for (UserInputTag tag : USER_INPUT_TAG_CACHE) {
     
    285240        return new ArrayList<>(getTagCache().getValues(key));
    286241    }
    287242
    288     protected static List<String> getPresetValues(String key) {
    289         return new ArrayList<>(PRESET_TAG_CACHE.getValues(key));
    290     }
    291 
    292243    protected static Collection<String> getUserInputValues(String key) {
    293244        List<String> values = new ArrayList<>();
    294245        for (UserInputTag tag : USER_INPUT_TAG_CACHE) {
     
    316267     * @param list the list to populate
    317268     */
    318269    public void populateWithMemberRoles(AutoCompletionList list) {
    319         list.add(PRESET_ROLE_CACHE, AutoCompletionItemPriority.IS_IN_STANDARD);
     270        list.add(TaggingPresets.getPresetRoles(), AutoCompletionItemPriority.IS_IN_STANDARD);
    320271        list.add(getRoleCache(), AutoCompletionItemPriority.IS_IN_DATASET);
    321272    }
    322273
     
    351302     * @param list the list to populate
    352303     */
    353304    public void populateWithKeys(AutoCompletionList list) {
    354         list.add(getPresetKeys(), AutoCompletionItemPriority.IS_IN_STANDARD);
     305        list.add(TaggingPresets.getPresetKeys(), AutoCompletionItemPriority.IS_IN_STANDARD);
    355306        list.add(new AutoCompletionListItem("source", AutoCompletionItemPriority.IS_IN_STANDARD));
    356307        list.add(getDataKeys(), AutoCompletionItemPriority.IS_IN_DATASET);
    357308        list.addUserInput(getUserInputKeys());
     
    377328     */
    378329    public void populateWithTagValues(AutoCompletionList list, List<String> keys) {
    379330        for (String key : keys) {
    380             list.add(getPresetValues(key), AutoCompletionItemPriority.IS_IN_STANDARD);
     331            list.add(TaggingPresets.getPresetValues(key), AutoCompletionItemPriority.IS_IN_STANDARD);
    381332            list.add(getDataValues(key), AutoCompletionItemPriority.IS_IN_DATASET);
    382333            list.addUserInput(getUserInputValues(key));
    383334        }
  • src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresets.java

     
    33
    44import java.util.ArrayList;
    55import java.util.Collection;
     6import java.util.Collections;
    67import java.util.HashMap;
     8import java.util.HashSet;
    79import java.util.Map;
     10import java.util.Set;
    811
    912import javax.swing.JMenu;
    1013import javax.swing.JMenuItem;
     
    1316import org.openstreetmap.josm.Main;
    1417import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1518import org.openstreetmap.josm.gui.MenuScroller;
    16 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
     19import org.openstreetmap.josm.gui.tagging.presets.items.CheckGroup;
     20import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem;
     21import org.openstreetmap.josm.gui.tagging.presets.items.Roles;
     22import org.openstreetmap.josm.gui.tagging.presets.items.Roles.Role;
     23import org.openstreetmap.josm.tools.MultiMap;
    1724import org.openstreetmap.josm.tools.SubclassFilteredCollection;
    1825
    1926/**
     
    2532    /** The collection of tagging presets */
    2633    private static final Collection<TaggingPreset> taggingPresets = new ArrayList<>();
    2734
     35    /** cache for key/value pairs found in the preset */
     36    private static final MultiMap<String, String> PRESET_TAG_CACHE = new MultiMap<>();
     37    /** cache for roles found in the preset */
     38    private static final Set<String> PRESET_ROLE_CACHE = new HashSet<>();
     39
    2840    /** The collection of listeners */
    2941    private static final Collection<TaggingPresetListener> listeners = new ArrayList<>();
    3042
     
    3850    public static void readFromPreferences() {
    3951        taggingPresets.clear();
    4052        taggingPresets.addAll(TaggingPresetReader.readFromPreferences(false, false));
     53        cachePresets(taggingPresets);
    4154    }
    4255
    4356    /**
     
    5366        if (taggingPresets.isEmpty()) {
    5467            Main.main.menu.presetsMenu.setVisible(false);
    5568        } else {
    56             AutoCompletionManager.cachePresets(taggingPresets);
    5769            Map<TaggingPresetMenu, JMenu> submenus = new HashMap<>();
    5870            for (final TaggingPreset p : taggingPresets) {
    5971                JMenu m = p.group != null ? submenus.get(p.group) : Main.main.menu.presetsMenu;
     
    8799    }
    88100
    89101    /**
     102     * Initialize the cache for presets. This is done only once.
     103     * @param presets Tagging presets to cache
     104     */
     105    private static void cachePresets(Collection<TaggingPreset> presets) {
     106        for (final TaggingPreset p : presets) {
     107            for (TaggingPresetItem item : p.data) {
     108                cachePresetItem(p, item);
     109            }
     110        }
     111    }
     112
     113    private static void cachePresetItem(TaggingPreset p, TaggingPresetItem item) {
     114        if (item instanceof KeyedItem) {
     115            KeyedItem ki = (KeyedItem) item;
     116            if (ki.key != null && ki.getValues() != null) {
     117                try {
     118                    PRESET_TAG_CACHE.putAll(ki.key, ki.getValues());
     119                } catch (NullPointerException e) {
     120                    Main.error(e, p + ": Unable to cache " + ki);
     121                }
     122            }
     123        } else if (item instanceof Roles) {
     124            Roles r = (Roles) item;
     125            for (Role i : r.roles) {
     126                if (i.key != null) {
     127                    PRESET_ROLE_CACHE.add(i.key);
     128                }
     129            }
     130        } else if (item instanceof CheckGroup) {
     131            for (KeyedItem check : ((CheckGroup) item).checks) {
     132                cachePresetItem(p, check);
     133            }
     134        }
     135    }
     136
     137    /**
    90138     * Replies a new collection containing all tagging presets.
    91139     * @return a new collection containing all tagging presets. Empty if presets are not initialized (never null)
    92140     */
    93141    public static Collection<TaggingPreset> getTaggingPresets() {
    94         return new ArrayList<>(taggingPresets);
     142        return Collections.unmodifiableCollection(taggingPresets);
    95143    }
    96144
    97145    /**
     146     * Replies a set of all roles in the tagging presets.
     147     * @return a set of all roles in the tagging presets.
     148     */
     149    public static Set<String> getPresetRoles() {
     150        return Collections.unmodifiableSet(PRESET_ROLE_CACHE);
     151    }
     152
     153    /**
     154     * Replies a set of all keys in the tagging presets.
     155     * @return a set of all keys in the tagging presets.
     156     */
     157    public static Set<String> getPresetKeys() {
     158        return Collections.unmodifiableSet(PRESET_TAG_CACHE.keySet());
     159    }
     160
     161    /**
     162     * Return set of values for a key in the tagging presets
     163     * @param key the key
     164     * @return set of values for a key in the tagging presets or null if none is found
     165     */
     166    public static Set<String> getPresetValues(String key) {
     167        Set<String> values = PRESET_TAG_CACHE.get(key);
     168        if (values != null)
     169            return Collections.unmodifiableSet(values);
     170        return null;
     171    }
     172
     173    /**
    98174     * Replies a new collection of all presets matching the parameters.
    99175     *
    100176     * @param t the preset types to include