View Javadoc
1   // ////////////////////////////////////////////////////////////////////////////
2   // dexterIM - Instant Messaging Framework
3   // Copyright (C) 2003 Christoph Walcher
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or
8   // (at your option) any later version.
9   //
10  // This program is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  // GNU General Public License for more details.
14  //
15  // You should have received a copy of the GNU General Public License
16  // along with this program; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  //////////////////////////////////////////////////////////////////////////////
19  package net.sf.dexterim.msn;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.jdom.Document;
29  import org.jdom.Element;
30  import org.jdom.JDOMException;
31  import org.jdom.input.SAXBuilder;
32  
33  
34  /***
35   * @author christoph
36   *
37   */
38  public class MsnConfiguration {
39  	private static final Log log = LogFactory.getLog(MsnConfiguration.class);
40  	
41  	private static MsnConfiguration instance;
42  	private List versions;
43  	private String hostInformation;
44  	private String challengeMagic;
45  	
46  	private MsnConfiguration() {
47  		
48  		SAXBuilder builder = new SAXBuilder();
49  	
50  		try {
51  			Document doc = builder.build(MsnConfiguration.class.getResource("protocol.xml"));
52  		
53  			Element root = doc.getRootElement();
54  			
55  			for (Iterator iter = root.getChildren().iterator(); iter.hasNext();) {
56  				Element element = (Element) iter.next();
57  				
58  				if ("versions".equals(element.getName())) {
59  					versions = parseProtocolVersions(element);
60  				}
61  				
62  				if ("hostinformation".equals(element.getName())) {
63  					hostInformation = element.getAttributeValue("id");
64  				}
65  				
66  				if ("challenge".equals(element.getName())) {
67  					this.challengeMagic = element.getAttributeValue("magic");
68  				}
69  			}
70  		}
71  		catch (JDOMException e) {
72  			log.fatal("Could not parse Msn protocol configuration file", e);
73  		}
74  		catch (IOException e) {
75  			log.fatal("Could not open Msn protocol configuration file", e);
76  		}
77  		
78  
79  		
80  	}
81  	
82  	private List parseProtocolVersions(Element versionsElement) {
83  		List versions = new ArrayList();
84  		
85  		for (Iterator iter = versionsElement.getChildren().iterator(); iter.hasNext();) {
86  			Element version = (Element) iter.next();
87  			
88  			versions.add(version.getAttributeValue("id"));
89  		}
90  		
91  		return versions;
92  	}
93  	
94  	public List getVersions() {
95  		return versions;
96  	}
97  	
98  	public static synchronized MsnConfiguration getInstance() {
99  		if (instance == null) {
100 			instance = new MsnConfiguration();
101 		}
102 		
103 		return instance;
104 	}
105 
106 	/***
107 	 * @return Returns the hostInformation.
108 	 */
109 	public String getHostInformation() {
110 		return hostInformation;
111 	}
112 
113 	/***
114 	 * @return Returns the challengeMagic.
115 	 */
116 	public String getChallengeMagic() {
117 		return challengeMagic;
118 	}
119 }