Search in sources :

Example 1 with RouteSegmentVisitor

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;
}
Also used : HashMap(java.util.HashMap) RouteSegmentVisitor(com.vaadin.flow.router.legacy.RouteLocation.RouteSegmentVisitor) Location(com.vaadin.flow.router.Location)

Example 2 with RouteSegmentVisitor

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();
}
Also used : ArrayList(java.util.ArrayList) RouteLocation(com.vaadin.flow.router.legacy.RouteLocation) RouteSegmentVisitor(com.vaadin.flow.router.legacy.RouteLocation.RouteSegmentVisitor) RouteLocation(com.vaadin.flow.router.legacy.RouteLocation)

Example 3 with RouteSegmentVisitor

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);
}
Also used : RouteLocation(com.vaadin.flow.router.legacy.RouteLocation) RouteSegmentVisitor(com.vaadin.flow.router.legacy.RouteLocation.RouteSegmentVisitor) RouteLocation(com.vaadin.flow.router.legacy.RouteLocation) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 4 with RouteSegmentVisitor

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RouteLocation(com.vaadin.flow.router.legacy.RouteLocation) RouteSegmentVisitor(com.vaadin.flow.router.legacy.RouteLocation.RouteSegmentVisitor) RouteLocation(com.vaadin.flow.router.legacy.RouteLocation) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Aggregations

RouteSegmentVisitor (com.vaadin.flow.router.legacy.RouteLocation.RouteSegmentVisitor)4 Location (com.vaadin.flow.router.Location)3 RouteLocation (com.vaadin.flow.router.legacy.RouteLocation)3 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1