1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.limmen.crs.storage.xml;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25 import javax.xml.parsers.ParserConfigurationException;
26
27 import org.limmen.crs.model.Configuration;
28 import org.limmen.crs.model.Documentation;
29 import org.limmen.crs.model.Setting;
30 import org.limmen.crs.model.SettingType;
31 import org.limmen.crs.storage.api.InputStreamResolver;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Node;
34 import org.w3c.dom.NodeList;
35 import org.xml.sax.SAXException;
36
37 public class ConfigurationXMLReader {
38
39 private static Configuration parseConfiguration(Node rootNode, InputStreamResolver resolver) throws IOException {
40
41 Node idNode = rootNode.getAttributes().getNamedItem("id");
42
43 Configuration configuration = new Configuration(idNode.getNodeValue());
44
45 NodeList nodeList = rootNode.getChildNodes();
46
47 for (int index = 0; index < nodeList.getLength(); index++) {
48
49 Node node = nodeList.item(index);
50
51 if (node.getNodeType() == Node.ELEMENT_NODE) {
52
53 if (node.getNodeName().equals("base")) {
54
55
56 configuration.setBaseConfiguration(
57 read(resolver.getInputStream(node.getTextContent()),
58 resolver));
59 }
60
61 if (node.getNodeName().equals("section")) {
62
63 parseSection(configuration, node);
64 }
65
66 if (node.getNodeName().equals("documentation")) {
67
68 configuration.addDocumentation(parseDocumentation(node));
69 }
70 }
71 }
72
73 return configuration;
74 }
75
76 private static Documentation parseDocumentation(Node rootNode) {
77
78 Documentation documentation = new Documentation();
79
80 Node langNode = rootNode.getAttributes().getNamedItem("lang");
81
82 documentation.setLanguage(langNode.getNodeValue());
83 documentation.setText(rootNode.getTextContent());
84
85 return documentation;
86 }
87
88 private static void parseSection(Configuration configuration, Node rootNode) {
89
90 Node idNode = rootNode.getAttributes().getNamedItem("id");
91
92 String sectionId = idNode.getNodeValue();
93
94 NodeList nodeList = rootNode.getChildNodes();
95
96 for (int index = 0; index < nodeList.getLength(); index++) {
97
98 Node node = nodeList.item(index);
99
100 if (node.getNodeType() == Node.ELEMENT_NODE) {
101
102 if (node.getNodeName().equals("documentation")) {
103
104 configuration.addDocumentation(sectionId, parseDocumentation(node));
105 }
106
107 if (node.getNodeName().equals("setting")) {
108
109 configuration.addSetting(sectionId, parseSetting(configuration, sectionId, node));
110 }
111 }
112 }
113 }
114
115 private static Setting parseSetting(Configuration configuration, String aSectionId, Node rootNode) {
116
117 Node idNode = rootNode.getAttributes().getNamedItem("id");
118
119 Setting setting = new Setting(idNode.getNodeValue());
120
121 NodeList nodeList = rootNode.getChildNodes();
122
123 for (int index = 0; index < nodeList.getLength(); index++) {
124
125 Node node = nodeList.item(index);
126
127 if (node.getNodeType() == Node.ELEMENT_NODE) {
128
129 if (node.getNodeName().equals("documentation")) {
130
131 configuration.addDocumentation(
132 aSectionId,
133 setting.getId(),
134 parseDocumentation(node));
135 }
136
137 if (node.getNodeName().equals("value")) {
138
139 setting.setValue(node.getTextContent());
140 }
141
142 if (node.getNodeName().equals("type")) {
143
144 setting.setType(SettingType.valueOf(node.getTextContent()));
145 }
146 }
147 }
148
149 return setting;
150 }
151
152 public static Configuration read(File aConfigurationFile, InputStreamResolver resolver) throws IOException {
153
154 FileInputStream fileInputStream = null;
155
156 try {
157
158 fileInputStream = new FileInputStream(aConfigurationFile);
159
160 return read(fileInputStream, resolver);
161 }
162 finally {
163
164 try {
165
166 fileInputStream.close();
167 }
168 catch (Throwable t) {
169
170
171 }
172 }
173 }
174
175 public static Configuration read(InputStream inputStream, InputStreamResolver resolver) throws IOException {
176
177 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
178 DocumentBuilder builder = null;
179
180 try {
181
182 builder = factory.newDocumentBuilder();
183 }
184 catch (ParserConfigurationException e) {
185
186 e.printStackTrace();
187 }
188
189 Document document = null;
190
191 try {
192
193 document = builder.parse(inputStream);
194 }
195 catch (SAXException e) {
196
197 throw new IOException(e);
198 }
199
200 return parseConfiguration(document.getFirstChild(), resolver);
201 }
202
203 public static Configuration read(String aConfigurationFile, InputStreamResolver resolver) throws IOException {
204
205 return read(new File(aConfigurationFile), resolver);
206 }
207 }