Changeset 17620 in josm


Ignore:
Timestamp:
2021-03-21T13:56:23+01:00 (3 years ago)
Author:
simon04
Message:

see #4626 - Extract interface MapCSSTagCheckerFixCommand

Location:
trunk/src/org/openstreetmap/josm/data/validation/tests
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r17619 r17620  
    2323import java.util.stream.Stream;
    2424
    25 import org.openstreetmap.josm.command.ChangePropertyCommand;
    26 import org.openstreetmap.josm.command.ChangePropertyKeyCommand;
    27 import org.openstreetmap.josm.command.Command;
    2825import org.openstreetmap.josm.data.osm.IPrimitive;
    2926import org.openstreetmap.josm.data.osm.OsmPrimitive;
    30 import org.openstreetmap.josm.data.osm.Tag;
    3127import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
    3228import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
     
    3733import org.openstreetmap.josm.gui.mappaint.Environment;
    3834import org.openstreetmap.josm.gui.mappaint.MultiCascade;
    39 import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
    4035import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
    4136import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleIndex;
     
    7772    public MapCSSTagChecker() {
    7873        super(tr("Tag checker (MapCSS based)"), tr("This test checks for errors in tag keys and values."));
    79     }
    80 
    81     /**
    82      * Represents a fix to a validation test. The fixing {@link Command} can be obtained by {@link #createCommand(OsmPrimitive, Selector)}.
    83      */
    84     @FunctionalInterface
    85     interface FixCommand {
    86         /**
    87          * Creates the fixing {@link Command} for the given primitive. The {@code matchingSelector} is used to evaluate placeholders
    88          * (cf. {@link MapCSSTagCheckerRule#insertArguments(Selector, String, OsmPrimitive)}).
    89          * @param p OSM primitive
    90          * @param matchingSelector  matching selector
    91          * @return fix command, or {@code null} if if cannot be created
    92          */
    93         Command createCommand(OsmPrimitive p, Selector matchingSelector);
    94 
    95         /**
    96          * Checks that object is either an {@link Expression} or a {@link String}.
    97          * @param obj object to check
    98          * @throws IllegalArgumentException if object is not an {@code Expression} or a {@code String}
    99          */
    100         static void checkObject(final Object obj) {
    101             CheckParameterUtil.ensureThat(obj instanceof Expression || obj instanceof String,
    102                     () -> "instance of Exception or String expected, but got " + obj);
    103         }
    104 
    105         /**
    106          * Evaluates given object as {@link Expression} or {@link String} on the matched {@link OsmPrimitive} and {@code matchingSelector}.
    107          * @param obj object to evaluate ({@link Expression} or {@link String})
    108          * @param p OSM primitive
    109          * @param matchingSelector matching selector
    110          * @return result string
    111          */
    112         static String evaluateObject(final Object obj, final OsmPrimitive p, final Selector matchingSelector) {
    113             final String s;
    114             if (obj instanceof Expression) {
    115                 s = (String) ((Expression) obj).evaluate(new Environment(p));
    116             } else if (obj instanceof String) {
    117                 s = (String) obj;
    118             } else {
    119                 return null;
    120             }
    121             return MapCSSTagCheckerRule.insertArguments(matchingSelector, s, p);
    122         }
    123 
    124         /**
    125          * Creates a fixing command which executes a {@link ChangePropertyCommand} on the specified tag.
    126          * @param obj object to evaluate ({@link Expression} or {@link String})
    127          * @return created fix command
    128          */
    129         static FixCommand fixAdd(final Object obj) {
    130             checkObject(obj);
    131             return new FixCommand() {
    132                 @Override
    133                 public Command createCommand(OsmPrimitive p, Selector matchingSelector) {
    134                     final Tag tag = Tag.ofString(FixCommand.evaluateObject(obj, p, matchingSelector));
    135                     return new ChangePropertyCommand(p, tag.getKey(), tag.getValue());
    136                 }
    137 
    138                 @Override
    139                 public String toString() {
    140                     return "fixAdd: " + obj;
    141                 }
    142             };
    143         }
    144 
    145         /**
    146          * Creates a fixing command which executes a {@link ChangePropertyCommand} to delete the specified key.
    147          * @param obj object to evaluate ({@link Expression} or {@link String})
    148          * @return created fix command
    149          */
    150         static FixCommand fixRemove(final Object obj) {
    151             checkObject(obj);
    152             return new FixCommand() {
    153                 @Override
    154                 public Command createCommand(OsmPrimitive p, Selector matchingSelector) {
    155                     final String key = FixCommand.evaluateObject(obj, p, matchingSelector);
    156                     return new ChangePropertyCommand(p, key, "");
    157                 }
    158 
    159                 @Override
    160                 public String toString() {
    161                     return "fixRemove: " + obj;
    162                 }
    163             };
    164         }
    165 
    166         /**
    167          * Creates a fixing command which executes a {@link ChangePropertyKeyCommand} on the specified keys
    168          * @param oldKey old key
    169          * @param newKey new key
    170          * @return created fix command
    171          */
    172         static FixCommand fixChangeKey(final String oldKey, final String newKey) {
    173             return new FixCommand() {
    174                 @Override
    175                 public Command createCommand(OsmPrimitive p, Selector matchingSelector) {
    176                     return new ChangePropertyKeyCommand(p,
    177                             MapCSSTagCheckerRule.insertArguments(matchingSelector, oldKey, p),
    178                             MapCSSTagCheckerRule.insertArguments(matchingSelector, newKey, p));
    179                 }
    180 
    181                 @Override
    182                 public String toString() {
    183                     return "fixChangeKey: " + oldKey + " => " + newKey;
    184                 }
    185             };
    186         }
    18774    }
    18875
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerRule.java

    r17619 r17620  
    5959     * Commands to apply in order to fix a matching primitive
    6060     */
    61     protected final List<MapCSSTagChecker.FixCommand> fixCommands;
     61    protected final List<MapCSSTagCheckerFixCommand> fixCommands;
    6262    /**
    6363     * Tags (or arbitrary strings) of alternatives to be presented to the user
     
    135135                                "Unsupported " + ai.key + " instruction. Allowed instructions are " + POSSIBLE_THROWS + '.', null);
    136136                    } else if ("fixAdd".equals(ai.key)) {
    137                         check.fixCommands.add(MapCSSTagChecker.FixCommand.fixAdd(ai.val));
     137                        check.fixCommands.add(MapCSSTagCheckerFixCommand.fixAdd(ai.val));
    138138                    } else if ("fixRemove".equals(ai.key)) {
    139139                        CheckParameterUtil.ensureThat(!(ai.val instanceof String) || !(val != null && val.contains("=")),
    140140                                "Unexpected '='. Please only specify the key to remove in: " + ai);
    141                         check.fixCommands.add(MapCSSTagChecker.FixCommand.fixRemove(ai.val));
     141                        check.fixCommands.add(MapCSSTagCheckerFixCommand.fixRemove(ai.val));
    142142                    } else if (val != null && "fixChangeKey".equals(ai.key)) {
    143143                        CheckParameterUtil.ensureThat(val.contains("=>"), "Separate old from new key by '=>'!");
     
    145145                        final String oldKey = Utils.removeWhiteSpaces(x[0]);
    146146                        final String newKey = Utils.removeWhiteSpaces(x[1]);
    147                         check.fixCommands.add(MapCSSTagChecker.FixCommand.fixChangeKey(oldKey, newKey));
     147                        check.fixCommands.add(MapCSSTagCheckerFixCommand.fixChangeKey(oldKey, newKey));
    148148                    } else if (val != null && "fixDeleteObject".equals(ai.key)) {
    149149                        CheckParameterUtil.ensureThat("this".equals(val), "fixDeleteObject must be followed by 'this'");
Note: See TracChangeset for help on using the changeset viewer.