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 org.jivesoftware.smack.ConnectionCreationListener;
021import org.jivesoftware.smack.SmackException.NoResponseException;
022import org.jivesoftware.smack.SmackException.NotConnectedException;
023import org.jivesoftware.smack.XMPPConnection;
024import org.jivesoftware.smack.XMPPConnectionRegistry;
025import org.jivesoftware.smack.XMPPException.XMPPErrorException;
026import org.jivesoftware.smack.packet.Message;
027import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
028import org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension;
029
030import java.util.List;
031
032/**
033 * Manages XHTML formatted texts within messages. A XHTMLManager provides a high level access to 
034 * get and set XHTML bodies to messages, enable and disable XHTML support and check if remote XMPP
035 * clients support XHTML.   
036 * 
037 * @author Gaston Dombiak
038 */
039public class XHTMLManager {
040    static {
041        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
042            public void connectionCreated(XMPPConnection connection) {
043                // Enable the XHTML support on every established connection
044                XHTMLManager.setServiceEnabled(connection, true);
045            }
046        });
047    }
048
049    /**
050     * Returns an Iterator for the XHTML bodies in the message. Returns null if 
051     * the message does not contain an XHTML extension.
052     *
053     * @param message an XHTML message
054     * @return an Iterator for the bodies in the message or null if none.
055     */
056    public static List<CharSequence> getBodies(Message message) {
057        XHTMLExtension xhtmlExtension = XHTMLExtension.from(message);
058        if (xhtmlExtension != null)
059            return xhtmlExtension.getBodies();
060        else
061            return null;
062    }
063
064    /**
065     * Adds an XHTML body to the message.
066     *
067     * @param message the message that will receive the XHTML body
068     * @param xhtmlText the string to add as an XHTML body to the message
069     */
070    public static void addBody(Message message, XHTMLText xhtmlText) {
071        XHTMLExtension xhtmlExtension = XHTMLExtension.from(message);
072        if (xhtmlExtension == null) {
073            // Create an XHTMLExtension and add it to the message
074            xhtmlExtension = new XHTMLExtension();
075            message.addExtension(xhtmlExtension);
076        }
077        // Add the required bodies to the message
078        xhtmlExtension.addBody(xhtmlText.toXML());
079    }
080
081    /**
082     * Returns true if the message contains an XHTML extension.
083     *
084     * @param message the message to check if contains an XHTML extentsion or not
085     * @return a boolean indicating whether the message is an XHTML message
086     */
087    public static boolean isXHTMLMessage(Message message) {
088        return message.getExtension(XHTMLExtension.ELEMENT, XHTMLExtension.NAMESPACE) != null;
089    }
090
091    /**
092     * Enables or disables the XHTML support on a given connection.<p>
093     *  
094     * Before starting to send XHTML messages to a user, check that the user can handle XHTML
095     * messages. Enable the XHTML support to indicate that this client handles XHTML messages.  
096     *
097     * @param connection the connection where the service will be enabled or disabled
098     * @param enabled indicates if the service will be enabled or disabled 
099     */
100    public synchronized static void setServiceEnabled(XMPPConnection connection, boolean enabled) {
101        if (isServiceEnabled(connection) == enabled)
102            return;
103
104        if (enabled) {
105            ServiceDiscoveryManager.getInstanceFor(connection).addFeature(XHTMLExtension.NAMESPACE);
106        }
107        else {
108            ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(XHTMLExtension.NAMESPACE);
109        }
110    }
111
112    /**
113     * Returns true if the XHTML support is enabled for the given connection.
114     *
115     * @param connection the connection to look for XHTML support
116     * @return a boolean indicating if the XHTML support is enabled for the given connection
117     */
118    public static boolean isServiceEnabled(XMPPConnection connection) {
119        return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(XHTMLExtension.NAMESPACE);
120    }
121
122    /**
123     * Returns true if the specified user handles XHTML messages.
124     *
125     * @param connection the connection to use to perform the service discovery
126     * @param userID the user to check. A fully qualified xmpp ID, e.g. jdoe@example.com
127     * @return a boolean indicating whether the specified user handles XHTML messages
128     * @throws XMPPErrorException 
129     * @throws NoResponseException 
130     * @throws NotConnectedException 
131     */
132    public static boolean isServiceEnabled(XMPPConnection connection, String userID)
133                    throws NoResponseException, XMPPErrorException, NotConnectedException {
134        return ServiceDiscoveryManager.getInstanceFor(connection).supportsFeature(userID, XHTMLExtension.NAMESPACE);
135    }
136}