1package eu.siacs.conversations.xmpp.forms;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.Iterator;
6import java.util.List;
7
8import eu.siacs.conversations.xml.Element;
9
10public class Field extends Element {
11
12 public Field(String name) {
13 super("field");
14 this.setAttribute("var",name);
15 }
16
17 private Field() {
18 super("field");
19 }
20
21 public String getFieldName() {
22 return this.getAttribute("var");
23 }
24
25 public void setValue(String value) {
26 this.children.clear();
27 this.addChild("value").setContent(value);
28 }
29
30 public void setValues(Collection<String> values) {
31 this.children.clear();
32 for(String value : values) {
33 this.addChild("value").setContent(value);
34 }
35 }
36
37 public void removeNonValueChildren() {
38 for(Iterator<Element> iterator = this.children.iterator(); iterator.hasNext();) {
39 Element element = iterator.next();
40 if (!element.getName().equals("value")) {
41 iterator.remove();
42 }
43 }
44 }
45
46 public static Field parse(Element element) {
47 Field field = new Field();
48 field.setAttributes(element.getAttributes());
49 field.setChildren(element.getChildren());
50 return field;
51 }
52
53 public String getValue() {
54 return findChildContent("value");
55 }
56
57 public List<String> getValues() {
58 List<String> values = new ArrayList<>();
59 for(Element child : getChildren()) {
60 if ("value".equals(child.getName())) {
61 values.add(child.getContent());
62 }
63 }
64 return values;
65 }
66
67 public String getLabel() {
68 return getAttribute("label");
69 }
70
71 public String getType() {
72 return getAttribute("type");
73 }
74
75 public boolean isRequired() {
76 return hasChild("required");
77 }
78}