2 * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.commons.network.navigation;
7 import javax.servlet.http.HttpServletRequest;
8 import java.net.MalformedURLException;
11 public class Navigation {
13 private NavigationItem root = new NavigationItem(null, null, null);
15 public NavigationItem getRoot(){
19 public NavigationItem getSelectedItem(final HttpServletRequest request) {
20 final String requestUrl = request.getRequestURL().toString();
22 final String requestPath;
24 requestPath = new URL(requestUrl).getPath();
25 } catch (MalformedURLException e) {
26 throw new IllegalArgumentException("Illegal request URL provided." , e);
29 @SuppressWarnings("unchecked") final NavigationItem match = root.getMatch(requestPath);
34 return root.getDefaultNavigationItem();
37 @SuppressWarnings("unchecked")
38 public String getTopMenu(final HttpServletRequest request) {
40 final NavigationItem selectedItem = getSelectedItem(request);
41 final StringBuilder result = new StringBuilder();
43 result.append("<div class=\"navigationMenu\">");
45 for (final NavigationItem item : root.getChildren()) {
47 if (item == selectedItem)
48 result.append("<a class=\"menuItemSelected\" href=\""
49 + item.getUrl() + "\">");
51 result.append("<a class=\"menuItem\" href=\""
52 + item.getUrl() + "\">");
54 result.append("<div>" + item.getTitle() + "</div>");
55 result.append("</a>");
58 result.append("</div>");
60 return result.toString();