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.oscar.client;
20  
21  import java.io.IOException;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  import net.sf.dexterim.oscar.OscarByteBuffer;
27  import net.sf.dexterim.oscar.entity.Word;
28  
29  import org.apache.commons.codec.DecoderException;
30  import org.apache.commons.codec.binary.Hex;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.jdom.Document;
34  import org.jdom.Element;
35  import org.jdom.JDOMException;
36  import org.jdom.input.SAXBuilder;
37  import org.xml.sax.InputSource;
38  
39  
40  /***
41   * @author christoph
42   *
43   */
44  public class ClientReadyRequestFactory {
45  	private static ClientReadyRequestFactory instance;
46  	
47  	private Map specifications;
48  	
49  	private static Log log = LogFactory.getLog(ClientReadyRequestFactory.class);
50  	
51  	private ClientReadyRequestFactory() {
52  		specifications = new HashMap();
53  		
54  		SAXBuilder builder = new SAXBuilder(false);
55  		
56  		try {
57  			Document doc = builder.build(new InputSource(getClass().getResourceAsStream("servicespecifications.xml")));
58  		
59  			Element root = doc.getRootElement();
60  			
61  			for (Iterator iter = root.getChildren().iterator(); iter.hasNext();) {
62  				Element element = (Element) iter.next();
63  
64  				ReadySpecification spec = new ReadySpecification();
65  				
66  				String numberString = element.getAttribute("number").getValue();
67  				byte[] number = Hex.decodeHex(numberString.toCharArray());
68  				spec.setNumber(new Word(number).getValue());
69  				
70  				String versionString = element.getAttribute("version").getValue();
71  				byte[] version = Hex.decodeHex(versionString.toCharArray());
72  				spec.setVersion(new Word(version).getValue());
73  				
74  				String toolString = element.getAttribute("tool_id").getValue();
75  				byte[] toolId = Hex.decodeHex(toolString.toCharArray());
76  				spec.setToolId(new Word(toolId).getValue());
77  				
78  				String toolVersionString = element.getAttribute("tool_version").getValue();
79  				byte[] toolVersion = Hex.decodeHex(toolVersionString.toCharArray());
80  				spec.setToolVersion(new Word(toolVersion).getValue());
81  
82  				log.debug("Inserting Family Spec for number " + spec.getNumber());
83  				
84  				specifications.put(new Integer(spec.getNumber()), spec);
85  			}
86  		}
87  		catch (JDOMException e) {
88  			// TODO Auto-generated catch block
89  			e.printStackTrace();
90  		}
91  		catch (IOException e) {
92  			// TODO Auto-generated catch block
93  			e.printStackTrace();
94  		}
95  		catch (DecoderException e) {
96  			// TODO Auto-generated catch block
97  			e.printStackTrace();
98  		}
99  	}
100 	
101 	public OscarByteBuffer getCode(Integer familyNumber) {
102 		log.debug("Retrieving Family Spec for number " + familyNumber);
103 		
104 		return ((ReadySpecification)specifications.get(familyNumber)).getBuffer();
105 	}
106 	
107 	/***
108 	 * @param family
109 	 * @return
110 	 */
111 	public boolean isFamilySupported(Integer family) {
112 		return specifications.containsKey(family);
113 	}
114 	
115 	public static synchronized ClientReadyRequestFactory getInstance() {
116 		if (instance == null) {
117 			instance = new ClientReadyRequestFactory();
118 		}
119 		
120 		return instance;
121 	}
122 }