Add XDataManager.isSupported(String)

also make FormField and Option implement NamedElement and some other
minor changes to data form API.
This commit is contained in:
Florian Schmaus 2015-01-21 09:49:57 +01:00
parent d97de5f42c
commit b265d2d2a2
2 changed files with 69 additions and 12 deletions

View File

@ -21,6 +21,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement;
@ -31,7 +32,7 @@ import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement;
*
* @author Gaston Dombiak
*/
public class FormField {
public class FormField implements NamedElement {
public static final String ELEMENT = "field";
@ -343,9 +344,13 @@ public class FormField {
}
}
@Override
public String getElementName() {
return ELEMENT;
}
public XmlStringBuilder toXML() {
XmlStringBuilder buf = new XmlStringBuilder();
buf.halfOpenElement(ELEMENT);
XmlStringBuilder buf = new XmlStringBuilder(this);
// Add attributes
buf.optAttribute("label", getLabel());
buf.optAttribute("var", getVariable());
@ -363,7 +368,7 @@ public class FormField {
buf.append(option.toXML());
}
buf.optElement(validateElement);
buf.closeElement(ELEMENT);
buf.closeElement(this);
return buf;
}
@ -391,7 +396,7 @@ public class FormField {
*
* @author Gaston Dombiak
*/
public static class Option {
public static class Option implements NamedElement {
public static final String ELEMENT = "option";
@ -430,9 +435,13 @@ public class FormField {
return getLabel();
}
@Override
public String getElementName() {
return ELEMENT;
}
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(ELEMENT);
XmlStringBuilder xml = new XmlStringBuilder(this);
// Add attribute
xml.optAttribute("label", getLabel());
xml.rightAngleBracket();
@ -440,7 +449,7 @@ public class FormField {
// Add element
xml.element("value", getValue());
xml.closeElement(ELEMENT);
xml.closeElement(this);
return xml;
}

View File

@ -1,6 +1,6 @@
/**
*
* Copyright 2014 Florian Schmaus
* Copyright 2014-2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,22 +16,70 @@
*/
package org.jivesoftware.smackx.xdata;
import java.util.Map;
import java.util.WeakHashMap;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.xdata.packet.DataForm;
public class XDataManager {
public class XDataManager extends Manager {
/**
* The value of {@link DataForm#NAMESPACE}.
*/
public static final String NAMESPACE = DataForm.NAMESPACE;
static {
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
@Override
public void connectionCreated(XMPPConnection connection) {
ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
serviceDiscoveryManager.addFeature(DataForm.NAMESPACE);
getInstanceFor(connection);
}
});
}
private static final Map<XMPPConnection, XDataManager> INSTANCES = new WeakHashMap<>();
/**
* Get the XDataManager for the given XMPP connection.
*
* @param connection
* @return the XDataManager
*/
public static synchronized XDataManager getInstanceFor(XMPPConnection connection) {
XDataManager xDataManager = INSTANCES.get(connection);
if (xDataManager == null) {
xDataManager = new XDataManager(connection);
INSTANCES.put(connection, xDataManager);
}
return xDataManager;
}
private XDataManager(XMPPConnection connection) {
super(connection);
ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
serviceDiscoveryManager.addFeature(NAMESPACE);
}
/**
* Check if the given entity supports data forms.
*
* @param jid the JID of the entity to check.
* @return true if the entity supports data forms.
* @throws NoResponseException
* @throws XMPPErrorException
* @throws NotConnectedException
* @see <a href="http://xmpp.org/extensions/xep-0004.html#disco">XEP-0004: Data Forms § 6. Service Discovery</a>
* @since 4.1
*/
public boolean isSupported(String jid) throws NoResponseException, XMPPErrorException, NotConnectedException {
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, NAMESPACE);
}
}