-/*
- * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
- * This project is released under Creative Commons Zero (CC0) license.
- */
-package eu.svjatoslav.commons.network.navigation;
-
-import javax.servlet.http.HttpServletRequest;
-import java.net.MalformedURLException;
-import java.net.URL;
-
-public class Navigation {
-
- private NavigationItem root = new NavigationItem(null, null, null);
-
- public NavigationItem getRoot(){
- return root;
- }
-
- public NavigationItem getSelectedItem(final HttpServletRequest request) {
- final String requestUrl = request.getRequestURL().toString();
-
- final String requestPath;
- try {
- requestPath = new URL(requestUrl).getPath();
- } catch (MalformedURLException e) {
- throw new IllegalArgumentException("Illegal request URL provided." , e);
- }
-
- @SuppressWarnings("unchecked") final NavigationItem match = root.getMatch(requestPath);
-
- if (match != null)
- return match;
-
- return root.getDefaultNavigationItem();
- }
-
- @SuppressWarnings("unchecked")
- public String getTopMenu(final HttpServletRequest request) {
-
- final NavigationItem selectedItem = getSelectedItem(request);
- final StringBuilder result = new StringBuilder();
-
- result.append("<div class=\"navigationMenu\">");
-
- for (final NavigationItem item : root.getChildren()) {
-
- if (item == selectedItem)
- result.append("<a class=\"menuItemSelected\" href=\""
- + item.getUrl() + "\">");
- else
- result.append("<a class=\"menuItem\" href=\""
- + item.getUrl() + "\">");
-
- result.append("<div>" + item.getTitle() + "</div>");
- result.append("</a>");
- }
-
- result.append("</div>");
-
- return result.toString();
- }
-
-}