--- /dev/null
+/*
+ * Svjatoslav Commons - shared library of common functionality.
+ * Copyright ©2012-2014, 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 java.util.ArrayList;
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+
+public class LocaleConfiguration {
+
+ Locale defaultLocale;
+
+ private final List<Locale> allowedLocales = new ArrayList<Locale>();
+
+ public LocaleConfiguration(final Locale defaultLocale,
+ final Locale... allowedLocales) {
+
+ this.defaultLocale = defaultLocale;
+
+ for (final Locale locale : allowedLocales)
+ this.getAllowedLocales().add(locale);
+ }
+
+ public Locale detectCurrentLocale(final HttpServletRequest request) {
+
+ final String sessionLocaleString = (String) request.getSession()
+ .getAttribute("locale");
+
+ Locale result = localeFromString(sessionLocaleString);
+ if (result == null)
+ result = defaultLocale;
+
+ final Locale requestLocale = localeFromString(request
+ .getParameter("locale"));
+ if (requestLocale != null)
+ result = requestLocale;
+
+ request.getSession().setAttribute("locale", result.asString());
+
+ return result;
+ }
+
+ private Locale localeFromString(final String localeString) {
+ for (final Locale locale : getAllowedLocales())
+ if (locale.asString().equals(localeString))
+ return locale;
+
+ return null;
+ }
+
+ public List<Locale> getAllowedLocales() {
+ return allowedLocales;
+ }
+
+}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2014, 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;
-
-public class LocaleHelper {
-
- public static Locale detectLocale(final HttpServletRequest request) {
-
- final String sessionLocaleString = (String) request.getSession()
- .getAttribute("locale");
-
- Locale sessionLocale = localeFromString(sessionLocaleString);
- if (sessionLocale == null)
- sessionLocale = Locale.ENG;
-
- final Locale requestLocale = localeFromString(request
- .getParameter("locale"));
- if (requestLocale != null)
- sessionLocale = requestLocale;
-
- request.getSession().setAttribute("locale", sessionLocale.asString());
-
- return sessionLocale;
- }
-
- public static Locale localeFromString(final String localeString) {
- for (final Locale locale : Locale.values())
- if (locale.asString().equals(localeString))
- return locale;
-
- return null;
- }
-
-}
import javax.servlet.http.HttpServletRequest;
import eu.svjatoslav.commons.network.Locale;
-import eu.svjatoslav.commons.network.LocaleHelper;
+import eu.svjatoslav.commons.network.LocaleConfiguration;
public class Navigation {
private NavigationItem defaultNavigationItem;
+ private final LocaleConfiguration localeConfiguration;
+
+ public Navigation(final LocaleConfiguration localeConfiguration) {
+ this.localeConfiguration = localeConfiguration;
+ }
+
public NavigationItem getDefaultNavigationItem() {
return defaultNavigationItem;
}
return "";
}
+ public LocaleConfiguration getLocaleConfiguration() {
+ return localeConfiguration;
+ }
+
public NavigationItem getRootNavigationItem() {
return rootNavigationItem;
}
public String getTopMenu(final HttpServletRequest request) {
- final Locale currentLocale = LocaleHelper.detectLocale(request);
+ final Locale currentLocale = localeConfiguration
+ .detectCurrentLocale(request);
final NavigationItem selectedItem = getSelectedItem(request);
final StringBuffer result = new StringBuffer();
private String matchingPattern;
private final Map<Locale, String> localeToTitle = new HashMap<Locale, String>();
- ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
- private Locale[] localeOrder;
- private NavigationItem parent;
+ final ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
+ final private NavigationItem parent;
+ final private Navigation navigation;
private final String linkUrl;
/**
*/
private String cssPrefix;
- public NavigationItem(final Locale... localeOrder) {
- if (localeOrder.length == 0)
- this.localeOrder = new Locale[] { Locale.ENG };
- else
- this.localeOrder = localeOrder;
-
+ public NavigationItem(final Navigation navigation) {
+ this.navigation = navigation;
+ parent = null;
matchingPattern = null;
linkUrl = null;
}
- public NavigationItem(final NavigationItem parent, final String linkUrl,
+ private NavigationItem(final Navigation navigation,
+ final NavigationItem parent, final String linkUrl,
final String... titles) {
+ this.navigation = navigation;
this.parent = parent;
this.linkUrl = linkUrl;
matchingPattern = linkUrl;
}
public NavigationItem addChild(final String linkUrl, final String... titles) {
-
- final NavigationItem item = new NavigationItem(this, linkUrl, titles);
+ final NavigationItem item = new NavigationItem(navigation, this,
+ linkUrl, titles);
subElements.add(item);
-
return item;
}
return linkUrl;
}
- public Locale[] getLocaleOrder() {
- if (localeOrder != null)
- return localeOrder;
-
- return parent.getLocaleOrder();
- }
-
NavigationItem getMatchingNavigationItem(final String requestPath) {
if (matchesUrl(requestPath))
return this;
return subElements;
}
- public String getTitle() {
- if (localeToTitle.size() != 1)
- throw new RuntimeException(
- "NavigationItem.getTitle() was called without locale parameter, but there there are multiple titles in diffirent locales available.");
-
- return localeToTitle.values().iterator().next();
- }
-
public String getTitle(final Locale locale) {
return localeToTitle.get(locale);
}
private void initializeLocalizedTitles(final String... titles) {
- final Locale[] locales = getLocaleOrder();
- if (locales.length != titles.length)
+ final List<Locale> locales = navigation.getLocaleConfiguration()
+ .getAllowedLocales();
+ if (locales.size() != titles.length)
throw new RuntimeException("There should be exactly "
- + locales.length + " title(s).");
+ + locales.size() + " title(s).");
for (int i = 0; i < titles.length; i++)
- localeToTitle.put(locales[i], titles[i]);
+ localeToTitle.put(locales.get(i), titles[i]);
}
public boolean matchesUrl(final String url) {