diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/FormField.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/FormField.java index 92d11bebf..1b9b30bf3 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/FormField.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/FormField.java @@ -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; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/XDataManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/XDataManager.java index 23eb4d9f4..af90282a2 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/XDataManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/XDataManager.java @@ -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 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 XEP-0004: Data Forms ยง 6. Service Discovery + * @since 4.1 + */ + public boolean isSupported(String jid) throws NoResponseException, XMPPErrorException, NotConnectedException { + return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, NAMESPACE); + } }