public abstract class IQ extends Stanza
IQ packets can contain a single child element that exists in a specific XML namespace. The combination of the element name and namespace determines what type of IQ stanza(/packet) it is. Some example IQ subpacket snippets:
Modifier and Type | Class and Description |
---|---|
static class |
IQ.IQChildElementXmlStringBuilder |
static class |
IQ.Type
A enum to represent the type of the IQ stanza.
|
Modifier and Type | Field and Description |
---|---|
static String |
IQ_ELEMENT |
static String |
QUERY_ELEMENT |
DEFAULT_LANGUAGE, ITEM, language, TEXT
Modifier | Constructor and Description |
---|---|
|
IQ(IQ iq) |
protected |
IQ(String childElementName) |
protected |
IQ(String childElementName,
String childElementNamespace) |
Modifier and Type | Method and Description |
---|---|
static ErrorIQ |
createErrorResponse(IQ request,
XMPPError error)
|
static IQ |
createResultIQ(IQ request)
Convenience method to create a new empty
IQ.Type.result
IQ based on a IQ.Type.get or IQ.Type.set
IQ. |
String |
getChildElementName() |
String |
getChildElementNamespace() |
XmlStringBuilder |
getChildElementXML()
Returns the sub-element XML section of the IQ packet, or the empty String if there
isn't one.
|
protected abstract IQ.IQChildElementXmlStringBuilder |
getIQChildElementBuilder(IQ.IQChildElementXmlStringBuilder xml)
This method must be overwritten by IQ subclasses to create their child content.
|
IQ.Type |
getType()
Returns the type of the IQ packet.
|
boolean |
isRequestIQ()
Return true if this IQ is a request IQ, i.e.
|
void |
setType(IQ.Type type)
Sets the type of the IQ packet.
|
XmlStringBuilder |
toXML()
Returns the XML representation of this Element.
|
addCommonAttributes, addExtension, addExtensions, appendErrorIfExists, getDefaultLanguage, getError, getExtension, getExtension, getExtensions, getExtensions, getExtensionsXML, getFrom, getLanguage, getPacketID, getStanzaId, getTo, hasExtension, hasExtension, hasStanzaIdSet, overrideExtension, removeExtension, removeExtension, setError, setFrom, setLanguage, setPacketID, setStanzaId, setTo, toString
public static final String IQ_ELEMENT
public static final String QUERY_ELEMENT
public IQ.Type getType()
public void setType(IQ.Type type)
Since the type of an IQ must present, an IllegalArgmentException will be thrown when type is
null
.
type
- the type of the IQ packet.public boolean isRequestIQ()
IQ.Type.get
or IQ.Type.set
.public final String getChildElementName()
public final String getChildElementNamespace()
public final XmlStringBuilder toXML()
Element
public final XmlStringBuilder getChildElementXML()
protected abstract IQ.IQChildElementXmlStringBuilder getIQChildElementBuilder(IQ.IQChildElementXmlStringBuilder xml)
For example to create an IQ with a extra attribute and an additional child element
<iq to='foo@example.org' id='123'>
<bar xmlns='example:bar' extraAttribute='blaz'>
<extraElement>elementText</extraElement>
</bar>
</iq>
the body of the getIQChildElementBuilder
looks like
// The builder 'xml' will already have the child element and the 'xmlns' attribute added
// So the current builder state is "<bar xmlns='example:bar'"
xml.attribute("extraAttribute", "blaz");
xml.rightAngleBracket();
xml.element("extraElement", "elementText");
// Do not close the 'bar' attribute by calling xml.closeElement('bar')
If your IQ only contains attributes and no child elements, i.e. it can be represented as empty element, then you
can mark it as such.
xml.attribute("myAttribute", "myAttributeValue"); xml.setEmptyElement();If your IQ does not contain any attributes or child elements (besides stanza(/packet) extensions), consider sub-classing
SimpleIQ
instead.xml
- a pre-created builder which already has the child element and the 'xmlns' attribute set.public static IQ createResultIQ(IQ request)
IQ.Type.result
IQ based on a IQ.Type.get
or IQ.Type.set
IQ. The new stanza(/packet) will be initialized with:IQ.Type.result
.
request
- the IQ.Type.get
or IQ.Type.set
IQ packet.IQ.Type.result
IQ based on the originating IQ.IllegalArgumentException
- if the IQ stanza(/packet) does not have a type of
IQ.Type.get
or IQ.Type.set
.public static ErrorIQ createErrorResponse(IQ request, XMPPError error)
IQ.Type.error
IQ
based on a IQ.Type.get
or IQ.Type.set
IQ. The new stanza(/packet) will be initialized with:IQ.Type.error
.
XMPPError
.
request
- the IQ.Type.get
or IQ.Type.set
IQ packet.error
- the error to associate with the created IQ packet.IQ.Type.error
IQ based on the originating IQ.IllegalArgumentException
- if the IQ stanza(/packet) does not have a type of
IQ.Type.get
or IQ.Type.set
.