diff flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java @ 1747:d2a17e990c70

Improved the Themes: we now support special themes for facets which need to match a given pattern string. flys-artifacts/trunk@3047 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 20 Oct 2011 13:45:45 +0000
parents 88a669785863
children 6ed439ff61bf
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java	Thu Oct 20 13:29:54 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java	Thu Oct 20 13:45:45 2011 +0000
@@ -1,5 +1,6 @@
 package de.intevation.flys.themes;
 
+import java.util.List;
 import java.util.Map;
 
 import javax.xml.xpath.XPathConstants;
@@ -61,12 +62,18 @@
      * @return a theme.
      */
     public static Theme getTheme(FLYSContext c, String name) {
+        return getTheme(c, name, null);
+    }
+
+
+    public static Theme getTheme(FLYSContext c, String name, String pattern) {
+        logger.debug("Search theme for: " + name + " - " + pattern);
         if (c == null || name == null) {
             logger.warn("Cannot search for theme.");
             return null;
         }
 
-        Map<String, String> map = (Map<String, String>)
+        Map<String, List<ThemeMapping>> map = (Map<String, List<ThemeMapping>>)
             c.get(FLYSContext.THEME_MAPPING);
 
         Map<String, Theme> t = (Map<String, Theme>)
@@ -77,13 +84,50 @@
             return null;
         }
 
-        String themeName = map.get(name);
+        List<ThemeMapping> mapping = map.get(name);
 
-        if (themeName == null) {
-            logger.warn("No theme found for mapping: " + name);
+        if (mapping == null) {
+            logger.warn("No theme fount for mapping: " + name);
+            return null;
         }
 
-        return t.get(themeName);
+        int patternLen = pattern != null ? pattern.length() : 0;
+
+        Theme defaultTheme = null;
+
+        for (ThemeMapping tm: mapping) {
+            if (name.equals(tm.getFrom())) {
+                String tmPattern = tm.getPatternStr();
+
+                int tmLen = tmPattern != null ? tmPattern.length() : 0;
+
+                if (tmLen == 0 && patternLen == 0) {
+                    // we want and get the default theme
+                    return t.get(tm.getTo());
+                }
+                else if (tmLen == 0 && patternLen > 0) {
+                    // found a defaultTheme, lets save it
+                    defaultTheme = t.get(tm.getTo());
+                }
+                else if (tm.applyPattern(pattern)) {
+                    // found the special theme we have searched for
+                    return t.get(tm.getTo());
+                }
+            }
+        }
+
+        String msg =
+            "No theme found for '" + name +
+            "' with pattern '" + pattern + "'.";
+
+        if (defaultTheme != null) {
+            logger.info(msg + " Use the default instead.");
+            return defaultTheme;
+        }
+
+        logger.warn(msg);
+
+        return null;
     }
 
 

http://dive4elements.wald.intevation.org