diff options
Diffstat (limited to 'src/main/java/eu/siacs/conversations/xmpp/forms')
-rw-r--r-- | src/main/java/eu/siacs/conversations/xmpp/forms/Data.java | 75 | ||||
-rw-r--r-- | src/main/java/eu/siacs/conversations/xmpp/forms/Field.java | 50 |
2 files changed, 125 insertions, 0 deletions
diff --git a/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java b/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java new file mode 100644 index 000000000..ff9acb3f5 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java @@ -0,0 +1,75 @@ +package eu.siacs.conversations.xmpp.forms; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import eu.siacs.conversations.xml.Element; + +public class Data extends Element { + + public Data() { + super("x"); + this.setAttribute("xmlns","jabber:x:data"); + } + + public List<Field> getFields() { + ArrayList<Field> fields = new ArrayList<Field>(); + for(Element child : getChildren()) { + if (child.getName().equals("field")) { + fields.add(Field.parse(child)); + } + } + return fields; + } + + public Field getFieldByName(String needle) { + for(Element child : getChildren()) { + if (child.getName().equals("field") && needle.equals(child.getAttribute("var"))) { + return Field.parse(child); + } + } + return null; + } + + public void put(String name, String value) { + Field field = getFieldByName(name); + if (field == null) { + field = new Field(name); + } + field.setValue(value); + } + + public void put(String name, Collection<String> values) { + Field field = getFieldByName(name); + if (field == null) { + field = new Field(name); + } + field.setValues(values); + } + + public void submit() { + this.setAttribute("type","submit"); + removeNonFieldChildren(); + for(Field field : getFields()) { + field.removeNonValueChildren(); + } + } + + private void removeNonFieldChildren() { + for(Iterator<Element> iterator = this.children.iterator(); iterator.hasNext();) { + Element element = iterator.next(); + if (!element.getName().equals("field")) { + iterator.remove(); + } + } + } + + public static Data parse(Element element) { + Data data = new Data(); + data.setAttributes(element.getAttributes()); + data.setChildren(element.getChildren()); + return data; + } +} diff --git a/src/main/java/eu/siacs/conversations/xmpp/forms/Field.java b/src/main/java/eu/siacs/conversations/xmpp/forms/Field.java new file mode 100644 index 000000000..ee2c51a9c --- /dev/null +++ b/src/main/java/eu/siacs/conversations/xmpp/forms/Field.java @@ -0,0 +1,50 @@ +package eu.siacs.conversations.xmpp.forms; + +import java.util.Collection; +import java.util.Iterator; + +import eu.siacs.conversations.xml.Element; + +public class Field extends Element { + + public Field(String name) { + super("field"); + this.setAttribute("var",name); + } + + private Field() { + super("field"); + } + + public String getName() { + 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; + } +} |