1package im.conversations.android.xmpp.model.stanza;
2
3import eu.siacs.conversations.xml.Element;
4import eu.siacs.conversations.xml.LocalizedContent;
5
6import im.conversations.android.annotation.XmlElement;
7import im.conversations.android.xmpp.model.jabber.Body;
8
9import java.util.Locale;
10
11@XmlElement
12public class Message extends Stanza {
13
14 public Message() {
15 super(Message.class);
16 }
17
18 public Message(Type type) {
19 this();
20 this.setType(type);
21 }
22
23 public LocalizedContent getBody() {
24 return findInternationalizedChildContentInDefaultNamespace("body");
25 }
26
27 public Type getType() {
28 final var value = this.getAttribute("type");
29 if (value == null) {
30 return Type.NORMAL;
31 } else {
32 try {
33 return Type.valueOf(value.toUpperCase(Locale.ROOT));
34 } catch (final IllegalArgumentException e) {
35 return null;
36 }
37 }
38 }
39
40 public void setType(final Type type) {
41 if (type == null || type == Type.NORMAL) {
42 this.removeAttribute("type");
43 } else {
44 this.setAttribute("type", type.toString().toLowerCase(Locale.ROOT));
45 }
46 }
47
48 public void setBody(final String text) {
49 removeChild(findChild("body"));
50 this.addExtension(new Body(text));
51 }
52
53 public void setAxolotlMessage(Element axolotlMessage) {
54 removeChild(findChild("body"));
55 prependChild(axolotlMessage);
56 }
57
58 public enum Type {
59 ERROR,
60 NORMAL,
61 GROUPCHAT,
62 HEADLINE,
63 CHAT
64 }
65}