KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smackx > XHTMLManager


1 /**
2  * $RCSfile$
3  * $Revision: 2407 $
4  * $Date: 2004-11-02 20:37:00 -0300 (Tue, 02 Nov 2004) $
5  *
6  * Copyright 2003-2004 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
21 package org.jivesoftware.smackx;
22
23 import java.util.Iterator JavaDoc;
24
25 import org.jivesoftware.smack.*;
26 import org.jivesoftware.smack.packet.Message;
27 import org.jivesoftware.smackx.packet.*;
28
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
38     private final static String JavaDoc namespace = "http://jabber.org/protocol/xhtml-im";
39
40     // Enable the XHTML support on every established connection
41
// The ServiceDiscoveryManager class should have been already initialized
42
static {
43         XMPPConnection.addConnectionListener(new ConnectionEstablishedListener() {
44             public void connectionEstablished(XMPPConnection connection) {
45                 XHTMLManager.setServiceEnabled(connection, true);
46             }
47         });
48     }
49
50     /**
51      * Returns an Iterator for the XHTML bodies in the message. Returns null if
52      * the message does not contain an XHTML extension.
53      *
54      * @param message an XHTML message
55      * @return an Iterator for the bodies in the message or null if none.
56      */

57     public static Iterator JavaDoc getBodies(Message message) {
58         XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension("html", namespace);
59         if (xhtmlExtension != null)
60             return xhtmlExtension.getBodies();
61         else
62             return null;
63     }
64
65     /**
66      * Adds an XHTML body to the message.
67      *
68      * @param message the message that will receive the XHTML body
69      * @param body the string to add as an XHTML body to the message
70      */

71     public static void addBody(Message message, String JavaDoc body) {
72         XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension("html", namespace);
73         if (xhtmlExtension == null) {
74             // Create an XHTMLExtension and add it to the message
75
xhtmlExtension = new XHTMLExtension();
76             message.addExtension(xhtmlExtension);
77         }
78         // Add the required bodies to the message
79
xhtmlExtension.addBody(body);
80     }
81
82     /**
83      * Returns true if the message contains an XHTML extension.
84      *
85      * @param message the message to check if contains an XHTML extentsion or not
86      * @return a boolean indicating whether the message is an XHTML message
87      */

88     public static boolean isXHTMLMessage(Message message) {
89         return message.getExtension("html", namespace) != null;
90     }
91
92     /**
93      * Enables or disables the XHTML support on a given connection.<p>
94      *
95      * Before starting to send XHTML messages to a user, check that the user can handle XHTML
96      * messages. Enable the XHTML support to indicate that this client handles XHTML messages.
97      *
98      * @param connection the connection where the service will be enabled or disabled
99      * @param enabled indicates if the service will be enabled or disabled
100      */

101     public synchronized static void setServiceEnabled(XMPPConnection connection, boolean enabled) {
102         if (isServiceEnabled(connection) == enabled)
103             return;
104
105         if (enabled) {
106             ServiceDiscoveryManager.getInstanceFor(connection).addFeature(namespace);
107         }
108         else {
109             ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(namespace);
110         }
111     }
112
113     /**
114      * Returns true if the XHTML support is enabled for the given connection.
115      *
116      * @param connection the connection to look for XHTML support
117      * @return a boolean indicating if the XHTML support is enabled for the given connection
118      */

119     public static boolean isServiceEnabled(XMPPConnection connection) {
120         return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(namespace);
121     }
122
123     /**
124      * Returns true if the specified user handles XHTML messages.
125      *
126      * @param connection the connection to use to perform the service discovery
127      * @param userID the user to check. A fully qualified xmpp ID, e.g. jdoe@example.com
128      * @return a boolean indicating whether the specified user handles XHTML messages
129      */

130     public static boolean isServiceEnabled(XMPPConnection connection, String JavaDoc userID) {
131         try {
132             DiscoverInfo result =
133                 ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(userID);
134             return result.containsFeature(namespace);
135         }
136         catch (XMPPException e) {
137             e.printStackTrace();
138             return false;
139         }
140     }
141 }
142
Popular Tags