MessagePacket.java

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