1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 22:32:06 +01:00

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.Collections;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement; import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement;
@ -31,7 +32,7 @@ import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement;
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class FormField { public class FormField implements NamedElement {
public static final String ELEMENT = "field"; public static final String ELEMENT = "field";
@ -343,9 +344,13 @@ public class FormField {
} }
} }
@Override
public String getElementName() {
return ELEMENT;
}
public XmlStringBuilder toXML() { public XmlStringBuilder toXML() {
XmlStringBuilder buf = new XmlStringBuilder(); XmlStringBuilder buf = new XmlStringBuilder(this);
buf.halfOpenElement(ELEMENT);
// Add attributes // Add attributes
buf.optAttribute("label", getLabel()); buf.optAttribute("label", getLabel());
buf.optAttribute("var", getVariable()); buf.optAttribute("var", getVariable());
@ -363,7 +368,7 @@ public class FormField {
buf.append(option.toXML()); buf.append(option.toXML());
} }
buf.optElement(validateElement); buf.optElement(validateElement);
buf.closeElement(ELEMENT); buf.closeElement(this);
return buf; return buf;
} }
@ -391,7 +396,7 @@ public class FormField {
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public static class Option { public static class Option implements NamedElement {
public static final String ELEMENT = "option"; public static final String ELEMENT = "option";
@ -430,9 +435,13 @@ public class FormField {
return getLabel(); return getLabel();
} }
@Override
public String getElementName() {
return ELEMENT;
}
public XmlStringBuilder toXML() { public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder(); XmlStringBuilder xml = new XmlStringBuilder(this);
xml.halfOpenElement(ELEMENT);
// Add attribute // Add attribute
xml.optAttribute("label", getLabel()); xml.optAttribute("label", getLabel());
xml.rightAngleBracket(); xml.rightAngleBracket();
@ -440,7 +449,7 @@ public class FormField {
// Add element // Add element
xml.element("value", getValue()); xml.element("value", getValue());
xml.closeElement(ELEMENT); xml.closeElement(this);
return xml; 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,22 +16,70 @@
*/ */
package org.jivesoftware.smackx.xdata; package org.jivesoftware.smackx.xdata;
import java.util.Map;
import java.util.WeakHashMap;
import org.jivesoftware.smack.ConnectionCreationListener; 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.XMPPConnection;
import org.jivesoftware.smack.XMPPConnectionRegistry; import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.xdata.packet.DataForm; 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 { static {
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() { XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
@Override @Override
public void connectionCreated(XMPPConnection connection) { public void connectionCreated(XMPPConnection connection) {
ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection); getInstanceFor(connection);
serviceDiscoveryManager.addFeature(DataForm.NAMESPACE);
} }
}); });
} }
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);
}
} }