Field.java

 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		setChildren(List.of(new Element("value").setContent(value)));
27	}
28
29	public void setValues(Collection<String> values) {
30		setChildren(values.stream().map(val -> new Element("value").setContent(val)).collect(Collectors.toList()));
31	}
32
33	public void removeNonValueChildren() {
34		setChildren(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.setAttributes(element.getAttributes());
40		field.setChildren(element.getChildren());
41		return field;
42	}
43
44	public String getValue() {
45		return findChildContent("value");
46	}
47
48	public List<String> getValues() {
49		List<String> values = new ArrayList<>();
50		for(Element child : getChildren()) {
51			if ("value".equals(child.getName())) {
52				values.add(child.getContent());
53			}
54		}
55		return values;
56	}
57
58	public String getLabel() {
59		return getAttribute("label");
60	}
61
62	public String getType() {
63		return getAttribute("type");
64	}
65
66	public boolean isRequired() {
67		return hasChild("required");
68	}
69}