1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }