ingo@0: /* ingo@0: * Copyright (c) 2010 by Intevation GmbH ingo@0: * ingo@0: * This program is free software under the LGPL (>=v2.1) ingo@0: * Read the file LGPL.txt coming with the software for details ingo@0: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@0: */ ingo@0: package de.intevation.artifacts.httpclient.utils; ingo@0: ingo@0: import java.io.File; ingo@0: import java.io.FileInputStream; ingo@0: import java.io.IOException; ingo@0: import java.util.HashMap; ingo@0: import java.util.Map; ingo@0: ingo@0: import org.w3c.dom.Document; ingo@0: import org.w3c.dom.Node; ingo@0: import org.w3c.dom.NodeList; ingo@0: ingo@0: import org.apache.log4j.Logger; ingo@0: ingo@0: /** ingo@0: * @author Ingo Weinzierl ingo@0: */ ingo@0: public class Configuration { ingo@0: ingo@0: private static final Logger logger = Logger.getLogger(Configuration.class); ingo@0: ingo@0: private File config; ingo@0: ingo@0: private Map serverSettings; ingo@0: private Map artifactSettings; ingo@0: private Map outputSettings; ingo@0: ingo@0: public static final String XPATH_SERVER = "/configuration/artifact-server"; ingo@0: public static final String XPATH_ARTIFACT = "/configuration/artifact"; ingo@0: public static final String XPATH_OUTPUT = "/configuration/output"; ingo@0: ingo@0: ingo@0: public Configuration(File config) { ingo@0: this.config = config; ingo@0: serverSettings = new HashMap(); ingo@0: artifactSettings = new HashMap(); ingo@0: outputSettings = new HashMap(); ingo@0: } ingo@0: ingo@0: ingo@0: public void initialize() ingo@0: throws IOException ingo@0: { ingo@0: Document conf = XMLUtils.readDocument(new FileInputStream(config)); ingo@0: ingo@0: if (conf == null) { ingo@0: throw new IOException("Can't read config: " + config.getName()); ingo@0: } ingo@0: ingo@0: readServerSettings(conf); ingo@0: readArtifactSettings(conf); ingo@0: readOutputSettings(conf); ingo@0: } ingo@0: ingo@0: ingo@0: private void readServerSettings(Document document) ingo@0: throws IOException ingo@0: { ingo@0: Node serverNode = XMLUtils.getNodeXPath(document, XPATH_SERVER); ingo@0: ingo@0: if (serverNode == null) { ingo@0: throw new IOException("No server configuration found."); ingo@0: } ingo@0: ingo@0: serverSettings = extractSettings(serverSettings, serverNode); ingo@0: } ingo@0: ingo@0: ingo@0: private void readArtifactSettings(Document document) ingo@0: throws IOException ingo@0: { ingo@0: Node artifactNode = XMLUtils.getNodeXPath(document, XPATH_ARTIFACT); ingo@0: ingo@0: if (artifactNode == null) { ingo@0: throw new IOException("No artifact configuration found."); ingo@0: } ingo@0: ingo@0: artifactSettings = extractSettings(artifactSettings, artifactNode); ingo@0: } ingo@0: ingo@0: ingo@0: private void readOutputSettings(Document document) ingo@0: throws IOException ingo@0: { ingo@0: Node outputNode = XMLUtils.getNodeXPath(document, XPATH_OUTPUT); ingo@0: ingo@0: if (outputNode == null) { ingo@0: throw new IOException("No output configuration found."); ingo@0: } ingo@0: ingo@0: outputSettings = extractSettings(outputSettings, outputNode); ingo@0: } ingo@0: ingo@0: ingo@0: private Map extractSettings(Map settings, Node node) { ingo@0: NodeList children = node.getChildNodes(); ingo@0: ingo@0: for (int i = 0; i < children.getLength(); i++) { ingo@0: Node child = children.item(i); ingo@0: if (child.getNodeType() == Node.ELEMENT_NODE) ingo@0: logger.debug(child.getNodeName() + "|" + child.getTextContent()); ingo@0: settings.put(child.getNodeName(), child.getTextContent()); ingo@0: } ingo@0: ingo@0: return settings; ingo@0: } ingo@0: ingo@0: ingo@0: public Object getServerSettings(String key) { ingo@0: return serverSettings.get(key); ingo@0: } ingo@0: ingo@0: ingo@0: public Object getArtifactSettings(String key) { ingo@0: return artifactSettings.get(key); ingo@0: } ingo@0: ingo@0: ingo@0: public Object getOutputSettings(String key) { ingo@0: return outputSettings.get(key); ingo@0: } ingo@0: } ingo@0: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: