IqPacket.java

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