# HG changeset patch # User Ingo Weinzierl # Date 1303823828 0 # Node ID 88a669785863ed66143b778ce2f13c6bff2909a1 # Parent 79401797f4e16b80356bd4bc1e226ad881926368 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 diff -r 79401797f4e1 -r 88a669785863 flys-artifacts/ChangeLog --- 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 + + * 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 * src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java: diff -r 79401797f4e1 -r 88a669785863 flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/FLYSContext.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. diff -r 79401797f4e1 -r 88a669785863 flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/FLYSContextFactory.java --- 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 mapping = new HashMap(); + + 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 : diff -r 79401797f4e1 -r 88a669785863 flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java --- 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 Ingo Weinzierl @@ -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 map = (Map) + c.get(FLYSContext.THEME_MAPPING); + + Map t = (Map) + 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); }