View Javadoc

1   /*
2    * Copyright (c) 2002 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: PropertyUtil.java,v 1.1.1.1 2004/05/19 12:33:40 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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           // FIXME make better, get file first, then search classpath!
55           // I think we need to work with the concept of a file
56           //ClassLoader loader = Thread.currentThread().getContextClassLoader();
57           //InputStream is = loader.getResourceAsStream(name);
58           FileInputStream is = new FileInputStream(name);
59           doc = DocumentUtil.getDocument(is);
60        } // end of if ()
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        } // end of if ()
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       } // end of while ()
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          } // end of while ()
133       }
134       return v;
135    }
136    
137    public static void main(String[] args){
138       
139    }
140 } // PropertyUtil