1 package org.xmpp.packet; 2 3 import org.dom4j.Element; 4 import org.dom4j.QName; 5 6 import java.lang.reflect.Constructor ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 import java.util.Random ; 10 11 20 public class IQ extends Packet { 21 22 private static int sequence = 0; 24 private static Random random = new Random (); 25 26 30 public IQ() { 31 this.element = docFactory.createDocument().addElement("iq"); 32 String id = String.valueOf(random.nextInt(1000) + "-" + sequence++); 33 setType(Type.get); 34 setID(id); 35 } 36 37 43 public IQ(Type type) { 44 this.element = docFactory.createDocument().addElement("iq"); 45 setType(type); 46 String id = String.valueOf(random.nextInt(1000) + "-" + sequence++); 47 setID(id); 48 } 49 50 56 public IQ(Type type, String ID) { 57 this.element = docFactory.createDocument().addElement("iq"); 58 setType(type); 59 setID(ID); 60 } 61 62 68 public IQ(Element element) { 69 super(element); 70 } 71 72 78 private IQ(IQ iq) { 79 Element elementCopy = iq.element.createCopy(); 80 docFactory.createDocument().add(elementCopy); 81 this.element = elementCopy; 82 } 83 84 90 public Type getType() { 91 String type = element.attributeValue("type"); 92 if (type != null) { 93 return Type.valueOf(type); 94 } 95 else { 96 return null; 97 } 98 } 99 100 106 public void setType(Type type) { 107 element.addAttribute("type", type==null?null:type.toString()); 108 } 109 110 123 public Element getChildElement() { 124 List elements = element.elements(); 125 if (elements.isEmpty()) { 126 return null; 127 } 128 else { 129 for (int i=0; i<elements.size(); i++) { 131 Element element = (Element)elements.get(i); 132 String namespace = element.getNamespaceURI(); 133 if (!namespace.equals("") && !namespace.equals("jabber:client") && 134 !namespace.equals("jabber:server")) 135 { 136 return element; 137 } 138 } 139 return null; 140 } 141 } 142 143 163 public void setChildElement(Element childElement) { 164 for (Iterator i=element.elementIterator(); i.hasNext(); ) { 165 element.remove((Element)i.next()); 166 } 167 element.add(childElement); 168 } 169 170 192 public Element setChildElement(String name, String namespace) { 193 for (Iterator i=element.elementIterator(); i.hasNext(); ) { 194 element.remove((Element)i.next()); 195 } 196 return element.addElement(name, namespace); 197 } 198 199 215 public void addExtension(PacketExtension extension) { 216 Element childElement = getChildElement(); 217 if (childElement == null) { 218 throw new IllegalStateException ("Cannot add packet extension when child element is null"); 219 } 220 childElement.add(extension.getElement()); 222 } 223 224 238 public PacketExtension getExtension(String name, String namespace) { 239 Element childElement = getChildElement(); 240 if (childElement == null) { 241 return null; 242 } 243 List extensions = childElement.elements(QName.get(name, namespace)); 245 if (!extensions.isEmpty()) { 246 Class extensionClass = PacketExtension.getExtensionClass(name, namespace); 247 if (extensionClass != null) { 248 try { 249 Constructor constructor = extensionClass.getDeclaredConstructor(new Class []{ 250 Element.class}); 251 return (PacketExtension) constructor.newInstance(new Object []{ 252 extensions.get(0)}); 253 } 254 catch (Exception e) { 255 } 256 } 257 } 258 return null; 259 } 260 261 278 public boolean deleteExtension(String name, String namespace) { 279 Element childElement = getChildElement(); 280 if (childElement == null) { 281 return false; 282 } 283 List extensions = childElement.elements(QName.get(name, namespace)); 285 if (!extensions.isEmpty()) { 286 childElement.remove((Element) extensions.get(0)); 287 return true; 288 } 289 return false; 290 } 291 292 297 public IQ createCopy() { 298 return new IQ(this); 299 } 300 301 319 public static IQ createResultIQ(IQ iq) { 320 if (!(iq.getType() == Type.get || iq.getType() == Type.set)) { 321 throw new IllegalArgumentException ("IQ must be of type 'set' or 'get'."); 322 } 323 IQ result = new IQ(Type.result, iq.getID()); 324 result.setFrom(iq.getTo()); 325 result.setTo(iq.getFrom()); 326 return result; 327 } 328 329 346 public enum Type { 347 348 351 get, 352 353 357 set, 358 359 362 result, 363 364 368 error; 369 370 } 371 } | Popular Tags |