comparison src/main/java/de/intevation/artifacts/httpclient/utils/Configuration.java @ 0:a1db30b33f43

Moved the experimental branch 'work-on-gwt' of the console-client to an own module 'http-client' and removed the console-client. http-client/trunk@1323 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 17 Feb 2011 10:31:40 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a1db30b33f43
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8 package de.intevation.artifacts.httpclient.utils;
9
10 import java.io.File;
11 import java.io.FileInputStream;
12 import java.io.IOException;
13 import java.util.HashMap;
14 import java.util.Map;
15
16 import org.w3c.dom.Document;
17 import org.w3c.dom.Node;
18 import org.w3c.dom.NodeList;
19
20 import org.apache.log4j.Logger;
21
22 /**
23 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
24 */
25 public class Configuration {
26
27 private static final Logger logger = Logger.getLogger(Configuration.class);
28
29 private File config;
30
31 private Map serverSettings;
32 private Map artifactSettings;
33 private Map outputSettings;
34
35 public static final String XPATH_SERVER = "/configuration/artifact-server";
36 public static final String XPATH_ARTIFACT = "/configuration/artifact";
37 public static final String XPATH_OUTPUT = "/configuration/output";
38
39
40 public Configuration(File config) {
41 this.config = config;
42 serverSettings = new HashMap();
43 artifactSettings = new HashMap();
44 outputSettings = new HashMap();
45 }
46
47
48 public void initialize()
49 throws IOException
50 {
51 Document conf = XMLUtils.readDocument(new FileInputStream(config));
52
53 if (conf == null) {
54 throw new IOException("Can't read config: " + config.getName());
55 }
56
57 readServerSettings(conf);
58 readArtifactSettings(conf);
59 readOutputSettings(conf);
60 }
61
62
63 private void readServerSettings(Document document)
64 throws IOException
65 {
66 Node serverNode = XMLUtils.getNodeXPath(document, XPATH_SERVER);
67
68 if (serverNode == null) {
69 throw new IOException("No server configuration found.");
70 }
71
72 serverSettings = extractSettings(serverSettings, serverNode);
73 }
74
75
76 private void readArtifactSettings(Document document)
77 throws IOException
78 {
79 Node artifactNode = XMLUtils.getNodeXPath(document, XPATH_ARTIFACT);
80
81 if (artifactNode == null) {
82 throw new IOException("No artifact configuration found.");
83 }
84
85 artifactSettings = extractSettings(artifactSettings, artifactNode);
86 }
87
88
89 private void readOutputSettings(Document document)
90 throws IOException
91 {
92 Node outputNode = XMLUtils.getNodeXPath(document, XPATH_OUTPUT);
93
94 if (outputNode == null) {
95 throw new IOException("No output configuration found.");
96 }
97
98 outputSettings = extractSettings(outputSettings, outputNode);
99 }
100
101
102 private Map extractSettings(Map settings, Node node) {
103 NodeList children = node.getChildNodes();
104
105 for (int i = 0; i < children.getLength(); i++) {
106 Node child = children.item(i);
107 if (child.getNodeType() == Node.ELEMENT_NODE)
108 logger.debug(child.getNodeName() + "|" + child.getTextContent());
109 settings.put(child.getNodeName(), child.getTextContent());
110 }
111
112 return settings;
113 }
114
115
116 public Object getServerSettings(String key) {
117 return serverSettings.get(key);
118 }
119
120
121 public Object getArtifactSettings(String key) {
122 return artifactSettings.get(key);
123 }
124
125
126 public Object getOutputSettings(String key) {
127 return outputSettings.get(key);
128 }
129 }
130 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org