From e31b284afa7958ad4a48cc3b992c150824e4616f Mon Sep 17 00:00:00 2001 From: vito-c Date: Sun, 4 Jan 2015 20:11:06 -0800 Subject: [PATCH] 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). --- documentation/extensions/xhtml.md | 26 +++++++++---------- .../smackx/xhtmlim/XHTMLManager.java | 13 +++++----- .../smackx/xhtmlim/XHTMLText.java | 3 --- .../smackx/xhtmlim/packet/XHTMLExtension.java | 4 +++ 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/documentation/extensions/xhtml.md b/documentation/extensions/xhtml.md index ab5fee5e4..d24ef791b 100644 --- a/documentation/extensions/xhtml.md +++ b/documentation/extensions/xhtml.md @@ -78,9 +78,10 @@ xhtmlText.appendOpenEmTag(); xhtmlText.append("!!!!"); xhtmlText.appendCloseEmTag(); xhtmlText.appendCloseParagraphTag(); +xhtmlText.appendCloseBodyTag(); // Add the XHTML text to the message -XHTMLManager.addBody(msg, xhtmlText.toString()); +XHTMLManager.addBody(msg, xhtmlText); ``` Send an XHTML Message @@ -109,7 +110,7 @@ a chat. // Create a message to send Message msg = chat.createMessage(); // Obtain the XHTML text to send from somewhere -String xhtmlBody = getXHTMLTextToSend(); +XHTMLText xhtmlBody = getXHTMLTextToSend(); // Add the XHTML text to the message 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 **#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. **Example** @@ -143,16 +144,15 @@ XHTML bodies of any received message. // Create a listener for the chat and display any XHTML content PacketListener packetListener = new PacketListener() { public void processPacket(Packet packet) { -Message message = (Message) packet; -// Obtain the XHTML bodies of the message -Iterator it = XHTMLManager.getBodies(message); -if (it != null) { - // Display the bodies on the console - while (it.hasNext()) { - String body = (String) it.next(); - System.out.println(body); - } -} + Message message = (Message) packet; + // Obtain the XHTML bodies of the message + List bodies = XHTMLManager.getBodies(message); + if (bodies != null) { + // Display the bodies on the console + for (CharSequence body : bodies) { + System.out.println(body); + } + } }; chat.addMessageListener(packetListener); ``` diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/XHTMLManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/XHTMLManager.java index 13cf6472d..9aea9378b 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/XHTMLManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/XHTMLManager.java @@ -37,11 +37,10 @@ import java.util.List; * @author Gaston Dombiak */ public class XHTMLManager { - // Enable the XHTML support on every established connection - // The ServiceDiscoveryManager class should have been already initialized static { XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() { public void connectionCreated(XMPPConnection connection) { + // Enable the XHTML support on every established connection XHTMLManager.setServiceEnabled(connection, true); } }); @@ -55,7 +54,7 @@ public class XHTMLManager { * @return an Iterator for the bodies in the message or null if none. */ public static List getBodies(Message message) { - XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension(XHTMLExtension.ELEMENT, XHTMLExtension.NAMESPACE); + XHTMLExtension xhtmlExtension = XHTMLExtension.from(message); if (xhtmlExtension != null) return xhtmlExtension.getBodies(); else @@ -66,17 +65,17 @@ public class XHTMLManager { * Adds an XHTML body to the message. * * @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) { - XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension(XHTMLExtension.ELEMENT, XHTMLExtension.NAMESPACE); + public static void addBody(Message message, XHTMLText xhtmlText) { + XHTMLExtension xhtmlExtension = XHTMLExtension.from(message); if (xhtmlExtension == null) { // Create an XHTMLExtension and add it to the message xhtmlExtension = new XHTMLExtension(); message.addExtension(xhtmlExtension); } // Add the required bodies to the message - xhtmlExtension.addBody(body); + xhtmlExtension.addBody(xhtmlText.toXML()); } /** diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/XHTMLText.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/XHTMLText.java index c5ebd3256..e72099f31 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/XHTMLText.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/XHTMLText.java @@ -398,12 +398,9 @@ public class XHTMLText { /** * Returns the text of the XHTMLText. * - * Note: Automatically adds the closing body tag. - * * @return the text of the XHTMLText */ public String toString() { - appendCloseBodyTag(); return text.toString(); } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/packet/XHTMLExtension.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/packet/XHTMLExtension.java index 0bf793169..41158f82d 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/packet/XHTMLExtension.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/packet/XHTMLExtension.java @@ -17,6 +17,7 @@ package org.jivesoftware.smackx.xhtmlim.packet; +import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.PacketExtension; 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); + } }