public abstract class Stanza extends Object implements TopLevelStreamElement
Every stanza has a unique ID (which is automatically generated, but can be overridden). Stanza IDs are required for IQ stanzas and recommended for presence and message stanzas. Optionally, the "to" and "from" fields can be set.
XMPP Stanzas are Message
, IQ
and Presence
. Which therefore subclass this
class. If you think you need to subclass this class, then you are doing something wrong.
Modifier and Type | Field and Description |
---|---|
protected static String |
DEFAULT_LANGUAGE |
static String |
ITEM |
protected String |
language
Optional value of the 'xml:lang' attribute of the outermost element of
the stanza.
|
static String |
TEXT |
Modifier | Constructor and Description |
---|---|
protected |
Stanza() |
protected |
Stanza(Stanza p) |
protected |
Stanza(String stanzaId) |
Modifier and Type | Method and Description |
---|---|
protected String |
addCommonAttributes(XmlStringBuilder xml,
String enclosingNamespace)
Add to, from, id and 'xml:lang' attributes
|
void |
addExtension(ExtensionElement extension)
Adds a stanza extension to the packet.
|
void |
addExtensions(Collection<ExtensionElement> extensions)
Adds a collection of stanza extensions to the packet.
|
protected void |
appendErrorIfExists(XmlStringBuilder xml,
String enclosingNamespace)
Append an XMPPError is this stanza has one set.
|
static String |
getDefaultLanguage()
Returns the default language used for all messages containing localized content.
|
StanzaError |
getError()
Returns the error associated with this packet, or null if there are
no errors.
|
ExtensionElement |
getExtension(String namespace)
Returns the first extension of this stanza that has the given namespace.
|
<PE extends ExtensionElement> |
getExtension(String elementName,
String namespace)
Returns the first extension that matches the specified element name and
namespace, or null if it doesn't exist.
|
List<ExtensionElement> |
getExtensions()
Returns a list of all extension elements of this stanza.
|
List<ExtensionElement> |
getExtensions(String elementName,
String namespace)
Return a list of all extensions with the given element name and namespace.
|
Jid |
getFrom()
Returns who the stanza is being sent "from" or null if
the value is not set.
|
String |
getLanguage()
Returns the xml:lang of this Stanza, or null if one has not been set.
|
String |
getPacketID()
Deprecated.
use
getStanzaId() instead. |
String |
getStanzaId()
Returns the unique ID of the stanza.
|
Jid |
getTo()
Returns who the stanza is being sent "to", or null if
the value is not set.
|
boolean |
hasExtension(String namespace)
Check if a stanza extension with the given namespace exists.
|
boolean |
hasExtension(String elementName,
String namespace)
Check if a stanza extension with the given element and namespace exists.
|
boolean |
hasStanzaIdSet()
Check if this stanza has an ID set.
|
protected void |
logCommonAttributes(StringBuilder sb) |
ExtensionElement |
overrideExtension(ExtensionElement extension)
Add the given extension and override eventually existing extensions with the same name and
namespace.
|
ExtensionElement |
removeExtension(ExtensionElement extension)
Removes a stanza extension from the packet.
|
ExtensionElement |
removeExtension(String elementName,
String namespace)
Remove the stanza extension with the given elementName and namespace.
|
void |
setError(StanzaError.Builder xmppErrorBuilder)
Sets the error for this stanza.
|
void |
setError(StanzaError error)
Deprecated.
|
void |
setFrom(Jid from)
Sets who the packet is being sent "from".
|
void |
setFrom(String from)
Deprecated.
use
setFrom(Jid) instead. |
void |
setLanguage(String language)
Sets the xml:lang of this Stanza.
|
void |
setPacketID(String packetID)
Deprecated.
use
setStanzaId(String) instead. |
String |
setStanzaId()
Set the stanza id if none is set.
|
void |
setStanzaId(String id)
Sets the unique ID of the packet.
|
void |
setTo(Jid to)
Sets who the packet is being sent "to".
|
void |
setTo(String to)
Deprecated.
use
setTo(Jid) instead. |
abstract String |
toString()
Returns a short String describing the Stanza.
|
public static final String TEXT
public static final String ITEM
protected static final String DEFAULT_LANGUAGE
protected String language
Such an attribute is defined for all stanza types. For IQ, see for example XEP-50 3.7: "The requester SHOULD provide its locale information using the "xml:lang " attribute on either the <iq/> (RECOMMENDED) or <command/> element."
protected Stanza()
public String getStanzaId()
null
.null
if the id is not available.@Deprecated public String getPacketID()
getStanzaId()
instead.public void setStanzaId(String id)
null
as the packet's id value.id
- the unique ID for the packet.@Deprecated public void setPacketID(String packetID)
setStanzaId(String)
instead.packetID
- public boolean hasStanzaIdSet()
public String setStanzaId()
public Jid getTo()
@Deprecated public void setTo(String to)
setTo(Jid)
instead.to
- who the stanza is being sent to.IllegalArgumentException
- if to is not a valid JID String.public void setTo(Jid to)
to
- who the packet is being sent to.public Jid getFrom()
@Deprecated public void setFrom(String from)
setFrom(Jid)
instead.from
- who the stanza is being sent to.IllegalArgumentException
- if from is not a valid JID String.public void setFrom(Jid from)
from
- who the packet is being sent to.public StanzaError getError()
@Deprecated public void setError(StanzaError error)
setError(org.jivesoftware.smack.packet.StanzaError.Builder)
instead.error
- the error to associate with this packet.public void setError(StanzaError.Builder xmppErrorBuilder)
xmppErrorBuilder
- the error to associate with this stanza.public String getLanguage()
public void setLanguage(String language)
language
- the xml:lang of this Stanza.public List<ExtensionElement> getExtensions()
public List<ExtensionElement> getExtensions(String elementName, String namespace)
Changes to the returned set will update the stanza extensions, if the returned set is not the empty set.
elementName
- the element name, must not be null.namespace
- the namespace of the element(s), must not be null.public ExtensionElement getExtension(String namespace)
When possible, use getExtension(String,String)
instead.
namespace
- the namespace of the extension that is desired.public <PE extends ExtensionElement> PE getExtension(String elementName, String namespace)
PE
- type of the ExtensionElement.elementName
- the XML element name of the extension. (May be null)namespace
- the XML element namespace of the extension.public void addExtension(ExtensionElement extension)
extension
- a stanza extension.public ExtensionElement overrideExtension(ExtensionElement extension)
extension
- the extension element to add.null
if there are none.public void addExtensions(Collection<ExtensionElement> extensions)
extensions
- a collection of stanza extensionspublic boolean hasExtension(String elementName, String namespace)
The argument elementName
may be null.
elementName
- namespace
- public boolean hasExtension(String namespace)
namespace
- public ExtensionElement removeExtension(String elementName, String namespace)
elementName
- namespace
- public ExtensionElement removeExtension(ExtensionElement extension)
extension
- the stanza extension to remove.public abstract String toString()
public static String getDefaultLanguage()
protected String addCommonAttributes(XmlStringBuilder xml, String enclosingNamespace)
xml
- the XmlStringBuilder
.enclosingNamespace
- the enclosing XML namespace.protected void logCommonAttributes(StringBuilder sb)
protected void appendErrorIfExists(XmlStringBuilder xml, String enclosingNamespace)
xml
- the XmlStringBuilder to append the error to.