+/*
+ * Svjatoslav Commons - shared library of common functionality.
+ * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
+ */
+
package eu.svjatoslav.commons.network.navigation;
+import eu.svjatoslav.commons.string.GlobMatcher;
+
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
-
-import eu.svjatoslav.commons.network.Locale;
-import eu.svjatoslav.commons.string.WildCardMatcher;
public class NavigationItem {
- private final String link;
- private final String matchingWildcard;
- private final Map<Locale, String> titles = new HashMap<Locale, String>();
- ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
-
- public NavigationItem(final String link) {
- this.link = link;
- matchingWildcard = link;
- }
-
- public NavigationItem(final String link, final String wildcard) {
- this.link = link;
- matchingWildcard = wildcard;
- }
-
- public void addSubNavigation(final NavigationItem item) {
- subElements.add(item);
- }
-
- public NavigationItem addTitle(final Locale locale, final String title) {
- titles.put(locale, title);
- return this;
- }
-
- public NavigationItem addTitle(final String title) {
- titles.put(Locale.ENG, title);
- return this;
- }
-
- public String getLink() {
- return link;
- }
-
- public List<NavigationItem> getSubElements() {
- return subElements;
- }
-
- public String getTitle() {
- return titles.get(Locale.ENG);
- }
-
- public String getTitle(final Locale locale) {
- return titles.get(locale);
- }
-
- public boolean matchesUrl(final String url) {
-
- return WildCardMatcher.match(url, matchingWildcard);
-
- }
-
+ private final ArrayList<NavigationItem> children = new ArrayList<>();
+ private final String url;
+ private String pattern;
+ private String title;
+ private boolean isDefault;
+
+ public NavigationItem(final String url, String pattern, String title ) {
+ this.url = url;
+ this.pattern = pattern;
+ this.title = title;
+ }
+
+ public NavigationItem setDefault(){
+ isDefault = true;
+ return this;
+ }
+
+ public boolean isDefault(){
+ return isDefault;
+ }
+
+ public void add(final NavigationItem navigationItem) {
+ children.add(navigationItem);
+ }
+
+ public String getUrl() {
+ return url;
+ }
+
+ NavigationItem getMatch(final String requestPath) {
+ if (matchesUrl(requestPath))
+ return this;
+
+ for (final NavigationItem childNavigationItem : children) {
+ final NavigationItem match = childNavigationItem.getMatch(requestPath);
+
+ if (match != null)
+ return match;
+ }
+ return null;
+ }
+
+ public List<NavigationItem> getChildren() {
+ return children;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public boolean matchesUrl(final String url) {
+ return GlobMatcher.match(url, pattern);
+ }
+
+ public NavigationItem getDefaultNavigationItem() {
+ for (NavigationItem child : children)
+ if (child.isDefault())
+ return child;
+ return null;
+ }
}