Changeset 11435 in josm for trunk


Ignore:
Timestamp:
2017-01-07T00:56:52+01:00 (8 years ago)
Author:
Don-vip
Message:

fix #14199 - JOSM drops empty tags on loading and thus prevents correcting them

Location:
trunk
Files:
3 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java

    r11373 r11435  
    538538    public void put(String key, String value) {
    539539        Map<String, String> originalKeys = getKeys();
    540         if (key == null || Utils.strip(key).isEmpty())
     540        if (key == null || Utils.isStripEmpty(key))
    541541            return;
    542542        else if (value == null) {
  • trunk/src/org/openstreetmap/josm/gui/history/VersionInfoPanel.java

    r11416 r11435  
    274274        // Update text, hide prefixing label if empty
    275275        if (label != null) {
    276             label.setVisible(text != null && !Utils.strip(text).isEmpty());
     276            label.setVisible(text != null && !Utils.isStripEmpty(text));
    277277        }
    278278        textArea.setText(text);
  • trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java

    r11387 r11435  
    965965
    966966        private void updateOkButtonState() {
    967             buttons.get(0).setEnabled(!Utils.strip(tfURL.getText()).isEmpty());
     967            buttons.get(0).setEnabled(!Utils.isStripEmpty(tfURL.getText()));
    968968        }
    969969
  • trunk/src/org/openstreetmap/josm/io/OsmReader.java

    r10702 r11435  
    2525import org.openstreetmap.josm.data.DataSource;
    2626import org.openstreetmap.josm.data.coor.LatLon;
     27import org.openstreetmap.josm.data.osm.AbstractPrimitive;
    2728import org.openstreetmap.josm.data.osm.Changeset;
    2829import org.openstreetmap.josm.data.osm.DataSet;
     
    4142import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    4243import org.openstreetmap.josm.tools.CheckParameterUtil;
     44import org.openstreetmap.josm.tools.Utils;
    4345import org.openstreetmap.josm.tools.date.DateUtils;
    4446
     
    377379        if (key == null || value == null) {
    378380            throwException(tr("Missing key or value attribute in tag."));
     381        } else if (Utils.isStripEmpty(key) && t instanceof AbstractPrimitive) {
     382            // #14199: Empty keys as ignored by AbstractPrimitive#put, but it causes problems to fix existing data
     383            // Drop the tag on import, but flag the primitive as modified
     384            ((AbstractPrimitive) t).setModified(true);
    379385        } else {
    380386            t.put(key.intern(), value.intern());
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r11394 r11435  
    735735
    736736    /**
     737     * Determines if the given String would be empty if stripped.
     738     * This is an efficient alternative to {@code strip(s).isEmpty()} that avoids to create useless String object.
     739     * @param str The string to test
     740     * @return {@code true} if the stripped version of {@code s} would be empty.
     741     * @since 11435
     742     */
     743    public static boolean isStripEmpty(String str) {
     744        if (str != null) {
     745            for (int i = 0; i < str.length(); i++) {
     746                if (!isStrippedChar(str.charAt(i), DEFAULT_STRIP)) {
     747                    return false;
     748                }
     749            }
     750        }
     751        return true;
     752    }
     753
     754    /**
    737755     * An alternative to {@link String#trim()} to effectively remove all leading
    738756     * and trailing white characters, including Unicode ones.
     
    774792        boolean leadingSkipChar = true;
    775793        while (leadingSkipChar && start < end) {
    776             char c = str.charAt(start);
    777             leadingSkipChar = Character.isWhitespace(c) || Character.isSpaceChar(c) || stripChar(skipChars, c);
     794            leadingSkipChar = isStrippedChar(str.charAt(start), skipChars);
    778795            if (leadingSkipChar) {
    779796                start++;
     
    782799        boolean trailingSkipChar = true;
    783800        while (trailingSkipChar && end > start + 1) {
    784             char c = str.charAt(end - 1);
    785             trailingSkipChar = Character.isWhitespace(c) || Character.isSpaceChar(c) || stripChar(skipChars, c);
     801            trailingSkipChar = isStrippedChar(str.charAt(end - 1), skipChars);
    786802            if (trailingSkipChar) {
    787803                end--;
     
    790806
    791807        return str.substring(start, end);
     808    }
     809
     810    private static boolean isStrippedChar(char c, final char ... skipChars) {
     811        return Character.isWhitespace(c) || Character.isSpaceChar(c) || stripChar(skipChars, c);
    792812    }
    793813
  • trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    r11330 r11435  
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertFalse;
     6import static org.junit.Assert.assertNull;
     7import static org.junit.Assert.assertTrue;
    58
    69import java.io.IOException;
     
    1013import java.util.Locale;
    1114
    12 import org.junit.Assert;
    1315import org.junit.Rule;
    1416import org.junit.Test;
     
    5355            "\u3000"; // IDEOGRAPHIC SPACE
    5456        // CHECKSTYLE.ON: SingleSpaceSeparator
    55         Assert.assertNull(Utils.strip(null));
    56         Assert.assertEquals("", Utils.strip(""));
    57         Assert.assertEquals("", Utils.strip(" "));
    58         Assert.assertEquals("", Utils.strip("  "));
    59         Assert.assertEquals("", Utils.strip("   "));
    60         Assert.assertEquals("", Utils.strip(someWhite));
    61         Assert.assertEquals("a", Utils.strip("a"));
    62         Assert.assertEquals("ab", Utils.strip("ab"));
    63         Assert.assertEquals("abc", Utils.strip("abc"));
    64         Assert.assertEquals("a", Utils.strip(" a"));
    65         Assert.assertEquals("ab", Utils.strip(" ab"));
    66         Assert.assertEquals("abc", Utils.strip(" abc"));
    67         Assert.assertEquals("a", Utils.strip("a "));
    68         Assert.assertEquals("ab", Utils.strip("ab "));
    69         Assert.assertEquals("abc", Utils.strip("abc "));
    70         Assert.assertEquals("a", Utils.strip(someWhite+"a"+someWhite));
    71         Assert.assertEquals("ab", Utils.strip(someWhite+"ab"+someWhite));
    72         Assert.assertEquals("abc", Utils.strip(someWhite+"abc"+someWhite));
     57        assertNull(Utils.strip(null));
     58        assertEquals("", Utils.strip(""));
     59        assertEquals("", Utils.strip(" "));
     60        assertEquals("", Utils.strip("  "));
     61        assertEquals("", Utils.strip("   "));
     62        assertEquals("", Utils.strip(someWhite));
     63        assertEquals("a", Utils.strip("a"));
     64        assertEquals("ab", Utils.strip("ab"));
     65        assertEquals("abc", Utils.strip("abc"));
     66        assertEquals("a", Utils.strip(" a"));
     67        assertEquals("ab", Utils.strip(" ab"));
     68        assertEquals("abc", Utils.strip(" abc"));
     69        assertEquals("a", Utils.strip("a "));
     70        assertEquals("ab", Utils.strip("ab "));
     71        assertEquals("abc", Utils.strip("abc "));
     72        assertEquals("a", Utils.strip(someWhite+"a"+someWhite));
     73        assertEquals("ab", Utils.strip(someWhite+"ab"+someWhite));
     74        assertEquals("abc", Utils.strip(someWhite+"abc"+someWhite));
    7375
    7476        // extended skip
    75         Assert.assertEquals("a", Utils.strip("a", "b"));
    76         Assert.assertEquals("b", Utils.strip("acbcac", "ac"));
     77        assertEquals("a", Utils.strip("a", "b"));
     78        assertEquals("b", Utils.strip("acbcac", "ac"));
     79    }
     80
     81    /**
     82     * Test of {@link Utils#isStripEmpty} method.
     83     */
     84    @Test
     85    public void testIsStripEmpty() {
     86        assertTrue(Utils.isStripEmpty(null));
     87        assertTrue(Utils.isStripEmpty(""));
     88        assertTrue(Utils.isStripEmpty(" "));
     89        assertTrue(Utils.isStripEmpty("  "));
     90        assertFalse(Utils.isStripEmpty("a"));
     91        assertFalse(Utils.isStripEmpty("foo"));
     92        assertFalse(Utils.isStripEmpty(" foo"));
     93        assertFalse(Utils.isStripEmpty("foo "));
     94        assertFalse(Utils.isStripEmpty(" foo "));
    7795    }
    7896
     
    82100    @Test
    83101    public void testToHexString() {
    84         Assert.assertEquals("", Utils.toHexString(null));
    85         Assert.assertEquals("", Utils.toHexString(new byte[0]));
    86         Assert.assertEquals("01", Utils.toHexString(new byte[]{0x1}));
    87         Assert.assertEquals("0102", Utils.toHexString(new byte[]{0x1, 0x2}));
    88         Assert.assertEquals("12", Utils.toHexString(new byte[]{0x12}));
    89         Assert.assertEquals("127f", Utils.toHexString(new byte[]{0x12, 0x7f}));
    90         Assert.assertEquals("fedc", Utils.toHexString(new byte[]{(byte) 0xfe, (byte) 0xdc}));
     102        assertEquals("", Utils.toHexString(null));
     103        assertEquals("", Utils.toHexString(new byte[0]));
     104        assertEquals("01", Utils.toHexString(new byte[]{0x1}));
     105        assertEquals("0102", Utils.toHexString(new byte[]{0x1, 0x2}));
     106        assertEquals("12", Utils.toHexString(new byte[]{0x12}));
     107        assertEquals("127f", Utils.toHexString(new byte[]{0x12, 0x7f}));
     108        assertEquals("fedc", Utils.toHexString(new byte[]{(byte) 0xfe, (byte) 0xdc}));
    91109    }
    92110
Note: See TracChangeset for help on using the changeset viewer.