XHTMLManager.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.xhtmlim;

  18. import org.jivesoftware.smack.ConnectionCreationListener;
  19. import org.jivesoftware.smack.SmackException.NoResponseException;
  20. import org.jivesoftware.smack.SmackException.NotConnectedException;
  21. import org.jivesoftware.smack.XMPPConnection;
  22. import org.jivesoftware.smack.XMPPConnectionRegistry;
  23. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  24. import org.jivesoftware.smack.packet.Message;
  25. import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
  26. import org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension;
  27. import org.jxmpp.jid.Jid;

  28. import java.util.List;

  29. /**
  30.  * Manages XHTML formatted texts within messages. A XHTMLManager provides a high level access to
  31.  * get and set XHTML bodies to messages, enable and disable XHTML support and check if remote XMPP
  32.  * clients support XHTML.  
  33.  *
  34.  * @author Gaston Dombiak
  35.  */
  36. public class XHTMLManager {
  37.     static {
  38.         XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
  39.             public void connectionCreated(XMPPConnection connection) {
  40.                 // Enable the XHTML support on every established connection
  41.                 XHTMLManager.setServiceEnabled(connection, true);
  42.             }
  43.         });
  44.     }

  45.     /**
  46.      * Returns an Iterator for the XHTML bodies in the message. Returns null if
  47.      * the message does not contain an XHTML extension.
  48.      *
  49.      * @param message an XHTML message
  50.      * @return an Iterator for the bodies in the message or null if none.
  51.      */
  52.     public static List<CharSequence> getBodies(Message message) {
  53.         XHTMLExtension xhtmlExtension = XHTMLExtension.from(message);
  54.         if (xhtmlExtension != null)
  55.             return xhtmlExtension.getBodies();
  56.         else
  57.             return null;
  58.     }

  59.     /**
  60.      * Adds an XHTML body to the message.
  61.      *
  62.      * @param message the message that will receive the XHTML body
  63.      * @param xhtmlText the string to add as an XHTML body to the message
  64.      */
  65.     public static void addBody(Message message, XHTMLText xhtmlText) {
  66.         XHTMLExtension xhtmlExtension = XHTMLExtension.from(message);
  67.         if (xhtmlExtension == null) {
  68.             // Create an XHTMLExtension and add it to the message
  69.             xhtmlExtension = new XHTMLExtension();
  70.             message.addExtension(xhtmlExtension);
  71.         }
  72.         // Add the required bodies to the message
  73.         xhtmlExtension.addBody(xhtmlText.toXML());
  74.     }

  75.     /**
  76.      * Returns true if the message contains an XHTML extension.
  77.      *
  78.      * @param message the message to check if contains an XHTML extentsion or not
  79.      * @return a boolean indicating whether the message is an XHTML message
  80.      */
  81.     public static boolean isXHTMLMessage(Message message) {
  82.         return message.getExtension(XHTMLExtension.ELEMENT, XHTMLExtension.NAMESPACE) != null;
  83.     }

  84.     /**
  85.      * Enables or disables the XHTML support on a given connection.<p>
  86.      *  
  87.      * Before starting to send XHTML messages to a user, check that the user can handle XHTML
  88.      * messages. Enable the XHTML support to indicate that this client handles XHTML messages.  
  89.      *
  90.      * @param connection the connection where the service will be enabled or disabled
  91.      * @param enabled indicates if the service will be enabled or disabled
  92.      */
  93.     public synchronized static void setServiceEnabled(XMPPConnection connection, boolean enabled) {
  94.         if (isServiceEnabled(connection) == enabled)
  95.             return;

  96.         if (enabled) {
  97.             ServiceDiscoveryManager.getInstanceFor(connection).addFeature(XHTMLExtension.NAMESPACE);
  98.         }
  99.         else {
  100.             ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(XHTMLExtension.NAMESPACE);
  101.         }
  102.     }

  103.     /**
  104.      * Returns true if the XHTML support is enabled for the given connection.
  105.      *
  106.      * @param connection the connection to look for XHTML support
  107.      * @return a boolean indicating if the XHTML support is enabled for the given connection
  108.      */
  109.     public static boolean isServiceEnabled(XMPPConnection connection) {
  110.         return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(XHTMLExtension.NAMESPACE);
  111.     }

  112.     /**
  113.      * Returns true if the specified user handles XHTML messages.
  114.      *
  115.      * @param connection the connection to use to perform the service discovery
  116.      * @param userID the user to check. A fully qualified xmpp ID, e.g. jdoe@example.com
  117.      * @return a boolean indicating whether the specified user handles XHTML messages
  118.      * @throws XMPPErrorException
  119.      * @throws NoResponseException
  120.      * @throws NotConnectedException
  121.      * @throws InterruptedException
  122.      */
  123.     public static boolean isServiceEnabled(XMPPConnection connection, Jid userID)
  124.                     throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  125.         return ServiceDiscoveryManager.getInstanceFor(connection).supportsFeature(userID, XHTMLExtension.NAMESPACE);
  126.     }
  127. }