MessagePacket.java

 1package eu.siacs.conversations.xmpp;
 2
 3import eu.siacs.conversations.xml.Element;
 4
 5public class MessagePacket extends Element {
 6	public static final int TYPE_CHAT = 0;
 7	public static final int TYPE_UNKNOWN = 1;
 8	public static final int TYPE_NO = 2;
 9	public static final int TYPE_GROUPCHAT = 3;
10	public static final int TYPE_ERROR = 4;
11
12	private MessagePacket(String name) {
13		super(name);
14	}
15	
16	public MessagePacket() {
17		super("message");
18	}
19	
20	public String getTo() {
21		return getAttribute("to");
22	}
23
24	public String getFrom() {
25		return getAttribute("from");
26	}
27	
28	public String getBody() {
29		Element body = this.findChild("body");
30		if (body!=null) {
31			return body.getContent();
32		} else {
33			return null;
34		}
35	}
36	
37	public void setTo(String to) {
38		setAttribute("to", to);
39	}
40	
41	public void setFrom(String from) {
42		setAttribute("from",from);
43	}
44	
45	public void setBody(String text) {
46		this.children.remove(findChild("body"));
47		Element body = new Element("body");
48		body.setContent(text);
49		this.children.add(body);
50	}
51
52	public void setType(int type) {
53		switch (type) {
54		case TYPE_CHAT:
55			this.setAttribute("type","chat");
56			break;
57		case TYPE_GROUPCHAT:
58			this.setAttribute("type", "groupchat");
59			break;
60		default:
61			this.setAttribute("type","chat");
62			break;
63		}
64	}
65	
66	public int getType() {
67		String type = getAttribute("type");
68		if (type==null) {
69			return TYPE_NO;
70		}
71		if (type.equals("chat")) {
72			return TYPE_CHAT;
73		} else if (type.equals("groupchat")) {
74			return TYPE_GROUPCHAT;
75		} else if (type.equals("error")) {
76			return TYPE_ERROR;
77		} else {
78			return TYPE_UNKNOWN;
79		}
80	}
81}