+++ /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;
-
-public enum Locale {
- ENG("eng"), EST("est"), RUS("rus");
-
- String asString;
-
- Locale(final String asString) {
- this.asString = asString;
- }
-
- public String asString() {
- return asString;
- }
-
-}
public class LocaleConfiguration {
- Locale defaultLocale;
+ String defaultLocale;
- private final List<Locale> allowedLocales = new ArrayList<Locale>();
+ private final List<String> allowedLocales = new ArrayList<String>();
- public LocaleConfiguration(final Locale defaultLocale,
- final Locale... allowedLocales) {
+ public LocaleConfiguration(final String defaultLocale,
+ final String... allowedLocales) {
this.defaultLocale = defaultLocale;
- for (final Locale locale : allowedLocales)
- this.getAllowedLocales().add(locale);
+ for (final String locale : allowedLocales)
+ getAllowedLocales().add(locale);
}
- public Locale detectCurrentLocale(final HttpServletRequest request) {
+ public String detectCurrentLocale(final HttpServletRequest request) {
final String sessionLocaleString = (String) request.getSession()
.getAttribute("locale");
- Locale result = localeFromString(sessionLocaleString);
- if (result == null)
- result = defaultLocale;
+ String result = defaultLocale;
+ if (isAllowedLocale(sessionLocaleString))
+ result = sessionLocaleString;
- final Locale requestLocale = localeFromString(request
- .getParameter("locale"));
- if (requestLocale != null)
+ final String requestLocale = request.getParameter("locale");
+
+ if (isAllowedLocale(requestLocale))
result = requestLocale;
- request.getSession().setAttribute("locale", result.asString());
+ request.getSession().setAttribute("locale", result);
return result;
}
- private Locale localeFromString(final String localeString) {
- for (final Locale locale : getAllowedLocales())
- if (locale.asString().equals(localeString))
- return locale;
-
- return null;
+ public List<String> getAllowedLocales() {
+ return allowedLocales;
}
- public List<Locale> getAllowedLocales() {
- return allowedLocales;
+ public boolean isAllowedLocale(final String locale) {
+ return allowedLocales.contains(locale);
}
}
import javax.servlet.http.HttpServletRequest;
-import eu.svjatoslav.commons.network.Locale;
import eu.svjatoslav.commons.network.LocaleConfiguration;
public class Navigation<NI extends NavigationItem> {
public String getTopMenu(final HttpServletRequest request) {
- final Locale currentLocale = localeConfiguration
+ final String currentLocale = localeConfiguration
.detectCurrentLocale(request);
final NI selectedItem = getSelectedItem(request);
import java.util.List;
import java.util.Map;
-import eu.svjatoslav.commons.network.Locale;
import eu.svjatoslav.commons.string.WildCardMatcher;
public class NavigationItem {
private String matchingPattern;
- private final Map<Locale, String> localeToTitle = new HashMap<Locale, String>();
+ private final Map<String, String> localeToTitle = new HashMap<String, String>();
private final ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
private final NavigationItem parent;
private final Navigation<NavigationItem> navigation;
return subElements;
}
- public String getTitle(final Locale locale) {
+ public String getTitle(final String locale) {
return localeToTitle.get(locale);
}
private void initializeLocalizedTitles(final String... titles) {
- final List<Locale> locales = getNavigation().getLocaleConfiguration()
+ final List<String> locales = getNavigation().getLocaleConfiguration()
.getAllowedLocales();
if (locales.size() != titles.length)
throw new RuntimeException("There should be exactly "