From e58e6fa75dfb5cf4f95059da91651caf0031d30f Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sat, 18 Apr 2020 19:01:25 +0200 Subject: [PATCH] xdata: add more helper methods to DataForm --- .../smackx/xdata/packet/DataForm.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java index cbe796492..3415041e4 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java @@ -20,6 +20,7 @@ package org.jivesoftware.smackx.xdata.packet; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; @@ -303,6 +304,20 @@ public class DataForm implements ExtensionElement { return Collections.unmodifiableList(extensionElements); } + /** + * Return the form type from the hidden form type field. + * + * @return the form type or null if this form has none set. + * @since 4.4.0 + */ + public String getFormType() { + FormField formTypeField = getHiddenFormTypeField(); + if (formTypeField == null) { + return null; + } + return formTypeField.getFirstValue(); + } + /** * Returns the hidden FORM_TYPE field or null if this data form has none. * @@ -364,6 +379,56 @@ public class DataForm implements ExtensionElement { return stanzaView.getExtension(DataForm.class); } + /** + * Get the data form with the given form type from a stanza view. + * + * @param stanzaView the stanza view to retrieve the data form from + * @param formType the form type + * @return the retrieved data form or null if there is no matching one + * @since 4.4.0 + */ + public static DataForm from(StanzaView stanzaView, String formType) { + List dataForms = stanzaView.getExtensions(DataForm.class); + return from(dataForms, formType); + } + + /** + * Return the first matching data form with the given form type from the given collection of data forms. + * + * @param dataForms the collection of data forms + * @param formType the form type to match for + * @return the first matching data form or null if there is none + * @since 4.4.0 + */ + public static DataForm from(Collection dataForms, String formType) { + for (DataForm dataForm : dataForms) { + if (formType.equals(dataForm.getFormType())) { + return dataForm; + } + } + return null; + } + + /** + * Remove the first matching data form with the given form type from the given collection. + * + * @param dataForms the collection of data forms + * @param formType the form type to match for + * @return the removed data form or null if there was none removed + * @since 4.4.0 + */ + public static DataForm remove(Collection dataForms, String formType) { + Iterator it = dataForms.iterator(); + while (it.hasNext()) { + DataForm dataForm = it.next(); + if (formType.equals(dataForm.getFormType())) { + it.remove(); + return dataForm; + } + } + return null; + } + /** * * Represents the fields that will be returned from a search. This information is useful when