Changeset 6534 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2013-12-26T01:43:30+01:00 (11 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java
r6532 r6534 29 29 import org.openstreetmap.josm.data.validation.TestError; 30 30 import org.openstreetmap.josm.gui.mappaint.Environment; 31 import org.openstreetmap.josm.gui.mappaint.mapcss.Expression Factory;31 import org.openstreetmap.josm.gui.mappaint.mapcss.Expression; 32 32 import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction; 33 33 import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule; … … 57 57 static class TagCheck implements Predicate<OsmPrimitive> { 58 58 protected final List<Selector> selector; 59 protected final List<Tag> change = new ArrayList<Tag>(); 59 protected final List<PrimitiveToTag> change = new ArrayList<PrimitiveToTag>(); 60 60 protected final Map<String, String> keyChange = new LinkedHashMap<String, String>(); 61 61 protected final List<Tag> alternatives = new ArrayList<Tag>(); … … 65 65 TagCheck(List<Selector> selector) { 66 66 this.selector = selector; 67 } 68 69 /** 70 * A function mapping the matched {@link OsmPrimitive} to a {@link Tag}. 71 */ 72 static abstract class PrimitiveToTag implements Utils.Function<OsmPrimitive, Tag> { 73 74 /** 75 * Creates a new mapping from an {@code MapCSS} object. 76 * In case of an {@link Expression}, that is evaluated on the matched {@link OsmPrimitive}. 77 * In case of a {@link String}, that is "compiled" to a {@link Tag} instance. 78 */ 79 static PrimitiveToTag ofMapCSSObject(final Object obj, final boolean keyOnly) { 80 if (obj instanceof Expression) { 81 return new PrimitiveToTag() { 82 @Override 83 public Tag apply(OsmPrimitive p) { 84 final String s = (String) ((Expression) obj).evaluate(new Environment().withPrimitive(p)); 85 return keyOnly? new Tag(s) : Tag.ofString(s); 86 } 87 }; 88 } else if (obj instanceof String) { 89 final Tag tag = keyOnly ? new Tag((String) obj) : Tag.ofString((String) obj); 90 return new PrimitiveToTag() { 91 @Override 92 public Tag apply(OsmPrimitive ignore) { 93 return tag; 94 } 95 }; 96 } else { 97 return null; 98 } 99 } 67 100 } 68 101 … … 72 105 if (i instanceof Instruction.AssignmentInstruction) { 73 106 final Instruction.AssignmentInstruction ai = (Instruction.AssignmentInstruction) i; 74 final String val = ai.val instanceof Expression Factory.ArrayFunction75 ? (String) ((Expression Factory.ArrayFunction) ai.val).evaluate(new Environment())107 final String val = ai.val instanceof Expression 108 ? (String) ((Expression) ai.val).evaluate(new Environment()) 76 109 : ai.val instanceof String 77 110 ? (String) ai.val … … 80 113 final Severity severity = Severity.valueOf(ai.key.substring("throw".length()).toUpperCase()); 81 114 check.errors.put(val, severity); 82 } else if ("fixAdd".equals(ai.key) && val != null) { 83 check.change.add(Tag.ofString(val)); 84 } else if ("fixRemove".equals(ai.key) && val != null) { 85 CheckParameterUtil.ensureThat(!val.contains("="), "Unexpected '='. Please only specify the key to remove!"); 86 check.change.add(new Tag(val)); 115 } else if ("fixAdd".equals(ai.key)) { 116 final PrimitiveToTag toTag = PrimitiveToTag.ofMapCSSObject(ai.val, false); 117 check.change.add(toTag); 118 } else if ("fixRemove".equals(ai.key)) { 119 CheckParameterUtil.ensureThat(!(ai.val instanceof String) || !val.contains("="), "Unexpected '='. Please only specify the key to remove!"); 120 final PrimitiveToTag toTag = PrimitiveToTag.ofMapCSSObject(ai.val, true); 121 check.change.add(toTag); 87 122 } else if ("fixChangeKey".equals(ai.key) && val != null) { 88 123 CheckParameterUtil.ensureThat(val.contains("=>"), "Separate old from new key by '=>'!"); … … 159 194 } 160 195 Collection<Command> cmds = new LinkedList<Command>(); 161 for (Tag tag : change) { 196 for (PrimitiveToTag toTag : change) { 197 final Tag tag = toTag.apply(p); 162 198 cmds.add(new ChangePropertyCommand(p, tag.getKey(), tag.getValue())); 163 199 } -
trunk/src/org/openstreetmap/josm/gui/mappaint/Cascade.java
r6322 r6534 115 115 116 116 private static Float toFloat(Object o) { 117 if (o instanceof Float) 118 return (Float) o; 119 if (o instanceof Double) 120 return new Float((Double) o); 121 if (o instanceof Integer) 122 return new Float((Integer) o); 117 if (o instanceof Number) 118 return ((Number) o).floatValue(); 123 119 if (o instanceof String && !((String) o).isEmpty()) { 124 120 try { -
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java
r6506 r6534 131 131 } 132 132 133 /** 134 * Creates a list of values, e.g., for the {@code dashes} property. 135 * @see {@link Arrays#asList(Object[])} 136 */ 133 137 public static List list(Object... args) { 134 138 return Arrays.asList(args); 135 139 } 136 140 137 public static Object get(List<? extends Object> objects, float index) { 138 int idx = Math.round(index); 139 if (idx >= 0 && idx < objects.size()) { 140 return objects.get(idx); 141 /** 142 * Get the {@code n}th element of the list {@code lst} (counting starts at 0). 143 * @since 5699 144 */ 145 public static Object get(List<?> lst, float n) { 146 int idx = Math.round(n); 147 if (idx >= 0 && idx < lst.size()) { 148 return lst.get(idx); 141 149 } 142 150 return null; 143 151 } 144 152 153 /** 154 * Splits string {@code toSplit} at occurrences of the separator string {@code sep} and returns a list of matches. 155 * @see {@link String#split(String)} 156 * @since 5699 157 */ 145 158 public static List<String> split(String sep, String toSplit) { 146 159 return Arrays.asList(toSplit.split(Pattern.quote(sep), -1)); 147 160 } 148 161 162 /** 163 * Creates a color value with the specified amounts of {@code r}ed, {@code g}reen, {@code b}lue (arguments from 0.0 to 1.0) 164 * @see {@link Color#Color(float, float, float)} 165 */ 149 166 public static Color rgb(float r, float g, float b) { 150 167 Color c; … … 157 174 } 158 175 176 /** 177 * Creates a color value from an HTML notation, i.e., {@code #rrggbb}. 178 */ 159 179 public static Color html2color(String html) { 160 180 return ColorHelper.html2color(html); 161 181 } 162 182 183 /** 184 * Computes the HTML notation ({@code #rrggbb}) for a color value). 185 */ 163 186 public static String color2html(Color c) { 164 187 return ColorHelper.color2html(c); 165 188 } 166 189 190 /** 191 * Get the value of the red color channel in the rgb color model 192 * @see {@link java.awt.Color#getRed()} 193 */ 167 194 public static float red(Color c) { 168 195 return Utils.color_int2float(c.getRed()); 169 196 } 170 197 198 /** 199 * Get the value of the green color channel in the rgb color model 200 * @see {@link java.awt.Color#getGreen()} 201 */ 171 202 public static float green(Color c) { 172 203 return Utils.color_int2float(c.getGreen()); 173 204 } 174 205 206 /** 207 * Get the value of the blue color channel in the rgb color model 208 * @see {@link java.awt.Color#getBlue()} 209 */ 175 210 public static float blue(Color c) { 176 211 return Utils.color_int2float(c.getBlue()); 177 212 } 178 213 214 /** 215 * Assembles the strings to one. 216 */ 179 217 public static String concat(Object... args) { 180 218 StringBuilder res = new StringBuilder(); … … 185 223 } 186 224 225 /** 226 * Returns the value of the property {@code key}, e.g., {@code prop("width")}. 227 */ 187 228 public Object prop(String key) { 188 229 return prop(key, null); 189 230 } 190 231 232 /** 233 * Returns the value of the property {@code key} from layer {@code layer}. 234 */ 191 235 public Object prop(String key, String layer) { 192 236 Cascade c; … … 199 243 } 200 244 245 /** 246 * Determines whether property {@code key} is set. 247 */ 201 248 public Boolean is_prop_set(String key) { 202 249 return is_prop_set(key, null); 203 250 } 204 251 252 /** 253 * Determines whether property {@code key} is set on layer {@code layer}. 254 */ 205 255 public Boolean is_prop_set(String key, String layer) { 206 256 Cascade c; … … 216 266 } 217 267 268 /** 269 * Gets the value of the key {@code key} from the object in question. 270 */ 218 271 public String tag(String key) { 219 272 return env.osm.get(key); 220 273 } 221 274 275 /** 276 * Gets the first non-null value of the key {@code key} from the object's parent(s). 277 */ 222 278 public String parent_tag(String key) { 223 279 if (env.parent == null) { … … 234 290 } 235 291 292 /** 293 * Determines whether the object has a tag with the given key. 294 */ 236 295 public boolean has_tag_key(String key) { 237 296 return env.osm.hasKey(key); 238 297 } 239 298 299 /** 300 * Returns the index of node in parent way or member in parent relation. 301 */ 240 302 public Float index() { 241 303 if (env.index == null) { … … 269 331 } 270 332 333 /** 334 * Determines if the objects {@code a} and {@code b} are equal. 335 * @see {@link Object#equals(Object)} 336 */ 271 337 public static boolean equal(Object a, Object b) { 272 338 // make sure the casts are done in a meaningful way, so … … 282 348 } 283 349 284 public Boolean JOSM_search(String s) { 350 /** 351 * Determines whether the JOSM search with {@code searchStr} applies to the object. 352 */ 353 public Boolean JOSM_search(String searchStr) { 285 354 Match m; 286 355 try { 287 m = SearchCompiler.compile(s, false, false); 356 m = SearchCompiler.compile(searchStr, false, false); 288 357 } catch (ParseError ex) { 289 358 return null; … … 292 361 } 293 362 294 public static String JOSM_pref(String s, String def) { 295 String res = Main.pref.get(s, null); 363 /** 364 * Obtains the JOSM'key {@link org.openstreetmap.josm.data.Preferences} string for key {@code key}, 365 * and defaults to {@code def} if that is null. 366 * @see {@link org.openstreetmap.josm.data.Preferences#get(String, String)} 367 */ 368 public static String JOSM_pref(String key, String def) { 369 String res = Main.pref.get(key, null); 296 370 return res != null ? res : def; 297 371 } 298 372 299 public static Color JOSM_pref_color(String s, Color def) { 300 Color res = Main.pref.getColor(s, null); 373 /** 374 * Obtains the JOSM'key {@link org.openstreetmap.josm.data.Preferences} color for key {@code key}, 375 * and defaults to {@code def} if that is null. 376 * @see {@link org.openstreetmap.josm.data.Preferences#getColor(String, java.awt.Color)} 377 */ 378 public static Color JOSM_pref_color(String key, Color def) { 379 Color res = Main.pref.getColor(key, null); 301 380 return res != null ? res : def; 302 381 } 303 382 383 /** 384 * Tests if string {@code target} matches pattern {@code pattern} 385 * @see {@link Pattern#matches(String, CharSequence)} 386 * @since 5699 387 */ 304 388 public static boolean regexp_test(String pattern, String target) { 305 389 return Pattern.matches(pattern, target); 306 390 } 307 391 392 /** 393 * Tests if string {@code target} matches pattern {@code pattern} 394 * @param flags a string that may contain "i" (case insensitive), "m" (multiline) and "s" ("dot all") 395 * @since 5699 396 */ 308 397 public static boolean regexp_test(String pattern, String target, String flags) { 309 398 int f = 0; … … 320 409 } 321 410 411 /** 412 * Tries to match string against pattern regexp and returns a list of capture groups in case of success. 413 * The first element (index 0) is the complete match (i.e. string). 414 * Further elements correspond to the bracketed parts of the regular expression. 415 * @param flags a string that may contain "i" (case insensitive), "m" (multiline) and "s" ("dot all") 416 * @since 5701 417 */ 322 418 public static List<String> regexp_match(String pattern, String target, String flags) { 323 419 int f = 0; … … 343 439 } 344 440 441 /** 442 * Tries to match string against pattern regexp and returns a list of capture groups in case of success. 443 * The first element (index 0) is the complete match (i.e. string). 444 * Further elements correspond to the bracketed parts of the regular expression. 445 * @since 5701 446 */ 345 447 public static List<String> regexp_match(String pattern, String target) { 346 448 Matcher m = Pattern.compile(pattern).matcher(target); … … 356 458 } 357 459 460 /** 461 * Returns the OSM id of the current object. 462 * @see {@link org.openstreetmap.josm.data.osm.AbstractPrimitive#generateUniqueId()} 463 */ 358 464 public long osm_id() { 359 465 return env.osm.getUniqueId(); 360 466 } 361 467 468 /** 469 * Translates some text for the current locale. The first argument is the text to translate, 470 * and the subsequent arguments are parameters for the string indicated by {@code {0}}, {@code {1}}, … 471 */ 362 472 public static String tr(String... args) { 363 473 final String text = args[0]; 364 474 System.arraycopy(args, 1, args, 0, args.length - 1); 365 475 return org.openstreetmap.josm.tools.I18n.tr(text, args); 476 } 477 478 /** 479 * Returns the substring of {@code s} starting at index {@code begin} (inclusive, 0-indexed). 480 * * @see {@link String#substring(int)} 481 */ 482 public static String substring(String s, /* due to missing Cascade.convertTo for int*/ float begin) { 483 return s == null ? null : s.substring((int) begin); 484 } 485 486 /** 487 * Returns the substring of {@code s} starting at index {@code begin} (inclusive) 488 * and ending at index {@code end}, (exclusive, 0-indexed). 489 * @see {@link String#substring(int, int)} 490 */ 491 public static String substring(String s, float begin, float end) { 492 return s == null ? null : s.substring((int) begin, (int) end); 493 } 494 495 /** 496 * Replaces in {@code s} every {@code} target} substring by {@code replacement}. 497 * * @see {@link String#replace(CharSequence, CharSequence)} 498 */ 499 public static String replace(String s, String target, String replacement) { 500 return s == null ? null : s.replace(target, replacement); 366 501 } 367 502 }
Note:
See TracChangeset
for help on using the changeset viewer.