Author: julien_viet
Date: 2010-09-12 19:07:32 -0400 (Sun, 12 Sep 2010)
New Revision: 4160
Added:
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestHierarchy.java
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/RouteDescriptor.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/RouterDescriptor.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Router.java
Log:
- rename SimpleRoute to SegmentRoute
- add nested route descriptor
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/RouteDescriptor.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/RouteDescriptor.java 2010-09-12
22:55:24 UTC (rev 4159)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/RouteDescriptor.java 2010-09-12
23:07:32 UTC (rev 4160)
@@ -21,7 +21,9 @@
import org.exoplatform.web.controller.QualifiedName;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
/**
@@ -37,10 +39,14 @@
/** . */
private final Map<QualifiedName, String> parameters;
+ /** . */
+ private final List<RouteDescriptor> children;
+
public RouteDescriptor(String path)
{
this.path = path;
this.parameters = new HashMap<QualifiedName, String>();
+ this.children = new ArrayList<RouteDescriptor>();
}
public String getPath()
@@ -63,4 +69,15 @@
{
return parameters;
}
+
+ public RouteDescriptor addChild(RouteDescriptor child)
+ {
+ children.add(child);
+ return this;
+ }
+
+ public List<RouteDescriptor> getChildren()
+ {
+ return children;
+ }
}
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/RouterDescriptor.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/RouterDescriptor.java 2010-09-12
22:55:24 UTC (rev 4159)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/RouterDescriptor.java 2010-09-12
23:07:32 UTC (rev 4160)
@@ -37,7 +37,7 @@
this.routes = new ArrayList<RouteDescriptor>();
}
- public void addRoute(RouteDescriptor controller)
+ public RouterDescriptor addRoute(RouteDescriptor controller)
{
if (controller == null)
{
@@ -46,6 +46,9 @@
//
routes.add(controller);
+
+ //
+ return this;
}
public Iterable<RouteDescriptor> getRoutes()
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java 2010-09-12
22:55:24 UTC (rev 4159)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java 2010-09-12
23:07:32 UTC (rev 4160)
@@ -20,6 +20,7 @@
package org.exoplatform.web.controller.router;
import org.exoplatform.web.controller.QualifiedName;
+import org.exoplatform.web.controller.metadata.RouteDescriptor;
import java.util.ArrayList;
import java.util.HashMap;
@@ -321,18 +322,8 @@
/** . */
private static final Pattern PARAMETER_REGEX =
Pattern.compile("^(?:\\{([^\\}]*)\\})?(.*)$");
- final Route append(
- String path,
- Map<QualifiedName, String> parameters)
+ final <R extends Route> R add(R route)
{
- Route route = append(path);
- route.terminal = true;
- route.routeParameters.putAll(parameters);
- return route;
- }
-
- <R extends Route> R add(R route)
- {
if (route.parent != null)
{
throw new IllegalArgumentException();
@@ -366,33 +357,61 @@
}
}
- Set<String> getSegmentNames()
+ final Set<String> getSegmentNames()
{
return segments.keySet();
}
- int getSegmentSize(String segmentName)
+ final int getSegmentSize(String segmentName)
{
List<SegmentRoute> routes = segments.get(segmentName);
return routes != null ? routes.size() : 0;
}
- SegmentRoute getSegment(String segmentName, int index)
+ final SegmentRoute getSegment(String segmentName, int index)
{
List<SegmentRoute> routes = segments.get(segmentName);
return routes != null ? routes.get(index) : null;
}
- int getPatternSize()
+ final int getPatternSize()
{
return patterns.size();
}
- PatternRoute getPattern(int index)
+ final PatternRoute getPattern(int index)
{
return patterns.get(index);
}
+ final Route append(RouteDescriptor descriptor)
+ {
+ Route route = append(descriptor.getPath());
+
+ //
+ route.terminal = true;
+ route.routeParameters.putAll(descriptor.getParameters());
+
+ //
+ for (RouteDescriptor childDescriptor : descriptor.getChildren())
+ {
+ route.append(childDescriptor);
+ }
+
+ //
+ return route;
+ }
+
+ final Route append(
+ String path,
+ Map<QualifiedName, String> parameters)
+ {
+ Route route = append(path);
+ route.terminal = true;
+ route.routeParameters.putAll(parameters);
+ return route;
+ }
+
/**
* Append a path, creates the necessary routes and returns the last route added.
*
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Router.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Router.java 2010-09-12
22:55:24 UTC (rev 4159)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Router.java 2010-09-12
23:07:32 UTC (rev 4160)
@@ -50,7 +50,7 @@
public void addRoute(RouteDescriptor routeMetaData)
{
- root.append(routeMetaData.getPath(), routeMetaData.getParameters());
+ root.append(routeMetaData);
}
public String render(Map<QualifiedName, String> parameters)
Added:
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestHierarchy.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestHierarchy.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestHierarchy.java 2010-09-12
23:07:32 UTC (rev 4160)
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.web.controller.router;
+
+import org.exoplatform.web.controller.QualifiedName;
+import org.exoplatform.web.controller.metadata.RouteDescriptor;
+import org.exoplatform.web.controller.metadata.RouterDescriptor;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class TestHierarchy extends AbstractTestController
+{
+
+ public void testFoo() throws Exception
+ {
+
+ RouteDescriptor descriptor = new RouteDescriptor("/a").
+ addParameter("foo", "bar").
+ addChild(new RouteDescriptor("/b").addParameter("juu",
"daa"));
+
+ //
+ Router router = new Router(new RouterDescriptor().addRoute(descriptor));
+
+ //
+ assertEquals(Collections.singletonMap(new QualifiedName("foo"),
"bar"), router.process("/a"));
+
+ //
+ Map<QualifiedName, String> expected = new HashMap<QualifiedName,
String>();
+ expected.put(new QualifiedName("foo"), "bar");
+ expected.put(new QualifiedName("juu"), "daa");
+ assertEquals(expected, router.process("/a/b"));
+ }
+}