|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package org.jconfig.utils; |
|
8 |
| |
|
9 |
| import org.jconfig.Configuration; |
|
10 |
| import org.jconfig.DefaultConfiguration; |
|
11 |
| import org.w3c.dom.Document; |
|
12 |
| import org.w3c.dom.NamedNodeMap; |
|
13 |
| import org.w3c.dom.Node; |
|
14 |
| import org.w3c.dom.NodeList; |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| public class XMLConfigParser2 { |
|
20 |
| |
|
21 |
0
| private XMLConfigParser2() {
|
|
22 |
| } |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
0
| private static void getVariables(Document doc,Configuration config) {
|
|
31 |
0
| NodeList nl = doc.getElementsByTagName("variables");
|
|
32 |
0
| for (int i = 0; i < nl.getLength(); i++) {
|
|
33 |
0
| Node n = nl.item(i);
|
|
34 |
0
| for (Node child = n.getFirstChild(); child != null; child = child.getNextSibling()) {
|
|
35 |
| |
|
36 |
0
| if (child.getNodeName().equals("variable")) {
|
|
37 |
0
| NamedNodeMap myAtt = child.getAttributes();
|
|
38 |
0
| Node myNode = myAtt.getNamedItem("name");
|
|
39 |
0
| String name = myNode.getNodeValue();
|
|
40 |
0
| myNode = myAtt.getNamedItem("value");
|
|
41 |
0
| String value = myNode.getNodeValue();
|
|
42 |
0
| if (name != null && value != null) {
|
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
0
| config.setVariable(name, value);
|
|
48 |
| } |
|
49 |
| } |
|
50 |
| } |
|
51 |
| } |
|
52 |
| } |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
0
| public static Configuration parse(Document doc,String configName) {
|
|
60 |
0
| Configuration configuration = new DefaultConfiguration(configName);
|
|
61 |
0
| String currentCategory;
|
|
62 |
0
| getVariables(doc,configuration);
|
|
63 |
| |
|
64 |
0
| NodeList nl = doc.getElementsByTagName("category");
|
|
65 |
0
| for (int i = 0; i < nl.getLength(); i++) {
|
|
66 |
| |
|
67 |
0
| Node n = nl.item(i);
|
|
68 |
| |
|
69 |
0
| NamedNodeMap curAtt = n.getAttributes();
|
|
70 |
0
| Node curNode = curAtt.getNamedItem("name");
|
|
71 |
0
| currentCategory = curNode.getNodeValue();
|
|
72 |
0
| configuration.setCategory(currentCategory);
|
|
73 |
| |
|
74 |
0
| for (Node child = n.getFirstChild(); child != null; child = child.getNextSibling()) {
|
|
75 |
0
| if (child.getNodeName().equals("property")) {
|
|
76 |
| |
|
77 |
| |
|
78 |
0
| NamedNodeMap myAtt = child.getAttributes();
|
|
79 |
0
| Node myNode = myAtt.getNamedItem("name");
|
|
80 |
0
| String name = myNode.getNodeValue();
|
|
81 |
0
| myNode = myAtt.getNamedItem("value");
|
|
82 |
0
| String value = myNode.getNodeValue();
|
|
83 |
| |
|
84 |
0
| if (name != null && value != null) {
|
|
85 |
| |
|
86 |
| |
|
87 |
0
| configuration.setProperty(name,value,currentCategory);
|
|
88 |
| } |
|
89 |
| } |
|
90 |
| } |
|
91 |
| } |
|
92 |
0
| return configuration;
|
|
93 |
| } |
|
94 |
| |
|
95 |
| } |