Changeset 17620 in josm
- Timestamp:
- 2021-03-21T13:56:23+01:00 (4 years ago)
- 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 23 23 import java.util.stream.Stream; 24 24 25 import org.openstreetmap.josm.command.ChangePropertyCommand;26 import org.openstreetmap.josm.command.ChangePropertyKeyCommand;27 import org.openstreetmap.josm.command.Command;28 25 import org.openstreetmap.josm.data.osm.IPrimitive; 29 26 import org.openstreetmap.josm.data.osm.OsmPrimitive; 30 import org.openstreetmap.josm.data.osm.Tag;31 27 import org.openstreetmap.josm.data.preferences.sources.SourceEntry; 32 28 import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper; … … 37 33 import org.openstreetmap.josm.gui.mappaint.Environment; 38 34 import org.openstreetmap.josm.gui.mappaint.MultiCascade; 39 import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;40 35 import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule; 41 36 import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleIndex; … … 77 72 public MapCSSTagChecker() { 78 73 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 @FunctionalInterface85 interface FixCommand {86 /**87 * Creates the fixing {@link Command} for the given primitive. The {@code matchingSelector} is used to evaluate placeholders88 * (cf. {@link MapCSSTagCheckerRule#insertArguments(Selector, String, OsmPrimitive)}).89 * @param p OSM primitive90 * @param matchingSelector matching selector91 * @return fix command, or {@code null} if if cannot be created92 */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 check98 * @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 primitive109 * @param matchingSelector matching selector110 * @return result string111 */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 command128 */129 static FixCommand fixAdd(final Object obj) {130 checkObject(obj);131 return new FixCommand() {132 @Override133 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 @Override139 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 command149 */150 static FixCommand fixRemove(final Object obj) {151 checkObject(obj);152 return new FixCommand() {153 @Override154 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 @Override160 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 keys168 * @param oldKey old key169 * @param newKey new key170 * @return created fix command171 */172 static FixCommand fixChangeKey(final String oldKey, final String newKey) {173 return new FixCommand() {174 @Override175 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 @Override182 public String toString() {183 return "fixChangeKey: " + oldKey + " => " + newKey;184 }185 };186 }187 74 } 188 75 -
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerRule.java
r17619 r17620 59 59 * Commands to apply in order to fix a matching primitive 60 60 */ 61 protected final List<MapCSSTagChecker .FixCommand> fixCommands;61 protected final List<MapCSSTagCheckerFixCommand> fixCommands; 62 62 /** 63 63 * Tags (or arbitrary strings) of alternatives to be presented to the user … … 135 135 "Unsupported " + ai.key + " instruction. Allowed instructions are " + POSSIBLE_THROWS + '.', null); 136 136 } else if ("fixAdd".equals(ai.key)) { 137 check.fixCommands.add(MapCSSTagChecker .FixCommand.fixAdd(ai.val));137 check.fixCommands.add(MapCSSTagCheckerFixCommand.fixAdd(ai.val)); 138 138 } else if ("fixRemove".equals(ai.key)) { 139 139 CheckParameterUtil.ensureThat(!(ai.val instanceof String) || !(val != null && val.contains("=")), 140 140 "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)); 142 142 } else if (val != null && "fixChangeKey".equals(ai.key)) { 143 143 CheckParameterUtil.ensureThat(val.contains("=>"), "Separate old from new key by '=>'!"); … … 145 145 final String oldKey = Utils.removeWhiteSpaces(x[0]); 146 146 final String newKey = Utils.removeWhiteSpaces(x[1]); 147 check.fixCommands.add(MapCSSTagChecker .FixCommand.fixChangeKey(oldKey, newKey));147 check.fixCommands.add(MapCSSTagCheckerFixCommand.fixChangeKey(oldKey, newKey)); 148 148 } else if (val != null && "fixDeleteObject".equals(ai.key)) { 149 149 CheckParameterUtil.ensureThat("this".equals(val), "fixDeleteObject must be followed by 'this'");
Note:
See TracChangeset
for help on using the changeset viewer.