1 20 21 package org.xmpp.packet; 22 23 import org.dom4j.Element; 24 25 import java.util.Iterator ; 26 27 43 public class Message extends Packet { 44 45 48 public Message() { 49 this.element = docFactory.createDocument().addElement("message"); 50 } 51 52 58 public Message(Element element) { 59 super(element); 60 } 61 62 68 private Message(Message message) { 69 Element elementCopy = message.element.createCopy(); 70 docFactory.createDocument().add(elementCopy); 71 this.element = elementCopy; 72 } 73 74 80 public Type getType() { 81 String type = element.attributeValue("type"); 82 if (type != null) { 83 return Type.valueOf(type); 84 } 85 else { 86 return Type.normal; 87 } 88 } 89 90 96 public void setType(Type type) { 97 element.addAttribute("type", type==null?null:type.toString()); 98 } 99 100 105 public String getSubject() { 106 return element.elementText("subject"); 107 } 108 109 114 public void setSubject(String subject) { 115 Element subjectElement = element.element("subject"); 116 if (subject == null && subjectElement != null) { 118 element.remove(subjectElement); 119 return; 120 } 121 if (subject == null) { 123 return; 124 } 125 if (subjectElement == null) { 126 subjectElement = element.addElement("subject"); 127 } 128 subjectElement.setText(subject); 129 } 130 131 136 public String getBody() { 137 return element.elementText("body"); 138 } 139 140 145 public void setBody(String body) { 146 Element bodyElement = element.element("body"); 147 if (body == null) { 149 if (bodyElement != null) { 150 element.remove(bodyElement); 151 } 152 return; 153 } 154 if (body == null) { 156 return; 157 } 158 if (bodyElement == null) { 159 bodyElement = element.addElement("body"); 160 } 161 bodyElement.setText(body); 162 } 163 164 172 public String getThread() { 173 return element.elementText("thread"); 174 } 175 176 183 public void setThread(String thread) { 184 Element threadElement = element.element("thread"); 185 if (thread == null) { 187 if (threadElement != null) { 188 element.remove(threadElement); 189 } 190 return; 191 } 192 193 if (threadElement == null) { 194 threadElement = element.addElement("thread"); 195 } 196 threadElement.setText(thread); 197 } 198 199 217 public Element getChildElement(String name, String namespace) { 218 for (Iterator i=element.elementIterator(name); i.hasNext(); ) { 219 Element element = (Element)i.next(); 220 if (element.getNamespaceURI().equals(namespace)) { 221 return element; 222 } 223 } 224 return null; 225 } 226 227 244 public Element addChildElement(String name, String namespace) { 245 return element.addElement(name, namespace); 246 } 247 248 253 public Message createCopy() { 254 return new Message(this); 255 } 256 257 272 public enum Type { 273 274 277 normal, 278 279 282 chat, 283 284 287 groupchat, 288 289 292 headline, 293 294 297 error; 298 } 299 } | Popular Tags |