<connection>scm:git:ssh://git@svjatoslav.eu/home/git/repositories/svjatoslav_commons.git</connection>
<developerConnection>scm:git:ssh://git@svjatoslav.eu/home/git/repositories/svjatoslav_commons.git
</developerConnection>
- <tag>HEAD</tag>
+ <tag>HEAD</tag>
</scm>
</project>
+++ /dev/null
-/*
- * 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;
-
-import javax.servlet.http.HttpServletRequest;
-import java.util.ArrayList;
-import java.util.List;
-
-public class LocaleConfiguration {
-
- final String defaultLocale;
- private final List<String> allowedLocales = new ArrayList<>();
-
- public LocaleConfiguration(final String defaultLocale,
- final String... allowedLocales) {
-
- this.defaultLocale = defaultLocale;
-
- for (final String locale : allowedLocales)
- getAllowedLocales().add(locale);
- }
-
- public String detectCurrentLocale(final HttpServletRequest request) {
-
- final String sessionLocaleString = (String) request.getSession()
- .getAttribute("locale");
-
- String result = defaultLocale;
- if (isAllowedLocale(sessionLocaleString))
- result = sessionLocaleString;
-
- final String requestLocale = request.getParameter("locale");
-
- if (isAllowedLocale(requestLocale))
- result = requestLocale;
-
- request.getSession().setAttribute("locale", result);
-
- return result;
- }
-
- public List<String> getAllowedLocales() {
- return allowedLocales;
- }
-
- public boolean isAllowedLocale(final String locale) {
- return allowedLocales.contains(locale);
- }
-
-}
package eu.svjatoslav.commons.network.navigation;
-import eu.svjatoslav.commons.network.LocaleConfiguration;
-
import javax.servlet.http.HttpServletRequest;
import java.net.MalformedURLException;
import java.net.URL;
-import java.util.List;
-
-public class Navigation<NI extends NavigationItem> {
- protected final LocaleConfiguration localeConfiguration;
- private NI rootNavigationItem;
- private NI defaultNavigationItem;
+public class Navigation {
- public Navigation(final LocaleConfiguration localeConfiguration) {
- this.localeConfiguration = localeConfiguration;
- }
+ private NavigationItem root = new NavigationItem(null, null, null);
- public NI getDefaultNavigationItem() {
- return defaultNavigationItem;
+ public NavigationItem getRoot(){
+ return root;
}
- private String getGetNotNullString(final String string) {
- if (string != null)
- return string;
-
- return "";
- }
-
- public LocaleConfiguration getLocaleConfiguration() {
- return localeConfiguration;
- }
-
- public NI getRootNavigationItem() {
- return rootNavigationItem;
- }
-
- public NI getSelectedItem(final HttpServletRequest request) {
+ public NavigationItem getSelectedItem(final HttpServletRequest request) {
final String requestUrl = request.getRequestURL().toString();
+ final String requestPath;
try {
- final String requestPath = new URL(requestUrl).getPath();
+ requestPath = new URL(requestUrl).getPath();
+ } catch (MalformedURLException e) {
+ throw new IllegalArgumentException("Illegal request URL provided." , e);
+ }
- @SuppressWarnings("unchecked") final NI match = (NI) rootNavigationItem.getMatchingNavigationItem(requestPath);
+ @SuppressWarnings("unchecked") final NavigationItem match = root.getMatch(requestPath);
- if (match != null)
- return match;
- } catch (final MalformedURLException e) {
- System.out.println("Cannot handle URL: " + requestUrl);
- e.printStackTrace();
- }
+ if (match != null)
+ return match;
- return getDefaultNavigationItem();
+ return root.getDefaultNavigationItem();
}
@SuppressWarnings("unchecked")
public String getTopMenu(final HttpServletRequest request) {
- final String currentLocale = localeConfiguration.detectCurrentLocale(request);
-
- final NI selectedItem = getSelectedItem(request);
+ final NavigationItem selectedItem = getSelectedItem(request);
final StringBuilder result = new StringBuilder();
- result.append("<div class=\"" + getGetNotNullString(rootNavigationItem.getCssPrefix()) + "navigationMenu\">");
+ result.append("<div class=\"navigationMenu\">");
- for (final NI item : (List<NI>) getRootNavigationItem().getSubElements()) {
+ for (final NavigationItem item : root.getChildren()) {
if (item == selectedItem)
- result.append("<a class=\"" + getGetNotNullString(item.getCssPrefix()) + "menuItemSelected\" href=\""
- + item.getLinkUrl() + "\">");
+ result.append("<a class=\"menuItemSelected\" href=\""
+ + item.getUrl() + "\">");
else
- result.append("<a class=\"" + getGetNotNullString(item.getCssPrefix()) + "menuItem\" href=\""
- + item.getLinkUrl() + "\">");
+ result.append("<a class=\"menuItem\" href=\""
+ + item.getUrl() + "\">");
- result.append("<div>" + item.getTitle(currentLocale) + "</div>");
+ result.append("<div>" + item.getTitle() + "</div>");
result.append("</a>");
}
return result.toString();
}
- public NI setDefaultNavigationItem(final NI defaultNavigationItem) {
- this.defaultNavigationItem = defaultNavigationItem;
- return defaultNavigationItem;
- }
-
- public NI setRootNavigationItem(final NI rootNavigationItem) {
- this.rootNavigationItem = rootNavigationItem;
- return rootNavigationItem;
- }
-
}
import eu.svjatoslav.commons.string.WildCardMatcher;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
public class NavigationItem {
- private final Map<String, String> localeToTitle = new HashMap<>();
- private final ArrayList<NavigationItem> subElements = new ArrayList<>();
- private final NavigationItem parent;
- private final Navigation<? extends NavigationItem> navigation;
- private final String linkUrl;
- private String matchingPattern;
- /**
- * CSS prefix is inherited to child menu items.
- */
- private String cssPrefix;
-
- /**
- * Used to build root navigation item.
- */
- public NavigationItem(final Navigation<? extends NavigationItem> navigation) {
- this.navigation = navigation;
- parent = null;
- matchingPattern = null;
- linkUrl = null;
- }
-
- public NavigationItem(final NavigationItem parent, final String linkUrl, final String... titles) {
-
- navigation = parent.getNavigation();
- this.parent = parent;
- this.linkUrl = linkUrl;
- matchingPattern = linkUrl;
+ private final ArrayList<NavigationItem> children = new ArrayList<>();
+ private final String url;
+ private String pattern;
+ private String title;
+ private boolean isDefault;
- initializeLocalizedTitles(titles);
-
- parent.addNavigationItem(this);
+ public NavigationItem(final String url, String pattern, String title ) {
+ this.url = url;
+ this.pattern = pattern;
+ this.title = title;
}
- public void addNavigationItem(final NavigationItem navigationItem) {
- subElements.add(navigationItem);
+ public NavigationItem setDefault(){
+ isDefault = true;
+ return this;
}
- protected String getCssPrefix() {
- if (cssPrefix != null)
- return cssPrefix;
-
- if (parent == null)
- return null;
-
- return parent.getCssPrefix();
+ public boolean isDefault(){
+ return isDefault;
}
- public void setCssPrefix(final String cssPrefix) {
- this.cssPrefix = cssPrefix;
+ public void add(final NavigationItem navigationItem) {
+ children.add(navigationItem);
}
- public String getLinkUrl() {
- return linkUrl;
+ public String getUrl() {
+ return url;
}
- NavigationItem getMatchingNavigationItem(final String requestPath) {
+ NavigationItem getMatch(final String requestPath) {
if (matchesUrl(requestPath))
return this;
- for (final NavigationItem childNavigationItem : subElements) {
- final NavigationItem match = childNavigationItem.getMatchingNavigationItem(requestPath);
+ for (final NavigationItem childNavigationItem : children) {
+ final NavigationItem match = childNavigationItem.getMatch(requestPath);
if (match != null)
return match;
return null;
}
- public Navigation<? extends NavigationItem> getNavigation() {
- return navigation;
- }
-
- public List<NavigationItem> getSubElements() {
- return subElements;
+ public List<NavigationItem> getChildren() {
+ return children;
}
- public String getTitle(final String locale) {
- return localeToTitle.get(locale);
- }
-
- private void initializeLocalizedTitles(final String... titles) {
- final List<String> locales = getNavigation().getLocaleConfiguration().getAllowedLocales();
- if (locales.size() != titles.length)
- throw new RuntimeException("There should be exactly " + locales.size() + " title(s).");
-
- for (int i = 0; i < titles.length; i++)
- localeToTitle.put(locales.get(i), titles[i]);
+ public String getTitle() {
+ return title;
}
public boolean matchesUrl(final String url) {
- return WildCardMatcher.match(url, matchingPattern);
- }
-
- public NavigationItem setPattern(final String pattern) {
- matchingPattern = pattern;
- return this;
+ return WildCardMatcher.match(url, pattern);
}
- @Override
- public String toString() {
- return "NavigationItem [matchingPattern=" + matchingPattern + ", localeToTitle=" + localeToTitle
- + ", subElements count=" + subElements.size() + ", linkUrl=" + linkUrl + ", cssPrefix=" + cssPrefix
- + "]";
+ public NavigationItem getDefaultNavigationItem() {
+ for (NavigationItem child : children)
+ if (child.isDefault())
+ return child;
+ return null;
}
-
}