use of com.vaadin.flow.router.legacy.RouteLocation.RouteSegmentVisitor in project flow by vaadin.
the class ViewRenderer method extractRoutePlaceholders.
private static Map<String, String> extractRoutePlaceholders(Location destination, RouteLocation routeDefinition) {
assert destination != null;
assert routeDefinition != null;
Map<String, String> routePlaceholders = new HashMap<>();
routeDefinition.visitSegments(new RouteSegmentVisitor() {
Optional<Location> maybeCurrentDestination = Optional.of(destination);
private String getNextDestinationSegment() {
Location currentDestination = maybeCurrentDestination.get();
/*
* This might return an empty optional, but in that case there
* shouldn't be any more calls to any accept method, as long as
* the current destination actually matches the route
* definition.
*/
maybeCurrentDestination = currentDestination.getSubLocation();
return currentDestination.getFirstSegment();
}
@Override
public void acceptWildcard() {
// This is always the last step, so not need to advance the
// destination
routePlaceholders.put("*", maybeCurrentDestination.get().getPath());
}
@Override
public void acceptSegment(String segmentName) {
// Just advance the current destination
getNextDestinationSegment();
}
@Override
public void acceptPlaceholder(String placeholderName) {
String placeholderValue = getNextDestinationSegment();
assert !routePlaceholders.containsKey(placeholderName);
routePlaceholders.put(placeholderName, placeholderValue);
}
});
return routePlaceholders;
}
use of com.vaadin.flow.router.legacy.RouteLocation.RouteSegmentVisitor in project flow by vaadin.
the class RouterLink method buildUrl.
/**
* Creates a URL for a route by populating placeholder and wildcard slots
* with the given parameter values. The slots are populated starting from
* the beginning of the route. The number of parameters must match the
* number of slots in the route.
*
* @param route
* the route to use, not <code>null</code>
* @param parameters
* the parameter values to set in the route
* @return a URL with all placeholder and wildcard slots populated
*/
public static String buildUrl(String route, String... parameters) {
assert route != null;
assert parameters != null;
Iterator<String> parametersIterator = Arrays.asList(parameters).iterator();
RouteLocation location = new RouteLocation(new Location(route));
List<String> urlSegments = new ArrayList<>(location.getSegments().size());
location.visitSegments(new RouteSegmentVisitor() {
@Override
public void acceptPlaceholder(String placeholderName) {
if (!parametersIterator.hasNext()) {
throw new IllegalArgumentException(route + " has more placeholders than the number of given parameters: " + Arrays.toString(parameters));
}
urlSegments.add(parametersIterator.next());
}
@Override
public void acceptWildcard() {
if (parametersIterator.hasNext()) {
urlSegments.add(parametersIterator.next());
} else {
urlSegments.add("");
}
}
@Override
public void acceptSegment(String segment) {
urlSegments.add(segment);
}
});
if (parametersIterator.hasNext()) {
throw new IllegalArgumentException(route + " has fewer placeholders than the number of given parameters: " + Arrays.toString(parameters));
}
return new Location(urlSegments).getPath();
}
use of com.vaadin.flow.router.legacy.RouteLocation.RouteSegmentVisitor in project flow by vaadin.
the class RouteLocationTest method walkRouteWithInvalidWildcard.
@Test(expected = IllegalArgumentException.class)
public void walkRouteWithInvalidWildcard() {
RouteLocation location = new RouteLocation(new Location("foo/*/*"));
RouteSegmentVisitor visitor = EasyMock.createMock(RouteSegmentVisitor.class);
location.visitSegments(visitor);
}
use of com.vaadin.flow.router.legacy.RouteLocation.RouteSegmentVisitor in project flow by vaadin.
the class RouteLocationTest method testWalkRoute.
@Test
public void testWalkRoute() {
RouteLocation location = new RouteLocation(new Location("foo/{bar}/*"));
AtomicInteger calls = new AtomicInteger();
location.visitSegments(new RouteSegmentVisitor() {
@Override
public void acceptSegment(String segmentName) {
Assert.assertEquals("foo", segmentName);
Assert.assertEquals(1, calls.incrementAndGet());
}
@Override
public void acceptPlaceholder(String placeholderName) {
Assert.assertEquals("bar", placeholderName);
Assert.assertEquals(2, calls.incrementAndGet());
}
@Override
public void acceptWildcard() {
Assert.assertEquals(3, calls.incrementAndGet());
}
});
Assert.assertEquals(3, calls.get());
}
Aggregations