Search in sources :

Example 46 with FormField

use of org.jivesoftware.smackx.xdata.FormField in project xabber-android by redsolution.

the class CapabilitiesManager method isValid.

private boolean isValid(DiscoverInfo discoverInfo) {
    Set<DiscoverInfo.Identity> identities = new TreeSet<>(new Comparator<DiscoverInfo.Identity>() {

        private int compare(String string1, String string2) {
            return (string1 == null ? "" : string1).compareTo(string2 == null ? "" : string2);
        }

        @Override
        public int compare(DiscoverInfo.Identity identity1, DiscoverInfo.Identity identity2) {
            int result;
            result = compare(identity1.getCategory(), identity2.getCategory());
            if (result != 0) {
                return result;
            }
            result = compare(identity1.getType(), identity2.getType());
            if (result != 0) {
                return result;
            }
            result = compare(identity1.getLanguage(), identity2.getLanguage());
            if (result != 0) {
                return result;
            }
            result = compare(identity1.getName(), identity2.getName());
            if (result != 0) {
                return result;
            }
            return 0;
        }
    });
    for (DiscoverInfo.Identity identity : discoverInfo.getIdentities()) {
        if (!identities.add(identity)) {
            return false;
        }
    }
    Set<String> features = new HashSet<>();
    for (DiscoverInfo.Feature feature : discoverInfo.getFeatures()) {
        if (!features.add(feature.getVar())) {
            return false;
        }
    }
    Set<String> formTypes = new HashSet<>();
    for (ExtensionElement packetExtension : discoverInfo.getExtensions()) if (packetExtension instanceof DataForm) {
        DataForm dataForm = (DataForm) packetExtension;
        String formType = null;
        for (FormField formField : dataForm.getFields()) {
            if (FORM_TYPE.equals(formField.getVariable())) {
                for (String value : formField.getValues()) {
                    if (formType != null && !formType.equals(value)) {
                        return false;
                    }
                    formType = value;
                }
            }
        }
        if (!formTypes.add(formType)) {
            return false;
        }
    }
    return true;
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) TreeSet(java.util.TreeSet) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) FormField(org.jivesoftware.smackx.xdata.FormField) HashSet(java.util.HashSet)

Aggregations

FormField (org.jivesoftware.smackx.xdata.FormField)46 DataForm (org.jivesoftware.smackx.xdata.packet.DataForm)18 Test (org.junit.Test)10 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)6 ArrayList (java.util.ArrayList)4 TreeSet (java.util.TreeSet)3 Element (org.jivesoftware.smack.packet.Element)3 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)3 Form (org.jivesoftware.smackx.xdata.Form)3 RangeValidateElement (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement)3 XmlPullParser (org.xmlpull.v1.XmlPullParser)3 LinkedList (java.util.LinkedList)2 IQ (org.jivesoftware.smack.packet.IQ)2 RemoteCommand (org.jivesoftware.smackx.commands.RemoteCommand)2 StreamInitiation (org.jivesoftware.smackx.si.packet.StreamInitiation)2 ValidateElement (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Method (java.lang.reflect.Method)1 MessageDigest (java.security.MessageDigest)1 HashMap (java.util.HashMap)1