Changeset 19261 in josm


Ignore:
Timestamp:
2024-11-25T23:08:52+01:00 (8 weeks ago)
Author:
taylor.smock
Message:

See #24014: Add multiline and normalize attributes to preset xsd

normalize="false" will prevent all whitespace normalization while
normalize="true" + multiline="true" will strip start and end whitespace and
inner whitespace that is not newlines.

The primary reason for this change is osmwiki:Key:inscription which can have
newlines in order to match the inscription.

This does not modify UI elements to support multiline editing.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/resources/data/defaultpresets.xml

    r19257 r19261  
    75337533            </combo>
    75347534            <optional>
    7535                 <text key="inscription" text="Inscription" />
     7535                <text key="inscription" text="Inscription" multiline="true" />
    75367536                <text key="artist_name" text="Artist Name" />
    75377537                <combo key="material" text="Material" values="concrete,granite,metal,plastic,steel,stone,wood" />
     
    75477547            <reference ref="religious_catholic_christian" />
    75487548            <text key="start_date" text="Start date" />
    7549             <text key="inscription" text="Inscription" />
     7549            <text key="inscription" text="Inscription" multiline="true" />
    75507550        </item> <!-- Wayside Cross -->
    75517551        <item name="Wayside Shrine" icon="presets/religion/wayside_shrine.svg" type="node,closedway" preset_name_label="true">
     
    75587558            <combo key="building" text="Building" values="wayside_shrine,yes" values_context="building" />
    75597559            <text key="start_date" text="Start date" />
    7560             <text key="inscription" text="Inscription" />
     7560            <text key="inscription" text="Inscription" multiline="true" />
    75617561        </item> <!-- Wayside Shrine -->
    75627562        <item name="Boundary Stone" icon="presets/landmark/boundary_stone.svg" type="node,closedway" preset_name_label="true">
     
    75667566            <optional>
    75677567                <text key="name" text="Name" />
    7568                 <text key="inscription" text="Inscription" />
     7568                <text key="inscription" text="Inscription" multiline="true" />
    75697569            </optional>
    75707570            <preset_link preset_name="Boundary Marker" text="Similar but different tags:" />
     
    75767576            <optional>
    75777577                <text key="name" text="Name" />
    7578                 <text key="inscription" text="Inscription" />
     7578                <text key="inscription" text="Inscription" multiline="true" />
    75797579            </optional>
    75807580        </item> <!-- Milestone -->
     
    89658965            <optional>
    89668966                <reference ref="name_ref" />
    8967                 <text key="inscription" text="Inscription" />
     8967                <text key="inscription" text="Inscription" multiline="true" />
    89688968                <combo key="material" text="Material" values="concrete,metal,plastic,steel,stone,wood" />
    89698969            </optional>
  • trunk/resources/data/tagging-preset.xsd

    r18966 r19261  
    649649            </annotation>
    650650        </attribute>
     651        <attribute name="multiline" type="boolean">
     652            <annotation>
     653                <documentation>
     654                    The text is expected to be multiline, and newlines must not be normalized (default is false)
     655                </documentation>
     656            </annotation>
     657        </attribute>
     658        <attribute name="normalize" type="boolean">
     659            <annotation>
     660                <documentation>
     661                    This performs normalization of the value by stripping extraneous spaces and consolidating whitespace (default is true for JOSM)
     662                </documentation>
     663            </annotation>
     664        </attribute>
    651665    </attributeGroup>
    652666
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/Text.java

    r18801 r19261  
    1414import java.util.Collections;
    1515import java.util.List;
     16import java.util.regex.Pattern;
    1617
    1718import javax.swing.AbstractButton;
     
    4849 */
    4950public class Text extends KeyedItem {
     51    private static final Pattern MULTILINE_WHITESPACE_PATTERN = Pattern.compile("[\\s&&[^\n]]+");
    5052
    5153    private static int auto_increment_selected; // NOSONAR
     
    241243        }
    242244
    243         v = Utils.removeWhiteSpaces(v);
     245        if (this.normalize) {
     246            if (this.multiline) {
     247                v = Utils.removeWhiteSpaces(MULTILINE_WHITESPACE_PATTERN, v);
     248            } else {
     249                v = Utils.removeWhiteSpaces(v);
     250            }
     251        }
    244252
    245253        if (!"false".equals(use_last_as_default) || auto_increment != null) {
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/TextItem.java

    r17608 r19261  
    2323    /** The context used for translating {@link #text} */
    2424    public String text_context; // NOSONAR
     25
     26    /** {@code true} if the value is a multiline value */
     27    public boolean multiline; // NOSONAR
     28    /** {@code true} if the value should be normalized */
     29    public boolean normalize = true; // NOSONAR
    2530
    2631    /** The localized version of {@link #text} */
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r19133 r19261  
    821821     */
    822822    public static String removeWhiteSpaces(String s) {
     823        return removeWhiteSpaces(WHITE_SPACES_PATTERN, s);
     824    }
     825
     826    /**
     827     * Removes leading, trailing, and multiple inner whitespaces from the given string, to be used as a key or value.
     828     * @param s The string
     829     * @param whitespaces The regex for whitespaces to remove outside the leading and trailing whitespaces (see {@link #strip(String)})
     830     * @return The string without leading, trailing or multiple inner whitespaces
     831     * @since 19261
     832     */
     833    public static String removeWhiteSpaces(Pattern whitespaces, String s) {
    823834        if (isEmpty(s)) {
    824835            return s;
    825836        }
    826         return strip(s).replaceAll("\\s+", " ");
     837        return whitespaces.matcher(strip(s)).replaceAll(" ");
    827838    }
    828839
  • trunk/test/unit/org/openstreetmap/josm/gui/tagging/presets/items/TextTest.java

    r18918 r19261  
    33
    44import static org.junit.jupiter.api.Assertions.assertEquals;
     5import static org.junit.jupiter.api.Assertions.assertInstanceOf;
    56import static org.junit.jupiter.api.Assertions.assertTrue;
    67
     8import java.text.MessageFormat;
     9import java.util.ArrayList;
     10import java.util.Collection;
     11import java.util.Collections;
     12import java.util.List;
     13import java.util.Map;
     14
     15import javax.swing.JComponent;
    716import javax.swing.JPanel;
    817
    918import org.junit.jupiter.api.Test;
     19import org.junit.jupiter.params.ParameterizedTest;
     20import org.junit.jupiter.params.provider.ValueSource;
     21import org.openstreetmap.josm.data.osm.Tag;
     22import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
    1023import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItemGuiSupport;
    1124import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItemTest;
     25import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetType;
     26import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
     27import org.openstreetmap.josm.gui.widgets.JosmTextField;
    1228import org.openstreetmap.josm.testutils.annotations.Main;
    1329
     
    3349        assertTrue(p.getComponentCount() > 0);
    3450    }
     51
     52    @org.openstreetmap.josm.testutils.annotations.TaggingPresets
     53    @ParameterizedTest
     54    @ValueSource(strings = {"\n\n\n\t\r {0}\n\n\n", "{0}"})
     55    void testNonRegression24023(String inscription) {
     56        // There is a bit of "extra" whitespace in the string (` \n`). It is somewhat deliberate. We probably ought to remove the ` ` at some time.
     57        final String expected = "This is a \nsample \ninscription";
     58        final String toTest = MessageFormat.format(inscription, expected).replace("sample ", "sample    ");
     59        final Collection<TaggingPreset> presets = TaggingPresets.getMatchingPresets(Collections.singleton(TaggingPresetType.NODE), Map.of("historic", "boundary_stone", "inscription", "bar"), false);
     60        assertEquals(1, presets.size());
     61        final TaggingPreset preset = presets.iterator().next();
     62        final Text text = assertInstanceOf(Text.class, preset.data.get(5));
     63        final List<Tag> changeCommands = new ArrayList<>(1);
     64        final JPanel panel = new JPanel();
     65        text.addToPanel(panel, TaggingPresetItemGuiSupport.create(false));
     66        JComponent value = assertInstanceOf(JComponent.class, panel.getComponent(1));
     67        while (value instanceof JPanel) {
     68            value = (JComponent) value.getComponent(0);
     69        }
     70        final JosmTextField textField = assertInstanceOf(JosmTextField.class, value, "Until we support multiline editing, this should be a text field");
     71        textField.setText(toTest);
     72        text.addCommands(changeCommands);
     73        assertTrue(text.multiline);
     74        assertTrue(text.normalize);
     75        assertEquals(1, changeCommands.size());
     76        assertEquals(expected, changeCommands.get(0).getValue(), "If the only difference is a trailing space was removed, update the test.");
     77    }
    3578}
Note: See TracChangeset for help on using the changeset viewer.