1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.backsource.alert;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Vector;
24 import java.util.Properties;
25 import java.util.StringTokenizer;
26 import java.io.InputStream;
27 import java.io.FileInputStream;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
30
31 import org.backsource.utils.xml.DocumentUtil;
32 import org.backsource.utils.xml.ElementUtil;
33 /***
34 * A singleton wich loads the xml definitions file, with some utility methods to use when parsing the xml.
35 *
36 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
37 * @version $Revision: 1.1.1.1 $
38 */
39
40 public class PropertyUtil{
41 private static PropertyUtil instance;
42 private Document doc;
43
44 private PropertyUtil (){
45
46 }
47
48 /***
49 * Loads the alert xml file from tne file given in {@link org.backsource.alert.PropertyConstants#DOCUMENT}, defaults to alert.xml.
50 */
51 public synchronized Document getAlertDocument() throws Exception {
52 if ( doc == null) {
53 String name = System.getProperty(PropertyConstants.DOCUMENT,PropertyConstants.DOCUMENT);
54
55
56
57
58 FileInputStream is = new FileInputStream(name);
59 doc = DocumentUtil.getDocument(is);
60 }
61 return doc;
62 }
63
64 /***
65 * Get the singleton instance wich owns the Document.
66 */
67 public synchronized static PropertyUtil instance() {
68 if ( instance == null) {
69 instance = new PropertyUtil();
70 }
71 return instance;
72 }
73
74
75 /***
76 * Get an Element list of al the agents in the alert xml file.
77 */
78 public static List getAgents() throws Exception {
79 PropertyUtil p = instance();
80 Document doc = p.getAlertDocument();
81 Element a = ElementUtil.getUniqueChild(doc.getDocumentElement(),"agents");
82 return ElementUtil.getElements(a);
83 }
84
85 /***
86 * Get an Element list of al the senders in the alert xml file.
87 */
88 public static List getSenders() throws Exception{
89 PropertyUtil p = instance();
90 Document doc = p.getAlertDocument();
91 Element a = ElementUtil.getUniqueChild(doc.getDocumentElement(),"senders");
92 return ElementUtil.getElements(a);
93 }
94
95 /***
96 * Get all properties that where found under the given element.
97 */
98 public static Properties getProperties(Element element) throws Exception {
99 Properties prop = new Properties();
100 Iterator props = ElementUtil.getElementsByTagName(element,"property").iterator();
101 while (props.hasNext()) {
102 Element e = (Element)props.next();
103 prop.setProperty(e.getAttribute("name"), ElementUtil.getContent(e));
104 }
105 return prop;
106
107 }
108
109
110
111 /***
112 * Legacy method, should NOT be used.
113 */
114 public static Properties getProperties() throws Exception {
115 String name = System.getProperty(PropertyConstants.PROPERTIES,PropertyConstants.PROPERTIES);
116 Properties prop = new Properties();
117 ClassLoader loader = Thread.currentThread().getContextClassLoader();
118 InputStream is = loader.getResourceAsStream(name);
119 prop.load(is);
120 return prop;
121 }
122
123 /***
124 * Get a list of values from a property with space separated values.
125 */
126 public static List getValues(String property) {
127 Vector v = new Vector();
128 if ( property != null) {
129 StringTokenizer tok = new StringTokenizer(property);
130 while (tok.hasMoreElements()) {
131 v.add(tok.nextElement());
132 }
133 }
134 return v;
135 }
136
137 public static void main(String[] args){
138
139 }
140 }