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		case TYPE_ERROR:
30			this.setAttribute("type", "error");
31			break;
32		default:
33			break;
34		}
35	}
36	
37	public IqPacket() {
38		super("iq");
39	}
40	
41	public Element query() {
42		Element query = findChild("query");
43		if (query==null) {
44			query = addChild("query");
45		}
46		return query;
47	}
48	
49	public Element query(String xmlns) {
50		Element query = query();
51		query.setAttribute("xmlns", xmlns);
52		return query();
53	}
54	
55	public int getType() {
56		String type = getAttribute("type");
57		if ("error".equals(type)) {
58			return TYPE_ERROR;
59		} else if ("result".equals(type)) {
60			return TYPE_RESULT;
61		} else if ("set".equals(type)) {
62			return TYPE_SET;
63		} else if ("get".equals(type)) {
64			return TYPE_GET;
65		} else {
66			return 1000;
67		}
68	}
69	
70	public IqPacket generateRespone(int type) {
71		IqPacket packet = new IqPacket(type);
72		packet.setTo(this.getFrom());
73		packet.setId(this.getId());
74		return packet;
75	}
76
77}