Search in sources :

Example 1 with RouteLocation

use of com.vaadin.flow.router.legacy.RouteLocation 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 2 with RouteLocation

use of com.vaadin.flow.router.legacy.RouteLocation 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 3 with RouteLocation

use of com.vaadin.flow.router.legacy.RouteLocation 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

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