1 20 21 package org.jivesoftware.smackx; 22 23 import java.util.*; 24 25 import org.jivesoftware.smack.packet.*; 26 import org.jivesoftware.smackx.packet.DataForm; 27 28 34 public class ReportedData { 35 36 private List columns = new ArrayList(); 37 private List rows = new ArrayList(); 38 private String title = ""; 39 40 46 public static ReportedData getReportedDataFrom(Packet packet) { 47 PacketExtension packetExtension = packet.getExtension("x","jabber:x:data"); 49 if (packetExtension != null) { 50 DataForm dataForm = (DataForm) packetExtension; 52 if (dataForm.getReportedData() != null) 53 return new ReportedData(dataForm); 54 } 55 return null; 57 } 58 59 60 66 private ReportedData(DataForm dataForm) { 67 for (Iterator fields = dataForm.getReportedData().getFields(); fields.hasNext();) { 69 FormField field = (FormField)fields.next(); 70 columns.add(new Column(field.getLabel(), field.getVariable(), field.getType())); 71 } 72 73 for (Iterator items = dataForm.getItems(); items.hasNext();) { 75 DataForm.Item item = (DataForm.Item)items.next(); 76 List fieldList = new ArrayList(columns.size()); 77 FormField field; 78 for (Iterator fields = item.getFields(); fields.hasNext();) { 79 field = (FormField) fields.next(); 80 List values = new ArrayList(); 82 for (Iterator it=field.getValues(); it.hasNext();) { 83 values.add(it.next()); 84 } 85 fieldList.add(new Field(field.getVariable(), values)); 86 } 87 rows.add(new Row(fieldList)); 88 } 89 90 this.title = dataForm.getTitle(); 92 } 93 94 99 public Iterator getRows() { 100 return Collections.unmodifiableList(new ArrayList(rows)).iterator(); 101 } 102 103 108 public Iterator getColumns() { 109 return Collections.unmodifiableList(new ArrayList(columns)).iterator(); 110 } 111 112 113 119 public String getTitle() { 120 return title; 121 } 122 123 129 public static class Column { 130 private String label; 131 private String variable; 132 private String type; 133 134 141 private Column(String label, String variable, String type) { 142 this.label = label; 143 this.variable = variable; 144 this.type = type; 145 } 146 147 152 public String getLabel() { 153 return label; 154 } 155 156 157 178 public String getType() { 179 return type; 180 } 181 182 183 188 public String getVariable() { 189 return variable; 190 } 191 192 193 } 194 195 public static class Row { 196 private List fields = new ArrayList(); 197 198 private Row(List fields) { 199 this.fields = fields; 200 } 201 202 208 public Iterator getValues(String variable) { 209 for(Iterator it=getFields();it.hasNext();) { 210 Field field = (Field) it.next(); 211 if (variable.equals(field.getVariable())) { 212 return field.getValues(); 213 } 214 } 215 return null; 216 } 217 218 223 private Iterator getFields() { 224 return Collections.unmodifiableList(new ArrayList(fields)).iterator(); 225 } 226 } 227 228 private static class Field { 229 private String variable; 230 private List values; 231 232 private Field(String variable, List values) { 233 this.variable = variable; 234 this.values = values; 235 } 236 237 242 public String getVariable() { 243 return variable; 244 } 245 246 251 public Iterator getValues() { 252 return Collections.unmodifiableList(values).iterator(); 253 } 254 } 255 } 256 | Popular Tags |