1 20 21 package org.jivesoftware.smackx.packet; 22 23 import org.jivesoftware.smack.packet.IQ; 24 import org.jivesoftware.smack.provider.IQProvider; 25 import org.xmlpull.v1.XmlPullParser; 26 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 38 public class OfflineMessageRequest extends IQ { 39 40 private List items = new ArrayList (); 41 private boolean purge = false; 42 private boolean fetch = false; 43 44 51 public Iterator getItems() { 52 synchronized (items) { 53 return Collections.unmodifiableList(new ArrayList (items)).iterator(); 54 } 55 } 56 57 62 public void addItem(Item item) { 63 synchronized (items) { 64 items.add(item); 65 } 66 } 67 68 73 public boolean isPurge() { 74 return purge; 75 } 76 77 82 public void setPurge(boolean purge) { 83 this.purge = purge; 84 } 85 86 91 public boolean isFetch() { 92 return fetch; 93 } 94 95 100 public void setFetch(boolean fetch) { 101 this.fetch = fetch; 102 } 103 104 public String getChildElementXML() { 105 StringBuffer buf = new StringBuffer (); 106 buf.append("<offline xmlns=\"http://jabber.org/protocol/offline\">"); 107 synchronized (items) { 108 for (int i = 0; i < items.size(); i++) { 109 Item item = (Item) items.get(i); 110 buf.append(item.toXML()); 111 } 112 } 113 if (purge) { 114 buf.append("<purge/>"); 115 } 116 if (fetch) { 117 buf.append("<fetch/>"); 118 } 119 buf.append(getExtensionsXML()); 121 buf.append("</offline>"); 122 return buf.toString(); 123 } 124 125 130 public static class Item { 131 private String action; 132 private String jid; 133 private String node; 134 135 140 public Item(String node) { 141 this.node = node; 142 } 143 144 public String getNode() { 145 return node; 146 } 147 148 155 public String getAction() { 156 return action; 157 } 158 159 165 public void setAction(String action) { 166 this.action = action; 167 } 168 169 public String getJid() { 170 return jid; 171 } 172 173 public void setJid(String jid) { 174 this.jid = jid; 175 } 176 177 public String toXML() { 178 StringBuffer buf = new StringBuffer (); 179 buf.append("<item"); 180 if (getAction() != null) { 181 buf.append(" action=\"").append(getAction()).append("\""); 182 } 183 if (getJid() != null) { 184 buf.append(" jid=\"").append(getJid()).append("\""); 185 } 186 if (getNode() != null) { 187 buf.append(" node=\"").append(getNode()).append("\""); 188 } 189 buf.append("/>"); 190 return buf.toString(); 191 } 192 } 193 194 public static class Provider implements IQProvider { 195 196 public IQ parseIQ(XmlPullParser parser) throws Exception { 197 OfflineMessageRequest request = new OfflineMessageRequest(); 198 boolean done = false; 199 while (!done) { 200 int eventType = parser.next(); 201 if (eventType == XmlPullParser.START_TAG) { 202 if (parser.getName().equals("item")) { 203 request.addItem(parseItem(parser)); 204 } 205 else if (parser.getName().equals("purge")) { 206 request.setPurge(true); 207 } 208 else if (parser.getName().equals("fetch")) { 209 request.setFetch(true); 210 } 211 } else if (eventType == XmlPullParser.END_TAG) { 212 if (parser.getName().equals("offline")) { 213 done = true; 214 } 215 } 216 } 217 218 return request; 219 } 220 221 private Item parseItem(XmlPullParser parser) throws Exception { 222 boolean done = false; 223 Item item = new Item(parser.getAttributeValue("", "node")); 224 item.setAction(parser.getAttributeValue("", "action")); 225 item.setJid(parser.getAttributeValue("", "jid")); 226 while (!done) { 227 int eventType = parser.next(); 228 if (eventType == XmlPullParser.END_TAG) { 229 if (parser.getName().equals("item")) { 230 done = true; 231 } 232 } 233 } 234 return item; 235 } 236 } 237 } 238 | Popular Tags |