001/** 002 * 003 * Copyright 2003-2007 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.jivesoftware.smackx.xhtmlim; 019 020import java.util.List; 021 022import org.jivesoftware.smack.ConnectionCreationListener; 023import org.jivesoftware.smack.SmackException.NoResponseException; 024import org.jivesoftware.smack.SmackException.NotConnectedException; 025import org.jivesoftware.smack.XMPPConnection; 026import org.jivesoftware.smack.XMPPConnectionRegistry; 027import org.jivesoftware.smack.XMPPException.XMPPErrorException; 028import org.jivesoftware.smack.packet.Message; 029 030import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; 031import org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension; 032 033import org.jxmpp.jid.Jid; 034 035/** 036 * Manages XHTML formatted texts within messages. A XHTMLManager provides a high level access to 037 * get and set XHTML bodies to messages, enable and disable XHTML support and check if remote XMPP 038 * clients support XHTML. 039 * 040 * @author Gaston Dombiak 041 */ 042public class XHTMLManager { 043 static { 044 XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() { 045 @Override 046 public void connectionCreated(XMPPConnection connection) { 047 // Enable the XHTML support on every established connection 048 XHTMLManager.setServiceEnabled(connection, true); 049 } 050 }); 051 } 052 053 /** 054 * Returns an Iterator for the XHTML bodies in the message. Returns null if 055 * the message does not contain an XHTML extension. 056 * 057 * @param message an XHTML message 058 * @return an Iterator for the bodies in the message or null if none. 059 */ 060 public static List<CharSequence> getBodies(Message message) { 061 XHTMLExtension xhtmlExtension = XHTMLExtension.from(message); 062 if (xhtmlExtension != null) 063 return xhtmlExtension.getBodies(); 064 else 065 return null; 066 } 067 068 /** 069 * Adds an XHTML body to the message. 070 * 071 * @param message the message that will receive the XHTML body 072 * @param xhtmlText the string to add as an XHTML body to the message 073 */ 074 public static void addBody(Message message, XHTMLText xhtmlText) { 075 XHTMLExtension xhtmlExtension = XHTMLExtension.from(message); 076 if (xhtmlExtension == null) { 077 // Create an XHTMLExtension and add it to the message 078 xhtmlExtension = new XHTMLExtension(); 079 message.addExtension(xhtmlExtension); 080 } 081 // Add the required bodies to the message 082 xhtmlExtension.addBody(xhtmlText.toXML()); 083 } 084 085 /** 086 * Returns true if the message contains an XHTML extension. 087 * 088 * @param message the message to check if contains an XHTML extension or not 089 * @return a boolean indicating whether the message is an XHTML message 090 */ 091 public static boolean isXHTMLMessage(Message message) { 092 return message.getExtension(XHTMLExtension.ELEMENT, XHTMLExtension.NAMESPACE) != null; 093 } 094 095 /** 096 * Enables or disables the XHTML support on a given connection.<p> 097 * 098 * Before starting to send XHTML messages to a user, check that the user can handle XHTML 099 * messages. Enable the XHTML support to indicate that this client handles XHTML messages. 100 * 101 * @param connection the connection where the service will be enabled or disabled 102 * @param enabled indicates if the service will be enabled or disabled 103 */ 104 public static synchronized void setServiceEnabled(XMPPConnection connection, boolean enabled) { 105 if (isServiceEnabled(connection) == enabled) 106 return; 107 108 if (enabled) { 109 ServiceDiscoveryManager.getInstanceFor(connection).addFeature(XHTMLExtension.NAMESPACE); 110 } 111 else { 112 ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(XHTMLExtension.NAMESPACE); 113 } 114 } 115 116 /** 117 * Returns true if the XHTML support is enabled for the given connection. 118 * 119 * @param connection the connection to look for XHTML support 120 * @return a boolean indicating if the XHTML support is enabled for the given connection 121 */ 122 public static boolean isServiceEnabled(XMPPConnection connection) { 123 return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(XHTMLExtension.NAMESPACE); 124 } 125 126 /** 127 * Returns true if the specified user handles XHTML messages. 128 * 129 * @param connection the connection to use to perform the service discovery 130 * @param userID the user to check. A fully qualified xmpp ID, e.g. jdoe@example.com 131 * @return a boolean indicating whether the specified user handles XHTML messages 132 * @throws XMPPErrorException 133 * @throws NoResponseException 134 * @throws NotConnectedException 135 * @throws InterruptedException 136 */ 137 public static boolean isServiceEnabled(XMPPConnection connection, Jid userID) 138 throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 139 return ServiceDiscoveryManager.getInstanceFor(connection).supportsFeature(userID, XHTMLExtension.NAMESPACE); 140 } 141}