Changeset 6558 in josm for trunk/src


Ignore:
Timestamp:
2013-12-28T23:40:51+01:00 (11 years ago)
Author:
simon04
Message:

fix #9327 - De-duplicate contents of defaultpresets.xml by introducing <chunk id="X"> and <reference ref="X" />

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetReader.java

    r6552 r6558  
    1010import java.io.InputStreamReader;
    1111import java.io.Reader;
     12import java.util.ArrayList;
    1213import java.util.Collection;
     14import java.util.HashMap;
     15import java.util.Iterator;
    1316import java.util.LinkedList;
    1417import java.util.List;
     18import java.util.Map;
    1519
    1620import javax.swing.JOptionPane;
     
    4448
    4549        return sources;
     50    }
     51
     52    /**
     53     * Holds a reference to a chunk of  items/objects.
     54     */
     55    public static class Chunk {
     56        public String id;
     57    }
     58
     59    /**
     60     * Holds a reference to an earlier item/object.
     61     */
     62    public static class Reference {
     63        public String ref;
    4664    }
    4765   
     
    6583        parser.map("list_entry", TaggingPresetItems.PresetListEntry.class);
    6684        parser.map("item_separator", TaggingPresetItems.ItemSeparator.class);
    67        
     85        parser.mapBoth("chunk", Chunk.class);
     86        parser.map("reference", Reference.class);
     87
    6888        LinkedList<TaggingPreset> all = new LinkedList<TaggingPreset>();
    6989        TaggingPresetMenu lastmenu = null;
     
    7191        final List<TaggingPresetItems.Check> checks = new LinkedList<TaggingPresetItems.Check>();
    7292        List<TaggingPresetItems.PresetListEntry> listEntries = new LinkedList<TaggingPresetItems.PresetListEntry>();
     93        final Map<String, List<Object>> byId = new HashMap<String, List<Object>>();
     94        String lastId = null;
     95        Iterator<Object> lastIdIterator = null;
    7396
    7497        if (validate) {
     
    78101        }
    79102        while (parser.hasNext()) {
    80             Object o = parser.next();
     103            final Object o;
     104            if (lastIdIterator != null && lastIdIterator.hasNext()) {
     105                // obtain elements from lastIdIterator with higher priority
     106                o = lastIdIterator.next();
     107            } else {
     108                o = parser.next();
     109            }
     110            if (o instanceof Chunk) {
     111                if (((Chunk) o).id.equals(lastId)) {
     112                    // reset last id on end of object, don't process further
     113                    lastId = null;
     114                    ((Chunk) o).id = null;
     115                    continue;
     116                } else if (lastId == null) {
     117                    // if preset item contains an id, store a mapping for later usage
     118                    lastId = ((Chunk) o).id;
     119                    byId.put(lastId, new ArrayList<Object>());
     120                    continue;
     121                } else {
     122                    throw new IllegalStateException("Cannot deal with nested id objects (lastId was expected to be null)");
     123                }
     124            } else if (lastId != null) {
     125                // add object to mapping for later usage
     126                byId.get(lastId).add(o);
     127                continue;
     128            }
     129            if (o instanceof Reference) {
     130                // if o is a reference, obtain the corresponding objects from the mapping,
     131                // and iterate over those before consuming the next element from parser.
     132                final String ref = ((Reference) o).ref;
     133                if (byId.get(ref) == null) {
     134                    throw new SAXException(tr("Reference {0} is being used before it was defined", ref));
     135                }
     136                lastIdIterator = byId.get(ref).iterator();
     137                continue;
     138            }
    81139            if (!(o instanceof TaggingPresetItem) && !checks.isEmpty()) {
    82140                all.getLast().data.addAll(checks);
Note: See TracChangeset for help on using the changeset viewer.