IqPacket.java

 1package eu.siacs.conversations.xmpp.stanzas;
 2
 3import eu.siacs.conversations.xml.Element;
 4
 5
 6public class IqPacket extends AbstractStanza {
 7	
 8	public static final int TYPE_ERROR = -1;
 9	public static final int TYPE_SET = 0;
10	public static final int TYPE_RESULT = 1;
11	public static final int TYPE_GET = 2;
12
13	private IqPacket(String name) {
14		super(name);
15	}
16
17	public IqPacket(int type) {
18		super("iq");
19		switch (type) {
20		case TYPE_SET:
21			this.setAttribute("type", "set");
22			break;
23		case TYPE_GET:
24			this.setAttribute("type", "get");
25			break;
26		case TYPE_RESULT:
27			this.setAttribute("type", "result");
28			break;
29		default:
30			break;
31		}
32	}
33	
34	public IqPacket() {
35		super("iq");
36	}
37	
38	public Element query() {
39		Element query = findChild("query");
40		if (query==null) {
41			query = addChild("query");
42		}
43		return query;
44	}
45	
46	public Element query(String xmlns) {
47		Element query = query();
48		query.setAttribute("xmlns", xmlns);
49		return query();
50	}
51	
52	public int getType() {
53		String type = getAttribute("type");
54		if ("error".equals(type)) {
55			return TYPE_ERROR;
56		} else if ("result".equals(type)) {
57			return TYPE_RESULT;
58		} else if ("set".equals(type)) {
59			return TYPE_SET;
60		} else if ("get".equals(type)) {
61			return TYPE_GET;
62		} else {
63			return 1000;
64		}
65	}
66
67}