Created dedicated locale configuration handler.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 21 Aug 2014 19:56:13 +0000 (22:56 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 21 Aug 2014 19:56:13 +0000 (22:56 +0300)
src/main/java/eu/svjatoslav/commons/network/LocaleConfiguration.java [new file with mode: 0755]
src/main/java/eu/svjatoslav/commons/network/LocaleHelper.java [deleted file]
src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java
src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java

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 (executable)
index 0000000..2230885
--- /dev/null
@@ -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<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;
+       }
+
+}
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 (executable)
index a196c3b..0000000
+++ /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;
-       }
-
-}
index 8b049bb..5e605ab 100755 (executable)
@@ -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();
index 14c8b0a..2a27c5d 100755 (executable)
@@ -21,9 +21,9 @@ public class NavigationItem {
 
        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;
 
        /**
@@ -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<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) {