Mercurial > dive4elements > river
comparison 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 |
comparison
equal
deleted
inserted
replaced
2736:8839086c4b7b | 2737:83b22ccf48da |
---|---|
1 package de.intevation.flys.themes; | 1 package de.intevation.flys.themes; |
2 | 2 |
3 import java.util.List; | 3 import java.util.List; |
4 import java.util.Map; | 4 import java.util.Map; |
5 import java.util.ArrayList; | |
6 import java.util.HashMap; | |
5 | 7 |
6 import javax.xml.xpath.XPathConstants; | 8 import javax.xml.xpath.XPathConstants; |
7 | 9 |
8 import org.apache.log4j.Logger; | 10 import org.apache.log4j.Logger; |
9 | 11 |
10 import org.w3c.dom.Document; | 12 import org.w3c.dom.Document; |
11 import org.w3c.dom.NamedNodeMap; | 13 import org.w3c.dom.NamedNodeMap; |
12 import org.w3c.dom.Node; | 14 import org.w3c.dom.Node; |
13 import org.w3c.dom.NodeList; | 15 import org.w3c.dom.NodeList; |
16 import org.w3c.dom.Element; | |
14 | 17 |
15 import de.intevation.artifacts.common.utils.XMLUtils; | 18 import de.intevation.artifacts.common.utils.XMLUtils; |
16 | 19 |
17 import de.intevation.flys.artifacts.context.FLYSContext; | 20 import de.intevation.flys.artifacts.context.FLYSContext; |
18 import de.intevation.flys.artifacts.FLYSArtifact; | 21 import de.intevation.flys.artifacts.FLYSArtifact; |
68 */ | 71 */ |
69 public static Theme getTheme( | 72 public static Theme getTheme( |
70 FLYSContext c, | 73 FLYSContext c, |
71 String name, | 74 String name, |
72 String pattern, | 75 String pattern, |
73 String output) | 76 String output, |
77 String groupName) | |
74 { | 78 { |
75 logger.debug("Search theme for: " + name + " - pattern: " + pattern); | 79 logger.debug("Search theme for: " + name + " - pattern: " + pattern); |
76 | 80 |
77 if (c == null || name == null) { | 81 if (c == null || name == null) { |
78 logger.warn("Cannot search for theme."); | 82 logger.warn("Cannot search for theme."); |
81 | 85 |
82 // Fetch mapping and themes. | 86 // Fetch mapping and themes. |
83 Map<String, List<ThemeMapping>> map = (Map<String, List<ThemeMapping>>) | 87 Map<String, List<ThemeMapping>> map = (Map<String, List<ThemeMapping>>) |
84 c.get(FLYSContext.THEME_MAPPING); | 88 c.get(FLYSContext.THEME_MAPPING); |
85 | 89 |
86 Map<String, Theme> t = (Map<String, Theme>) | 90 List<ThemeGroup> tgs = (List<ThemeGroup>) |
87 c.get(FLYSContext.THEMES); | 91 c.get(FLYSContext.THEMES); |
92 | |
93 ThemeGroup group = null; | |
94 for (ThemeGroup tg: tgs) { | |
95 if(tg.getName().equals(groupName)) { | |
96 group = tg; | |
97 } | |
98 } | |
99 | |
100 if (group == null) { | |
101 return null; | |
102 } | |
103 | |
104 Map<String, Theme> t = group.getThemes(); | |
88 | 105 |
89 FLYSArtifact artifact = (FLYSArtifact) c.get(FLYSContext.ARTIFACT_KEY); | 106 FLYSArtifact artifact = (FLYSArtifact) c.get(FLYSContext.ARTIFACT_KEY); |
90 | 107 |
91 if (map == null || map.size() == 0 || t == null || t.size() == 0) { | 108 if (map == null || map.size() == 0 || t == null || t.size() == 0) { |
92 logger.warn("No mappings or themes found. Cannot retrieve theme!"); | 109 logger.warn("No mappings or themes found. Cannot retrieve theme!"); |
119 | 136 |
120 return null; | 137 return null; |
121 } | 138 } |
122 | 139 |
123 | 140 |
141 public static List<ThemeGroup> getThemeGroups(FLYSContext c) { | |
142 List<ThemeGroup> tgs = (List<ThemeGroup>) | |
143 c.get(FLYSContext.THEMES); | |
144 return tgs; | |
145 } | |
146 | |
147 | |
148 public static List<Theme> getThemes (FLYSContext c, String name) { | |
149 List<ThemeGroup> tgs = (List<ThemeGroup>) | |
150 c.get(FLYSContext.THEMES); | |
151 if (tgs == null) { | |
152 return null; | |
153 } | |
154 | |
155 List<Theme> themes = new ArrayList<Theme>(); | |
156 for (ThemeGroup tg: tgs) { | |
157 themes.add(tg.getThemeByName(name)); | |
158 } | |
159 return themes; | |
160 } | |
161 | |
124 protected static String getName(Node config) { | 162 protected static String getName(Node config) { |
125 return (String) XMLUtils.xpath(config, "@name", XPathConstants.STRING); | 163 return (String) XMLUtils.xpath(config, "@name", XPathConstants.STRING); |
126 } | 164 } |
127 | 165 |
128 | 166 |
130 return (String) XMLUtils.xpath(config, "@desc", XPathConstants.STRING); | 168 return (String) XMLUtils.xpath(config, "@desc", XPathConstants.STRING); |
131 } | 169 } |
132 | 170 |
133 | 171 |
134 protected static void parseInherits(Document themeCfg, Node cfg, Theme t) { | 172 protected static void parseInherits(Document themeCfg, Node cfg, Theme t) { |
173 parseInherits(themeCfg, cfg, t, null); | |
174 } | |
175 | |
176 protected static void parseInherits( | |
177 Document themeCfg, | |
178 Node cfg, | |
179 Theme t, | |
180 Map<String, Node> themes | |
181 ) { | |
135 logger.debug("ThemeFactory.parseInherits"); | 182 logger.debug("ThemeFactory.parseInherits"); |
136 | 183 |
137 NodeList inherits = (NodeList) XMLUtils.xpath( | 184 NodeList inherits = (NodeList) XMLUtils.xpath( |
138 cfg, "inherits/inherit", XPathConstants.NODESET); | 185 cfg, "inherits/inherit", XPathConstants.NODESET); |
139 | 186 |
144 return; | 191 return; |
145 } | 192 } |
146 | 193 |
147 logger.debug("Theme inherits from " + num + " other themes."); | 194 logger.debug("Theme inherits from " + num + " other themes."); |
148 | 195 |
196 if (themes == null) { | |
197 themes = buildThemeMap(themeCfg); | |
198 } | |
199 | |
149 for (int i = 0; i < num; i++) { | 200 for (int i = 0; i < num; i++) { |
150 Node inherit = inherits.item(i); | 201 Node inherit = inherits.item(i); |
151 String from = (String) XMLUtils.xpath( | 202 String from = ((Element)inherit).getAttribute("from"); |
152 inherit, "@from", XPathConstants.STRING); | 203 |
153 | 204 Node tmp = themes.get(from); |
154 Node tmp = getThemeNode(themeCfg, from); | 205 |
155 | 206 parseInherits(themeCfg, tmp, t, themes); |
156 parseInherits(themeCfg, tmp, t); | |
157 parseFields(tmp, t); | 207 parseFields(tmp, t); |
158 } | 208 } |
159 } | 209 } |
160 | 210 |
161 | 211 protected static Map<String, Node> buildThemeMap(Document themeCfg) { |
162 protected static Node getThemeNode(Document themeCfg, String name) { | 212 Map<String, Node> map = new HashMap<String, Node>(); |
163 if (name == null) { | 213 String xpath = "/themes/themegroup/theme"; |
164 logger.warn("Cannot search theme config without name!"); | 214 |
165 return null; | 215 NodeList nodes = (NodeList)XMLUtils.xpath( |
166 } | 216 themeCfg, xpath, XPathConstants.NODESET); |
167 | 217 |
168 logger.debug("Search for theme: " + name); | 218 if (nodes != null) { |
169 | 219 for (int i = 0, N = nodes.getLength(); i < N; ++i) { |
170 String xpath = "/themes/theme[@name='" + name + "']"; | 220 Node node = nodes.item(i); |
171 | 221 String name = ((Element)node).getAttribute("name"); |
172 return (Node) XMLUtils.xpath(themeCfg, xpath, XPathConstants.NODE); | 222 map.put(name, node); |
223 } | |
224 } | |
225 return map; | |
173 } | 226 } |
174 | 227 |
175 | 228 |
176 protected static void parseFields(Node config, Theme theme) { | 229 protected static void parseFields(Node config, Theme theme) { |
177 if (config == null || theme == null) { | 230 if (config == null || theme == null) { |
198 } | 251 } |
199 } | 252 } |
200 | 253 |
201 | 254 |
202 protected static void addField(Theme theme, Node field) { | 255 protected static void addField(Theme theme, Node field) { |
203 String name = (String) XMLUtils.xpath( | 256 String name = ((Element)field).getAttribute("name"); |
204 field, "@name", XPathConstants.STRING); | |
205 | 257 |
206 logger.debug("Add field: " + name); | 258 logger.debug("Add field: " + name); |
207 | 259 |
208 NamedNodeMap attrs = field.getAttributes(); | 260 NamedNodeMap attrs = field.getAttributes(); |
209 | 261 |