1 20 21 package org.jivesoftware.smackx; 22 23 import java.util.Iterator ; 24 25 import org.jivesoftware.smack.*; 26 import org.jivesoftware.smack.packet.Message; 27 import org.jivesoftware.smackx.packet.*; 28 29 36 public class XHTMLManager { 37 38 private final static String namespace = "http://jabber.org/protocol/xhtml-im"; 39 40 static { 43 XMPPConnection.addConnectionListener(new ConnectionEstablishedListener() { 44 public void connectionEstablished(XMPPConnection connection) { 45 XHTMLManager.setServiceEnabled(connection, true); 46 } 47 }); 48 } 49 50 57 public static Iterator getBodies(Message message) { 58 XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension("html", namespace); 59 if (xhtmlExtension != null) 60 return xhtmlExtension.getBodies(); 61 else 62 return null; 63 } 64 65 71 public static void addBody(Message message, String body) { 72 XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension("html", namespace); 73 if (xhtmlExtension == null) { 74 xhtmlExtension = new XHTMLExtension(); 76 message.addExtension(xhtmlExtension); 77 } 78 xhtmlExtension.addBody(body); 80 } 81 82 88 public static boolean isXHTMLMessage(Message message) { 89 return message.getExtension("html", namespace) != null; 90 } 91 92 101 public synchronized static void setServiceEnabled(XMPPConnection connection, boolean enabled) { 102 if (isServiceEnabled(connection) == enabled) 103 return; 104 105 if (enabled) { 106 ServiceDiscoveryManager.getInstanceFor(connection).addFeature(namespace); 107 } 108 else { 109 ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(namespace); 110 } 111 } 112 113 119 public static boolean isServiceEnabled(XMPPConnection connection) { 120 return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(namespace); 121 } 122 123 130 public static boolean isServiceEnabled(XMPPConnection connection, String userID) { 131 try { 132 DiscoverInfo result = 133 ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(userID); 134 return result.containsFeature(namespace); 135 } 136 catch (XMPPException e) { 137 e.printStackTrace(); 138 return false; 139 } 140 } 141 } 142 | Popular Tags |