2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2020, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.commons.network.navigation;
12 import javax.servlet.http.HttpServletRequest;
13 import java.net.MalformedURLException;
16 public class Navigation {
18 private NavigationItem root = new NavigationItem(null, null, null);
20 public NavigationItem getRoot(){
24 public NavigationItem getSelectedItem(final HttpServletRequest request) {
25 final String requestUrl = request.getRequestURL().toString();
27 final String requestPath;
29 requestPath = new URL(requestUrl).getPath();
30 } catch (MalformedURLException e) {
31 throw new IllegalArgumentException("Illegal request URL provided." , e);
34 @SuppressWarnings("unchecked") final NavigationItem match = root.getMatch(requestPath);
39 return root.getDefaultNavigationItem();
42 @SuppressWarnings("unchecked")
43 public String getTopMenu(final HttpServletRequest request) {
45 final NavigationItem selectedItem = getSelectedItem(request);
46 final StringBuilder result = new StringBuilder();
48 result.append("<div class=\"navigationMenu\">");
50 for (final NavigationItem item : root.getChildren()) {
52 if (item == selectedItem)
53 result.append("<a class=\"menuItemSelected\" href=\""
54 + item.getUrl() + "\">");
56 result.append("<a class=\"menuItem\" href=\""
57 + item.getUrl() + "\">");
59 result.append("<div>" + item.getTitle() + "</div>");
60 result.append("</a>");
63 result.append("</div>");
65 return result.toString();