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