blob: a446819c479861bf84c2285643a07cf720060c42 [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/**
2 * $RCSfile$
3 * $Revision$
4 * $Date$
5 *
6 * Copyright 2003-2007 Jive Software.
7 *
8 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21package org.jivesoftware.smackx;
22
23import org.jivesoftware.smack.ConnectionCreationListener;
24import org.jivesoftware.smack.Connection;
25import org.jivesoftware.smack.XMPPException;
26import org.jivesoftware.smack.packet.Message;
27import org.jivesoftware.smackx.packet.DiscoverInfo;
28import org.jivesoftware.smackx.packet.XHTMLExtension;
29
30import java.util.Iterator;
31
32/**
33 * Manages XHTML formatted texts within messages. A XHTMLManager provides a high level access to
34 * get and set XHTML bodies to messages, enable and disable XHTML support and check if remote XMPP
35 * clients support XHTML.
36 *
37 * @author Gaston Dombiak
38 */
39public class XHTMLManager {
40
41 private final static String namespace = "http://jabber.org/protocol/xhtml-im";
42
43 // Enable the XHTML support on every established connection
44 // The ServiceDiscoveryManager class should have been already initialized
45 static {
46 Connection.addConnectionCreationListener(new ConnectionCreationListener() {
47 public void connectionCreated(Connection connection) {
48 XHTMLManager.setServiceEnabled(connection, true);
49 }
50 });
51 }
52
53 /**
54 * Returns an Iterator for the XHTML bodies in the message. Returns null if
55 * the message does not contain an XHTML extension.
56 *
57 * @param message an XHTML message
58 * @return an Iterator for the bodies in the message or null if none.
59 */
60 public static Iterator<String> getBodies(Message message) {
61 XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension("html", namespace);
62 if (xhtmlExtension != null)
63 return xhtmlExtension.getBodies();
64 else
65 return null;
66 }
67
68 /**
69 * Adds an XHTML body to the message.
70 *
71 * @param message the message that will receive the XHTML body
72 * @param body the string to add as an XHTML body to the message
73 */
74 public static void addBody(Message message, String body) {
75 XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension("html", namespace);
76 if (xhtmlExtension == null) {
77 // Create an XHTMLExtension and add it to the message
78 xhtmlExtension = new XHTMLExtension();
79 message.addExtension(xhtmlExtension);
80 }
81 // Add the required bodies to the message
82 xhtmlExtension.addBody(body);
83 }
84
85 /**
86 * Returns true if the message contains an XHTML extension.
87 *
88 * @param message the message to check if contains an XHTML extentsion or not
89 * @return a boolean indicating whether the message is an XHTML message
90 */
91 public static boolean isXHTMLMessage(Message message) {
92 return message.getExtension("html", namespace) != null;
93 }
94
95 /**
96 * Enables or disables the XHTML support on a given connection.<p>
97 *
98 * Before starting to send XHTML messages to a user, check that the user can handle XHTML
99 * 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 synchronized static void setServiceEnabled(Connection connection, boolean enabled) {
105 if (isServiceEnabled(connection) == enabled)
106 return;
107
108 if (enabled) {
109 ServiceDiscoveryManager.getInstanceFor(connection).addFeature(namespace);
110 }
111 else {
112 ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(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(Connection connection) {
123 return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(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 */
133 public static boolean isServiceEnabled(Connection connection, String userID) {
134 try {
135 DiscoverInfo result =
136 ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(userID);
137 return result.containsFeature(namespace);
138 }
139 catch (XMPPException e) {
140 e.printStackTrace();
141 return false;
142 }
143 }
144}