View Javadoc

1   /*
2    * Created on 23.03.2004
3    *
4    * To change the template for this generated file go to
5    * Window - Preferences - Java - Code Generation - Code and Comments
6    */
7   package net.sf.dexterim.oscar;
8   
9   import java.net.URL;
10  
11  import javax.xml.parsers.DocumentBuilder;
12  import javax.xml.parsers.DocumentBuilderFactory;
13  
14  import net.sf.dexterim.oscar.io.MessageParser;
15  import net.sf.dexterim.oscar.io.ReceiptParser;
16  
17  import org.w3c.dom.Document;
18  import org.w3c.dom.Element;
19  import org.w3c.dom.NamedNodeMap;
20  import org.w3c.dom.Node;
21  import org.w3c.dom.NodeList;
22  
23  /***
24   * @author christoph
25   * 
26   * To change the template for this generated type comment go to Window -
27   * Preferences - Java - Code Generation - Code and Comments
28   */
29  public class OscarConfiguration {
30  
31  	private static OscarConfiguration instance;
32  
33  	private OscarConfiguration() {
34  	}
35  	
36  	public void configure() {
37  		DocumentBuilder builder;
38  		
39  		try {
40  			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
41  			
42  			URL resource = this.getClass().getClassLoader().getResource(
43  					"net/sf/dexterim/oscar/oscar.xml");
44  
45  			Document doc = builder.parse(resource.toExternalForm());
46  			Element parsers = doc.getDocumentElement();
47  
48  			NodeList parserList = parsers.getChildNodes();
49  
50  			for (int i = 0; i < parserList.getLength(); i++) {
51  				Node node = parserList.item(i);
52  
53  				if (node.getNodeType() == Node.ELEMENT_NODE) {
54  
55  					NamedNodeMap attributes = node.getAttributes();
56  					
57  					int channel = Integer.parseInt(attributes.getNamedItem("channel")
58  							.getNodeValue());
59  
60  					String clazzName = attributes.getNamedItem("class").getNodeValue();
61  					Class clazz = getClass().getClassLoader().loadClass(clazzName);
62  					
63  					Object obj = clazz.newInstance();
64  
65  					if (obj instanceof MessageParser) {
66  						ReceiptParser.getInstance().register(channel, (MessageParser)obj);
67  					}
68  				}
69  			}
70  		}
71  		catch (Exception e) {
72  			e.printStackTrace();
73  		}
74  	}
75  
76  	public static synchronized OscarConfiguration getInstance() {
77  		if (instance == null) {
78  			instance = new OscarConfiguration();
79  		}
80  
81  		return instance;
82  	}
83  }