1 20 21 package org.jivesoftware.smack.packet; 22 23 import org.jivesoftware.smack.util.StringUtils; 24 25 42 public abstract class IQ extends Packet { 43 44 private Type type = Type.GET; 45 46 51 public Type getType() { 52 return type; 53 } 54 55 60 public void setType(Type type) { 61 if (type == null) { 62 this.type = Type.GET; 63 } 64 else { 65 this.type = type; 66 } 67 } 68 69 public String toXML() { 70 StringBuffer buf = new StringBuffer (); 71 buf.append("<iq "); 72 if (getPacketID() != null) { 73 buf.append("id=\"" + getPacketID() + "\" "); 74 } 75 if (getTo() != null) { 76 buf.append("to=\"").append(StringUtils.escapeForXML(getTo())).append("\" "); 77 } 78 if (getFrom() != null) { 79 buf.append("from=\"").append(StringUtils.escapeForXML(getFrom())).append("\" "); 80 } 81 if (type == null) { 82 buf.append("type=\"get\">"); 83 } 84 else { 85 buf.append("type=\"").append(getType()).append("\">"); 86 } 87 String queryXML = getChildElementXML(); 89 if (queryXML != null) { 90 buf.append(queryXML); 91 } 92 XMPPError error = getError(); 94 if (error != null) { 95 buf.append(error.toXML()); 96 } 97 buf.append("</iq>"); 98 return buf.toString(); 99 } 100 101 109 public abstract String getChildElementXML(); 110 111 121 public static class Type { 122 123 public static final Type GET = new Type("get"); 124 public static final Type SET = new Type("set"); 125 public static final Type RESULT = new Type("result"); 126 public static final Type ERROR = new Type("error"); 127 128 135 public static Type fromString(String type) { 136 if (type == null) { 137 return null; 138 } 139 type = type.toLowerCase(); 140 if (GET.toString().equals(type)) { 141 return GET; 142 } 143 else if (SET.toString().equals(type)) { 144 return SET; 145 } 146 else if (ERROR.toString().equals(type)) { 147 return ERROR; 148 } 149 else if (RESULT.toString().equals(type)) { 150 return RESULT; 151 } 152 else { 153 return null; 154 } 155 } 156 157 private String value; 158 159 private Type(String value) { 160 this.value = value; 161 } 162 163 public String toString() { 164 return value; 165 } 166 } 167 } 168 | Popular Tags |