1 20 21 package org.jivesoftware.smack.packet; 22 23 import org.jivesoftware.smack.util.StringUtils; 24 25 49 public class Message extends Packet { 50 51 private Type type = Type.NORMAL; 52 private String subject = null; 53 private String body = null; 54 private String thread = null; 55 56 59 public Message() { 60 } 61 62 67 public Message(String to) { 68 if (to == null) { 69 throw new IllegalArgumentException ("Parameter cannot be null"); 70 } 71 setTo(to); 72 } 73 74 80 public Message(String to, Type type) { 81 if (to == null || type == null) { 82 throw new IllegalArgumentException ("Parameters cannot be null."); 83 } 84 setTo(to); 85 this.type = type; 86 } 87 88 93 public Type getType() { 94 return type; 95 } 96 97 102 public void setType(Type type) { 103 if (type == null) { 104 throw new IllegalArgumentException ("Type cannot be null."); 105 } 106 this.type = type; 107 } 108 109 115 public String getSubject() { 116 return subject; 117 } 118 119 125 public void setSubject(String subject) { 126 this.subject = subject; 127 } 128 129 135 public String getBody() { 136 return body; 137 } 138 139 143 public void setBody(String body) { 144 this.body = body; 145 } 146 147 153 public String getThread() { 154 return thread; 155 } 156 157 163 public void setThread(String thread) { 164 this.thread = thread; 165 } 166 167 public String toXML() { 168 StringBuffer buf = new StringBuffer (); 169 buf.append("<message"); 170 if (getPacketID() != null) { 171 buf.append(" id=\"").append(getPacketID()).append("\""); 172 } 173 if (getTo() != null) { 174 buf.append(" to=\"").append(StringUtils.escapeForXML(getTo())).append("\""); 175 } 176 if (getFrom() != null) { 177 buf.append(" from=\"").append(StringUtils.escapeForXML(getFrom())).append("\""); 178 } 179 if (type != Type.NORMAL) { 180 buf.append(" type=\"").append(type).append("\""); 181 } 182 buf.append(">"); 183 if (subject != null) { 184 buf.append("<subject>").append(StringUtils.escapeForXML(subject)).append("</subject>"); 185 } 186 if (body != null) { 187 buf.append("<body>").append(StringUtils.escapeForXML(body)).append("</body>"); 188 } 189 if (thread != null) { 190 buf.append("<thread>").append(thread).append("</thread>"); 191 } 192 if (type == Type.ERROR) { 194 XMPPError error = getError(); 195 if (error != null) { 196 buf.append(error.toXML()); 197 } 198 } 199 buf.append(getExtensionsXML()); 201 buf.append("</message>"); 202 return buf.toString(); 203 } 204 205 208 public static class Type { 209 210 213 public static final Type NORMAL = new Type("normal"); 214 215 218 public static final Type CHAT = new Type("chat"); 219 220 223 public static final Type GROUP_CHAT = new Type("groupchat"); 224 225 228 public static final Type HEADLINE = new Type("headline"); 229 230 233 public static final Type ERROR = new Type("error"); 234 235 241 public static Type fromString(String type) { 242 if (type == null) { 243 return NORMAL; 244 } 245 type = type.toLowerCase(); 246 if (CHAT.toString().equals(type)) { 247 return CHAT; 248 } 249 else if (GROUP_CHAT.toString().equals(type)) { 250 return GROUP_CHAT; 251 } 252 else if (HEADLINE.toString().equals(type)) { 253 return HEADLINE; 254 } 255 else if (ERROR.toString().equals(type)) { 256 return ERROR; 257 } 258 else { 259 return NORMAL; 260 } 261 } 262 263 private String value; 264 265 private Type(String value) { 266 this.value = value; 267 } 268 269 public String toString() { 270 return value; 271 } 272 } 273 } | Popular Tags |