Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContextFactory.java @ 9555:ef5754ba5573
Implemented legend aggregation based on type of themes.
Added theme-editor style configuration for aggregated legend entries.
Only configured themes get aggregated.
author | gernotbelger |
---|---|
date | Tue, 23 Oct 2018 16:26:48 +0200 |
parents | c26fb37899ca |
children |
comparison
equal
deleted
inserted
replaced
9554:33ce8eba9806 | 9555:ef5754ba5573 |
---|---|
7 */ | 7 */ |
8 | 8 |
9 package org.dive4elements.river.artifacts.context; | 9 package org.dive4elements.river.artifacts.context; |
10 | 10 |
11 import java.io.File; | 11 import java.io.File; |
12 | |
13 import java.util.ArrayList; | 12 import java.util.ArrayList; |
13 import java.util.Collections; | |
14 import java.util.HashMap; | 14 import java.util.HashMap; |
15 import java.util.List; | 15 import java.util.List; |
16 import java.util.Map; | 16 import java.util.Map; |
17 | 17 |
18 import javax.xml.xpath.XPathConstants; | 18 import javax.xml.xpath.XPathConstants; |
19 | 19 |
20 import org.apache.log4j.Logger; | 20 import org.apache.log4j.Logger; |
21 | |
22 import org.dive4elements.artifactdatabase.state.State; | 21 import org.dive4elements.artifactdatabase.state.State; |
23 import org.dive4elements.artifactdatabase.state.StateEngine; | 22 import org.dive4elements.artifactdatabase.state.StateEngine; |
24 | |
25 import org.dive4elements.artifactdatabase.transition.Transition; | 23 import org.dive4elements.artifactdatabase.transition.Transition; |
26 import org.dive4elements.artifactdatabase.transition.TransitionEngine; | 24 import org.dive4elements.artifactdatabase.transition.TransitionEngine; |
27 | |
28 import org.dive4elements.artifacts.ArtifactContextFactory; | 25 import org.dive4elements.artifacts.ArtifactContextFactory; |
26 import org.dive4elements.artifacts.ContextInjector; | |
29 import org.dive4elements.artifacts.GlobalContext; | 27 import org.dive4elements.artifacts.GlobalContext; |
30 | |
31 import org.dive4elements.artifacts.ContextInjector; | |
32 | |
33 import org.dive4elements.artifacts.common.utils.Config; | 28 import org.dive4elements.artifacts.common.utils.Config; |
34 import org.dive4elements.artifacts.common.utils.ElementConverter; | 29 import org.dive4elements.artifacts.common.utils.ElementConverter; |
35 import org.dive4elements.artifacts.common.utils.XMLUtils; | 30 import org.dive4elements.artifacts.common.utils.XMLUtils; |
36 | |
37 import org.dive4elements.river.artifacts.model.Module; | 31 import org.dive4elements.river.artifacts.model.Module; |
38 import org.dive4elements.river.artifacts.model.RiverFactory; | 32 import org.dive4elements.river.artifacts.model.RiverFactory; |
39 import org.dive4elements.river.artifacts.model.ZoomScale; | 33 import org.dive4elements.river.artifacts.model.ZoomScale; |
40 | |
41 import org.dive4elements.river.artifacts.states.StateFactory; | 34 import org.dive4elements.river.artifacts.states.StateFactory; |
42 | |
43 import org.dive4elements.river.artifacts.transitions.TransitionFactory; | 35 import org.dive4elements.river.artifacts.transitions.TransitionFactory; |
44 | |
45 import org.dive4elements.river.exports.GeneratorLookup; | 36 import org.dive4elements.river.exports.GeneratorLookup; |
46 import org.dive4elements.river.exports.OutGenerator; | 37 import org.dive4elements.river.exports.OutGenerator; |
47 | |
48 import org.dive4elements.river.model.River; | 38 import org.dive4elements.river.model.River; |
49 import org.dive4elements.river.themes.Theme; | 39 import org.dive4elements.river.themes.Theme; |
50 import org.dive4elements.river.themes.ThemeFactory; | 40 import org.dive4elements.river.themes.ThemeFactory; |
51 import org.dive4elements.river.themes.ThemeGroup; | 41 import org.dive4elements.river.themes.ThemeGroup; |
52 import org.dive4elements.river.themes.ThemeMapping; | 42 import org.dive4elements.river.themes.ThemeMapping; |
53 | |
54 import org.w3c.dom.Document; | 43 import org.w3c.dom.Document; |
55 import org.w3c.dom.Element; | 44 import org.w3c.dom.Element; |
56 import org.w3c.dom.Node; | 45 import org.w3c.dom.Node; |
57 import org.w3c.dom.NodeList; | 46 import org.w3c.dom.NodeList; |
58 | 47 |
66 | 55 |
67 /** The log that is used in this class. */ | 56 /** The log that is used in this class. */ |
68 private static Logger log = Logger.getLogger(RiverContextFactory.class); | 57 private static Logger log = Logger.getLogger(RiverContextFactory.class); |
69 | 58 |
70 /** The XPath to the artifacts configured in the configuration. */ | 59 /** The XPath to the artifacts configured in the configuration. */ |
71 public static final String XPATH_ARTIFACTS = | 60 private static final String XPATH_ARTIFACTS = "/artifact-database/artifacts/artifact"; |
72 "/artifact-database/artifacts/artifact"; | |
73 | 61 |
74 /** The XPath to the name of the artifact. */ | 62 /** The XPath to the name of the artifact. */ |
75 public static final String XPATH_ARTIFACT_NAME = "/artifact/@name"; | 63 private static final String XPATH_ARTIFACT_NAME = "/artifact/@name"; |
76 | 64 |
77 /** The XPath to the xlink ref in an artifact configuration. */ | 65 /** The XPath to the xlink ref in an artifact configuration. */ |
78 public static final String XPATH_XLINK = "xlink:href"; | 66 private static final String XPATH_XLINK = "xlink:href"; |
79 | 67 |
80 /** The XPath to the transitions configured in the artifact config. */ | 68 /** The XPath to the transitions configured in the artifact config. */ |
81 public static final String XPATH_TRANSITIONS = | 69 private static final String XPATH_TRANSITIONS = "/artifact/states/transition"; |
82 "/artifact/states/transition"; | |
83 | 70 |
84 /** The XPath to the states configured in the artifact config. */ | 71 /** The XPath to the states configured in the artifact config. */ |
85 public static final String XPATH_STATES = | 72 private static final String XPATH_STATES = "/artifact/states/state"; |
86 "/artifact/states/state"; | 73 |
87 | 74 private static final String XPATH_OUTPUT_GENERATORS = "/artifact-database/output-generators//output-generator"; |
88 public static final String XPATH_OUTPUT_GENERATORS = | 75 |
89 "/artifact-database/output-generators//output-generator"; | 76 private static final String XPATH_THEME_CONFIG = "/artifact-database/flys/themes/configuration/text()"; |
90 | 77 |
91 public static final String XPATH_THEME_CONFIG = | 78 private static final String XPATH_THEMES = "theme"; |
92 "/artifact-database/flys/themes/configuration/text()"; | 79 |
93 | 80 private static final String XPATH_LEGEND_GROUP = "/themes/legendgroup"; |
94 public static final String XPATH_THEMES = | 81 |
95 "theme"; | 82 private static final String XPATH_THEME_GROUPS = "/themes/themegroup"; |
96 | 83 |
97 public static final String XPATH_THEME_GROUPS = | 84 private static final String XPATH_THEME_MAPPINGS = "/themes/mappings/mapping"; |
98 "/themes/themegroup"; | 85 |
99 | 86 private static final String XPATH_RIVER_WMS = "/artifact-database/floodmap/river"; |
100 public static final String XPATH_THEME_MAPPINGS = | 87 |
101 "/themes/mappings/mapping"; | 88 private static final String XPATH_MODULES = "/artifact-database/modules/module"; |
102 | 89 |
103 public static final String XPATH_RIVER_WMS = | 90 private static final String XPATH_ZOOM_SCALES = "/artifact-database/options/zoom-scales/zoom-scale"; |
104 "/artifact-database/floodmap/river"; | 91 |
105 | 92 private static final String XPATH_DGM_PATH = "/artifact-database/options/dgm-path/text()"; |
106 public static final String XPATH_MODULES = | |
107 "/artifact-database/modules/module"; | |
108 | |
109 private static final String XPATH_ZOOM_SCALES = | |
110 "/artifact-database/options/zoom-scales/zoom-scale"; | |
111 | |
112 private static final String XPATH_DGM_PATH = | |
113 "/artifact-database/options/dgm-path/text()"; | |
114 | 93 |
115 private static GlobalContext GLOBAL_CONTEXT_INSTANCE; | 94 private static GlobalContext GLOBAL_CONTEXT_INSTANCE; |
116 | |
117 | 95 |
118 /** | 96 /** |
119 * Creates a new D4EArtifactContext object and initialize all | 97 * Creates a new D4EArtifactContext object and initialize all |
120 * components required by the application. | 98 * components required by the application. |
121 * | 99 * |
122 * @param config The artifact server configuration. | 100 * @param config |
101 * The artifact server configuration. | |
123 * @return a D4EArtifactContext. | 102 * @return a D4EArtifactContext. |
124 */ | 103 */ |
125 @Override | 104 @Override |
126 public GlobalContext createArtifactContext(Document config) { | 105 public GlobalContext createArtifactContext(final Document config) { |
127 RiverContext context = new RiverContext(config); | 106 final RiverContext context = new RiverContext(config); |
128 | 107 |
129 configureTransitions(config, context); | 108 configureTransitions(config, context); |
130 configureStates(config, context); | 109 configureStates(config, context); |
131 configureOutGenerators(config, context); | 110 configureOutGenerators(config, context); |
132 configureThemes(config, context); | 111 configureThemes(config, context); |
133 configureThemesMappings(config, context); | 112 configureThemesMappings(config, context); |
113 configureLegend(config, context); | |
134 configureFloodmapWMS(config, context); | 114 configureFloodmapWMS(config, context); |
135 configureModules(config, context); | 115 configureModules(config, context); |
136 configureZoomScales(config, context); | 116 configureZoomScales(config, context); |
137 configureDGMPath(config, context); | 117 configureDGMPath(config, context); |
138 | 118 |
145 | 125 |
146 public static synchronized GlobalContext getGlobalContext() { | 126 public static synchronized GlobalContext getGlobalContext() { |
147 return GLOBAL_CONTEXT_INSTANCE; | 127 return GLOBAL_CONTEXT_INSTANCE; |
148 } | 128 } |
149 | 129 |
150 | 130 private void configureDGMPath(final Document config, final RiverContext context) { |
151 private void configureDGMPath(Document config, RiverContext context) { | 131 final String dgmPath = (String) XMLUtils.xpath(config, XPATH_DGM_PATH, XPathConstants.STRING); |
152 String dgmPath = (String) XMLUtils.xpath( | |
153 config, | |
154 XPATH_DGM_PATH, | |
155 XPathConstants.STRING); | |
156 | 132 |
157 context.put("dgm-path", dgmPath); | 133 context.put("dgm-path", dgmPath); |
158 } | 134 } |
159 | 135 |
160 | 136 private void configureZoomScales(final Document config, final RiverContext context) { |
161 protected void configureZoomScales(Document config, RiverContext context) { | 137 final NodeList list = (NodeList) XMLUtils.xpath(config, XPATH_ZOOM_SCALES, XPathConstants.NODESET); |
162 NodeList list = (NodeList)XMLUtils.xpath( | 138 final ZoomScale scale = new ZoomScale(); |
163 config, | |
164 XPATH_ZOOM_SCALES, | |
165 XPathConstants.NODESET); | |
166 ZoomScale scale = new ZoomScale(); | |
167 for (int i = 0; i < list.getLength(); i++) { | 139 for (int i = 0; i < list.getLength(); i++) { |
168 Element element = (Element)list.item(i); | 140 final Element element = (Element) list.item(i); |
169 String river = "default"; | 141 String river = "default"; |
170 double range = 0d; | 142 double range = 0d; |
171 double radius = 10d; | 143 double radius = 10d; |
172 if (element.hasAttribute("river")) { | 144 if (element.hasAttribute("river")) { |
173 river = element.getAttribute("river"); | 145 river = element.getAttribute("river"); |
174 } | 146 } |
175 if (!element.hasAttribute("range")) { | 147 if (!element.hasAttribute("range")) { |
176 continue; | 148 continue; |
177 } | 149 } else { |
178 else { | 150 final String r = element.getAttribute("range"); |
179 String r = element.getAttribute("range"); | |
180 try { | 151 try { |
181 range = Double.parseDouble(r); | 152 range = Double.parseDouble(r); |
182 } | 153 } |
183 catch (NumberFormatException nfe) { | 154 catch (final NumberFormatException nfe) { |
184 continue; | 155 continue; |
185 } | 156 } |
186 } | 157 } |
187 if (!element.hasAttribute("radius")) { | 158 if (!element.hasAttribute("radius")) { |
188 continue; | 159 continue; |
189 } | 160 } else { |
190 else { | 161 final String r = element.getAttribute("radius"); |
191 String r = element.getAttribute("radius"); | |
192 try { | 162 try { |
193 radius = Double.parseDouble(r); | 163 radius = Double.parseDouble(r); |
194 } | 164 } |
195 catch (NumberFormatException nfe) { | 165 catch (final NumberFormatException nfe) { |
196 continue; | 166 continue; |
197 } | 167 } |
198 } | 168 } |
199 scale.addRange(river, range, radius); | 169 scale.addRange(river, range, radius); |
200 } | 170 } |
201 context.put("zoomscale", scale); | 171 context.put("zoomscale", scale); |
202 } | 172 } |
203 | |
204 | 173 |
205 /** | 174 /** |
206 * This method initializes the transition configuration. | 175 * This method initializes the transition configuration. |
207 * | 176 * |
208 * @param config the config document. | 177 * @param config |
209 * @param context the RiverContext. | 178 * the config document. |
210 */ | 179 * @param context |
211 protected void configureTransitions( | 180 * the RiverContext. |
212 Document config, | 181 */ |
213 RiverContext context | 182 private void configureTransitions(final Document config, final RiverContext context) { |
214 ) { | 183 final TransitionEngine engine = new TransitionEngine(); |
215 TransitionEngine engine = new TransitionEngine(); | 184 |
216 | 185 final List<Document> artifacts = getArtifactConfigurations(config); |
217 List<Document> artifacts = getArtifactConfigurations(config); | |
218 log.info("Found " + artifacts.size() + " artifacts in the config."); | 186 log.info("Found " + artifacts.size() + " artifacts in the config."); |
219 | 187 |
220 for (Document doc: artifacts) { | 188 for (final Document doc : artifacts) { |
221 | 189 |
222 String artName = (String) XMLUtils.xpath( | 190 final String artName = (String) XMLUtils.xpath(doc, XPATH_ARTIFACT_NAME, XPathConstants.STRING); |
223 doc, XPATH_ARTIFACT_NAME, XPathConstants.STRING); | 191 |
224 | 192 final NodeList list = (NodeList) XMLUtils.xpath(doc, XPATH_TRANSITIONS, XPathConstants.NODESET); |
225 NodeList list = (NodeList) XMLUtils.xpath( | |
226 doc, XPATH_TRANSITIONS, XPathConstants.NODESET); | |
227 | 193 |
228 if (list == null) { | 194 if (list == null) { |
229 log.warn("The artifact " + artName + | 195 log.warn("The artifact " + artName + " has no transitions configured."); |
230 " has no transitions configured."); | |
231 continue; | 196 continue; |
232 } | 197 } |
233 | 198 |
234 int trans = list.getLength(); | 199 final int trans = list.getLength(); |
235 | 200 |
236 log.info( | 201 log.info("Artifact '" + artName + "' has " + trans + " transitions."); |
237 "Artifact '" + artName + "' has " + trans + " transitions."); | |
238 | 202 |
239 for (int i = 0; i < trans; i++) { | 203 for (int i = 0; i < trans; i++) { |
240 Transition t = TransitionFactory.createTransition( | 204 final Transition t = TransitionFactory.createTransition(list.item(i)); |
241 list.item(i)); | 205 final String s = t.getFrom(); |
242 String s = t.getFrom(); | |
243 engine.addTransition(s, t); | 206 engine.addTransition(s, t); |
244 } | 207 } |
245 } | 208 } |
246 | 209 |
247 context.put(RiverContext.TRANSITION_ENGINE_KEY, engine); | 210 context.put(RiverContext.TRANSITION_ENGINE_KEY, engine); |
248 } | 211 } |
249 | 212 |
250 | |
251 /** | 213 /** |
252 * This method returns all artifact documents defined in | 214 * This method returns all artifact documents defined in |
253 * <code>config</code>. <br>NOTE: The artifact configurations need to be | 215 * <code>config</code>. <br> |
216 * NOTE: The artifact configurations need to be | |
254 * stored in own files referenced by an xlink. | 217 * stored in own files referenced by an xlink. |
255 * | 218 * |
256 * @param config The global configuration. | 219 * @param config |
220 * The global configuration. | |
257 * | 221 * |
258 * @return an array of Artifact configurations. | 222 * @return an array of Artifact configurations. |
259 */ | 223 */ |
260 protected List<Document> getArtifactConfigurations(Document config) { | 224 private List<Document> getArtifactConfigurations(final Document config) { |
261 NodeList artifacts = (NodeList) XMLUtils.xpath( | 225 final NodeList artifacts = (NodeList) XMLUtils.xpath(config, XPATH_ARTIFACTS, XPathConstants.NODESET); |
262 config, XPATH_ARTIFACTS, XPathConstants.NODESET); | 226 |
263 | 227 final int count = artifacts.getLength(); |
264 int count = artifacts.getLength(); | 228 |
265 | 229 final ArrayList<Document> docs = new ArrayList<>(count); |
266 ArrayList<Document> docs = new ArrayList<Document>(count); | |
267 | 230 |
268 for (int i = 0; i < count; i++) { | 231 for (int i = 0; i < count; i++) { |
269 Element tmp = (Element) artifacts.item(i); | 232 final Element tmp = (Element) artifacts.item(i); |
270 | 233 |
271 String xlink = tmp.getAttribute(XPATH_XLINK); | 234 String xlink = tmp.getAttribute(XPATH_XLINK); |
272 xlink = Config.replaceConfigDir(xlink); | 235 xlink = Config.replaceConfigDir(xlink); |
273 | 236 |
274 if (!xlink.isEmpty()) { | 237 if (!xlink.isEmpty()) { |
275 File file = new File(xlink); | 238 final File file = new File(xlink); |
276 if (!file.isFile() || !file.canRead()) { | 239 if (!file.isFile() || !file.canRead()) { |
277 log.warn("Artifact configuration '" | 240 log.warn("Artifact configuration '" + file + "' not found."); |
278 + file + "' not found."); | |
279 } else { | 241 } else { |
280 Document doc = XMLUtils.parseDocument(file); | 242 final Document doc = XMLUtils.parseDocument(file); |
281 if (doc != null) { | 243 if (doc != null) { |
282 docs.add(doc); | 244 docs.add(doc); |
283 } | 245 } |
284 } | 246 } |
285 continue; | 247 continue; |
286 } | 248 } |
287 Document doc = XMLUtils.newDocument(); | 249 final Document doc = XMLUtils.newDocument(); |
288 Node copy = doc.adoptNode(tmp.cloneNode(true)); | 250 final Node copy = doc.adoptNode(tmp.cloneNode(true)); |
289 doc.appendChild(copy); | 251 doc.appendChild(copy); |
290 docs.add(doc); | 252 docs.add(doc); |
291 } | 253 } |
292 return docs; | 254 return docs; |
293 } | 255 } |
294 | 256 |
295 | |
296 /** | 257 /** |
297 * This method initializes the transition configuration. | 258 * This method initializes the transition configuration. |
298 * | 259 * |
299 * @param config the config document. | 260 * @param config |
300 * @param context the RiverContext. | 261 * the config document. |
301 */ | 262 * @param context |
302 protected void configureStates(Document config, RiverContext context) { | 263 * the RiverContext. |
303 StateEngine engine = new StateEngine(); | 264 */ |
304 | 265 private void configureStates(final Document config, final RiverContext context) { |
305 List<Document> artifacts = getArtifactConfigurations(config); | 266 final StateEngine engine = new StateEngine(); |
267 | |
268 final List<Document> artifacts = getArtifactConfigurations(config); | |
306 log.info("Found " + artifacts.size() + " artifacts in the config."); | 269 log.info("Found " + artifacts.size() + " artifacts in the config."); |
307 | 270 |
308 for (Document doc: artifacts) { | 271 for (final Document doc : artifacts) { |
309 List<State> states = new ArrayList<State>(); | 272 final List<State> states = new ArrayList<>(); |
310 | 273 |
311 String artName = (String) XMLUtils.xpath( | 274 final String artName = (String) XMLUtils.xpath(doc, XPATH_ARTIFACT_NAME, XPathConstants.STRING); |
312 doc, XPATH_ARTIFACT_NAME, XPathConstants.STRING); | 275 |
313 | 276 final NodeList stateList = (NodeList) XMLUtils.xpath(doc, XPATH_STATES, XPathConstants.NODESET); |
314 NodeList stateList = (NodeList) XMLUtils.xpath( | |
315 doc, XPATH_STATES, XPathConstants.NODESET); | |
316 | 277 |
317 if (stateList == null) { | 278 if (stateList == null) { |
318 log.warn("The artifact " + artName + | 279 log.warn("The artifact " + artName + " has no states configured."); |
319 " has no states configured."); | |
320 continue; | 280 continue; |
321 } | 281 } |
322 | 282 |
323 int count = stateList.getLength(); | 283 final int count = stateList.getLength(); |
324 | 284 |
325 log.info( | 285 log.info("Artifact '" + artName + "' has " + count + " states."); |
326 "Artifact '" + artName + "' has " + count + " states."); | |
327 | 286 |
328 for (int i = 0; i < count; i++) { | 287 for (int i = 0; i < count; i++) { |
329 states.add(StateFactory.createState( | 288 states.add(StateFactory.createState(stateList.item(i))); |
330 stateList.item(i))); | |
331 } | 289 } |
332 | 290 |
333 engine.addStates(artName, states); | 291 engine.addStates(artName, states); |
334 } | 292 } |
335 | 293 |
336 context.put(RiverContext.STATE_ENGINE_KEY, engine); | 294 context.put(RiverContext.STATE_ENGINE_KEY, engine); |
337 } | 295 } |
338 | 296 |
339 | |
340 /** | 297 /** |
341 * This method intializes the provided output generators. | 298 * This method intializes the provided output generators. |
342 * | 299 * |
343 * @param config the config document. | 300 * @param config |
344 * @param context the RiverContext. | 301 * the config document. |
345 */ | 302 * @param context |
346 protected void configureOutGenerators( | 303 * the RiverContext. |
347 Document config, | 304 */ |
348 RiverContext context | 305 private void configureOutGenerators(final Document config, final RiverContext context) { |
349 ) { | 306 final NodeList outGenerators = (NodeList) XMLUtils.xpath(config, XPATH_OUTPUT_GENERATORS, XPathConstants.NODESET); |
350 NodeList outGenerators = (NodeList) XMLUtils.xpath( | 307 |
351 config, | 308 final int num = outGenerators == null ? 0 : outGenerators.getLength(); |
352 XPATH_OUTPUT_GENERATORS, | |
353 XPathConstants.NODESET); | |
354 | |
355 int num = outGenerators == null ? 0 : outGenerators.getLength(); | |
356 | 309 |
357 if (num == 0) { | 310 if (num == 0) { |
358 log.warn("No output generators configured in this application."); | 311 log.warn("No output generators configured in this application."); |
359 return; | 312 return; |
360 } | 313 } |
361 | 314 |
362 log.info("Found " + num + " configured output generators."); | 315 log.info("Found " + num + " configured output generators."); |
363 | 316 |
364 GeneratorLookup generators = new GeneratorLookup(); | 317 final GeneratorLookup generators = new GeneratorLookup(); |
365 | 318 |
366 int idx = 0; | 319 int idx = 0; |
367 | 320 |
368 for (int i = 0; i < num; i++) { | 321 for (int i = 0; i < num; i++) { |
369 Element item = (Element)outGenerators.item(i); | 322 final Element item = (Element) outGenerators.item(i); |
370 | 323 |
371 String names = item.getAttribute("names").trim(); | 324 final String names = item.getAttribute("names").trim(); |
372 String clazz = item.getAttribute("class").trim(); | 325 final String clazz = item.getAttribute("class").trim(); |
373 String converter = item.getAttribute("converter").trim(); | 326 final String converter = item.getAttribute("converter").trim(); |
374 String injectors = item.getAttribute("injectors").trim(); | 327 final String injectors = item.getAttribute("injectors").trim(); |
375 | 328 |
376 if (names.isEmpty() || clazz.isEmpty()) { | 329 if (names.isEmpty() || clazz.isEmpty()) { |
377 continue; | 330 continue; |
378 } | 331 } |
379 | 332 |
380 Class<OutGenerator> generatorClass = null; | 333 Class<OutGenerator> generatorClass = null; |
381 | 334 |
382 try { | 335 try { |
383 generatorClass = (Class<OutGenerator>)Class.forName(clazz); | 336 generatorClass = (Class<OutGenerator>) Class.forName(clazz); |
384 } | 337 } |
385 catch (ClassNotFoundException cnfe) { | 338 catch (final ClassNotFoundException cnfe) { |
386 log.error(cnfe, cnfe); | 339 log.error(cnfe, cnfe); |
387 continue; | 340 continue; |
388 } | 341 } |
389 | 342 |
390 Object cfg = null; | 343 Object cfg = null; |
391 | 344 |
392 if (!converter.isEmpty()) { | 345 if (!converter.isEmpty()) { |
393 try { | 346 try { |
394 ElementConverter ec = | 347 final ElementConverter ec = (ElementConverter) Class.forName(converter).newInstance(); |
395 (ElementConverter)Class.forName(converter) | |
396 .newInstance(); | |
397 cfg = ec.convert(item); | 348 cfg = ec.convert(item); |
398 } | 349 } |
399 catch (ClassNotFoundException cnfe) { | 350 catch (final ClassNotFoundException cnfe) { |
400 log.error(cnfe, cnfe); | 351 log.error(cnfe, cnfe); |
401 } | 352 } |
402 catch (InstantiationException ie) { | 353 catch (final InstantiationException ie) { |
403 log.error(ie); | 354 log.error(ie); |
404 } | 355 } |
405 catch (IllegalAccessException iae) { | 356 catch (final IllegalAccessException iae) { |
406 log.error(iae); | 357 log.error(iae); |
407 } | 358 } |
408 } | 359 } |
409 | 360 |
410 List<ContextInjector> cis = null; | 361 List<ContextInjector> cis = null; |
411 | 362 |
412 if (!injectors.isEmpty()) { | 363 if (!injectors.isEmpty()) { |
413 cis = new ArrayList<ContextInjector>(); | 364 cis = new ArrayList<>(); |
414 for (String injector: injectors.split("[\\s,]+")) { | 365 for (final String injector : injectors.split("[\\s,]+")) { |
415 try { | 366 try { |
416 ContextInjector ci = (ContextInjector)Class | 367 final ContextInjector ci = (ContextInjector) Class.forName(injector).newInstance(); |
417 .forName(injector) | |
418 .newInstance(); | |
419 ci.setup(item); | 368 ci.setup(item); |
420 cis.add(ci); | 369 cis.add(ci); |
421 } | 370 } |
422 catch (ClassNotFoundException cnfe) { | 371 catch (final ClassNotFoundException cnfe) { |
423 log.error(cnfe, cnfe); | 372 log.error(cnfe, cnfe); |
424 } | 373 } |
425 catch (InstantiationException ie) { | 374 catch (final InstantiationException ie) { |
426 log.error(ie); | 375 log.error(ie); |
427 } | 376 } |
428 catch (IllegalAccessException iae) { | 377 catch (final IllegalAccessException iae) { |
429 log.error(iae); | 378 log.error(iae); |
430 } | 379 } |
431 } | 380 } |
432 } | 381 } |
433 | 382 |
434 for (String key: names.split("[\\s,]+")) { | 383 for (String key : names.split("[\\s,]+")) { |
435 if (!(key = key.trim()).isEmpty()) { | 384 if (!(key = key.trim()).isEmpty()) { |
436 generators.putGenerator(key, generatorClass, cfg, cis); | 385 generators.putGenerator(key, generatorClass, cfg, cis); |
437 idx++; | 386 idx++; |
438 } | 387 } |
439 } | 388 } |
442 log.info("Successfully loaded " + idx + " output generators."); | 391 log.info("Successfully loaded " + idx + " output generators."); |
443 context.put(RiverContext.OUTGENERATORS_KEY, generators); | 392 context.put(RiverContext.OUTGENERATORS_KEY, generators); |
444 context.put(RiverContext.FACETFILTER_KEY, generators); | 393 context.put(RiverContext.FACETFILTER_KEY, generators); |
445 } | 394 } |
446 | 395 |
447 | |
448 /** | 396 /** |
449 * This methods reads the configured themes and puts them into the | 397 * This methods reads the configured themes and puts them into the |
450 * RiverContext. | 398 * RiverContext. |
451 * | 399 * |
452 * @param config The global configuration. | 400 * @param config |
453 * @param context The RiverContext. | 401 * The global configuration. |
454 */ | 402 * @param context |
455 protected void configureThemes(Document config, RiverContext context) { | 403 * The RiverContext. |
404 */ | |
405 private void configureThemes(final Document config, final RiverContext context) { | |
456 log.debug("RiverContextFactory.configureThemes"); | 406 log.debug("RiverContextFactory.configureThemes"); |
457 | 407 |
458 Document cfg = getThemeConfig(config); | 408 final Document cfg = getThemeConfig(config); |
459 | 409 |
460 NodeList themeGroups = (NodeList) XMLUtils.xpath( | 410 final NodeList themeGroups = (NodeList) XMLUtils.xpath(cfg, XPATH_THEME_GROUPS, XPathConstants.NODESET); |
461 cfg, XPATH_THEME_GROUPS, XPathConstants.NODESET); | 411 |
462 | 412 final int groupNum = themeGroups != null ? themeGroups.getLength() : 0; |
463 int groupNum = themeGroups != null ? themeGroups.getLength() : 0; | |
464 | 413 |
465 if (groupNum == 0) { | 414 if (groupNum == 0) { |
466 log.warn("There are no theme groups configured!"); | 415 log.warn("There are no theme groups configured!"); |
467 } | 416 } |
468 | 417 |
469 log.info("Found " + groupNum + " theme groups in configuration"); | 418 log.info("Found " + groupNum + " theme groups in configuration"); |
470 | 419 |
471 List<ThemeGroup> groups = new ArrayList<ThemeGroup>(); | 420 final List<ThemeGroup> groups = new ArrayList<>(); |
472 | 421 |
473 for (int g = 0; g < groupNum; g++) { | 422 for (int g = 0; g < groupNum; g++) { |
474 Element themeGroup = (Element) themeGroups.item(g); | 423 final Element themeGroup = (Element) themeGroups.item(g); |
475 NodeList themes = (NodeList) XMLUtils.xpath( | 424 |
476 themeGroup, XPATH_THEMES, XPathConstants.NODESET); | 425 final Map<String, Theme> theThemes = readThemes(cfg, themeGroup); |
477 | 426 |
478 int num = themes != null ? themes.getLength() : 0; | 427 if (theThemes.size() == 0) { |
479 | |
480 if (num == 0) { | |
481 log.warn("There are no themes configured!"); | 428 log.warn("There are no themes configured!"); |
482 return; | 429 return; |
483 } | 430 } |
484 | 431 |
485 log.info("Theme group has " + num + " themes."); | 432 final String gName = themeGroup.getAttribute("name"); |
486 | |
487 Map<String, Theme> theThemes = new HashMap<String, Theme>(); | |
488 | |
489 for (int i = 0; i < num; i++) { | |
490 Node theme = themes.item(i); | |
491 | |
492 Theme theTheme = ThemeFactory.createTheme(cfg, theme); | |
493 | |
494 if (theme != null) { | |
495 theThemes.put(theTheme.getName(), theTheme); | |
496 } | |
497 } | |
498 String gName = themeGroup.getAttribute("name"); | |
499 groups.add(new ThemeGroup(gName, theThemes)); | 433 groups.add(new ThemeGroup(gName, theThemes)); |
500 | 434 |
501 log.info( | 435 log.info("Initialized " + theThemes.size() + "/" + theThemes.size() + " themes " + "of theme-group '" + gName + "'"); |
502 "Initialized " + theThemes.size() + "/" + num + " themes " + | |
503 "of theme-group '" + gName + "'"); | |
504 } | 436 } |
505 context.put(RiverContext.THEMES, groups); | 437 context.put(RiverContext.THEMES, groups); |
506 } | 438 } |
507 | 439 |
508 /** | 440 /** |
441 * This methods reads the configured themes and puts them into the | |
442 * RiverContext. | |
443 * | |
444 * @param config | |
445 * The global configuration. | |
446 * @param context | |
447 * The RiverContext. | |
448 */ | |
449 private void configureLegend(final Document config, final RiverContext context) { | |
450 log.debug("RiverContextFactory.configureLegend"); | |
451 | |
452 final Map<String, Theme> legendGroup = readLegend(config); | |
453 context.put(RiverContext.LEGEND, legendGroup); | |
454 } | |
455 | |
456 private Map<String, Theme> readLegend(final Document config) { | |
457 | |
458 final Document cfg = getThemeConfig(config); | |
459 | |
460 final Node legendGroup = (Node) XMLUtils.xpath(cfg, XPATH_LEGEND_GROUP, XPathConstants.NODE); | |
461 if (legendGroup == null) { | |
462 log.warn("There is no legend group configured"); | |
463 return Collections.emptyMap(); | |
464 } | |
465 | |
466 return readThemes(cfg, legendGroup); | |
467 } | |
468 | |
469 private Map<String, Theme> readThemes(final Document cfg, final Node themeGroup) { | |
470 final NodeList themes = (NodeList) XMLUtils.xpath(themeGroup, XPATH_THEMES, XPathConstants.NODESET); | |
471 if (themes == null) | |
472 return Collections.emptyMap(); | |
473 | |
474 final int num = themes.getLength(); | |
475 log.info("Theme group has " + num + " themes."); | |
476 final Map<String, Theme> theThemes = new HashMap<>(); | |
477 | |
478 for (int i = 0; i < num; i++) { | |
479 final Node theme = themes.item(i); | |
480 | |
481 final Theme theTheme = ThemeFactory.createTheme(cfg, theme); | |
482 theThemes.put(theTheme.getName(), theTheme); | |
483 } | |
484 | |
485 return theThemes; | |
486 } | |
487 | |
488 /** | |
509 * This method is used to retrieve the theme configuration document. | 489 * This method is used to retrieve the theme configuration document. |
510 * | 490 * |
511 * @param config The global configuration. | 491 * @param config |
492 * The global configuration. | |
512 * | 493 * |
513 * @return the theme configuration. | 494 * @return the theme configuration. |
514 */ | 495 */ |
515 protected Document getThemeConfig(Document config) { | 496 private Document getThemeConfig(final Document config) { |
516 String themeConfig = (String) XMLUtils.xpath( | 497 String themeConfig = (String) XMLUtils.xpath(config, XPATH_THEME_CONFIG, XPathConstants.STRING); |
517 config, | |
518 XPATH_THEME_CONFIG, | |
519 XPathConstants.STRING); | |
520 | 498 |
521 themeConfig = Config.replaceConfigDir(themeConfig); | 499 themeConfig = Config.replaceConfigDir(themeConfig); |
522 | 500 |
523 log.debug("Parse theme cfg: " + themeConfig); | 501 log.debug("Parse theme cfg: " + themeConfig); |
524 | 502 |
525 return XMLUtils.parseDocument( | 503 return XMLUtils.parseDocument(new File(themeConfig), true, XMLUtils.CONF_RESOLVER); |
526 new File(themeConfig), true, XMLUtils.CONF_RESOLVER); | 504 } |
527 } | 505 |
528 | 506 private void configureThemesMappings(final Document cfg, final RiverContext context) { |
529 | |
530 protected void configureThemesMappings( | |
531 Document cfg, | |
532 RiverContext context | |
533 ) { | |
534 log.debug("RiverContextFactory.configureThemesMappings"); | 507 log.debug("RiverContextFactory.configureThemesMappings"); |
535 | 508 |
536 Document config = getThemeConfig(cfg); | 509 final Document config = getThemeConfig(cfg); |
537 | 510 |
538 NodeList mappings = (NodeList) XMLUtils.xpath( | 511 final NodeList mappings = (NodeList) XMLUtils.xpath(config, XPATH_THEME_MAPPINGS, XPathConstants.NODESET); |
539 config, XPATH_THEME_MAPPINGS, XPathConstants.NODESET); | 512 |
540 | 513 final int num = mappings != null ? mappings.getLength() : 0; |
541 int num = mappings != null ? mappings.getLength() : 0; | |
542 | 514 |
543 if (num == 0) { | 515 if (num == 0) { |
544 log.warn("No theme <--> facet mappins found!"); | 516 log.warn("No theme <--> facet mappins found!"); |
545 return; | 517 return; |
546 } | 518 } |
547 | 519 |
548 Map<String, List<ThemeMapping>> mapping = | 520 final Map<String, List<ThemeMapping>> mapping = new HashMap<>(); |
549 new HashMap<String, List<ThemeMapping>>(); | |
550 | 521 |
551 for (int i = 0; i < num; i++) { | 522 for (int i = 0; i < num; i++) { |
552 Element node = (Element)mappings.item(i); | 523 final Element node = (Element) mappings.item(i); |
553 | 524 |
554 String from = node.getAttribute("from"); | 525 final String from = node.getAttribute("from"); |
555 String to = node.getAttribute("to"); | 526 final String to = node.getAttribute("to"); |
556 String pattern = node.getAttribute("pattern"); | 527 final String pattern = node.getAttribute("pattern"); |
557 String masterAttrPattern = node.getAttribute("masterAttr"); | 528 final String masterAttrPattern = node.getAttribute("masterAttr"); |
558 String outputPattern = node.getAttribute("output"); | 529 final String outputPattern = node.getAttribute("output"); |
559 | 530 |
560 if (from.length() > 0 && to.length() > 0) { | 531 if (from.length() > 0 && to.length() > 0) { |
561 List<ThemeMapping> tm = mapping.get(from); | 532 List<ThemeMapping> tm = mapping.get(from); |
562 | 533 |
563 if (tm == null) { | 534 if (tm == null) { |
564 tm = new ArrayList<ThemeMapping>(); | 535 tm = new ArrayList<>(); |
565 mapping.put(from, tm); | 536 mapping.put(from, tm); |
566 } | 537 } |
567 | 538 |
568 tm.add(new ThemeMapping( | 539 tm.add(new ThemeMapping(from, to, pattern, masterAttrPattern, outputPattern)); |
569 from, to, pattern, masterAttrPattern, outputPattern)); | |
570 } | 540 } |
571 } | 541 } |
572 | 542 |
573 log.debug("Found " + mapping.size() + " theme mappings."); | 543 log.debug("Found " + mapping.size() + " theme mappings."); |
574 | 544 |
575 context.put(RiverContext.THEME_MAPPING, mapping); | 545 context.put(RiverContext.THEME_MAPPING, mapping); |
576 } | 546 } |
577 | |
578 | 547 |
579 /** | 548 /** |
580 * Reads configured floodmap river WMSs from floodmap.xml and | 549 * Reads configured floodmap river WMSs from floodmap.xml and |
581 * loads them into the given RiverContext. | 550 * loads them into the given RiverContext. |
551 * | |
582 * @param cfg | 552 * @param cfg |
583 * @param context | 553 * @param context |
584 */ | 554 */ |
585 protected void configureFloodmapWMS(Document cfg, RiverContext context) { | 555 private void configureFloodmapWMS(final Document cfg, final RiverContext context) { |
586 Map<String, String> riverWMS = new HashMap<String, String>(); | 556 final Map<String, String> riverWMS = new HashMap<>(); |
587 | 557 |
588 NodeList rivers = (NodeList) XMLUtils.xpath( | 558 final NodeList rivers = (NodeList) XMLUtils.xpath(cfg, XPATH_RIVER_WMS, XPathConstants.NODESET); |
589 cfg, XPATH_RIVER_WMS, XPathConstants.NODESET); | 559 |
590 | 560 final int num = rivers != null ? rivers.getLength() : 0; |
591 int num = rivers != null ? rivers.getLength() : 0; | |
592 | 561 |
593 for (int i = 0; i < num; i++) { | 562 for (int i = 0; i < num; i++) { |
594 Element e = (Element) rivers.item(i); | 563 final Element e = (Element) rivers.item(i); |
595 | 564 |
596 String river = e.getAttribute("name"); | 565 final String river = e.getAttribute("name"); |
597 String url = XMLUtils.xpathString(e, "river-wms/@url", null); | 566 final String url = XMLUtils.xpathString(e, "river-wms/@url", null); |
598 | 567 |
599 if (river != null && url != null) { | 568 if (river != null && url != null) { |
600 riverWMS.put(river, url); | 569 riverWMS.put(river, url); |
601 } | 570 } |
602 } | 571 } |
604 log.debug("Found " + riverWMS.size() + " river WMS."); | 573 log.debug("Found " + riverWMS.size() + " river WMS."); |
605 | 574 |
606 context.put(RiverContext.RIVER_WMS, riverWMS); | 575 context.put(RiverContext.RIVER_WMS, riverWMS); |
607 } | 576 } |
608 | 577 |
609 | |
610 /** | 578 /** |
611 * This method initializes the modules configuration. | 579 * This method initializes the modules configuration. |
612 * | 580 * |
613 * @param config the config document. | 581 * @param config |
614 * @param context the RiverContext. | 582 * the config document. |
615 */ | 583 * @param context |
616 protected void configureModules(Document cfg, RiverContext context) { | 584 * the RiverContext. |
617 NodeList modulenodes = (NodeList) XMLUtils.xpath( | 585 */ |
618 cfg, XPATH_MODULES, XPathConstants.NODESET); | 586 private void configureModules(final Document cfg, final RiverContext context) { |
587 final NodeList modulenodes = (NodeList) XMLUtils.xpath(cfg, XPATH_MODULES, XPathConstants.NODESET); | |
619 | 588 |
620 final int num = modulenodes != null ? modulenodes.getLength() : 0; | 589 final int num = modulenodes != null ? modulenodes.getLength() : 0; |
621 | 590 |
622 final List<Module> modules = new ArrayList<>(num); | 591 final List<Module> modules = new ArrayList<>(num); |
623 | 592 |
625 final Element e = (Element) modulenodes.item(i); | 594 final Element e = (Element) modulenodes.item(i); |
626 final String modulename = e.getAttribute("name"); | 595 final String modulename = e.getAttribute("name"); |
627 final String attrselected = e.getAttribute("selected"); | 596 final String attrselected = e.getAttribute("selected"); |
628 final boolean selected = Boolean.parseBoolean(attrselected); | 597 final boolean selected = Boolean.parseBoolean(attrselected); |
629 final String group = e.getAttribute("group"); | 598 final String group = e.getAttribute("group"); |
630 | 599 |
631 log.debug("Loaded module " + modulename); | 600 log.debug("Loaded module " + modulename); |
632 | 601 |
633 final NodeList children = e.getChildNodes(); | 602 final NodeList children = e.getChildNodes(); |
634 final List<String> rivers = new ArrayList<>(children.getLength()); | 603 final List<String> rivers = new ArrayList<>(children.getLength()); |
635 for (int j = 0; j < children.getLength(); j++) { | 604 for (int j = 0; j < children.getLength(); j++) { |
636 if (children.item(j).getNodeType() != Node.ELEMENT_NODE) { | 605 if (children.item(j).getNodeType() != Node.ELEMENT_NODE) { |
637 continue; | 606 continue; |
638 } | 607 } |
639 | 608 |
640 final Element ce = (Element)children.item(j); | 609 final Element ce = (Element) children.item(j); |
641 if (ce.hasAttribute("uuid")) { | 610 if (ce.hasAttribute("uuid")) { |
642 rivers.add(ce.getAttribute("uuid")); | 611 rivers.add(ce.getAttribute("uuid")); |
643 } | 612 } else if (ce.hasAttribute("name")) { |
644 else if (ce.hasAttribute("name")) { | |
645 final List<River> allRivers = RiverFactory.getRivers(); | 613 final List<River> allRivers = RiverFactory.getRivers(); |
646 final String name = ce.getAttribute("name"); | 614 final String name = ce.getAttribute("name"); |
647 for (final River r: allRivers) { | 615 for (final River r : allRivers) { |
648 if (name.equals(r.getName())) { | 616 if (name.equals(r.getName())) { |
649 rivers.add(r.getModelUuid()); | 617 rivers.add(r.getModelUuid()); |
650 break; | 618 break; |
651 } | 619 } |
652 } | 620 } |
655 modules.add(new Module(modulename, selected, group, rivers)); | 623 modules.add(new Module(modulename, selected, group, rivers)); |
656 } | 624 } |
657 context.put(RiverContext.MODULES, modules); | 625 context.put(RiverContext.MODULES, modules); |
658 } | 626 } |
659 } | 627 } |
660 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |