1package eu.siacs.conversations.xmpp.forms;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.List;
6import java.util.stream.Collectors;
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 replaceChildren(List.of(new Element("value").setContent(value)));
27 }
28
29 public void setValues(Collection<String> values) {
30 replaceChildren(values.stream().map(val -> new Element("value").setContent(val)).collect(Collectors.toList()));
31 }
32
33 public void removeNonValueChildren() {
34 replaceChildren(getChildren().stream().filter(element -> element.getName().equals("value")).collect(Collectors.toList()));
35 }
36
37 public static Field parse(Element element) {
38 Field field = new Field();
39 field.bindTo(element);
40 return field;
41 }
42
43 public String getValue() {
44 return findChildContent("value");
45 }
46
47 public List<String> getValues() {
48 List<String> values = new ArrayList<>();
49 for(Element child : getChildren()) {
50 if ("value".equals(child.getName())) {
51 values.add(child.getContent());
52 }
53 }
54 return values;
55 }
56
57 public String getLabel() {
58 return getAttribute("label");
59 }
60
61 public String getType() {
62 return getAttribute("type");
63 }
64
65 public boolean isRequired() {
66 return hasChild("required");
67 }
68
69 public List<Option> getOptions() {
70 return Option.forField(this);
71 }
72}