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.entity;
20  
21  import java.io.IOException;
22  import java.util.Iterator;
23  
24  import net.sf.dexterim.codec.HexPrinter;
25  import net.sf.dexterim.oscar.OscarByteBuffer;
26  
27  import org.apache.commons.codec.DecoderException;
28  import org.apache.commons.codec.binary.Hex;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.jdom.Document;
32  import org.jdom.Element;
33  import org.jdom.JDOMException;
34  import org.jdom.input.SAXBuilder;
35  
36  
37  /***
38   * @author christoph
39   */
40  public class CapabilitiesBuilder {
41  	
42  	private static CapabilitiesBuilder instance;
43  	private static Log log = LogFactory.getLog(CapabilitiesBuilder.class);
44  	
45  	private Document doc;
46  	
47  	private CapabilitiesBuilder() {
48  		SAXBuilder builder = new SAXBuilder(false);
49  		
50  		try {
51  			this.doc = builder.build(this.getClass().getResourceAsStream("capabilities.xml"));
52  		}
53  		catch (JDOMException e) {
54  			e.printStackTrace();
55  		}
56  		catch (IOException e) {
57  			e.printStackTrace();
58  		}
59  	}
60  	
61  	public Capabilities buildCapabilities(String dialect) {
62  		BasicCapabilities result = new BasicCapabilities();
63  
64  		Element capabilities = doc.getRootElement();
65  
66  		for (Iterator iter = capabilities.getChildren().iterator(); iter.hasNext();) {
67  			Element element = (Element) iter.next();
68  
69  			String elementDialect = element.getAttribute("dialect").getValue();
70  
71  			if (dialect.equals(elementDialect)) {
72  
73  				try {
74  					String name = element.getAttribute("name").getValue();
75  					String codeString = element.getChild("code").getTextTrim();
76  
77  					byte[] code = Hex.decodeHex(codeString.toCharArray());
78  
79  					log.debug("Capability " + name + " Code " + HexPrinter.format(code));
80  					
81  					result.add(new Capability(name, OscarByteBuffer.wrap(code)));
82  				}
83  				catch (DecoderException e) {
84  					e.printStackTrace();
85  				}
86  			}
87  		}
88  
89  		return result;
90  	}
91  
92  	/***
93  	 * 
94  	 */
95  	public static synchronized CapabilitiesBuilder getInstance() {
96  		if (instance == null) {
97  			instance = new CapabilitiesBuilder();
98  		}
99  		
100 		return instance;
101 	}
102 }