1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 14:22:05 +01:00

Don't append closing body in XHTMLText.toString()

Also add XHTMLExtension.from(Message) and change XHTMLManager.addBody()
signature so that it expects a XHTMLText (Modifications to the original
patch by Florian Schmaus).
This commit is contained in:
vito-c 2015-01-04 20:11:06 -08:00 committed by Florian Schmaus
parent b77d61527c
commit e31b284afa
4 changed files with 23 additions and 23 deletions

View file

@ -78,9 +78,10 @@ xhtmlText.appendOpenEmTag();
xhtmlText.append("!!!!"); xhtmlText.append("!!!!");
xhtmlText.appendCloseEmTag(); xhtmlText.appendCloseEmTag();
xhtmlText.appendCloseParagraphTag(); xhtmlText.appendCloseParagraphTag();
xhtmlText.appendCloseBodyTag();
// Add the XHTML text to the message // Add the XHTML text to the message
XHTMLManager.addBody(msg, xhtmlText.toString()); XHTMLManager.addBody(msg, xhtmlText);
``` ```
Send an XHTML Message Send an XHTML Message
@ -109,7 +110,7 @@ a chat.
// Create a message to send // Create a message to send
Message msg = chat.createMessage(); Message msg = chat.createMessage();
// Obtain the XHTML text to send from somewhere // Obtain the XHTML text to send from somewhere
String xhtmlBody = getXHTMLTextToSend(); XHTMLText xhtmlBody = getXHTMLTextToSend();
// Add the XHTML text to the message // Add the XHTML text to the message
XHTMLManager.addBody(msg, xhtmlBody); XHTMLManager.addBody(msg, xhtmlBody);
@ -131,7 +132,7 @@ XHTML bodies where each body should be for a different language.
To get the XHTML bodies of a given message just send the message To get the XHTML bodies of a given message just send the message
**#getBodies(Message)** to the class _**XHTMLManager**_. The answer of this **#getBodies(Message)** to the class _**XHTMLManager**_. The answer of this
message will be an _**Iterator**_ with the different XHTML bodies of the message will be an _**List**_ with the different XHTML bodies of the
message or null if none. message or null if none.
**Example** **Example**
@ -143,16 +144,15 @@ XHTML bodies of any received message.
// Create a listener for the chat and display any XHTML content // Create a listener for the chat and display any XHTML content
PacketListener packetListener = new PacketListener() { PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) { public void processPacket(Packet packet) {
Message message = (Message) packet; Message message = (Message) packet;
// Obtain the XHTML bodies of the message // Obtain the XHTML bodies of the message
Iterator it = XHTMLManager.getBodies(message); List<CharSequence> bodies = XHTMLManager.getBodies(message);
if (it != null) { if (bodies != null) {
// Display the bodies on the console // Display the bodies on the console
while (it.hasNext()) { for (CharSequence body : bodies) {
String body = (String) it.next(); System.out.println(body);
System.out.println(body); }
} }
}
}; };
chat.addMessageListener(packetListener); chat.addMessageListener(packetListener);
``` ```

View file

@ -37,11 +37,10 @@ import java.util.List;
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class XHTMLManager { public class XHTMLManager {
// Enable the XHTML support on every established connection
// The ServiceDiscoveryManager class should have been already initialized
static { static {
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() { XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
public void connectionCreated(XMPPConnection connection) { public void connectionCreated(XMPPConnection connection) {
// Enable the XHTML support on every established connection
XHTMLManager.setServiceEnabled(connection, true); XHTMLManager.setServiceEnabled(connection, true);
} }
}); });
@ -55,7 +54,7 @@ public class XHTMLManager {
* @return an Iterator for the bodies in the message or null if none. * @return an Iterator for the bodies in the message or null if none.
*/ */
public static List<CharSequence> getBodies(Message message) { public static List<CharSequence> getBodies(Message message) {
XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension(XHTMLExtension.ELEMENT, XHTMLExtension.NAMESPACE); XHTMLExtension xhtmlExtension = XHTMLExtension.from(message);
if (xhtmlExtension != null) if (xhtmlExtension != null)
return xhtmlExtension.getBodies(); return xhtmlExtension.getBodies();
else else
@ -66,17 +65,17 @@ public class XHTMLManager {
* Adds an XHTML body to the message. * Adds an XHTML body to the message.
* *
* @param message the message that will receive the XHTML body * @param message the message that will receive the XHTML body
* @param body the string to add as an XHTML body to the message * @param xhtmlText the string to add as an XHTML body to the message
*/ */
public static void addBody(Message message, String body) { public static void addBody(Message message, XHTMLText xhtmlText) {
XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension(XHTMLExtension.ELEMENT, XHTMLExtension.NAMESPACE); XHTMLExtension xhtmlExtension = XHTMLExtension.from(message);
if (xhtmlExtension == null) { if (xhtmlExtension == null) {
// Create an XHTMLExtension and add it to the message // Create an XHTMLExtension and add it to the message
xhtmlExtension = new XHTMLExtension(); xhtmlExtension = new XHTMLExtension();
message.addExtension(xhtmlExtension); message.addExtension(xhtmlExtension);
} }
// Add the required bodies to the message // Add the required bodies to the message
xhtmlExtension.addBody(body); xhtmlExtension.addBody(xhtmlText.toXML());
} }
/** /**

View file

@ -398,12 +398,9 @@ public class XHTMLText {
/** /**
* Returns the text of the XHTMLText. * Returns the text of the XHTMLText.
* *
* Note: Automatically adds the closing body tag.
*
* @return the text of the XHTMLText * @return the text of the XHTMLText
*/ */
public String toString() { public String toString() {
appendCloseBodyTag();
return text.toString(); return text.toString();
} }

View file

@ -17,6 +17,7 @@
package org.jivesoftware.smackx.xhtmlim.packet; package org.jivesoftware.smackx.xhtmlim.packet;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
@ -122,4 +123,7 @@ public class XHTMLExtension implements PacketExtension {
} }
} }
public static XHTMLExtension from(Message message) {
return message.getExtension(ELEMENT, NAMESPACE);
}
} }