diff flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java @ 2737:83b22ccf48da

Introduced theme groups and added new service to get themes filtered by name. flys-artifacts/trunk@4470 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 23 May 2012 08:46:45 +0000
parents 6f047a407f84
children faf73245ec73
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java	Wed May 23 08:12:49 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java	Wed May 23 08:46:45 2012 +0000
@@ -2,6 +2,8 @@
 
 import java.util.List;
 import java.util.Map;
+import java.util.ArrayList;
+import java.util.HashMap;
 
 import javax.xml.xpath.XPathConstants;
 
@@ -11,6 +13,7 @@
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+import org.w3c.dom.Element;
 
 import de.intevation.artifacts.common.utils.XMLUtils;
 
@@ -70,7 +73,8 @@
         FLYSContext c,
         String name,
         String pattern,
-        String output)
+        String output,
+        String groupName)
     {
         logger.debug("Search theme for: " + name + " - pattern: " + pattern);
 
@@ -83,9 +87,22 @@
         Map<String, List<ThemeMapping>> map = (Map<String, List<ThemeMapping>>)
             c.get(FLYSContext.THEME_MAPPING);
 
-        Map<String, Theme> t = (Map<String, Theme>)
+        List<ThemeGroup> tgs = (List<ThemeGroup>)
             c.get(FLYSContext.THEMES);
 
+        ThemeGroup group = null;
+        for (ThemeGroup tg: tgs) {
+            if(tg.getName().equals(groupName)) {
+                group = tg;
+            }
+        }
+
+        if (group == null) {
+            return null;
+        }
+
+        Map<String, Theme> t = group.getThemes();
+
         FLYSArtifact artifact = (FLYSArtifact) c.get(FLYSContext.ARTIFACT_KEY);
 
         if (map == null || map.size() == 0 || t == null || t.size() == 0) {
@@ -121,6 +138,27 @@
     }
 
 
+    public static List<ThemeGroup> getThemeGroups(FLYSContext c) {
+        List<ThemeGroup> tgs = (List<ThemeGroup>)
+            c.get(FLYSContext.THEMES);
+        return tgs;
+    }
+
+
+    public static List<Theme> getThemes (FLYSContext c, String name) {
+        List<ThemeGroup> tgs = (List<ThemeGroup>)
+            c.get(FLYSContext.THEMES);
+        if (tgs == null) {
+            return null;
+        }
+
+        List<Theme> themes = new ArrayList<Theme>();
+        for (ThemeGroup tg: tgs) {
+            themes.add(tg.getThemeByName(name));
+        }
+        return themes;
+    }
+
     protected static String getName(Node config) {
         return (String) XMLUtils.xpath(config, "@name", XPathConstants.STRING);
     }
@@ -132,6 +170,15 @@
 
 
     protected static void parseInherits(Document themeCfg, Node cfg, Theme t) {
+        parseInherits(themeCfg, cfg, t, null);
+    }
+
+    protected static void parseInherits(
+        Document themeCfg,
+        Node     cfg,
+        Theme    t,
+        Map<String, Node> themes
+    ) {
         logger.debug("ThemeFactory.parseInherits");
 
         NodeList inherits = (NodeList) XMLUtils.xpath(
@@ -146,30 +193,36 @@
 
         logger.debug("Theme inherits from " + num + " other themes.");
 
+        if (themes == null) {
+            themes = buildThemeMap(themeCfg);
+        }
+
         for (int i = 0; i < num; i++) {
             Node inherit = inherits.item(i);
-            String from  = (String) XMLUtils.xpath(
-                inherit, "@from", XPathConstants.STRING);
+            String from = ((Element)inherit).getAttribute("from");
 
-            Node tmp = getThemeNode(themeCfg, from);
+            Node tmp = themes.get(from);
 
-            parseInherits(themeCfg, tmp, t);
+            parseInherits(themeCfg, tmp, t, themes);
             parseFields(tmp, t);
         }
     }
 
+    protected static Map<String, Node> buildThemeMap(Document themeCfg) {
+        Map<String, Node> map = new HashMap<String, Node>();
+        String xpath = "/themes/themegroup/theme";
 
-    protected static Node getThemeNode(Document themeCfg, String name) {
-        if (name == null) {
-            logger.warn("Cannot search theme config without name!");
-            return null;
+        NodeList nodes = (NodeList)XMLUtils.xpath(
+            themeCfg, xpath, XPathConstants.NODESET);
+
+        if (nodes != null) {
+            for (int i = 0, N = nodes.getLength(); i < N; ++i) {
+                Node node = nodes.item(i);
+                String name = ((Element)node).getAttribute("name");
+                map.put(name, node);
+            }
         }
-
-        logger.debug("Search for theme: " + name);
-
-        String xpath = "/themes/theme[@name='" + name + "']";
-
-        return (Node) XMLUtils.xpath(themeCfg, xpath, XPathConstants.NODE);
+        return map;
     }
 
 
@@ -200,8 +253,7 @@
 
 
     protected static void addField(Theme theme, Node field) {
-        String name = (String) XMLUtils.xpath(
-            field, "@name", XPathConstants.STRING);
+        String name = ((Element)field).getAttribute("name");
 
         logger.debug("Add field: " + name);
 

http://dive4elements.wald.intevation.org