teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
teichmann@5863: * Software engineering by Intevation GmbH
teichmann@5863: *
teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3)
teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the
teichmann@5994: * documentation coming with Dive4Elements River for details.
teichmann@5863: */
teichmann@5863:
teichmann@5831: package org.dive4elements.river.artifacts.context;
ingo@106:
teichmann@7099: import java.io.File;
christian@4656:
ingo@106: import java.util.ArrayList;
ingo@295: import java.util.HashMap;
ingo@106: import java.util.List;
ingo@295: import java.util.Map;
ingo@106:
ingo@106: import javax.xml.xpath.XPathConstants;
ingo@106:
tom@9726: import org.apache.logging.log4j.Logger;
tom@9726: import org.apache.logging.log4j.LogManager;
teichmann@7099:
teichmann@7099: import org.dive4elements.artifactdatabase.state.State;
teichmann@7099: import org.dive4elements.artifactdatabase.state.StateEngine;
teichmann@7099:
teichmann@7099: import org.dive4elements.artifactdatabase.transition.Transition;
teichmann@7099: import org.dive4elements.artifactdatabase.transition.TransitionEngine;
teichmann@7099:
teichmann@7099: import org.dive4elements.artifacts.ArtifactContextFactory;
teichmann@7099: import org.dive4elements.artifacts.GlobalContext;
teichmann@7099:
teichmann@8219: import org.dive4elements.artifacts.ContextInjector;
teichmann@8219:
teichmann@7099: import org.dive4elements.artifacts.common.utils.Config;
teichmann@7099: import org.dive4elements.artifacts.common.utils.ElementConverter;
teichmann@7099: import org.dive4elements.artifacts.common.utils.XMLUtils;
teichmann@7099:
teichmann@7099: import org.dive4elements.river.artifacts.model.Module;
rrenkert@7756: import org.dive4elements.river.artifacts.model.RiverFactory;
teichmann@7099: import org.dive4elements.river.artifacts.model.ZoomScale;
teichmann@7099:
teichmann@7099: import org.dive4elements.river.artifacts.states.StateFactory;
teichmann@7099:
teichmann@7099: import org.dive4elements.river.artifacts.transitions.TransitionFactory;
teichmann@7099:
teichmann@7226: import org.dive4elements.river.exports.GeneratorLookup;
teichmann@7099: import org.dive4elements.river.exports.OutGenerator;
teichmann@7099:
rrenkert@7756: import org.dive4elements.river.model.River;
teichmann@7099: import org.dive4elements.river.themes.Theme;
teichmann@7099: import org.dive4elements.river.themes.ThemeFactory;
teichmann@7099: import org.dive4elements.river.themes.ThemeGroup;
teichmann@7099: import org.dive4elements.river.themes.ThemeMapping;
teichmann@7099:
ingo@106: import org.w3c.dom.Document;
ingo@106: import org.w3c.dom.Element;
ingo@295: import org.w3c.dom.Node;
ingo@106: import org.w3c.dom.NodeList;
ingo@106:
ingo@106: /**
ingo@106: * The ArtifactContextFactory is used to initialize basic components and put
ingo@106: * them into the global context of the application.
ingo@106: *
ingo@106: * @author Ingo Weinzierl
ingo@106: */
teichmann@5866: public class RiverContextFactory implements ArtifactContextFactory {
ingo@106:
teichmann@8202: /** The log that is used in this class. */
tom@9726: private static Logger log = LogManager.getLogger(RiverContextFactory.class);
ingo@106:
ingo@106: /** The XPath to the artifacts configured in the configuration. */
ingo@106: public static final String XPATH_ARTIFACTS =
ingo@106: "/artifact-database/artifacts/artifact";
ingo@106:
ingo@106: /** The XPath to the name of the artifact. */
ingo@107: public static final String XPATH_ARTIFACT_NAME = "/artifact/@name";
ingo@106:
ingo@106: /** The XPath to the xlink ref in an artifact configuration. */
ingo@106: public static final String XPATH_XLINK = "xlink:href";
ingo@106:
ingo@106: /** The XPath to the transitions configured in the artifact config. */
ingo@106: public static final String XPATH_TRANSITIONS =
ingo@106: "/artifact/states/transition";
ingo@106:
ingo@107: /** The XPath to the states configured in the artifact config. */
ingo@107: public static final String XPATH_STATES =
ingo@107: "/artifact/states/state";
ingo@107:
ingo@295: public static final String XPATH_OUTPUT_GENERATORS =
teichmann@8157: "/artifact-database/output-generators//output-generator";
ingo@295:
ingo@341: public static final String XPATH_THEME_CONFIG =
ingo@341: "/artifact-database/flys/themes/configuration/text()";
ingo@341:
ingo@341: public static final String XPATH_THEMES =
raimund@2737: "theme";
raimund@2737:
raimund@2737: public static final String XPATH_THEME_GROUPS =
raimund@2737: "/themes/themegroup";
ingo@341:
ingo@345: public static final String XPATH_THEME_MAPPINGS =
ingo@345: "/themes/mappings/mapping";
ingo@345:
ingo@958: public static final String XPATH_RIVER_WMS =
ingo@1062: "/artifact-database/floodmap/river";
ingo@958:
tom@8856: public static final String XPATH_MODULES =
tom@8856: "/artifact-database/modules/module";
bjoern@3630:
tom@8856: private static final String XPATH_ZOOM_SCALES =
tom@8856: "/artifact-database/options/zoom-scales/zoom-scale";
rrenkert@4619:
tom@8856: private static final String XPATH_DGM_PATH =
tom@8856: "/artifact-database/options/dgm-path/text()";
rrenkert@5152:
teichmann@6933: private static GlobalContext GLOBAL_CONTEXT_INSTANCE;
teichmann@6933:
rrenkert@5152:
ingo@106: /**
teichmann@5867: * Creates a new D4EArtifactContext object and initialize all
ingo@106: * components required by the application.
ingo@106: *
ingo@106: * @param config The artifact server configuration.
teichmann@5867: * @return a D4EArtifactContext.
ingo@106: */
christian@4656: @Override
ingo@939: public GlobalContext createArtifactContext(Document config) {
teichmann@5866: RiverContext context = new RiverContext(config);
ingo@106:
ingo@106: configureTransitions(config, context);
ingo@107: configureStates(config, context);
ingo@295: configureOutGenerators(config, context);
ingo@341: configureThemes(config, context);
ingo@345: configureThemesMappings(config, context);
christian@4656: configureFloodmapWMS(config, context);
bjoern@3630: configureModules(config, context);
rrenkert@4619: configureZoomScales(config, context);
rrenkert@5152: configureDGMPath(config, context);
ingo@106:
teichmann@6933: synchronized (RiverContextFactory.class) {
teichmann@6933: GLOBAL_CONTEXT_INSTANCE = context;
teichmann@6933: }
teichmann@6933:
ingo@106: return context;
ingo@106: }
ingo@106:
teichmann@6933: public static synchronized GlobalContext getGlobalContext() {
teichmann@6933: return GLOBAL_CONTEXT_INSTANCE;
teichmann@6933: }
teichmann@6933:
ingo@106:
teichmann@5866: private void configureDGMPath(Document config, RiverContext context) {
rrenkert@5152: String dgmPath = (String) XMLUtils.xpath(
rrenkert@5152: config,
rrenkert@5152: XPATH_DGM_PATH,
rrenkert@5152: XPathConstants.STRING);
rrenkert@5152:
rrenkert@5152: context.put("dgm-path", dgmPath);
rrenkert@5152: }
rrenkert@5152:
rrenkert@5152:
teichmann@5866: protected void configureZoomScales(Document config, RiverContext context) {
rrenkert@4619: NodeList list = (NodeList)XMLUtils.xpath(
rrenkert@4619: config,
rrenkert@4619: XPATH_ZOOM_SCALES,
rrenkert@4619: XPathConstants.NODESET);
rrenkert@4619: ZoomScale scale = new ZoomScale();
rrenkert@4619: for (int i = 0; i < list.getLength(); i++) {
rrenkert@4619: Element element = (Element)list.item(i);
rrenkert@4619: String river = "default";
rrenkert@4619: double range = 0d;
rrenkert@4619: double radius = 10d;
rrenkert@4619: if (element.hasAttribute("river")) {
rrenkert@4619: river = element.getAttribute("river");
rrenkert@4619: }
rrenkert@4619: if (!element.hasAttribute("range")) {
rrenkert@4619: continue;
rrenkert@4619: }
rrenkert@4619: else {
rrenkert@4619: String r = element.getAttribute("range");
rrenkert@4619: try {
rrenkert@4619: range = Double.parseDouble(r);
rrenkert@4619: }
rrenkert@4619: catch (NumberFormatException nfe) {
rrenkert@4619: continue;
rrenkert@4619: }
rrenkert@4619: }
rrenkert@4619: if (!element.hasAttribute("radius")) {
rrenkert@4619: continue;
rrenkert@4619: }
rrenkert@4619: else {
rrenkert@4619: String r = element.getAttribute("radius");
rrenkert@4619: try {
rrenkert@4619: radius = Double.parseDouble(r);
rrenkert@4619: }
rrenkert@4619: catch (NumberFormatException nfe) {
rrenkert@4619: continue;
rrenkert@4619: }
rrenkert@4619: }
rrenkert@4619: scale.addRange(river, range, radius);
rrenkert@4619: }
rrenkert@4619: context.put("zoomscale", scale);
rrenkert@4619: }
rrenkert@4619:
rrenkert@4619:
ingo@106: /**
ingo@106: * This method initializes the transition configuration.
ingo@106: *
ingo@106: * @param config the config document.
teichmann@5866: * @param context the RiverContext.
ingo@106: */
tom@8856: protected void configureTransitions(
tom@8856: Document config,
tom@8856: RiverContext context
tom@8856: ) {
ingo@106: TransitionEngine engine = new TransitionEngine();
ingo@106:
teichmann@8086: List artifacts = getArtifactConfigurations(config);
teichmann@8202: log.info("Found " + artifacts.size() + " artifacts in the config.");
ingo@106:
ingo@106: for (Document doc: artifacts) {
ingo@106:
ingo@106: String artName = (String) XMLUtils.xpath(
ingo@106: doc, XPATH_ARTIFACT_NAME, XPathConstants.STRING);
ingo@106:
ingo@111: NodeList list = (NodeList) XMLUtils.xpath(
ingo@106: doc, XPATH_TRANSITIONS, XPathConstants.NODESET);
ingo@106:
ingo@111: if (list == null) {
teichmann@8202: log.warn("The artifact " + artName +
felix@2141: " has no transitions configured.");
ingo@107: continue;
ingo@106: }
ingo@106:
ingo@111: int trans = list.getLength();
ingo@106:
teichmann@8202: log.info(
ingo@106: "Artifact '" + artName + "' has " + trans + " transitions.");
ingo@106:
ingo@106: for (int i = 0; i < trans; i++) {
tom@8856: Transition t = TransitionFactory.createTransition(
tom@8856: list.item(i));
ingo@111: String s = t.getFrom();
ingo@111: engine.addTransition(s, t);
ingo@106: }
ingo@106: }
ingo@106:
teichmann@5866: context.put(RiverContext.TRANSITION_ENGINE_KEY, engine);
ingo@106: }
ingo@106:
ingo@106:
ingo@106: /**
ingo@106: * This method returns all artifact documents defined in
ingo@106: * config
.
NOTE: The artifact configurations need to be
ingo@106: * stored in own files referenced by an xlink.
ingo@106: *
ingo@106: * @param config The global configuration.
ingo@106: *
ingo@106: * @return an array of Artifact configurations.
ingo@106: */
teichmann@8086: protected List getArtifactConfigurations(Document config) {
ingo@106: NodeList artifacts = (NodeList) XMLUtils.xpath(
ingo@106: config, XPATH_ARTIFACTS, XPathConstants.NODESET);
ingo@106:
ingo@106: int count = artifacts.getLength();
ingo@106:
teichmann@8086: ArrayList docs = new ArrayList(count);
ingo@106:
ingo@106: for (int i = 0; i < count; i++) {
ingo@106: Element tmp = (Element) artifacts.item(i);
ingo@106:
ingo@106: String xlink = tmp.getAttribute(XPATH_XLINK);
ingo@106: xlink = Config.replaceConfigDir(xlink);
ingo@106:
teichmann@8272: if (!xlink.isEmpty()) {
teichmann@8272: File file = new File(xlink);
teichmann@8272: if (!file.isFile() || !file.canRead()) {
tom@8856: log.warn("Artifact configuration '"
tom@8856: + file + "' not found.");
teichmann@8272: } else {
teichmann@8272: Document doc = XMLUtils.parseDocument(file);
teichmann@8272: if (doc != null) {
teichmann@8272: docs.add(doc);
teichmann@8272: }
teichmann@8272: }
teichmann@8086: continue;
teichmann@8086: }
teichmann@8272: Document doc = XMLUtils.newDocument();
teichmann@8272: Node copy = doc.adoptNode(tmp.cloneNode(true));
teichmann@8272: doc.appendChild(copy);
teichmann@8272: docs.add(doc);
ingo@106: }
teichmann@8086: return docs;
ingo@106: }
ingo@107:
ingo@107:
ingo@107: /**
ingo@107: * This method initializes the transition configuration.
ingo@107: *
ingo@107: * @param config the config document.
teichmann@5866: * @param context the RiverContext.
ingo@107: */
teichmann@5866: protected void configureStates(Document config, RiverContext context) {
ingo@107: StateEngine engine = new StateEngine();
ingo@107:
teichmann@8086: List artifacts = getArtifactConfigurations(config);
teichmann@8202: log.info("Found " + artifacts.size() + " artifacts in the config.");
ingo@107:
ingo@107: for (Document doc: artifacts) {
ingo@107: List states = new ArrayList();
ingo@107:
ingo@107: String artName = (String) XMLUtils.xpath(
ingo@107: doc, XPATH_ARTIFACT_NAME, XPathConstants.STRING);
ingo@107:
ingo@107: NodeList stateList = (NodeList) XMLUtils.xpath(
ingo@107: doc, XPATH_STATES, XPathConstants.NODESET);
ingo@107:
ingo@107: if (stateList == null) {
teichmann@8202: log.warn("The artifact " + artName +
felix@2141: " has no states configured.");
ingo@107: continue;
ingo@107: }
ingo@107:
ingo@107: int count = stateList.getLength();
ingo@107:
teichmann@8202: log.info(
ingo@107: "Artifact '" + artName + "' has " + count + " states.");
ingo@107:
ingo@107: for (int i = 0; i < count; i++) {
ingo@107: states.add(StateFactory.createState(
ingo@107: stateList.item(i)));
ingo@107: }
ingo@107:
ingo@107: engine.addStates(artName, states);
ingo@107: }
ingo@107:
teichmann@5866: context.put(RiverContext.STATE_ENGINE_KEY, engine);
ingo@107: }
ingo@295:
ingo@295:
ingo@295: /**
ingo@295: * This method intializes the provided output generators.
ingo@295: *
ingo@295: * @param config the config document.
teichmann@5866: * @param context the RiverContext.
ingo@295: */
tom@8856: protected void configureOutGenerators(
tom@8856: Document config,
tom@8856: RiverContext context
tom@8856: ) {
ingo@295: NodeList outGenerators = (NodeList) XMLUtils.xpath(
ingo@295: config,
ingo@295: XPATH_OUTPUT_GENERATORS,
ingo@295: XPathConstants.NODESET);
ingo@295:
ingo@295: int num = outGenerators == null ? 0 : outGenerators.getLength();
ingo@295:
ingo@295: if (num == 0) {
teichmann@8202: log.warn("No output generators configured in this application.");
ingo@295: return;
ingo@295: }
ingo@295:
teichmann@8202: log.info("Found " + num + " configured output generators.");
ingo@295:
teichmann@7226: GeneratorLookup generators = new GeneratorLookup();
teichmann@7037:
ingo@295: int idx = 0;
ingo@295:
ingo@295: for (int i = 0; i < num; i++) {
teichmann@7037: Element item = (Element)outGenerators.item(i);
ingo@295:
teichmann@8254: String names = item.getAttribute("names").trim();
teichmann@8254: String clazz = item.getAttribute("class").trim();
teichmann@8254: String converter = item.getAttribute("converter").trim();
teichmann@8254: String injectors = item.getAttribute("injectors").trim();
ingo@295:
teichmann@7074: if (names.isEmpty() || clazz.isEmpty()) {
ingo@295: continue;
ingo@295: }
ingo@295:
teichmann@7073: Class generatorClass = null;
teichmann@7073:
ingo@295: try {
teichmann@7073: generatorClass = (Class)Class.forName(clazz);
ingo@295: }
ingo@295: catch (ClassNotFoundException cnfe) {
teichmann@8202: log.error(cnfe, cnfe);
teichmann@7073: continue;
teichmann@7073: }
teichmann@7073:
teichmann@7099: Object cfg = null;
teichmann@7099:
teichmann@7099: if (!converter.isEmpty()) {
teichmann@7099: try {
teichmann@7099: ElementConverter ec =
teichmann@7099: (ElementConverter)Class.forName(converter)
teichmann@7099: .newInstance();
teichmann@7099: cfg = ec.convert(item);
teichmann@7099: }
teichmann@7099: catch (ClassNotFoundException cnfe) {
teichmann@8202: log.error(cnfe, cnfe);
teichmann@7099: }
teichmann@7099: catch (InstantiationException ie) {
teichmann@8202: log.error(ie);
teichmann@7099: }
teichmann@7099: catch (IllegalAccessException iae) {
teichmann@8202: log.error(iae);
teichmann@7099: }
teichmann@7099: }
teichmann@7099:
teichmann@8254: List cis = null;
teichmann@8219:
teichmann@8254: if (!injectors.isEmpty()) {
teichmann@8254: cis = new ArrayList();
teichmann@8254: for (String injector: injectors.split("[\\s,]+")) {
teichmann@8254: try {
tom@8856: ContextInjector ci = (ContextInjector)Class
tom@8856: .forName(injector)
teichmann@8254: .newInstance();
teichmann@8254: ci.setup(item);
teichmann@8254: cis.add(ci);
teichmann@8254: }
teichmann@8254: catch (ClassNotFoundException cnfe) {
teichmann@8254: log.error(cnfe, cnfe);
teichmann@8254: }
teichmann@8254: catch (InstantiationException ie) {
teichmann@8254: log.error(ie);
teichmann@8254: }
teichmann@8254: catch (IllegalAccessException iae) {
teichmann@8254: log.error(iae);
teichmann@8254: }
teichmann@8219: }
teichmann@8219: }
teichmann@8219:
teichmann@8254: for (String key: names.split("[\\s,]+")) {
teichmann@7073: if (!(key = key.trim()).isEmpty()) {
teichmann@8254: generators.putGenerator(key, generatorClass, cfg, cis);
teichmann@7073: idx++;
teichmann@7073: }
ingo@295: }
ingo@295: }
ingo@295:
teichmann@8202: log.info("Successfully loaded " + idx + " output generators.");
teichmann@5866: context.put(RiverContext.OUTGENERATORS_KEY, generators);
teichmann@7227: context.put(RiverContext.FACETFILTER_KEY, generators);
ingo@295: }
ingo@341:
ingo@341:
ingo@341: /**
ingo@341: * This methods reads the configured themes and puts them into the
teichmann@5866: * RiverContext.
ingo@341: *
ingo@341: * @param config The global configuration.
teichmann@5866: * @param context The RiverContext.
ingo@341: */
teichmann@5866: protected void configureThemes(Document config, RiverContext context) {
teichmann@8202: log.debug("RiverContextFactory.configureThemes");
ingo@341:
ingo@341: Document cfg = getThemeConfig(config);
ingo@341:
raimund@2737: NodeList themeGroups = (NodeList) XMLUtils.xpath(
raimund@2737: cfg, XPATH_THEME_GROUPS, XPathConstants.NODESET);
ingo@341:
raimund@2737: int groupNum = themeGroups != null ? themeGroups.getLength() : 0;
ingo@341:
raimund@2737: if (groupNum == 0) {
teichmann@8202: log.warn("There are no theme groups configured!");
ingo@341: }
sascha@3256:
teichmann@8202: log.info("Found " + groupNum + " theme groups in configuration");
ingo@341:
raimund@2737: List groups = new ArrayList();
ingo@341:
raimund@2737: for (int g = 0; g < groupNum; g++) {
raimund@2737: Element themeGroup = (Element) themeGroups.item(g);
raimund@2737: NodeList themes = (NodeList) XMLUtils.xpath(
raimund@2737: themeGroup, XPATH_THEMES, XPathConstants.NODESET);
ingo@341:
raimund@2737: int num = themes != null ? themes.getLength() : 0;
ingo@341:
raimund@2737: if (num == 0) {
teichmann@8202: log.warn("There are no themes configured!");
raimund@2737: return;
raimund@2737: }
raimund@2737:
teichmann@8202: log.info("Theme group has " + num + " themes.");
raimund@2737:
raimund@2737: Map theThemes = new HashMap();
raimund@2737:
raimund@2737: for (int i = 0; i < num; i++) {
raimund@2737: Node theme = themes.item(i);
raimund@2737:
raimund@2737: Theme theTheme = ThemeFactory.createTheme(cfg, theme);
raimund@2737:
raimund@2737: if (theme != null) {
raimund@2737: theThemes.put(theTheme.getName(), theTheme);
raimund@2737: }
raimund@2737: }
raimund@2737: String gName = themeGroup.getAttribute("name");
raimund@2737: groups.add(new ThemeGroup(gName, theThemes));
sascha@3256:
teichmann@8202: log.info(
ingo@3239: "Initialized " + theThemes.size() + "/" + num + " themes " +
ingo@3239: "of theme-group '" + gName + "'");
raimund@2737: }
teichmann@5866: context.put(RiverContext.THEMES, groups);
ingo@341: }
ingo@341:
ingo@341: /**
ingo@341: * This method is used to retrieve the theme configuration document.
ingo@341: *
ingo@341: * @param config The global configuration.
ingo@341: *
ingo@341: * @return the theme configuration.
ingo@341: */
ingo@341: protected Document getThemeConfig(Document config) {
ingo@341: String themeConfig = (String) XMLUtils.xpath(
ingo@341: config,
ingo@341: XPATH_THEME_CONFIG,
ingo@341: XPathConstants.STRING);
ingo@341:
ingo@341: themeConfig = Config.replaceConfigDir(themeConfig);
ingo@341:
teichmann@8202: log.debug("Parse theme cfg: " + themeConfig);
ingo@341:
tom@8856: return XMLUtils.parseDocument(
tom@8856: new File(themeConfig), true, XMLUtils.CONF_RESOLVER);
ingo@341: }
ingo@345:
ingo@345:
tom@8856: protected void configureThemesMappings(
tom@8856: Document cfg,
tom@8856: RiverContext context
tom@8856: ) {
teichmann@8202: log.debug("RiverContextFactory.configureThemesMappings");
ingo@345:
ingo@345: Document config = getThemeConfig(cfg);
ingo@345:
ingo@345: NodeList mappings = (NodeList) XMLUtils.xpath(
ingo@345: config, XPATH_THEME_MAPPINGS, XPathConstants.NODESET);
ingo@345:
ingo@345: int num = mappings != null ? mappings.getLength() : 0;
ingo@345:
ingo@345: if (num == 0) {
teichmann@8202: log.warn("No theme <--> facet mappins found!");
ingo@345: return;
ingo@345: }
ingo@345:
ingo@1747: Map> mapping =
ingo@1747: new HashMap>();
ingo@345:
ingo@345: for (int i = 0; i < num; i++) {
raimund@2742: Element node = (Element)mappings.item(i);
ingo@345:
raimund@2742: String from = node.getAttribute("from");
raimund@2742: String to = node.getAttribute("to");
raimund@2742: String pattern = node.getAttribute("pattern");
raimund@2742: String masterAttrPattern = node.getAttribute("masterAttr");
raimund@2742: String outputPattern = node.getAttribute("output");
ingo@1747:
raimund@2742: if (from.length() > 0 && to.length() > 0) {
ingo@1747: List tm = mapping.get(from);
ingo@1747:
ingo@1747: if (tm == null) {
ingo@1747: tm = new ArrayList();
ingo@1747: mapping.put(from, tm);
ingo@1747: }
ingo@1747:
felix@1822: tm.add(new ThemeMapping(
felix@1828: from, to, pattern, masterAttrPattern, outputPattern));
ingo@345: }
ingo@345: }
ingo@345:
teichmann@8202: log.debug("Found " + mapping.size() + " theme mappings.");
ingo@345:
teichmann@5866: context.put(RiverContext.THEME_MAPPING, mapping);
ingo@345: }
ingo@958:
ingo@958:
christian@4656: /**
christian@4656: * Reads configured floodmap river WMSs from floodmap.xml and
teichmann@5866: * loads them into the given RiverContext.
christian@4656: * @param cfg
christian@4656: * @param context
christian@4656: */
teichmann@5866: protected void configureFloodmapWMS(Document cfg, RiverContext context) {
ingo@958: Map riverWMS = new HashMap();
ingo@958:
ingo@958: NodeList rivers = (NodeList) XMLUtils.xpath(
ingo@958: cfg, XPATH_RIVER_WMS, XPathConstants.NODESET);
ingo@958:
ingo@958: int num = rivers != null ? rivers.getLength() : 0;
ingo@958:
ingo@958: for (int i = 0; i < num; i++) {
ingo@958: Element e = (Element) rivers.item(i);
ingo@958:
ingo@1062: String river = e.getAttribute("name");
ingo@1062: String url = XMLUtils.xpathString(e, "river-wms/@url", null);
ingo@958:
ingo@958: if (river != null && url != null) {
ingo@958: riverWMS.put(river, url);
ingo@958: }
ingo@958: }
ingo@958:
teichmann@8202: log.debug("Found " + riverWMS.size() + " river WMS.");
ingo@958:
teichmann@5866: context.put(RiverContext.RIVER_WMS, riverWMS);
ingo@958: }
bjoern@3630:
bjoern@3630:
bjoern@3630: /**
bjoern@3630: * This method initializes the modules configuration.
bjoern@3630: *
bjoern@3630: * @param config the config document.
teichmann@5866: * @param context the RiverContext.
bjoern@3630: */
teichmann@5866: protected void configureModules(Document cfg, RiverContext context) {
bjoern@3630: NodeList modulenodes = (NodeList) XMLUtils.xpath(
bjoern@3630: cfg, XPATH_MODULES, XPathConstants.NODESET);
bjoern@3630:
bjoern@3630: int num = modulenodes != null ? modulenodes.getLength() : 0;
bjoern@3630: ArrayList modules = new ArrayList(num);
bjoern@3630:
bjoern@3630: for (int i = 0; i < num; i++) {
bjoern@3630: Element e = (Element) modulenodes.item(i);
bjoern@3630: String modulename = e.getAttribute("name");
bjoern@3630: String attrselected = e.getAttribute("selected");
bjoern@3630: boolean selected = attrselected == null ? false :
bjoern@3630: attrselected.equalsIgnoreCase("true");
teichmann@8202: log.debug("Loaded module " + modulename);
rrenkert@7756: NodeList children = e.getChildNodes();
rrenkert@7756: List rivers = new ArrayList(children.getLength());
rrenkert@7756: for (int j = 0; j < children.getLength(); j++) {
rrenkert@7756: if (children.item(j).getNodeType() != Node.ELEMENT_NODE) {
rrenkert@7756: continue;
rrenkert@7756: }
rrenkert@7756: Element ce = (Element)children.item(j);
rrenkert@7756: if (ce.hasAttribute("uuid")) {
rrenkert@7756: rivers.add(ce.getAttribute("uuid"));
rrenkert@7756: }
rrenkert@7756: else if (ce.hasAttribute("name")) {
rrenkert@7756: List allRivers = RiverFactory.getRivers();
rrenkert@7756: String name = ce.getAttribute("name");
rrenkert@7756: for (River r: allRivers) {
rrenkert@7756: if (name.equals(r.getName())) {
rrenkert@7756: rivers.add(r.getModelUuid());
rrenkert@7756: break;
rrenkert@7756: }
rrenkert@7756: }
rrenkert@7756: }
rrenkert@7756: }
rrenkert@7756: modules.add(new Module(modulename, selected, rivers));
bjoern@3630: }
teichmann@5866: context.put(RiverContext.MODULES, modules);
bjoern@3630: }
ingo@106: }
ingo@106: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :