From: Svjatoslav Agejenko Date: Thu, 21 Aug 2014 19:56:13 +0000 (+0300) Subject: Created dedicated locale configuration handler. X-Git-Tag: svjatoslavcommons-1.8~97 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=869bf9eeef97f5364d46b76f64c94cec67846c28;p=svjatoslav_commons.git Created dedicated locale configuration handler. --- diff --git a/src/main/java/eu/svjatoslav/commons/network/LocaleConfiguration.java b/src/main/java/eu/svjatoslav/commons/network/LocaleConfiguration.java new file mode 100755 index 0000000..2230885 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/network/LocaleConfiguration.java @@ -0,0 +1,63 @@ +/* + * 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 allowedLocales = new ArrayList(); + + 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 getAllowedLocales() { + return allowedLocales; + } + +} diff --git a/src/main/java/eu/svjatoslav/commons/network/LocaleHelper.java b/src/main/java/eu/svjatoslav/commons/network/LocaleHelper.java deleted file mode 100755 index a196c3b..0000000 --- a/src/main/java/eu/svjatoslav/commons/network/LocaleHelper.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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; - } - -} diff --git a/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java b/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java index 8b049bb..5e605ab 100755 --- a/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java @@ -15,7 +15,7 @@ import java.net.URL; 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 { @@ -23,6 +23,12 @@ public class Navigation { private NavigationItem defaultNavigationItem; + private final LocaleConfiguration localeConfiguration; + + public Navigation(final LocaleConfiguration localeConfiguration) { + this.localeConfiguration = localeConfiguration; + } + public NavigationItem getDefaultNavigationItem() { return defaultNavigationItem; } @@ -34,6 +40,10 @@ public class Navigation { return ""; } + public LocaleConfiguration getLocaleConfiguration() { + return localeConfiguration; + } + public NavigationItem getRootNavigationItem() { return rootNavigationItem; } @@ -59,7 +69,8 @@ public class Navigation { 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(); diff --git a/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java b/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java index 14c8b0a..2a27c5d 100755 --- a/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java @@ -21,9 +21,9 @@ public class NavigationItem { private String matchingPattern; private final Map localeToTitle = new HashMap(); - ArrayList subElements = new ArrayList(); - private Locale[] localeOrder; - private NavigationItem parent; + final ArrayList subElements = new ArrayList(); + final private NavigationItem parent; + final private Navigation navigation; private final String linkUrl; /** @@ -31,18 +31,17 @@ public class NavigationItem { */ 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; @@ -51,10 +50,9 @@ public class NavigationItem { } 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; } @@ -72,13 +70,6 @@ public class NavigationItem { return linkUrl; } - public Locale[] getLocaleOrder() { - if (localeOrder != null) - return localeOrder; - - return parent.getLocaleOrder(); - } - NavigationItem getMatchingNavigationItem(final String requestPath) { if (matchesUrl(requestPath)) return this; @@ -97,26 +88,19 @@ public class NavigationItem { 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 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) {