changeset 345:88a669785863

Initialized the facet2theme mappings configured in themes.xml and added a function to the ThemeFactory that retrieves a theme from FLYSContext based on its name. flys-artifacts/trunk@1747 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 26 Apr 2011 13:17:08 +0000
parents 79401797f4e1
children 16161de47662
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/FLYSContext.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/FLYSContextFactory.java flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java
diffstat 4 files changed, 94 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Apr 22 15:49:35 2011 +0000
+++ b/flys-artifacts/ChangeLog	Tue Apr 26 13:17:08 2011 +0000
@@ -1,3 +1,13 @@
+2011-04-26  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/context/FLYSContext.java,
+	  src/main/java/de/intevation/flys/artifacts/context/FLYSContextFactory.java:
+	  The facet-2-theme mappings are initialized at startup and stored in the
+	  FLYSContext.
+
+	* src/main/java/de/intevation/flys/themes/ThemeFactory.java: Added a
+	  function that retrieves a theme from FLYSContext based on its name.
+
 2011-04-22	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/FLYSContext.java	Fri Apr 22 15:49:35 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/FLYSContext.java	Tue Apr 26 13:17:08 2011 +0000
@@ -34,6 +34,10 @@
     public static final String THEMES =
         "flys.themes.map";
 
+    /** The key that is used to store a map of theme mappings in the context.*/
+    public static final String THEME_MAPPING =
+        "flys.themes.mapping.map";
+
 
     /**
      * The default constructor.
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/FLYSContextFactory.java	Fri Apr 22 15:49:35 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/FLYSContextFactory.java	Tue Apr 26 13:17:08 2011 +0000
@@ -69,6 +69,9 @@
     public static final String XPATH_THEMES =
         "/themes/theme";
 
+    public static final String XPATH_THEME_MAPPINGS =
+        "/themes/mappings/mapping";
+
     /**
      * Creates a new FLYSArtifactContext object and initialize all
      * components required by the application.
@@ -83,6 +86,7 @@
         configureStates(config, context);
         configureOutGenerators(config, context);
         configureThemes(config, context);
+        configureThemesMappings(config, context);
 
         return context;
     }
@@ -318,5 +322,42 @@
 
         return XMLUtils.parseDocument(new File(themeConfig));
     }
+
+
+    protected void configureThemesMappings(Document cfg, FLYSContext context) {
+        logger.debug("FLYSContextFactory.configureThemesMappings");
+
+        Document config = getThemeConfig(cfg);
+
+        NodeList mappings = (NodeList) XMLUtils.xpath(
+            config, XPATH_THEME_MAPPINGS, XPathConstants.NODESET);
+
+        int num = mappings != null ? mappings.getLength() : 0;
+
+        if (num == 0) {
+            logger.warn("No theme <--> facet mappins found!");
+            return;
+        }
+
+        Map<String, String> mapping = new HashMap<String, String>();
+
+        for (int i = 0; i < num; i++) {
+            Node node = mappings.item(i);
+
+            String from = (String) XMLUtils.xpath(
+                node, "@from", XPathConstants.STRING);
+
+            String to = (String) XMLUtils.xpath(
+                node, "@to", XPathConstants.STRING);
+
+            if (from != null && to != null) {
+                mapping.put(from, to);
+            }
+        }
+
+        logger.debug("Found " + mapping.size() + " theme/facet mappings.");
+
+        context.put(FLYSContext.THEME_MAPPING, mapping);
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java	Fri Apr 22 15:49:35 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java	Tue Apr 26 13:17:08 2011 +0000
@@ -1,5 +1,7 @@
 package de.intevation.flys.themes;
 
+import java.util.Map;
+
 import javax.xml.xpath.XPathConstants;
 
 import org.apache.log4j.Logger;
@@ -11,6 +13,8 @@
 
 import de.intevation.artifacts.common.utils.XMLUtils;
 
+import de.intevation.flys.artifacts.context.FLYSContext;
+
 
 /**
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
@@ -48,6 +52,41 @@
     }
 
 
+    /**
+     * Returns the theme for a specified output type and facet.
+     *
+     * @param c The FLYSContext that stores the mappings and themes.
+     * @param name The name of the mapping.
+     *
+     * @return a theme.
+     */
+    public static Theme getTheme(FLYSContext c, String name) {
+        if (c == null || name == null) {
+            logger.warn("Cannot search for theme.");
+            return null;
+        }
+
+        Map<String, String> map = (Map<String, String>)
+            c.get(FLYSContext.THEME_MAPPING);
+
+        Map<String, Theme> t = (Map<String, Theme>)
+            c.get(FLYSContext.THEMES);
+
+        if (map == null || map.size() == 0 || t == null || t.size() == 0) {
+            logger.warn("No mappings or themes found. Cannot retrieve theme!");
+            return null;
+        }
+
+        String themeName = map.get(name);
+
+        if (themeName == null) {
+            logger.warn("No theme found for mapping: " + name);
+        }
+
+        return t.get(themeName);
+    }
+
+
     protected static String getName(Node config) {
         return (String) XMLUtils.xpath(config, "@name", XPathConstants.STRING);
     }

http://dive4elements.wald.intevation.org