1 20 21 package org.xmpp.forms; 22 23 import org.dom4j.Element; 24 import org.dom4j.QName; 25 import org.xmpp.packet.PacketExtension; 26 27 import java.util.*; 28 import java.text.SimpleDateFormat ; 29 import java.text.ParseException ; 30 31 50 public class DataForm extends PacketExtension { 51 52 private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat ("yyyyMMdd'T'HH:mm:ss"); 53 54 57 public static final String ELEMENT_NAME = "x"; 58 59 62 public static final String NAMESPACE = "jabber:x:data"; 63 64 static { 65 UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0")); 66 registeredExtensions.put(QName.get(ELEMENT_NAME, NAMESPACE), DataForm.class); 68 } 69 70 78 public static Date parseDate(String date) throws ParseException { 79 return UTC_FORMAT.parse(date); 80 } 81 82 88 static String encode(Object object) { 89 if (object instanceof String ) { 90 return object.toString(); 91 } 92 else if (object instanceof Date) { 93 return UTC_FORMAT.format(object); 94 } 95 else if (object instanceof Boolean ) { 96 return Boolean.TRUE.equals(object) ? "1" : "0"; 97 } 98 return object.toString(); 99 } 100 101 public DataForm(Type type) { 102 super(ELEMENT_NAME, NAMESPACE); 103 element.addAttribute("type", type.toString()); 105 } 106 107 public DataForm(Element element) { 108 super(element); 109 } 110 111 117 public DataForm.Type getType() { 118 String type = element.attributeValue("type"); 119 if (type != null) { 120 return DataForm.Type.valueOf(type); 121 } 122 else { 123 return null; 124 } 125 } 126 127 133 public void setTitle(String title) { 134 if (element.element("title") != null) { 136 element.remove(element.element("title")); 137 } 138 element.addElement("title").setText(title); 139 } 140 141 147 public String getTitle() { 148 return element.elementTextTrim("title"); 149 } 150 151 158 public List<String > getInstructions() { 159 List<String > answer = new ArrayList<String >(); 160 for (Iterator it = element.elementIterator("instructions"); it.hasNext();) { 161 answer.add(((Element) it.next()).getTextTrim()); 162 } 163 return Collections.unmodifiableList(answer); 164 } 165 166 173 public void addInstruction(String instruction) { 174 element.addElement("instructions").setText(instruction); 175 } 176 177 180 public void clearInstructions() { 181 for (Iterator it = element.elementIterator("instructions"); it.hasNext();) { 182 it.next(); 183 it.remove(); 184 } 185 } 186 187 192 public FormField addField() { 193 return new FormField(element.addElement("field")); 194 } 195 196 201 public List<FormField> getFields() { 202 List<FormField> answer = new ArrayList<FormField>(); 203 for (Iterator it = element.elementIterator("field"); it.hasNext();) { 204 answer.add(new FormField((Element) it.next())); 205 } 206 return answer; 207 } 208 209 219 public void addReportedField(String variable, String label, FormField.Type type) { 220 Element reported = element.element("reported"); 221 synchronized (element) { 222 if (reported == null) { 223 reported = element.element("reported"); 224 if (reported == null) { 225 reported = element.addElement("reported"); 226 } 227 } 228 } 229 FormField newField = new FormField(reported.addElement("field")); 230 newField.setVariable(variable); 231 newField.setType(type); 232 newField.setLabel(label); 233 } 234 235 245 public void addItemFields(Map<String ,Object > fields) { 246 Element item = element.addElement("item"); 247 for (String var : fields.keySet()) { 249 Element field = item.addElement("field"); 250 field.addAttribute("var", var); 251 Object value = fields.get(var); 252 if (value instanceof Collection) { 253 for (Iterator it = ((Collection) value).iterator(); it.hasNext();) { 255 field.addElement("value").setText(encode(it.next())); 256 } 257 } 258 else { 259 field.addElement("value").setText(encode(value)); 260 } 261 } 262 } 263 264 public DataForm createCopy() { 265 return new DataForm(this.getElement().createCopy()); 266 } 267 268 271 public enum Type { 272 275 form, 276 277 280 submit, 281 282 286 cancel, 287 288 292 result; 293 } 294 } 295 | Popular Tags |