Ticket #17995: 17995.2.patch
File 17995.2.patch, 6.2 KB (added by , 5 years ago) |
---|
-
src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java
338 338 public Object evaluate(Environment env) { 339 339 Object[] convertedArgs; 340 340 341 int start = 0; 342 int offset = 0; 341 343 if (needsEnvironment) { 342 convertedArgs = new Object[args.size()+1]; 344 start = 1; 345 offset = 1; 346 convertedArgs = new Object[args.size() + 1]; 343 347 convertedArgs[0] = env; 344 for (int i = 1; i < convertedArgs.length; ++i) {345 convertedArgs[i] = Cascade.convertTo(args.get(i-1).evaluate(env), expectedParameterTypes[i]);346 if (convertedArgs[i] == null && !nullable) {347 return null;348 }349 }350 348 } else { 351 349 convertedArgs = new Object[args.size()]; 352 for (int i = 0; i < convertedArgs.length; ++i) { 353 convertedArgs[i] = Cascade.convertTo(args.get(i).evaluate(env), expectedParameterTypes[i]); 354 if (convertedArgs[i] == null && !nullable) { 355 return null; 350 } 351 352 for (int i = start; i < convertedArgs.length; ++i) { 353 if (!expectedParameterTypes[i].isArray()) { 354 convertedArgs[i] = Cascade.convertTo(args.get(i - offset).evaluate(env), expectedParameterTypes[i]); 355 } else { 356 Class<?> clazz = expectedParameterTypes[i].getComponentType(); 357 Object[] varargs = (Object[]) Array.newInstance(clazz, args.size() - i + 1); 358 for (int j = 0; j < args.size() - i + 1; ++j) { 359 varargs[j] = Cascade.convertTo(args.get(j + i - 1).evaluate(env), clazz); 356 360 } 361 convertedArgs[i] = expectedParameterTypes[i].cast(varargs); 362 break; 357 363 } 364 if (convertedArgs[i] == null && !nullable) { 365 return null; 366 } 358 367 } 368 359 369 Object result = null; 360 370 try { 361 371 result = m.invoke(null, convertedArgs); -
src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java
494 494 } 495 495 496 496 /** 497 * Sort an array of strings 498 * @param sortables The array to sort 499 * @return The sorted array 500 * @since xxx 501 */ 502 public static List<String> sort(String... sortables) { 503 Logging.setLogLevel(Logging.LEVEL_DEBUG); 504 Logging.debug("Sorting"); 505 Arrays.parallelSort(sortables); 506 Logging.debug("{}", (Object[]) sortables); 507 return Arrays.asList(sortables); 508 } 509 510 /** 497 511 * Returns the role of current object in parent relation, or role of child if current object is a relation. 498 512 * @param env the environment 499 513 * @return role of current object in parent relation, or role of child if current object is a relation … … 504 518 } 505 519 506 520 /** 507 * Returns t rue if role is in relation. Returns false if not a relation or it does not have the role.521 * Returns the number of primitives in a relation with the specified roles. 508 522 * @param env the environment 509 523 * @param roles The roles to count in the relation 510 524 * @return The number of relation members with the specified role -
test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTest.java
9 9 10 10 import java.awt.Color; 11 11 import java.io.StringReader; 12 import java.util.Arrays; 12 13 import java.util.List; 13 14 14 15 import org.junit.Rule; … … 407 408 } 408 409 409 410 @Test 411 public void testSort() throws Exception { 412 assertEquals(Arrays.asList(new String[] {"alpha", "beta"}), Functions.sort("beta", "alpha")); 413 Way way1 = TestUtils.newWay("highway=residential name=Alpha alt_name=Beta", new Node(new LatLon(0.001, 0.001)), 414 new Node(new LatLon(0.002, 0.002))); 415 416 MapCSSStyleSource source = new MapCSSStyleSource("way[highway] {sorted: join_list(\",\", sort(tag(\"alt_name\"), tag(\"name\")));}"); 417 source.loadStyleSource(); 418 assertEquals(1, source.rules.size()); 419 Environment e = new Environment(way1, new MultiCascade(), Environment.DEFAULT_LAYER, null); 420 assertTrue(source.rules.get(0).selector.matches(e)); 421 source.rules.get(0).declaration.execute(e); 422 assertEquals(Functions.join(",", "Alpha", "Beta"), e.getCascade(Environment.DEFAULT_LAYER).get("sorted", null, String.class)); 423 } 424 425 @Test 410 426 public void testCountRoles() throws Exception { 411 427 DataSet ds = new DataSet(); 412 428 Way way1 = TestUtils.newWay("highway=residential name=1", … … 445 461 /* Check with non-relation */ 446 462 e = new Environment(way1, new MultiCascade(), Environment.DEFAULT_LAYER, null); 447 463 assertEquals(0, Functions.count_roles(e, "from", "to")); 464 465 /* Check with actual call to mapcss functions */ 466 MapCSSStyleSource source = new MapCSSStyleSource("relation[type=destination_sign] {roles: count_roles(\"from\");}"); 467 source.loadStyleSource(); 468 assertEquals(1, source.rules.size()); 469 e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null); 470 assertTrue(source.rules.get(0).selector.matches(e)); 471 source.rules.get(0).declaration.execute(e); 472 assertEquals((Integer) 1, e.getCascade(Environment.DEFAULT_LAYER).get("roles", null, Integer.class)); 448 473 } 449 474 450 475 @Test