1 20 21 package org.xmpp.packet; 22 23 import org.dom4j.Element; 24 25 import java.util.Iterator ; 26 27 37 public class Presence extends Packet { 38 39 42 public Presence() { 43 this.element = docFactory.createDocument().addElement("presence"); 44 } 45 46 51 public Presence(Presence.Type type) { 52 this(); 53 setType(type); 54 } 55 56 62 public Presence(Element element) { 63 super(element); 64 } 65 66 72 private Presence(Presence presence) { 73 Element elementCopy = presence.element.createCopy(); 74 docFactory.createDocument().add(elementCopy); 75 this.element = elementCopy; 76 } 77 78 85 public boolean isAvailable() { 86 return getType() == null; 87 } 88 89 97 public Type getType() { 98 String type = element.attributeValue("type"); 99 if (type == null) { 100 return null; 101 } 102 else { 103 return Type.valueOf(type); 104 } 105 } 106 107 113 public void setType(Type type) { 114 element.addAttribute("type", type==null?null:type.toString()); 115 } 116 117 127 public Show getShow() { 128 String show = element.elementText("show"); 129 if (show == null) { 130 return null; 131 } 132 else { 133 return Show.valueOf(show); 134 } 135 } 136 137 147 public void setShow(Show show) { 148 Element showElement = element.element("show"); 149 if (show == null) { 151 if (showElement != null) { 152 element.remove(showElement); 153 } 154 return; 155 } 156 if (showElement == null) { 157 if (!isAvailable()) { 158 throw new IllegalArgumentException ("Cannot set 'show' if 'type' attribute is set."); 159 } 160 showElement = element.addElement("show"); 161 } 162 showElement.setText(show.toString()); 163 } 164 165 171 public String getStatus() { 172 return element.elementText("status"); 173 } 174 175 181 public void setStatus(String status) { 182 Element statusElement = element.element("status"); 183 if (status == null) { 185 if (statusElement != null) { 186 element.remove(statusElement); 187 } 188 return; 189 } 190 191 if (statusElement == null) { 192 statusElement = element.addElement("status"); 193 } 194 statusElement.setText(status); 195 } 196 197 204 public int getPriority() { 205 String priority = element.elementText("priority"); 206 if (priority == null) { 207 return 0; 208 } 209 else { 210 try { 211 return Integer.parseInt(priority); 212 } 213 catch (Exception e) { 214 return 0; 215 } 216 } 217 } 218 219 226 public void setPriority(int priority) { 227 if (priority < -128 || priority > 128) { 228 throw new IllegalArgumentException ("Priority value of " + priority + 229 " is outside the valid range of -128 through 128"); 230 } 231 Element priorityElement = element.element("priority"); 232 if (priorityElement == null) { 233 priorityElement = element.addElement("priority"); 234 } 235 priorityElement.setText(Integer.toString(priority)); 236 } 237 238 256 public Element getChildElement(String name, String namespace) { 257 for (Iterator i=element.elementIterator(name); i.hasNext(); ) { 258 Element element = (Element)i.next(); 259 if (element.getNamespaceURI().equals(namespace)) { 260 return element; 261 } 262 } 263 return null; 264 } 265 266 283 public Element addChildElement(String name, String namespace) { 284 return element.addElement(name, namespace); 285 } 286 287 292 public Presence createCopy() { 293 return new Presence(this); 294 } 295 296 318 public enum Type { 319 320 323 unavailable, 324 325 328 subscribe, 329 330 333 subscribed, 334 335 338 unsubscribe, 339 340 344 unsubscribed, 345 346 350 probe, 351 352 356 error; 357 } 358 359 374 public enum Show { 375 376 379 chat, 380 381 384 away, 385 386 389 xa, 390 391 394 dnd; 395 } 396 } | Popular Tags |