1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-25 21:42:07 +01:00

Make <body/> (XML) language optional

This commit is contained in:
Florian Schmaus 2018-05-09 16:47:28 +02:00
parent ad932caf78
commit 4b8f7ca6b6
2 changed files with 16 additions and 17 deletions

View file

@ -315,8 +315,8 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
private Body getMessageBody(String language) { private Body getMessageBody(String language) {
language = determineLanguage(language); language = determineLanguage(language);
for (Body body : bodies) { for (Body body : getBodies()) {
if (language.equals(body.language)) { if (Objects.equals(language, body.language) || (language != null && language.equals(this.language) && body.language == null)) {
return body; return body;
} }
} }
@ -388,9 +388,11 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
*/ */
public boolean removeBody(String language) { public boolean removeBody(String language) {
language = determineLanguage(language); language = determineLanguage(language);
for (Body body : bodies) { for (Body body : getBodies()) {
if (language.equals(body.language)) { String bodyLanguage = body.getLanguage();
return bodies.remove(body); if (Objects.equals(bodyLanguage, language)) {
removeExtension(body);
return true;
} }
} }
return false; return false;
@ -650,9 +652,6 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
} }
public Body(String language, String message, BodyElementNamespace namespace) { public Body(String language, String message, BodyElementNamespace namespace) {
if (language == null) {
throw new NullPointerException("Language cannot be null.");
}
if (message == null) { if (message == null) {
throw new NullPointerException("Message cannot be null."); throw new NullPointerException("Message cannot be null.");
} }
@ -662,9 +661,10 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
} }
/** /**
* Returns the language of this message body. * Returns the language of this message body or {@code null} if the body extension element does not explicitly
* set a language, but instead inherits it from the outer element (usually a {@link Message} stanza).
* *
* @return the language of this message body. * @return the language of this message body or {@code null}.
*/ */
public String getLanguage() { public String getLanguage() {
return language; return language;
@ -683,7 +683,9 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + this.language.hashCode(); if (language != null) {
result = prime * result + this.language.hashCode();
}
result = prime * result + this.message.hashCode(); result = prime * result + this.message.hashCode();
return result; return result;
} }
@ -701,7 +703,7 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
} }
Body other = (Body) obj; Body other = (Body) obj;
// simplified comparison because language and message are always set // simplified comparison because language and message are always set
return this.language.equals(other.language) && this.message.equals(other.message); return Objects.equals(this.language, other.language) && this.message.equals(other.message);
} }
@Override @Override
@ -717,7 +719,7 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
@Override @Override
public XmlStringBuilder toXML(String enclosingNamespace) { public XmlStringBuilder toXML(String enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.xmllangAttribute(getLanguage()).rightAngleBracket(); xml.optXmlLangAttribute(getLanguage()).rightAngleBracket();
xml.escape(message); xml.escape(message);
xml.closeElement(getElementName()); xml.closeElement(getElementName());
return xml; return xml;

View file

@ -28,11 +28,8 @@ public class BodyElementProvider extends ExtensionElementProvider<Message.Body>
@Override @Override
public Message.Body parse(XmlPullParser parser, int initialDepth) throws Exception { public Message.Body parse(XmlPullParser parser, int initialDepth) throws Exception {
String xmlLang = ParserUtils.getXmlLang(parser); String xmlLang = ParserUtils.getXmlLang(parser);
if (xmlLang == null) {
xmlLang = "en";
}
String body = parseElementText(parser); String body = parseElementText(parser);
return new Message.Body(xmlLang, body); return new Message.Body(xmlLang, body);
} }
} }