mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 17:49:38 +02:00
Added support for multiple message bodies and language on the message. SMACK-99
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@8113 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
985bc79aca
commit
8e08a8ba4a
11 changed files with 926 additions and 42 deletions
|
@ -22,6 +22,8 @@ package org.jivesoftware.smack.packet;
|
|||
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Represents XMPP message packets. A message can be one of several types:
|
||||
*
|
||||
|
@ -50,8 +52,10 @@ public class Message extends Packet {
|
|||
|
||||
private Type type = Type.normal;
|
||||
private String subject = null;
|
||||
private String body = null;
|
||||
private String thread = null;
|
||||
private String language;
|
||||
|
||||
private final Set<Body> bodies = new HashSet<Body>();
|
||||
|
||||
/**
|
||||
* Creates a new, "normal" message.
|
||||
|
@ -65,9 +69,6 @@ public class Message extends Packet {
|
|||
* @param to the recipient of the message.
|
||||
*/
|
||||
public Message(String to) {
|
||||
if (to == null) {
|
||||
throw new IllegalArgumentException("Parameter cannot be null");
|
||||
}
|
||||
setTo(to);
|
||||
}
|
||||
|
||||
|
@ -78,15 +79,13 @@ public class Message extends Packet {
|
|||
* @param type the message type.
|
||||
*/
|
||||
public Message(String to, Type type) {
|
||||
if (to == null || type == null) {
|
||||
throw new IllegalArgumentException("Parameters cannot be null.");
|
||||
}
|
||||
setTo(to);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of the message.
|
||||
* Returns the type of the message. If no type has been set this method will return {@link
|
||||
* org.jivesoftware.smack.packet.Message.Type#normal}.
|
||||
*
|
||||
* @return the type of the message.
|
||||
*/
|
||||
|
@ -98,6 +97,7 @@ public class Message extends Packet {
|
|||
* Sets the type of the message.
|
||||
*
|
||||
* @param type the type of the message.
|
||||
* @throws IllegalArgumentException if null is passed in as the type
|
||||
*/
|
||||
public void setType(Type type) {
|
||||
if (type == null) {
|
||||
|
@ -133,16 +133,115 @@ public class Message extends Packet {
|
|||
* @return the body of the message.
|
||||
*/
|
||||
public String getBody() {
|
||||
return body;
|
||||
return getBody(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the body corresponding to the language. If the language is null, the method result
|
||||
* will be the same as {@link #getBody()}. Null will be returned if the language does not have
|
||||
* a corresponding body.
|
||||
*
|
||||
* @param language the language of the body to return.
|
||||
* @return the body related to the passed in language.
|
||||
* @since 3.0.2
|
||||
*/
|
||||
public String getBody(String language) {
|
||||
language = parseXMLLang(language);
|
||||
for (Body body : bodies) {
|
||||
if ((body.langauge == null && language == null)
|
||||
|| (body != null && body.langauge.equals(language))) {
|
||||
return body.message;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a set of all bodies in this Message, including the default message body accessible
|
||||
* from {@link #getBody()}.
|
||||
*
|
||||
* @return a collection of all bodies in this Message.
|
||||
* @since 3.0.2
|
||||
*/
|
||||
public Collection<Body> getBodies() {
|
||||
return Collections.unmodifiableCollection(bodies);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the body of the message. The body is the main message contents.
|
||||
*
|
||||
*
|
||||
* @param body the body of the message.
|
||||
*/
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
if (body == null) {
|
||||
removeBody("");
|
||||
return;
|
||||
}
|
||||
addBody(null, body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a body with a corresponding language.
|
||||
*
|
||||
* @param language the language of the body being added.
|
||||
* @param body the body being added to the message.
|
||||
* @return the new {@link org.jivesoftware.smack.packet.Message.Body}
|
||||
* @throws NullPointerException if the body is null, a null pointer exception is thrown
|
||||
* @since 3.0.2
|
||||
*/
|
||||
public Body addBody(String language, String body) {
|
||||
if (body == null) {
|
||||
throw new NullPointerException("Body must be specified");
|
||||
}
|
||||
language = parseXMLLang(language);
|
||||
|
||||
Body messageBody = new Body(language, body);
|
||||
bodies.add(messageBody);
|
||||
return messageBody;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the body with the given language from the message.
|
||||
*
|
||||
* @param language the language of the body which is to be removed
|
||||
* @return true if a body was removed and false if it was not.
|
||||
*/
|
||||
public boolean removeBody(String language) {
|
||||
language = parseXMLLang(language);
|
||||
|
||||
for (Body body : bodies) {
|
||||
if (language.equals(body.langauge)) {
|
||||
return bodies.remove(body);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the body from the message and returns true if the body was removed.
|
||||
*
|
||||
* @param body the body being removed from the message.
|
||||
* @return true if the body was successfully removed and false if it was not.
|
||||
* @since 3.0.2
|
||||
*/
|
||||
public boolean removeBody(Body body) {
|
||||
return bodies.remove(body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all the languages being used for the bodies, not including the default body.
|
||||
*
|
||||
* @return the languages being used for the bodies.
|
||||
* @since 3.0.2
|
||||
*/
|
||||
public Collection<String> getBodyLanguages() {
|
||||
List<String> languages = new ArrayList<String>(bodies.size());
|
||||
for (Body body : bodies) {
|
||||
if (!parseXMLLang(body.langauge).equals(getDefaultLanguage())) {
|
||||
languages.add(body.langauge);
|
||||
}
|
||||
}
|
||||
return Collections.unmodifiableCollection(languages);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -165,9 +264,35 @@ public class Message extends Packet {
|
|||
this.thread = thread;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the xml:lang of this Message.
|
||||
*
|
||||
* @return the xml:lang of this Message.
|
||||
* @since 3.0.2
|
||||
*/
|
||||
private String getLanguage() {
|
||||
return language;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the xml:lang of this Message.
|
||||
*
|
||||
* @param language the xml:lang of this Message.
|
||||
* @since 3.0.2
|
||||
*/
|
||||
public void setLanguage(String language) {
|
||||
this.language = language;
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<message");
|
||||
if (getXmlns() != null) {
|
||||
buf.append(" xmlns=\"").append(getXmlns()).append("\"");
|
||||
}
|
||||
if (language != null) {
|
||||
buf.append(" xml:lang=\"").append(getLanguage()).append("\"");
|
||||
}
|
||||
if (getPacketID() != null) {
|
||||
buf.append(" id=\"").append(getPacketID()).append("\"");
|
||||
}
|
||||
|
@ -184,8 +309,19 @@ public class Message extends Packet {
|
|||
if (subject != null) {
|
||||
buf.append("<subject>").append(StringUtils.escapeForXML(subject)).append("</subject>");
|
||||
}
|
||||
if (body != null) {
|
||||
buf.append("<body>").append(StringUtils.escapeForXML(body)).append("</body>");
|
||||
// Add the body in the default language
|
||||
if (getBody() != null) {
|
||||
buf.append("<body>").append(StringUtils.escapeForXML(getBody())).append("</body>");
|
||||
}
|
||||
// Add the bodies in other languages
|
||||
for (Body body : getBodies()) {
|
||||
// Skip the default language
|
||||
if (DEFAULT_LANGUAGE.equals(body.getLanguage()) || body.getLanguage() == null) {
|
||||
continue;
|
||||
}
|
||||
buf.append("<body xml:lang=\"").append(body.getLanguage()).append("\">");
|
||||
buf.append(StringUtils.escapeForXML(body.getMessage()));
|
||||
buf.append("</body>");
|
||||
}
|
||||
if (thread != null) {
|
||||
buf.append("<thread>").append(thread).append("</thread>");
|
||||
|
@ -203,6 +339,102 @@ public class Message extends Packet {
|
|||
return buf.toString();
|
||||
}
|
||||
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Message message = (Message) o;
|
||||
|
||||
if(!super.equals(message)) { return false; }
|
||||
if (bodies.size() != message.bodies.size() || !bodies.containsAll(message.bodies)) {
|
||||
return false;
|
||||
}
|
||||
if (language != null ? !language.equals(message.language) : message.language != null) {
|
||||
return false;
|
||||
}
|
||||
if (subject != null ? !subject.equals(message.subject) : message.subject != null) {
|
||||
return false;
|
||||
}
|
||||
if (thread != null ? !thread.equals(message.thread) : message.thread != null) {
|
||||
return false;
|
||||
}
|
||||
return type == message.type;
|
||||
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
int result;
|
||||
result = (type != null ? type.hashCode() : 0);
|
||||
result = 31 * result + (subject != null ? subject.hashCode() : 0);
|
||||
result = 31 * result + (thread != null ? thread.hashCode() : 0);
|
||||
result = 31 * result + (language != null ? language.hashCode() : 0);
|
||||
result = 31 * result + bodies.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a message body, its language and the content of the message.
|
||||
*/
|
||||
public static class Body {
|
||||
|
||||
private String message;
|
||||
private String langauge;
|
||||
|
||||
private Body(String language, String message) {
|
||||
if (message == null) {
|
||||
throw new NullPointerException("Message cannot be null.");
|
||||
}
|
||||
this.langauge = language;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the language of this message body. If the language is null, then, no language
|
||||
* was specified.
|
||||
*
|
||||
* @return the language of this message body.
|
||||
*/
|
||||
public String getLanguage() {
|
||||
if (DEFAULT_LANGUAGE.equals(langauge)) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return langauge;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message content.
|
||||
*
|
||||
* @return the content of the message.
|
||||
*/
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) { return false; }
|
||||
|
||||
Body body = (Body) o;
|
||||
|
||||
if (langauge != null ? !langauge.equals(body.langauge) : body.langauge != null) {
|
||||
return false;
|
||||
}
|
||||
return message.equals(body.message);
|
||||
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
int result;
|
||||
result = message.hashCode();
|
||||
result = 31 * result + (langauge != null ? langauge.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the type of a message.
|
||||
*/
|
||||
|
|
|
@ -42,6 +42,11 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
|||
*/
|
||||
public abstract class Packet {
|
||||
|
||||
protected static final String DEFAULT_LANGUAGE =
|
||||
java.util.Locale.getDefault().getLanguage().toLowerCase();
|
||||
|
||||
private static String DEFAULT_XML_NS = null;
|
||||
|
||||
/**
|
||||
* Constant used as packetID to indicate that a packet has no id. To indicate that a packet
|
||||
* has no id set this constant as the packet's id. When the packet is asked for its id the
|
||||
|
@ -60,23 +65,29 @@ public abstract class Packet {
|
|||
*/
|
||||
private static long id = 0;
|
||||
|
||||
private String xmlns = DEFAULT_XML_NS;
|
||||
|
||||
/**
|
||||
* Returns the next unique id. Each id made up of a short alphanumeric
|
||||
* prefix along with a unique numeric value.
|
||||
*
|
||||
* @return the next id.
|
||||
*/
|
||||
private static synchronized String nextID() {
|
||||
public static synchronized String nextID() {
|
||||
return prefix + Long.toString(id++);
|
||||
}
|
||||
|
||||
public static void setDefaultXmlns(String defaultXmlns) {
|
||||
DEFAULT_XML_NS = defaultXmlns;
|
||||
}
|
||||
|
||||
private String packetID = null;
|
||||
private String to = null;
|
||||
private String from = null;
|
||||
private final List<PacketExtension> packetExtensions
|
||||
= new CopyOnWriteArrayList<PacketExtension>();
|
||||
|
||||
private Map<String,Object> properties = null;
|
||||
private final Map<String,Object> properties = null;
|
||||
private XMPPError error = null;
|
||||
|
||||
/**
|
||||
|
@ -280,9 +291,6 @@ public abstract class Packet {
|
|||
if (!(value instanceof Serializable)) {
|
||||
throw new IllegalArgumentException("Value must be serialiazble");
|
||||
}
|
||||
if (properties == null) {
|
||||
properties = new HashMap<String, Object>();
|
||||
}
|
||||
properties.put(name, value);
|
||||
}
|
||||
|
||||
|
@ -403,4 +411,52 @@ public abstract class Packet {
|
|||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public String getXmlns() {
|
||||
return this.xmlns;
|
||||
}
|
||||
|
||||
protected static String parseXMLLang(String language) {
|
||||
if (language == null || "".equals(language)) {
|
||||
language = DEFAULT_LANGUAGE;
|
||||
}
|
||||
return language;
|
||||
}
|
||||
|
||||
protected static String getDefaultLanguage() {
|
||||
return DEFAULT_LANGUAGE;
|
||||
}
|
||||
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Packet packet = (Packet) o;
|
||||
|
||||
if (error != null ? !error.equals(packet.error) : packet.error != null) { return false; }
|
||||
if (from != null ? !from.equals(packet.from) : packet.from != null) { return false; }
|
||||
if (!packetExtensions.equals(packet.packetExtensions)) { return false; }
|
||||
if (packetID != null ? !packetID.equals(packet.packetID) : packet.packetID != null) {
|
||||
return false;
|
||||
}
|
||||
if (properties != null ? !properties.equals(packet.properties)
|
||||
: packet.properties != null) {
|
||||
return false;
|
||||
}
|
||||
if (to != null ? !to.equals(packet.to) : packet.to != null) { return false; }
|
||||
return !(xmlns != null ? !xmlns.equals(packet.xmlns) : packet.xmlns != null);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
int result;
|
||||
result = (xmlns != null ? xmlns.hashCode() : 0);
|
||||
result = 31 * result + (packetID != null ? packetID.hashCode() : 0);
|
||||
result = 31 * result + (to != null ? to.hashCode() : 0);
|
||||
result = 31 * result + (from != null ? from.hashCode() : 0);
|
||||
result = 31 * result + packetExtensions.hashCode();
|
||||
result = 31 * result + properties.hashCode();
|
||||
result = 31 * result + (error != null ? error.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -62,6 +62,7 @@ public class Presence extends Packet {
|
|||
private String status = null;
|
||||
private int priority = Integer.MIN_VALUE;
|
||||
private Mode mode = null;
|
||||
private String language;
|
||||
|
||||
/**
|
||||
* Creates a new presence update. Status, priority, and mode are left un-set.
|
||||
|
@ -69,7 +70,7 @@ public class Presence extends Packet {
|
|||
* @param type the type.
|
||||
*/
|
||||
public Presence(Type type) {
|
||||
this.type = type;
|
||||
setType(type);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -81,10 +82,10 @@ public class Presence extends Packet {
|
|||
* @param mode the mode type for this presence update.
|
||||
*/
|
||||
public Presence(Type type, String status, int priority, Mode mode) {
|
||||
this.type = type;
|
||||
this.status = status;
|
||||
this.priority = priority;
|
||||
this.mode = mode;
|
||||
setType(type);
|
||||
setStatus(status);
|
||||
setPriority(priority);
|
||||
setMode(mode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -128,6 +129,9 @@ public class Presence extends Packet {
|
|||
* @param type the type of the presence packet.
|
||||
*/
|
||||
public void setType(Type type) {
|
||||
if(type == null) {
|
||||
throw new NullPointerException("Type cannot be null");
|
||||
}
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
|
@ -196,9 +200,35 @@ public class Presence extends Packet {
|
|||
this.mode = mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the xml:lang of this Presence, or null if one has not been set.
|
||||
*
|
||||
* @return the xml:lang of this Presence, or null if one has not been set.
|
||||
* @since 3.0.2
|
||||
*/
|
||||
private String getLanguage() {
|
||||
return language;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the xml:lang of this Presence.
|
||||
*
|
||||
* @param language the xml:lang of this Presence.
|
||||
* @since 3.0.2
|
||||
*/
|
||||
public void setLanguage(String language) {
|
||||
this.language = language;
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<presence");
|
||||
if(getXmlns() != null) {
|
||||
buf.append(" xmlns=\"").append(getXmlns()).append("\"");
|
||||
}
|
||||
if (language != null) {
|
||||
buf.append(" xml:lang=\"").append(getLanguage()).append("\"");
|
||||
}
|
||||
if (getPacketID() != null) {
|
||||
buf.append(" id=\"").append(getPacketID()).append("\"");
|
||||
}
|
||||
|
@ -241,8 +271,8 @@ public class Presence extends Packet {
|
|||
if (mode != null) {
|
||||
buf.append(": ").append(mode);
|
||||
}
|
||||
if (status != null) {
|
||||
buf.append(" (").append(status).append(")");
|
||||
if (getStatus() != null) {
|
||||
buf.append(" (").append(getStatus()).append(")");
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
|
|
@ -61,13 +61,17 @@ public class PacketParserUtils {
|
|||
message.setTo(parser.getAttributeValue("", "to"));
|
||||
message.setFrom(parser.getAttributeValue("", "from"));
|
||||
message.setType(Message.Type.fromString(parser.getAttributeValue("", "type")));
|
||||
String language = getLanguageAttribute(parser);
|
||||
if (language != null && !"".equals(language.trim())) {
|
||||
message.setLanguage(language);
|
||||
}
|
||||
|
||||
// Parse sub-elements. We include extra logic to make sure the values
|
||||
// are only read once. This is because it's possible for the names to appear
|
||||
// in arbitrary sub-elements.
|
||||
boolean done = false;
|
||||
String subject = null;
|
||||
String body = null;
|
||||
String body;
|
||||
String thread = null;
|
||||
Map<String, Object> properties = null;
|
||||
while (!done) {
|
||||
|
@ -81,9 +85,9 @@ public class PacketParserUtils {
|
|||
}
|
||||
}
|
||||
else if (elementName.equals("body")) {
|
||||
if (body == null) {
|
||||
body = parser.nextText();
|
||||
}
|
||||
String xmlLang = getLanguageAttribute(parser);
|
||||
body = parser.nextText();
|
||||
message.addBody(xmlLang, body);
|
||||
}
|
||||
else if (elementName.equals("thread")) {
|
||||
if (thread == null) {
|
||||
|
@ -111,7 +115,6 @@ public class PacketParserUtils {
|
|||
}
|
||||
}
|
||||
message.setSubject(subject);
|
||||
message.setBody(body);
|
||||
message.setThread(thread);
|
||||
// Set packet properties.
|
||||
if (properties != null) {
|
||||
|
@ -140,13 +143,18 @@ public class PacketParserUtils {
|
|||
System.err.println("Found invalid presence type " + typeString);
|
||||
}
|
||||
}
|
||||
|
||||
Presence presence = new Presence(type);
|
||||
presence.setTo(parser.getAttributeValue("", "to"));
|
||||
presence.setFrom(parser.getAttributeValue("", "from"));
|
||||
String id = parser.getAttributeValue("", "id");
|
||||
presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
|
||||
|
||||
String language = getLanguageAttribute(parser);
|
||||
if (language != null && !"".equals(language.trim())) {
|
||||
presence.setLanguage(language);
|
||||
}
|
||||
presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
|
||||
|
||||
// Parse sub-elements
|
||||
boolean done = false;
|
||||
while (!done) {
|
||||
|
@ -403,6 +411,18 @@ public class PacketParserUtils {
|
|||
return extension;
|
||||
}
|
||||
|
||||
private static String getLanguageAttribute(XmlPullParser parser) {
|
||||
for (int i = 0; i < parser.getAttributeCount(); i++) {
|
||||
String attributeName = parser.getAttributeName(i);
|
||||
if ( "xml:lang".equals(attributeName) ||
|
||||
("lang".equals(attributeName) &&
|
||||
"xml".equals(parser.getAttributePrefix(i)))) {
|
||||
return parser.getAttributeValue(i);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Object parseWithIntrospection(String elementName,
|
||||
Class objectClass, XmlPullParser parser) throws Exception
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue