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.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<CharSequence> bodies = XHTMLManager.getBodies(message);
if (bodies != null) {
// Display the bodies on the console
for (CharSequence body : bodies) {
System.out.println(body);
}
}
};
chat.addMessageListener(packetListener);
```

View File

@ -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<CharSequence> 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());
}
/**

View File

@ -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();
}

View File

@ -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);
}
}