1 20 21 package org.xmpp.packet; 22 23 import org.dom4j.DocumentFactory; 24 import org.dom4j.Element; 25 import org.dom4j.QName; 26 import org.dom4j.io.OutputFormat; 27 import org.dom4j.io.XMLWriter; 28 29 import java.io.StringWriter ; 30 import java.lang.reflect.Constructor ; 31 import java.util.List ; 32 33 48 public abstract class Packet { 49 50 protected static DocumentFactory docFactory = DocumentFactory.getInstance(); 51 52 protected Element element; 53 54 59 public Packet(Element element) { 60 this.element = element; 61 String to = element.attributeValue("to"); 63 if (to != null) { 64 JID toJID = new JID(to); 65 element.addAttribute("to", toJID.toString()); 66 } 67 String from = element.attributeValue("from"); 68 if (from != null) { 69 JID fromJID = new JID(from); 70 element.addAttribute("from", fromJID.toString()); 71 } 72 } 73 74 79 protected Packet() { 80 81 } 82 83 89 public String getID() { 90 return element.attributeValue("id"); 91 } 92 93 98 public void setID(String ID) { 99 element.addAttribute("id", ID); 100 } 101 102 110 public JID getTo() { 111 String to = element.attributeValue("to"); 112 if (to == null) { 113 return null; 114 } 115 else { 116 return new JID(to, null); 121 } 122 } 123 124 130 public void setTo(String to) { 131 if (to != null) { 133 to = new JID(to).toString(); 134 } 135 element.addAttribute("to", to); 136 } 137 138 144 public void setTo(JID to) { 145 if (to == null) { 146 element.addAttribute("to", null); 147 } 148 else { 149 element.addAttribute("to", to.toString()); 150 } 151 } 152 153 161 public JID getFrom() { 162 String from = element.attributeValue("from"); 163 if (from == null) { 164 return null; 165 } 166 else { 167 return new JID(from, null); 172 } 173 } 174 175 181 public void setFrom(String from) { 182 if (from != null) { 184 from = new JID(from).toString(); 185 } 186 element.addAttribute("from", from); 187 } 188 189 195 public void setFrom(JID from) { 196 if (from == null) { 197 element.addAttribute("from", null); 198 } 199 else { 200 element.addAttribute("from", from.toString()); 201 } 202 } 203 204 213 public void addExtension(PacketExtension extension) { 214 element.add(extension.getElement()); 215 } 216 217 227 public PacketExtension getExtension(String name, String namespace) { 228 List extensions = element.elements(QName.get(name, namespace)); 229 if (!extensions.isEmpty()) { 230 Class extensionClass = PacketExtension.getExtensionClass(name, namespace); 231 if (extensionClass != null) { 232 try { 233 Constructor constructor = extensionClass.getDeclaredConstructor(new Class []{ 234 Element.class}); 235 return (PacketExtension) constructor.newInstance(new Object []{ 236 extensions.get(0)}); 237 } catch (Exception e) { 238 } 239 } 240 } 241 return null; 242 } 243 244 257 public boolean deleteExtension(String name, String namespace) { 258 List extensions = element.elements(QName.get(name, namespace)); 259 if (!extensions.isEmpty()) { 260 element.remove((Element) extensions.get(0)); 261 return true; 262 } 263 return false; 264 } 265 266 271 public PacketError getError() { 272 Element error = element.element("error"); 273 if (error != null) { 274 return new PacketError(element); 275 } 276 return null; 277 } 278 279 285 public void setError(PacketError error) { 286 if (element == null) { 287 throw new NullPointerException ("Error cannot be null"); 288 } 289 element.addAttribute("type", "error"); 291 if (element.element("error") != null) { 293 element.remove(element.element("error")); 294 } 295 element.add(error.getElement()); 297 } 298 299 308 public void setError(PacketError.Condition condition) { 309 setError(new PacketError(condition)); 310 } 311 312 317 public abstract Packet createCopy(); 318 319 326 public Element getElement() { 327 return element; 328 } 329 330 335 public String toXML() { 336 return element.asXML(); 337 } 338 339 public String toString() { 340 StringWriter out = new StringWriter (); 341 XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); 342 try { 343 writer.write(element); 344 } 345 catch (Exception e) { } 346 return out.toString(); 347 } 348 } | Popular Tags |