aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/xmpp/forms/Field.java
blob: a05f9538efc9ec60d873f0cade358a60096c200e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package de.pixart.messenger.xmpp.forms;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import de.pixart.messenger.xml.Element;

public class Field extends Element {

    public Field(String name) {
        super("field");
        this.setAttribute("var", name);
    }

    private Field() {
        super("field");
    }

    public String getFieldName() {
        return this.getAttribute("var");
    }

    public void setValue(String value) {
        this.children.clear();
        this.addChild("value").setContent(value);
    }

    public void setValues(Collection<String> values) {
        this.children.clear();
        for (String value : values) {
            this.addChild("value").setContent(value);
        }
    }

    public void removeNonValueChildren() {
        for (Iterator<Element> iterator = this.children.iterator(); iterator.hasNext(); ) {
            Element element = iterator.next();
            if (!element.getName().equals("value")) {
                iterator.remove();
            }
        }
    }

    public static Field parse(Element element) {
        Field field = new Field();
        field.setAttributes(element.getAttributes());
        field.setChildren(element.getChildren());
        return field;
    }

    public String getValue() {
        return findChildContent("value");
    }

    public List<String> getValues() {
        List<String> values = new ArrayList<>();
        for (Element child : getChildren()) {
            if ("value".equals(child.getName())) {
                String content = child.getContent();
                if (content != null) {
                    values.add(content);
                }
            }
        }
        return values;
    }

    public String getLabel() {
        return getAttribute("label");
    }

    public String getType() {
        return getAttribute("type");
    }

    public boolean isRequired() {
        return hasChild("required");
    }
}