public abstract class Stanza extends java.lang.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 java.lang.String |
DEFAULT_LANGUAGE |
static java.lang.String |
ITEM |
protected java.lang.String |
language
Optional value of the 'xml:lang' attribute of the outermost element of
the stanza.
|
static java.lang.String |
TEXT |
Modifier | Constructor and Description |
---|---|
protected |
Stanza() |
protected |
Stanza(Stanza p) |
protected |
Stanza(java.lang.String stanzaId) |
Modifier and Type | Method and Description |
---|---|
protected void |
addCommonAttributes(XmlStringBuilder xml)
Add to, from, id and 'xml:lang' attributes
|
void |
addExtension(ExtensionElement extension)
Adds a stanza(/packet) extension to the packet.
|
void |
addExtensions(java.util.Collection<ExtensionElement> extensions)
Adds a collection of stanza(/packet) extensions to the packet.
|
protected void |
appendErrorIfExists(XmlStringBuilder xml)
Append an XMPPError is this stanza(/packet) has one set.
|
static java.lang.String |
getDefaultLanguage()
Returns the default language used for all messages containing localized content.
|
XMPPError |
getError()
Returns the error associated with this packet, or null if there are
no errors.
|
ExtensionElement |
getExtension(java.lang.String namespace)
Returns the first extension of this stanza(/packet) that has the given namespace.
|
<PE extends ExtensionElement> |
getExtension(java.lang.String elementName,
java.lang.String namespace)
Returns the first extension that matches the specified element name and
namespace, or null if it doesn't exist.
|
java.util.List<ExtensionElement> |
getExtensions()
Returns a list of all extension elements of this stanza.
|
java.util.List<ExtensionElement> |
getExtensions(java.lang.String elementName,
java.lang.String namespace)
Return a list of all extensions with the given element name and namespace.
|
protected XmlStringBuilder |
getExtensionsXML()
Returns the extension sub-packets (including properties data) as an XML
String, or the Empty String if there are no stanza(/packet) extensions.
|
Jid |
getFrom()
Returns who the stanza(/packet) is being sent "from" or null if
the value is not set.
|
java.lang.String |
getLanguage()
Returns the xml:lang of this Stanza, or null if one has not been set.
|
java.lang.String |
getPacketID()
Deprecated.
use
getStanzaId() instead. |
java.lang.String |
getStanzaId()
Returns the unique ID of the stanza.
|
Jid |
getTo()
Returns who the stanza(/packet) is being sent "to", or null if
the value is not set.
|
boolean |
hasExtension(java.lang.String namespace)
Check if a stanza(/packet) extension with the given namespace exists.
|
boolean |
hasExtension(java.lang.String elementName,
java.lang.String namespace)
Check if a stanza(/packet) extension with the given element and namespace exists.
|
boolean |
hasStanzaIdSet()
Check if this stanza has an ID set.
|
protected void |
logCommonAttributes(java.lang.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(/packet) extension from the packet.
|
ExtensionElement |
removeExtension(java.lang.String elementName,
java.lang.String namespace)
Remove the stanza(/packet) extension with the given elementName and namespace.
|
void |
setError(XMPPError.Builder xmppErrorBuilder)
Sets the error for this stanza.
|
void |
setError(XMPPError error)
Deprecated.
|
void |
setFrom(Jid from)
Sets who the packet is being sent "from".
|
void |
setFrom(java.lang.String from)
Deprecated.
use
setFrom(Jid) instead. |
void |
setLanguage(java.lang.String language)
Sets the xml:lang of this Stanza.
|
void |
setPacketID(java.lang.String packetID)
Deprecated.
use
setStanzaId(String) instead. |
java.lang.String |
setStanzaId()
Set the stanza id if none is set.
|
void |
setStanzaId(java.lang.String id)
Sets the unique ID of the packet.
|
void |
setTo(Jid to)
Sets who the packet is being sent "to".
|
void |
setTo(java.lang.String to)
Deprecated.
use
setTo(Jid) instead. |
abstract java.lang.String |
toString()
Returns a short String describing the Stanza.
|
public static final java.lang.String TEXT
public static final java.lang.String ITEM
protected static final java.lang.String DEFAULT_LANGUAGE
protected java.lang.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."
public java.lang.String getStanzaId()
null
.null
if the id is not available.@Deprecated public java.lang.String getPacketID()
getStanzaId()
instead.public void setStanzaId(java.lang.String id)
null
as the packet's id value.id
- the unique ID for the packet.@Deprecated public void setPacketID(java.lang.String packetID)
setStanzaId(String)
instead.packetID
- public boolean hasStanzaIdSet()
public java.lang.String setStanzaId()
public Jid getTo()
@Deprecated public void setTo(java.lang.String to)
setTo(Jid)
instead.to
- who the stanza(/packet) is being sent to.java.lang.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(java.lang.String from)
setFrom(Jid)
instead.from
- who the stanza(/packet) is being sent to.java.lang.IllegalArgumentException
- if from is not a valid JID String.public void setFrom(Jid from)
from
- who the packet is being sent to.public XMPPError getError()
@Deprecated public void setError(XMPPError error)
setError(org.jivesoftware.smack.packet.XMPPError.Builder)
instead.error
- the error to associate with this packet.public void setError(XMPPError.Builder xmppErrorBuilder)
xmppErrorBuilder
- the error to associate with this stanza.public java.lang.String getLanguage()
public void setLanguage(java.lang.String language)
language
- the xml:lang of this Stanza.public java.util.List<ExtensionElement> getExtensions()
public java.util.List<ExtensionElement> getExtensions(java.lang.String elementName, java.lang.String namespace)
Changes to the returned set will update the stanza(/packet) 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(java.lang.String namespace)
When possible, use getExtension(String,String)
instead.
namespace
- the namespace of the extension that is desired.public <PE extends ExtensionElement> PE getExtension(java.lang.String elementName, java.lang.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(/packet) extension.public ExtensionElement overrideExtension(ExtensionElement extension)
extension
- the extension element to add.null
if there are none.public void addExtensions(java.util.Collection<ExtensionElement> extensions)
extensions
- a collection of stanza(/packet) extensionspublic boolean hasExtension(java.lang.String elementName, java.lang.String namespace)
The argument elementName
may be null.
elementName
- namespace
- public boolean hasExtension(java.lang.String namespace)
namespace
- public ExtensionElement removeExtension(java.lang.String elementName, java.lang.String namespace)
elementName
- namespace
- public ExtensionElement removeExtension(ExtensionElement extension)
extension
- the stanza(/packet) extension to remove.public abstract java.lang.String toString()
toString
in class java.lang.Object
protected final XmlStringBuilder getExtensionsXML()
public static java.lang.String getDefaultLanguage()
protected void addCommonAttributes(XmlStringBuilder xml)
xml
- protected void logCommonAttributes(java.lang.StringBuilder sb)
protected void appendErrorIfExists(XmlStringBuilder xml)
xml
- the XmlStringBuilder to append the error to.