check matching navigation items recursively
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 26 Dec 2013 18:24:17 +0000 (20:24 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 26 Dec 2013 18:24:17 +0000 (20:24 +0200)
src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java
src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java

index 1afd5c7..408372a 100755 (executable)
@@ -25,9 +25,11 @@ public class Navigation {
                try {
                        final String requestPath = new URL(requestUrl).getPath();
 
-                       for (final NavigationItem item : rootNavigationItem.subElements)
-                               if (item.matchesUrl(requestPath))
-                                       return item;
+                       final NavigationItem match = rootNavigationItem
+                                       .getMatchingNavigationItem(requestPath);
+
+                       if (match != null)
+                               return match;
                } catch (final MalformedURLException e) {
                        System.out.println("Cannot handle URL: " + requestUrl);
                        e.printStackTrace();
index eef52fb..a9ad856 100755 (executable)
@@ -55,6 +55,20 @@ public class NavigationItem {
                return parent.getLocaleOrder();
        }
 
+       NavigationItem getMatchingNavigationItem(final String requestPath) {
+               if (matchesUrl(requestPath))
+                       return this;
+
+               for (final NavigationItem childNavigationItem : subElements) {
+                       final NavigationItem match = childNavigationItem
+                                       .getMatchingNavigationItem(requestPath);
+
+                       if (match != null)
+                               return match;
+               }
+               return null;
+       }
+
        public List<NavigationItem> getSubElements() {
                return subElements;
        }
@@ -82,7 +96,6 @@ public class NavigationItem {
 
        public boolean matchesUrl(final String url) {
                return WildCardMatcher.match(url, matchingPattern);
-
        }
 
        public NavigationItem setPattern(final String pattern) {