📄 iq.java
字号:
package org.xmpp.packet;
import org.dom4j.Element;
import org.dom4j.QName;
import java.lang.reflect.Constructor;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
/**
* IQ (Info/Query) packet. IQ packets are used to get and set information
* on the server, including authentication, roster operations, and creating
* accounts. Each IQ packet has a specific type that indicates what type of action
* is being taken: "get", "set", "result", or "error".<p>
*
* IQ packets can contain a single child element that exists in a extended XML
* namespace.
*/
public class IQ extends Packet {
// Sequence and random number generator used for creating unique ID's.
private static int sequence = 0;
private static Random random = new Random();
/**
* Constructs a new IQ with an automatically generated ID and a type
* of {@link Type#get IQ.Type.get}.
*/
public IQ() {
this.element = docFactory.createDocument().addElement("iq");
String id = String.valueOf(random.nextInt(1000) + "-" + sequence++);
setType(Type.get);
setID(id);
}
/**
* Constructs a new IQ using the specified type. A packet ID will
* be automatically generated.
*
* @param type the IQ type.
*/
public IQ(Type type) {
this.element = docFactory.createDocument().addElement("iq");
setType(type);
String id = String.valueOf(random.nextInt(1000) + "-" + sequence++);
setID(id);
}
/**
* Constructs a new IQ using the specified type and ID.
*
* @param ID the packet ID of the IQ.
* @param type the IQ type.
*/
public IQ(Type type, String ID) {
this.element = docFactory.createDocument().addElement("iq");
setType(type);
setID(ID);
}
/**
* Constructs a new IQ using an existing Element. This is useful
* for parsing incoming IQ Elements into IQ objects.
*
* @param element the IQ Element.
*/
public IQ(Element element) {
super(element);
}
/**
* Constructs a new IQ using an existing Element. This is useful
* for parsing incoming IQ Elements into IQ objects. Stringprep validation
* on the TO address can be disabled. The FROM address will not be validated since the
* server is the one that sets that value.
*
* @param element the IQ Element.
* @param skipValidation true if stringprep should not be applied to the TO address.
*/
public IQ(Element element, boolean skipValidation) {
super(element, skipValidation);
}
/**
* Constructs a new IQ that is a copy of an existing IQ.
*
* @param iq the iq packet.
* @see #createCopy()
*/
private IQ(IQ iq) {
Element elementCopy = iq.element.createCopy();
docFactory.createDocument().add(elementCopy);
this.element = elementCopy;
// Copy cached JIDs (for performance reasons)
this.toJID = iq.toJID;
this.fromJID = iq.fromJID;
}
/**
* Returns the type of this IQ.
*
* @return the IQ type.
* @see Type
*/
public Type getType() {
String type = element.attributeValue("type");
if (type != null) {
return Type.valueOf(type);
}
else {
return null;
}
}
/**
* Sets the type of this IQ.
*
* @param type the IQ type.
* @see Type
*/
public void setType(Type type) {
element.addAttribute("type", type==null?null:type.toString());
}
/**
* Convenience routine to indicate if this is a request stanza. (get or set)
*
* @return True or false if this is a request stanza
*/
public boolean isRequest() {
Type type = getType();
return (type != null && (type.equals(Type.get) || type.equals(Type.set)));
}
/**
* Convenience routine to indicate if this is a response stanza. (result or error)
*
* @return True or false if this is a response stanza
*/
public boolean isResponse() {
Type type = getType();
return (type != null && (type.equals(Type.result) || type.equals(Type.error)));
}
/**
* Returns the child element of this IQ. IQ packets may have a single child
* element in an extended namespace. This is a convenience method to
* avoid manipulating the underlying packet's Element instance directly.<p>
*
* An IQ child element in extended namespaces is used to extend the features
* of XMPP. Although any valid XML can be included in a child element
* in an extended namespace, many common features have been standardized
* as <a href="http://www.jabber.org/jeps">Jabber Enhancement Proposals</a>
* (JEPs).
*
* @return the child element.
*/
public Element getChildElement() {
List elements = element.elements();
if (elements.isEmpty()) {
return null;
}
else {
// Search for a child element that is in a different namespace.
for (int i=0; i<elements.size(); i++) {
Element element = (Element)elements.get(i);
String namespace = element.getNamespaceURI();
if (!namespace.equals("") && !namespace.equals("jabber:client") &&
!namespace.equals("jabber:server"))
{
return element;
}
}
return null;
}
}
/**
* Sets the child element of this IQ. IQ packets may have a single child
* element in an extended namespace. This is a convenience method to
* avoid manipulating this underlying packet's Element instance directly.<p>
*
* A sample use of this method might look like the following:
* <pre>
* IQ iq = new IQ("time_1");
* iq.setTo("mary@example.com");
* iq.setType(IQ.Type.GET);
* iq.setChildElement(docFactory.createElement("query", "jabber:iq:time"));</pre><p>
*
* An IQ child element in extended namespaces is used to extend the features
* of XMPP. Although any valid XML can be included in a child element
* in an extended namespace, many common features have been standardized
* as <a href="http://www.jabber.org/jeps">Jabber Enhancement Proposals</a>
* (JEPs).
*
* @param childElement the child element.
*/
public void setChildElement(Element childElement) {
for (Iterator i=element.elementIterator(); i.hasNext(); ) {
element.remove((Element)i.next());
}
element.add(childElement);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -