1 package org.smartcomps.twister.common.configuration;
2
3 import org.dom4j.Document;
4 import org.dom4j.Node;
5 import org.dom4j.io.SAXReader;
6
7 import java.io.File;
8 import java.util.*;
9
10 /***
11 * Read the XML configuration file and keeps it in memory to be queried easily later on.
12 */
13 public class XMLConfigurationReader {
14
15 private static Document twisterDoc;
16
17 public static void loadConfiguration() throws ConfigurationException {
18 String twisterConfig = System.getProperty("twister.config");
19 SAXReader reader = new SAXReader();
20 try {
21 if (twisterConfig == null) {
22 twisterDoc = reader.read(ClassLoader.getSystemResource("twister-configuration.xml"));
23 } else {
24 twisterDoc = reader.read(new File(twisterConfig));
25 }
26 } catch (Exception e) {
27 throw new ConfigurationException("Could not load twister configuration xml file " +
28 "'twister-configuration.xml', please make sure that the file is present in your classpath.", e);
29 }
30 }
31
32 protected static String getNodeValue(String path) {
33 return twisterDoc.selectSingleNode(path).getText();
34 }
35
36 protected static List getNodeValues(String path) {
37 List nodes = twisterDoc.selectNodes(path + "/text()");
38 List result = new ArrayList(nodes.size());
39 for (int m = 0; m < nodes.size(); m++) {
40 Node node = (Node) nodes.get(m);
41 result.add(node.getText());
42 }
43 return result;
44 }
45
46 protected static String getAttributeValue(String path, String attributeName) {
47 Node node = twisterDoc.selectSingleNode(path);
48 return node.valueOf("@" + attributeName);
49 }
50
51 protected static String getAttributeValue(String path, String whereAttrName,
52 String whereAttrValue, String attributeName) {
53 Node node = twisterDoc.selectSingleNode(path + "[@" + whereAttrName + "='" +
54 whereAttrValue + "']");
55 return node.valueOf("@" + attributeName);
56 }
57
58 protected static Map getAttributeValues(String path, String keyAttribute, String valAttribute) {
59 List nodeList = twisterDoc.selectNodes(path);
60 Map result = new HashMap();
61 for (int m = 0; m < nodeList.size(); m++) {
62 Node node = (Node) nodeList.get(m);
63 result.put(node.valueOf("@" + keyAttribute), node.valueOf("@" + valAttribute));
64 }
65 return result;
66 }
67
68 // public static void main(String[] args) {
69 // Map map = XMLConfigurationReader.getProcessDefMapping();
70 // Set entries = map.entrySet();
71 // Iterator iterator = entries.iterator();
72 // while (iterator.hasNext()) {
73 // Map.Entry entry = (Map.Entry) iterator.next();
74 // System.out.println(entry);
75 // }
76 //
77 // List list = XMLConfigurationReader.getUncheckedDefSchema();
78 // for (int i = 0; i < list.size(); i++) {
79 // String str = (String) list.get(i);
80 // System.out.println(str);
81 // }
82 // }
83 }
This page was automatically generated by Maven