1 20 21 package org.jivesoftware.smackx.packet; 22 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.jivesoftware.smack.packet.PacketExtension; 29 import org.jivesoftware.smackx.FormField; 30 31 37 public class DataForm implements PacketExtension { 38 39 private String type; 40 private String title; 41 private List instructions = new ArrayList (); 42 private ReportedData reportedData; 43 private List items = new ArrayList (); 44 private List fields = new ArrayList (); 45 46 public DataForm(String type) { 47 this.type = type; 48 } 49 50 66 public String getType() { 67 return type; 68 } 69 70 76 public String getTitle() { 77 return title; 78 } 79 80 88 public Iterator getInstructions() { 89 synchronized (instructions) { 90 return Collections.unmodifiableList(new ArrayList (instructions)).iterator(); 91 } 92 } 93 94 99 public ReportedData getReportedData() { 100 return reportedData; 101 } 102 103 108 public Iterator getItems() { 109 synchronized (items) { 110 return Collections.unmodifiableList(new ArrayList (items)).iterator(); 111 } 112 } 113 114 119 public Iterator getFields() { 120 synchronized (fields) { 121 return Collections.unmodifiableList(new ArrayList (fields)).iterator(); 122 } 123 } 124 125 public String getElementName() { 126 return "x"; 127 } 128 129 public String getNamespace() { 130 return "jabber:x:data"; 131 } 132 133 139 public void setTitle(String title) { 140 this.title = title; 141 } 142 143 150 public void setInstructions(List instructions) { 151 this.instructions = instructions; 152 } 153 154 159 public void setReportedData(ReportedData reportedData) { 160 this.reportedData = reportedData; 161 } 162 163 168 public void addField(FormField field) { 169 synchronized (fields) { 170 fields.add(field); 171 } 172 } 173 174 181 public void addInstruction(String instruction) { 182 synchronized (instructions) { 183 instructions.add(instruction); 184 } 185 } 186 187 192 public void addItem(Item item) { 193 synchronized (items) { 194 items.add(item); 195 } 196 } 197 198 public String toXML() { 199 StringBuffer buf = new StringBuffer (); 200 buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append( 201 "\" type=\"" + getType() +"\">"); 202 if (getTitle() != null) { 203 buf.append("<title>").append(getTitle()).append("</title>"); 204 } 205 for (Iterator it=getInstructions(); it.hasNext();) { 206 buf.append("<instructions>").append(it.next()).append("</instructions>"); 207 } 208 if (getReportedData() != null) { 210 buf.append(getReportedData().toXML()); 211 } 212 for (Iterator i = getItems(); i.hasNext();) { 214 Item item = (Item) i.next(); 215 buf.append(item.toXML()); 216 } 217 for (Iterator i = getFields(); i.hasNext();) { 219 FormField field = (FormField) i.next(); 220 buf.append(field.toXML()); 221 } 222 buf.append("</").append(getElementName()).append(">"); 223 return buf.toString(); 224 } 225 226 233 public static class ReportedData { 234 private List fields = new ArrayList (); 235 236 public ReportedData(List fields) { 237 this.fields = fields; 238 } 239 240 245 public Iterator getFields() { 246 return Collections.unmodifiableList(new ArrayList (fields)).iterator(); 247 } 248 249 public String toXML() { 250 StringBuffer buf = new StringBuffer (); 251 buf.append("<reported>"); 252 for (Iterator i = getFields(); i.hasNext();) { 254 FormField field = (FormField) i.next(); 255 buf.append(field.toXML()); 256 } 257 buf.append("</reported>"); 258 return buf.toString(); 259 } 260 } 261 262 268 public static class Item { 269 private List fields = new ArrayList (); 270 271 public Item(List fields) { 272 this.fields = fields; 273 } 274 275 280 public Iterator getFields() { 281 return Collections.unmodifiableList(new ArrayList (fields)).iterator(); 282 } 283 284 public String toXML() { 285 StringBuffer buf = new StringBuffer (); 286 buf.append("<item>"); 287 for (Iterator i = getFields(); i.hasNext();) { 289 FormField field = (FormField) i.next(); 290 buf.append(field.toXML()); 291 } 292 buf.append("</item>"); 293 return buf.toString(); 294 } 295 } 296 } 297 | Popular Tags |