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 Element body = new Element("body");
46 body.setContent(text);
47 this.children.add(body);
48 }
49
50 public void setType(int type) {
51 switch (type) {
52 case TYPE_CHAT:
53 this.setAttribute("type","chat");
54 break;
55 case TYPE_GROUPCHAT:
56 this.setAttribute("type", "groupchat");
57 break;
58 default:
59 this.setAttribute("type","chat");
60 break;
61 }
62 }
63
64 public int getType() {
65 String type = getAttribute("type");
66 if (type==null) {
67 return TYPE_NO;
68 }
69 if (type.equals("chat")) {
70 return TYPE_CHAT;
71 } else if (type.equals("groupchat")) {
72 return TYPE_GROUPCHAT;
73 } else {
74 return TYPE_UNKNOWN;
75 }
76 }
77}