Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java @ 2424:092e519ff461
merged flys-artifacts/2.6.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:26 +0200 |
parents | 6f047a407f84 |
children | 83b22ccf48da |
comparison
equal
deleted
inserted
replaced
2392:8112ec686a9a | 2424:092e519ff461 |
---|---|
1 package de.intevation.flys.themes; | |
2 | |
3 import java.util.List; | |
4 import java.util.Map; | |
5 | |
6 import javax.xml.xpath.XPathConstants; | |
7 | |
8 import org.apache.log4j.Logger; | |
9 | |
10 import org.w3c.dom.Document; | |
11 import org.w3c.dom.NamedNodeMap; | |
12 import org.w3c.dom.Node; | |
13 import org.w3c.dom.NodeList; | |
14 | |
15 import de.intevation.artifacts.common.utils.XMLUtils; | |
16 | |
17 import de.intevation.flys.artifacts.context.FLYSContext; | |
18 import de.intevation.flys.artifacts.FLYSArtifact; | |
19 | |
20 /** | |
21 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
22 * | |
23 * Mapping-matching rules: | |
24 * | |
25 */ | |
26 public class ThemeFactory { | |
27 | |
28 private static Logger logger = Logger.getLogger(ThemeFactory.class); | |
29 | |
30 /** Trivial, hidden constructor. */ | |
31 private ThemeFactory() { | |
32 } | |
33 | |
34 | |
35 /** | |
36 * Creates a new theme from <i>config</i>. | |
37 * | |
38 * @param themeCfg The theme config document that is used to fetch parent | |
39 * themes. | |
40 * @param config The theme config node. | |
41 * | |
42 * @return a new theme. | |
43 */ | |
44 public static Theme createTheme(Document themeCfg, Node config) { | |
45 String name = getName(config); | |
46 String desc = getDescription(config); | |
47 | |
48 logger.debug("Create new theme: " + name); | |
49 | |
50 Theme theme = new DefaultTheme(name, desc); | |
51 | |
52 parseInherits(themeCfg, config, theme); | |
53 parseFields(config, theme); | |
54 parseAttrs(config, theme); | |
55 | |
56 return theme; | |
57 } | |
58 | |
59 | |
60 /** | |
61 * Get first matching theme for facet. | |
62 * | |
63 * @param name Name to match "from" of theme mapping. | |
64 * @param pattern String to 'compare' to pattern in mapping. | |
65 * @param output Name of the current output | |
66 * | |
67 * @return First matching theme. | |
68 */ | |
69 public static Theme getTheme( | |
70 FLYSContext c, | |
71 String name, | |
72 String pattern, | |
73 String output) | |
74 { | |
75 logger.debug("Search theme for: " + name + " - pattern: " + pattern); | |
76 | |
77 if (c == null || name == null) { | |
78 logger.warn("Cannot search for theme."); | |
79 return null; | |
80 } | |
81 | |
82 // Fetch mapping and themes. | |
83 Map<String, List<ThemeMapping>> map = (Map<String, List<ThemeMapping>>) | |
84 c.get(FLYSContext.THEME_MAPPING); | |
85 | |
86 Map<String, Theme> t = (Map<String, Theme>) | |
87 c.get(FLYSContext.THEMES); | |
88 | |
89 FLYSArtifact artifact = (FLYSArtifact) c.get(FLYSContext.ARTIFACT_KEY); | |
90 | |
91 if (map == null || map.size() == 0 || t == null || t.size() == 0) { | |
92 logger.warn("No mappings or themes found. Cannot retrieve theme!"); | |
93 return null; | |
94 } | |
95 | |
96 List<ThemeMapping> mapping = map.get(name); | |
97 | |
98 if (mapping == null) { | |
99 logger.warn("No theme found for mapping: " + name); | |
100 return null; | |
101 } | |
102 | |
103 // Take first mapping of which all conditions are satisfied. | |
104 for (ThemeMapping tm: mapping) { | |
105 if (name.equals(tm.getFrom()) | |
106 && tm.applyPattern(pattern) | |
107 && tm.masterAttrMatches(artifact) | |
108 && tm.outputMatches(output)) | |
109 { | |
110 return t.get(tm.getTo()); | |
111 } | |
112 } | |
113 | |
114 String msg = | |
115 "No theme found for '" + name + | |
116 "' with pattern '" + pattern + "' and output " + output + "."; | |
117 | |
118 logger.warn(msg); | |
119 | |
120 return null; | |
121 } | |
122 | |
123 | |
124 protected static String getName(Node config) { | |
125 return (String) XMLUtils.xpath(config, "@name", XPathConstants.STRING); | |
126 } | |
127 | |
128 | |
129 protected static String getDescription(Node config) { | |
130 return (String) XMLUtils.xpath(config, "@desc", XPathConstants.STRING); | |
131 } | |
132 | |
133 | |
134 protected static void parseInherits(Document themeCfg, Node cfg, Theme t) { | |
135 logger.debug("ThemeFactory.parseInherits"); | |
136 | |
137 NodeList inherits = (NodeList) XMLUtils.xpath( | |
138 cfg, "inherits/inherit", XPathConstants.NODESET); | |
139 | |
140 int num = inherits != null ? inherits.getLength() : 0; | |
141 | |
142 if (num == 0) { | |
143 logger.debug("Theme does not inherit from other themes."); | |
144 return; | |
145 } | |
146 | |
147 logger.debug("Theme inherits from " + num + " other themes."); | |
148 | |
149 for (int i = 0; i < num; i++) { | |
150 Node inherit = inherits.item(i); | |
151 String from = (String) XMLUtils.xpath( | |
152 inherit, "@from", XPathConstants.STRING); | |
153 | |
154 Node tmp = getThemeNode(themeCfg, from); | |
155 | |
156 parseInherits(themeCfg, tmp, t); | |
157 parseFields(tmp, t); | |
158 } | |
159 } | |
160 | |
161 | |
162 protected static Node getThemeNode(Document themeCfg, String name) { | |
163 if (name == null) { | |
164 logger.warn("Cannot search theme config without name!"); | |
165 return null; | |
166 } | |
167 | |
168 logger.debug("Search for theme: " + name); | |
169 | |
170 String xpath = "/themes/theme[@name='" + name + "']"; | |
171 | |
172 return (Node) XMLUtils.xpath(themeCfg, xpath, XPathConstants.NODE); | |
173 } | |
174 | |
175 | |
176 protected static void parseFields(Node config, Theme theme) { | |
177 if (config == null || theme == null) { | |
178 logger.warn("Parsing fields without node or theme is senseless!"); | |
179 return; | |
180 } | |
181 | |
182 NodeList fields = (NodeList) XMLUtils.xpath( | |
183 config, "fields/field", XPathConstants.NODESET); | |
184 | |
185 int num = fields != null ? fields.getLength() : 0; | |
186 | |
187 logger.debug("Found " + num + " own fields in this theme."); | |
188 | |
189 if (num == 0) { | |
190 logger.debug("Theme has no own fields."); | |
191 return; | |
192 } | |
193 | |
194 for (int i = 0; i < num; i++) { | |
195 Node field = fields.item(i); | |
196 | |
197 addField(theme, field); | |
198 } | |
199 } | |
200 | |
201 | |
202 protected static void addField(Theme theme, Node field) { | |
203 String name = (String) XMLUtils.xpath( | |
204 field, "@name", XPathConstants.STRING); | |
205 | |
206 logger.debug("Add field: " + name); | |
207 | |
208 NamedNodeMap attrs = field.getAttributes(); | |
209 | |
210 int num = attrs != null ? attrs.getLength() : 0; | |
211 | |
212 if (num == 0) { | |
213 logger.warn("This field has no attributes."); | |
214 return; | |
215 } | |
216 | |
217 ThemeField theField = new DefaultThemeField(name); | |
218 | |
219 for (int i = 0; i < num; i++) { | |
220 Node attr = attrs.item(i); | |
221 | |
222 String key = attr.getNodeName(); | |
223 String value = attr.getNodeValue(); | |
224 | |
225 theField.setAttribute(key, value); | |
226 } | |
227 | |
228 theme.addField(name, theField); | |
229 } | |
230 | |
231 | |
232 protected static void parseAttrs(Node config, Theme theme) { | |
233 NamedNodeMap attrs = config.getAttributes(); | |
234 | |
235 int num = attrs != null ? attrs.getLength() : 0; | |
236 | |
237 if (num == 0) { | |
238 logger.debug("Theme has no attributes set."); | |
239 return; | |
240 } | |
241 | |
242 for (int i = 0; i < num; i++) { | |
243 Node attr = attrs.item(i); | |
244 | |
245 String name = attr.getNodeName(); | |
246 String value = attr.getNodeValue(); | |
247 | |
248 theme.addAttribute(name, value); | |
249 } | |
250 } | |
251 } | |
252 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |