Skip to content

Commit

Permalink
Add support for XEP-0350: Data Forms Geolocation Element
Browse files Browse the repository at this point in the history
Fixes SMACK-871.
  • Loading branch information
Flowdalic committed Jun 12, 2019
1 parent fa0c16d commit c6c904c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions documentation/extensions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Smack Extensions and currently supported XEPs of smack-extensions
| Best Practices for Resource Locking | [XEP-0296](https://xmpp.org/extensions/xep-0296.html) | n/a | Specifies best practices to be followed by Jabber/XMPP clients about when to lock into, and unlock away from, resources. |
| Last Message Correction | [XEP-0308](https://xmpp.org/extensions/xep-0308.html) | n/a | Provides a method for indicating that a message is a correction of the last sent message. |
| Last User Interaction in Presence | [XEP-0319](https://xmpp.org/extensions/xep-0319.html) | n/a | Communicate time of last user interaction via XMPP presence notifications. |
| Data Forms Geolocation Element | [XEP-0350](https://xmpp.org/extensions/xep-0350.html) | n/a | Allows to include XEP-0080 gelocation data in XEP-0004 data forms. |
| [Group Chat Invitations](invitation.md) | n/a | n/a | Send invitations to other users to join a group chat room. |
| [Jive Properties](properties.md) | n/a | n/a | TODO |

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2015-2017 Ishan Khanna, Fernando Ramirez
* Copyright 2015-2017 Ishan Khanna, Fernando Ramirez 2019 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,10 +29,12 @@
import org.jivesoftware.smack.packet.Message;

import org.jivesoftware.smackx.geoloc.packet.GeoLocation;
import org.jivesoftware.smackx.geoloc.provider.GeoLocationProvider;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jivesoftware.smackx.xdata.provider.FormFieldChildElementProviderManager;

import org.jxmpp.jid.Jid;

Expand All @@ -41,6 +43,9 @@ public final class GeoLocationManager extends Manager {
private static final Map<XMPPConnection, GeoLocationManager> INSTANCES = new WeakHashMap<>();

static {
FormFieldChildElementProviderManager.addFormFieldChildElementProvider(
GeoLocationProvider.GeoLocationFormFieldChildElementProvider.INSTANCE);

XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
@Override
public void connectionCreated(XMPPConnection connection) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2015-2017 Ishan Khanna, Fernando Ramirez
* Copyright 2015-2017 Ishan Khanna, Fernando Ramirez, 2019 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,23 +22,32 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.namespace.QName;

import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;

import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.FormFieldChildElement;

/**
* A GeoLocation Extension packet, which is used by the XMPP clients to exchange their respective geographic locations.
*
* @see <a href="http://www.xmpp.org/extensions/xep-0080.html">XEP-0080</a>
* @author Ishan Khanna
*/
public final class GeoLocation implements Serializable, ExtensionElement {
public final class GeoLocation implements Serializable, ExtensionElement, FormFieldChildElement {

private static final long serialVersionUID = 1L;

public static final String NAMESPACE = "http://jabber.org/protocol/geoloc";

public static final String ELEMENT = "geoloc";

public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

private static final Logger LOGGER = Logger.getLogger(GeoLocation.class.getName());

private final Double accuracy;
Expand Down Expand Up @@ -208,6 +217,11 @@ public URI getUri() {
return uri;
}

@Override
public QName getQName() {
return QNAME;
}

@Override
public String getElementName() {
return ELEMENT;
Expand Down Expand Up @@ -254,10 +268,19 @@ public static Builder builder() {
return new GeoLocation.Builder();
}

@Override
public boolean isExclusiveElement() {
return true;
}

public static GeoLocation from(Message message) {
return message.getExtension(ELEMENT, NAMESPACE);
}

public static GeoLocation from(FormField formField) {
return (GeoLocation) formField.getFormFieldChildElement(QNAME);
}

public static class Builder {

private Double accuracy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import java.io.IOException;

import javax.xml.namespace.QName;

import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.parsing.SmackParsingException.SmackTextParseException;
import org.jivesoftware.smack.parsing.SmackParsingException.SmackUriSyntaxParsingException;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
Expand All @@ -27,9 +30,12 @@
import org.jivesoftware.smack.xml.XmlPullParserException;

import org.jivesoftware.smackx.geoloc.packet.GeoLocation;
import org.jivesoftware.smackx.xdata.provider.FormFieldChildElementProvider;

public class GeoLocationProvider extends ExtensionElementProvider<GeoLocation> {

public static final GeoLocationProvider INSTANCE = new GeoLocationProvider();

@Override
public GeoLocation parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException,
SmackTextParseException, SmackUriSyntaxParsingException {
Expand Down Expand Up @@ -130,4 +136,21 @@ public GeoLocation parse(XmlPullParser parser, int initialDepth, XmlEnvironment
return builder.build();
}

public static class GeoLocationFormFieldChildElementProvider extends FormFieldChildElementProvider<GeoLocation> {

public static final GeoLocationFormFieldChildElementProvider INSTANCE = new GeoLocationFormFieldChildElementProvider();

@Override
public QName getQName() {
return GeoLocation.QNAME;
}

@Override
public GeoLocation parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException {
return GeoLocationProvider.INSTANCE.parse(parser, initialDepth, xmlEnvironment);
}

}

}

0 comments on commit c6c904c

Please sign in to comment.