From 90c0064394918bf8d25ade2ba531e93c930cc2bc Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 3 Sep 2014 18:11:32 +0200 Subject: [PATCH] Add XmlLangStanza, to share the language field between Message and Presence. --- .../jivesoftware/smack/packet/Message.java | 25 +---------- .../jivesoftware/smack/packet/Presence.java | 25 +---------- .../smack/packet/XmlLangStanza.java | 45 +++++++++++++++++++ 3 files changed, 49 insertions(+), 46 deletions(-) create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/packet/XmlLangStanza.java diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java index 9bbfce8fa..d5f74c342 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java @@ -51,13 +51,12 @@ import org.jivesoftware.smack.util.XmlStringBuilder; * * @author Matt Tucker */ -public class Message extends Packet { +public class Message extends XmlLangStanza { public static final String BODY = "body"; private Type type = Type.normal; private String thread = null; - private String language; private final Set subjects = new HashSet(); private final Set bodies = new HashSet(); @@ -374,26 +373,6 @@ 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 - */ - public 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; - } - private String determineLanguage(String language) { // empty string is passed by #setSubject() and #setBody() and is the same as null @@ -416,7 +395,7 @@ public class Message extends Packet { public XmlStringBuilder toXML() { XmlStringBuilder buf = new XmlStringBuilder(); buf.halfOpenElement("message"); - buf.xmllangAttribute(getLanguage()); + buf.xmllangAttribute(language); addCommonAttributes(buf); if (type != Type.normal) { buf.attribute("type", type); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java index a9df24971..198d8e44d 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java @@ -55,7 +55,7 @@ import org.jivesoftware.smack.util.XmlStringBuilder; * @see RosterPacket * @author Matt Tucker */ -public class Presence extends Packet { +public class Presence extends XmlLangStanza { public static final String ELEMENT = "presence"; @@ -63,7 +63,6 @@ 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. @@ -204,31 +203,11 @@ 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 - */ - public 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; - } - @Override public XmlStringBuilder toXML() { XmlStringBuilder buf = new XmlStringBuilder(); buf.halfOpenElement(ELEMENT); - buf.xmllangAttribute(getLanguage()); + buf.xmllangAttribute(language); addCommonAttributes(buf); if (type != Type.available) { buf.attribute("type", type); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/XmlLangStanza.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/XmlLangStanza.java new file mode 100644 index 000000000..59be3b9cb --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/XmlLangStanza.java @@ -0,0 +1,45 @@ +/** + * + * Copyright © 2014 Florian Schmaus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smack.packet; + + +/** + * Abstract class for stanzas that can contain a 'xml:lang' definition in their + * top level element, ie. Message and Presence stanzas. + */ +public abstract class XmlLangStanza extends Packet { + protected String language; + + /** + * Returns the xml:lang of this Stanza, or null if one has not been set. + * + * @return the xml:lang of this Stanza, or null. + */ + public String getLanguage() { + return language; + } + + /** + * Sets the xml:lang of this Stanza. + * + * @param language the xml:lang of this Stanza. + */ + public void setLanguage(String language) { + this.language = language; + } + +}