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 java.util.List;

  19. import org.jivesoftware.smack.ConnectionCreationListener;
  20. import org.jivesoftware.smack.SmackException.NoResponseException;
  21. import org.jivesoftware.smack.SmackException.NotConnectedException;
  22. import org.jivesoftware.smack.XMPPConnection;
  23. import org.jivesoftware.smack.XMPPConnectionRegistry;
  24. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  25. import org.jivesoftware.smack.packet.Message;
  26. import org.jivesoftware.smack.packet.MessageBuilder;
  27. import org.jivesoftware.smack.packet.MessageView;

  28. import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
  29. import org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension;

  30. import org.jxmpp.jid.Jid;

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

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

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

  78.     /**
  79.      * Adds an XHTML body to the message.
  80.      *
  81.      * @param message the message that will receive the XHTML body
  82.      * @param xhtmlText the string to add as an XHTML body to the message
  83.      * @deprecated use {@link #addBody(MessageBuilder, XHTMLText)} instead.
  84.      */
  85.     // TODO: Remove in Smack 4.6
  86.     @Deprecated
  87.     public static void addBody(Message message, XHTMLText xhtmlText) {
  88.         XHTMLExtension xhtmlExtension = XHTMLExtension.from(message);
  89.         if (xhtmlExtension == null) {
  90.             // Create an XHTMLExtension and add it to the message
  91.             xhtmlExtension = new XHTMLExtension();
  92.             message.addExtension(xhtmlExtension);
  93.         }
  94.         // Add the required bodies to the message
  95.         xhtmlExtension.addBody(xhtmlText.toXML());
  96.     }

  97.     /**
  98.      * Returns true if the message contains an XHTML extension.
  99.      *
  100.      * @param message the message to check if contains an XHTML extension or not
  101.      * @return a boolean indicating whether the message is an XHTML message
  102.      */
  103.     public static boolean isXHTMLMessage(Message message) {
  104.         return message.getExtensionElement(XHTMLExtension.ELEMENT, XHTMLExtension.NAMESPACE) != null;
  105.     }

  106.     /**
  107.      * Enables or disables the XHTML support on a given connection.<p>
  108.      *
  109.      * Before starting to send XHTML messages to a user, check that the user can handle XHTML
  110.      * messages. Enable the XHTML support to indicate that this client handles XHTML messages.
  111.      *
  112.      * @param connection the connection where the service will be enabled or disabled
  113.      * @param enabled indicates if the service will be enabled or disabled
  114.      */
  115.     public static synchronized void setServiceEnabled(XMPPConnection connection, boolean enabled) {
  116.         if (isServiceEnabled(connection) == enabled)
  117.             return;

  118.         if (enabled) {
  119.             ServiceDiscoveryManager.getInstanceFor(connection).addFeature(XHTMLExtension.NAMESPACE);
  120.         }
  121.         else {
  122.             ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(XHTMLExtension.NAMESPACE);
  123.         }
  124.     }

  125.     /**
  126.      * Returns true if the XHTML support is enabled for the given connection.
  127.      *
  128.      * @param connection the connection to look for XHTML support
  129.      * @return a boolean indicating if the XHTML support is enabled for the given connection
  130.      */
  131.     public static boolean isServiceEnabled(XMPPConnection connection) {
  132.         return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(XHTMLExtension.NAMESPACE);
  133.     }

  134.     /**
  135.      * Returns true if the specified user handles XHTML messages.
  136.      *
  137.      * @param connection the connection to use to perform the service discovery
  138.      * @param userID the user to check. A fully qualified xmpp ID, e.g. jdoe@example.com
  139.      * @return a boolean indicating whether the specified user handles XHTML messages
  140.      * @throws XMPPErrorException if there was an XMPP error returned.
  141.      * @throws NoResponseException if there was no response from the remote entity.
  142.      * @throws NotConnectedException if the XMPP connection is not connected.
  143.      * @throws InterruptedException if the calling thread was interrupted.
  144.      */
  145.     public static boolean isServiceEnabled(XMPPConnection connection, Jid userID)
  146.                     throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  147.         return ServiceDiscoveryManager.getInstanceFor(connection).supportsFeature(userID, XHTMLExtension.NAMESPACE);
  148.     }
  149. }