Suggested improvements (SMACK-799)
This commit is contained in:
parent
32b3212a9b
commit
5f13ece9bd
|
@ -16,8 +16,6 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.reference;
|
package org.jivesoftware.smackx.reference;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
|
@ -25,12 +23,7 @@ import org.jivesoftware.smack.ConnectionCreationListener;
|
||||||
import org.jivesoftware.smack.Manager;
|
import org.jivesoftware.smack.Manager;
|
||||||
import org.jivesoftware.smack.XMPPConnection;
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
import org.jivesoftware.smack.XMPPConnectionRegistry;
|
import org.jivesoftware.smack.XMPPConnectionRegistry;
|
||||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
|
||||||
import org.jivesoftware.smack.packet.Stanza;
|
|
||||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||||
import org.jivesoftware.smackx.reference.element.ReferenceElement;
|
|
||||||
|
|
||||||
import org.jxmpp.jid.BareJid;
|
|
||||||
|
|
||||||
public final class ReferenceManager extends Manager {
|
public final class ReferenceManager extends Manager {
|
||||||
|
|
||||||
|
@ -66,44 +59,4 @@ public final class ReferenceManager extends Manager {
|
||||||
}
|
}
|
||||||
return manager;
|
return manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a reference to another users bare jid to a stanza.
|
|
||||||
*
|
|
||||||
* @param stanza stanza.
|
|
||||||
* @param begin start index of the mention in the messages body.
|
|
||||||
* @param end end index of the mention in the messages body.
|
|
||||||
* @param jid referenced jid.
|
|
||||||
*/
|
|
||||||
public static void addMention(Stanza stanza, int begin, int end, BareJid jid) {
|
|
||||||
ReferenceElement reference = new ReferenceElement(begin, end, ReferenceElement.Type.mention, null,
|
|
||||||
"xmpp:" + jid.toString());
|
|
||||||
stanza.addExtension(reference);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a list of all reference extensions contained in a stanza.
|
|
||||||
* If there are no reference elements, return an empty list.
|
|
||||||
*
|
|
||||||
* @param stanza stanza
|
|
||||||
* @return list of all references contained in the stanza
|
|
||||||
*/
|
|
||||||
public static List<ReferenceElement> getReferencesFromStanza(Stanza stanza) {
|
|
||||||
List<ReferenceElement> references = new ArrayList<>();
|
|
||||||
List<ExtensionElement> extensions = stanza.getExtensions(ReferenceElement.ELEMENT, NAMESPACE);
|
|
||||||
for (ExtensionElement e : extensions) {
|
|
||||||
references.add((ReferenceElement) e);
|
|
||||||
}
|
|
||||||
return references;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return true, if the stanza contains at least one reference extension.
|
|
||||||
*
|
|
||||||
* @param stanza stanza
|
|
||||||
* @return true if stanza contains references
|
|
||||||
*/
|
|
||||||
public static boolean containsReferences(Stanza stanza) {
|
|
||||||
return getReferencesFromStanza(stanza).size() > 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,10 +16,19 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.reference.element;
|
package org.jivesoftware.smackx.reference.element;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
|
import org.jivesoftware.smack.packet.Stanza;
|
||||||
|
import org.jivesoftware.smack.util.Objects;
|
||||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||||
import org.jivesoftware.smackx.reference.ReferenceManager;
|
import org.jivesoftware.smackx.reference.ReferenceManager;
|
||||||
|
|
||||||
|
import org.jxmpp.jid.BareJid;
|
||||||
|
|
||||||
public class ReferenceElement implements ExtensionElement {
|
public class ReferenceElement implements ExtensionElement {
|
||||||
|
|
||||||
public static final String ELEMENT = "reference";
|
public static final String ELEMENT = "reference";
|
||||||
|
@ -38,7 +47,7 @@ public class ReferenceElement implements ExtensionElement {
|
||||||
private final Integer end;
|
private final Integer end;
|
||||||
private final Type type;
|
private final Type type;
|
||||||
private final String anchor;
|
private final String anchor;
|
||||||
private final String uri;
|
private final URI uri;
|
||||||
|
|
||||||
// Non-XEP-compliant, but needed for SIMS
|
// Non-XEP-compliant, but needed for SIMS
|
||||||
private final ExtensionElement child;
|
private final ExtensionElement child;
|
||||||
|
@ -53,7 +62,7 @@ public class ReferenceElement implements ExtensionElement {
|
||||||
* @param uri
|
* @param uri
|
||||||
* @param child
|
* @param child
|
||||||
*/
|
*/
|
||||||
public ReferenceElement(Integer begin, Integer end, Type type, String anchor, String uri, ExtensionElement child) {
|
public ReferenceElement(Integer begin, Integer end, Type type, String anchor, URI uri, ExtensionElement child) {
|
||||||
if (begin != null && begin < 0) {
|
if (begin != null && begin < 0) {
|
||||||
throw new IllegalArgumentException("Attribute 'begin' MUST NOT be smaller than 0.");
|
throw new IllegalArgumentException("Attribute 'begin' MUST NOT be smaller than 0.");
|
||||||
}
|
}
|
||||||
|
@ -63,9 +72,7 @@ public class ReferenceElement implements ExtensionElement {
|
||||||
if (begin != null && end != null && begin >= end) {
|
if (begin != null && end != null && begin >= end) {
|
||||||
throw new IllegalArgumentException("Attribute 'begin' MUST be smaller than attribute 'end'.");
|
throw new IllegalArgumentException("Attribute 'begin' MUST be smaller than attribute 'end'.");
|
||||||
}
|
}
|
||||||
if (type == null) {
|
Objects.requireNonNull(type);
|
||||||
throw new NullPointerException("Attribute 'type' MUST NOT be null.");
|
|
||||||
}
|
|
||||||
// TODO: The uri attribute is not mandatory according to SIMS, but it is according to references.
|
// TODO: The uri attribute is not mandatory according to SIMS, but it is according to references.
|
||||||
/*if (uri == null) {
|
/*if (uri == null) {
|
||||||
throw new NullPointerException("Attribute 'uri' MUST NOT be null.");
|
throw new NullPointerException("Attribute 'uri' MUST NOT be null.");
|
||||||
|
@ -87,7 +94,7 @@ public class ReferenceElement implements ExtensionElement {
|
||||||
* @param anchor
|
* @param anchor
|
||||||
* @param uri
|
* @param uri
|
||||||
*/
|
*/
|
||||||
public ReferenceElement(Integer begin, Integer end, Type type, String anchor, String uri) {
|
public ReferenceElement(Integer begin, Integer end, Type type, String anchor, URI uri) {
|
||||||
this(begin, end, type, anchor, uri, null);
|
this(begin, end, type, anchor, uri, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,10 +114,55 @@ public class ReferenceElement implements ExtensionElement {
|
||||||
return anchor;
|
return anchor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUri() {
|
public URI getUri() {
|
||||||
return uri;
|
return uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a reference to another users bare jid to a stanza.
|
||||||
|
*
|
||||||
|
* @param stanza stanza.
|
||||||
|
* @param begin start index of the mention in the messages body.
|
||||||
|
* @param end end index of the mention in the messages body.
|
||||||
|
* @param jid referenced jid.
|
||||||
|
*/
|
||||||
|
public static void addMention(Stanza stanza, int begin, int end, BareJid jid) {
|
||||||
|
URI uri;
|
||||||
|
try {
|
||||||
|
uri = new URI("xmpp:" + jid.toString());
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
throw new AssertionError("Cannot create URI from bareJid.");
|
||||||
|
}
|
||||||
|
ReferenceElement reference = new ReferenceElement(begin, end, ReferenceElement.Type.mention, null, uri);
|
||||||
|
stanza.addExtension(reference);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a list of all reference extensions contained in a stanza.
|
||||||
|
* If there are no reference elements, return an empty list.
|
||||||
|
*
|
||||||
|
* @param stanza stanza
|
||||||
|
* @return list of all references contained in the stanza
|
||||||
|
*/
|
||||||
|
public static List<ReferenceElement> getReferencesFromStanza(Stanza stanza) {
|
||||||
|
List<ReferenceElement> references = new ArrayList<>();
|
||||||
|
List<ExtensionElement> extensions = stanza.getExtensions(ReferenceElement.ELEMENT, ReferenceManager.NAMESPACE);
|
||||||
|
for (ExtensionElement e : extensions) {
|
||||||
|
references.add((ReferenceElement) e);
|
||||||
|
}
|
||||||
|
return references;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true, if the stanza contains at least one reference extension.
|
||||||
|
*
|
||||||
|
* @param stanza stanza
|
||||||
|
* @return true if stanza contains references
|
||||||
|
*/
|
||||||
|
public static boolean containsReferences(Stanza stanza) {
|
||||||
|
return getReferencesFromStanza(stanza).size() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getNamespace() {
|
public String getNamespace() {
|
||||||
return ReferenceManager.NAMESPACE;
|
return ReferenceManager.NAMESPACE;
|
||||||
|
@ -128,7 +180,7 @@ public class ReferenceElement implements ExtensionElement {
|
||||||
.optIntAttribute(ATTR_END, end != null ? end : -1)
|
.optIntAttribute(ATTR_END, end != null ? end : -1)
|
||||||
.attribute(ATTR_TYPE, type.toString())
|
.attribute(ATTR_TYPE, type.toString())
|
||||||
.optAttribute(ATTR_ANCHOR, anchor)
|
.optAttribute(ATTR_ANCHOR, anchor)
|
||||||
.optAttribute(ATTR_URI, uri);
|
.optAttribute(ATTR_URI, uri != null ? uri.toString() : null);
|
||||||
|
|
||||||
if (child == null) {
|
if (child == null) {
|
||||||
return xml.closeEmptyElement();
|
return xml.closeEmptyElement();
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.reference.provider;
|
package org.jivesoftware.smackx.reference.provider;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
import org.jivesoftware.smack.provider.ExtensionElementProvider;
|
import org.jivesoftware.smack.provider.ExtensionElementProvider;
|
||||||
import org.jivesoftware.smack.provider.ProviderManager;
|
import org.jivesoftware.smack.provider.ProviderManager;
|
||||||
|
@ -35,9 +37,10 @@ public class ReferenceProvider extends ExtensionElementProvider<ReferenceElement
|
||||||
String typeString = parser.getAttributeValue(null, ReferenceElement.ATTR_TYPE);
|
String typeString = parser.getAttributeValue(null, ReferenceElement.ATTR_TYPE);
|
||||||
ReferenceElement.Type type = ReferenceElement.Type.valueOf(typeString);
|
ReferenceElement.Type type = ReferenceElement.Type.valueOf(typeString);
|
||||||
String anchor = parser.getAttributeValue(null, ReferenceElement.ATTR_ANCHOR);
|
String anchor = parser.getAttributeValue(null, ReferenceElement.ATTR_ANCHOR);
|
||||||
String uri = parser.getAttributeValue(null, ReferenceElement.ATTR_URI);
|
String uriString = parser.getAttributeValue(null, ReferenceElement.ATTR_URI);
|
||||||
|
URI uri = uriString != null ? new URI(uriString) : null;
|
||||||
ExtensionElement child = null;
|
ExtensionElement child = null;
|
||||||
while (true) {
|
outerloop: while (true) {
|
||||||
int eventType = parser.next();
|
int eventType = parser.next();
|
||||||
if (eventType == XmlPullParser.START_TAG) {
|
if (eventType == XmlPullParser.START_TAG) {
|
||||||
String elementName = parser.getName();
|
String elementName = parser.getName();
|
||||||
|
@ -48,8 +51,10 @@ public class ReferenceProvider extends ExtensionElementProvider<ReferenceElement
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (eventType == XmlPullParser.END_TAG) {
|
if (eventType == XmlPullParser.END_TAG) {
|
||||||
return new ReferenceElement(begin, end, type, anchor, uri, child);
|
break outerloop;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return new ReferenceElement(begin, end, type, anchor, uri, child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,9 @@ import static junit.framework.TestCase.assertEquals;
|
||||||
import static junit.framework.TestCase.assertNull;
|
import static junit.framework.TestCase.assertNull;
|
||||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
||||||
import org.jivesoftware.smack.test.util.TestUtils;
|
import org.jivesoftware.smack.test.util.TestUtils;
|
||||||
import org.jivesoftware.smackx.reference.element.ReferenceElement;
|
import org.jivesoftware.smackx.reference.element.ReferenceElement;
|
||||||
|
@ -36,14 +39,14 @@ public class ReferenceTest extends SmackTestSuite {
|
||||||
"end='78' " +
|
"end='78' " +
|
||||||
"type='mention' " +
|
"type='mention' " +
|
||||||
"uri='xmpp:juliet@capulet.lit' />";
|
"uri='xmpp:juliet@capulet.lit' />";
|
||||||
ReferenceElement element = new ReferenceElement(72, 78, ReferenceElement.Type.mention, null,
|
URI uri = new URI("xmpp:juliet@capulet.lit");
|
||||||
"xmpp:juliet@capulet.lit");
|
ReferenceElement element = new ReferenceElement(72, 78, ReferenceElement.Type.mention, null, uri);
|
||||||
assertXMLEqual(xml, element.toXML().toString());
|
assertXMLEqual(xml, element.toXML().toString());
|
||||||
assertEquals(72, (int) element.getBegin());
|
assertEquals(72, (int) element.getBegin());
|
||||||
assertEquals(78, (int) element.getEnd());
|
assertEquals(78, (int) element.getEnd());
|
||||||
assertEquals(ReferenceElement.Type.mention, element.getType());
|
assertEquals(ReferenceElement.Type.mention, element.getType());
|
||||||
assertNull(element.getAnchor());
|
assertNull(element.getAnchor());
|
||||||
assertEquals("xmpp:juliet@capulet.lit", element.getUri());
|
assertEquals(uri, element.getUri());
|
||||||
|
|
||||||
ReferenceElement parsed = ReferenceProvider.TEST_PROVIDER.parse(TestUtils.getParser(xml));
|
ReferenceElement parsed = ReferenceProvider.TEST_PROVIDER.parse(TestUtils.getParser(xml));
|
||||||
assertXMLEqual(xml, parsed.toXML().toString());
|
assertXMLEqual(xml, parsed.toXML().toString());
|
||||||
|
@ -59,38 +62,38 @@ public class ReferenceTest extends SmackTestSuite {
|
||||||
String xml = "<reference xmlns='urn:xmpp:reference:0' " +
|
String xml = "<reference xmlns='urn:xmpp:reference:0' " +
|
||||||
"type='data' " +
|
"type='data' " +
|
||||||
"uri='xmpp:fdp.shakespeare.lit?;node=fdp/submitted/stan.isode.net/accidentreport;item=ndina872be' />";
|
"uri='xmpp:fdp.shakespeare.lit?;node=fdp/submitted/stan.isode.net/accidentreport;item=ndina872be' />";
|
||||||
ReferenceElement element = new ReferenceElement(null, null, ReferenceElement.Type.data, null,
|
URI uri = new URI("xmpp:fdp.shakespeare.lit?;node=fdp/submitted/stan.isode.net/accidentreport;item=ndina872be");
|
||||||
"xmpp:fdp.shakespeare.lit?;node=fdp/submitted/stan.isode.net/accidentreport;item=ndina872be");
|
ReferenceElement element = new ReferenceElement(null, null, ReferenceElement.Type.data, null, uri);
|
||||||
assertXMLEqual(xml, element.toXML().toString());
|
assertXMLEqual(xml, element.toXML().toString());
|
||||||
|
|
||||||
assertNull(element.getBegin());
|
assertNull(element.getBegin());
|
||||||
assertNull(element.getEnd());
|
assertNull(element.getEnd());
|
||||||
assertNull(element.getAnchor());
|
assertNull(element.getAnchor());
|
||||||
assertEquals(ReferenceElement.Type.data, element.getType());
|
assertEquals(ReferenceElement.Type.data, element.getType());
|
||||||
assertEquals("xmpp:fdp.shakespeare.lit?;node=fdp/submitted/stan.isode.net/accidentreport;item=ndina872be", element.getUri());
|
assertEquals(uri, element.getUri());
|
||||||
|
|
||||||
ReferenceElement parsed = ReferenceProvider.TEST_PROVIDER.parse(TestUtils.getParser(xml));
|
ReferenceElement parsed = ReferenceProvider.TEST_PROVIDER.parse(TestUtils.getParser(xml));
|
||||||
assertXMLEqual(xml, parsed.toXML().toString());
|
assertXMLEqual(xml, parsed.toXML().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void beginGreaterEndIllegalTest() {
|
public void beginGreaterEndIllegalTest() throws URISyntaxException {
|
||||||
new ReferenceElement(100, 10, ReferenceElement.Type.mention, null, "test@test.test");
|
new ReferenceElement(100, 10, ReferenceElement.Type.mention, null, new URI("xmpp:test@test.test"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void beginSmallerZeroTest() {
|
public void beginSmallerZeroTest() throws URISyntaxException {
|
||||||
new ReferenceElement(-1, 12, ReferenceElement.Type.data, null, "test@test.test");
|
new ReferenceElement(-1, 12, ReferenceElement.Type.data, null, new URI("xmpp:test@test.test"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void endSmallerZeroTest() {
|
public void endSmallerZeroTest() throws URISyntaxException {
|
||||||
new ReferenceElement(12, -2, ReferenceElement.Type.mention, null, "test@test.test");
|
new ReferenceElement(12, -2, ReferenceElement.Type.mention, null, new URI("xmpp:test@test.test"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
@Test(expected = NullPointerException.class)
|
||||||
public void typeArgumentNullTest() {
|
public void typeArgumentNullTest() throws URISyntaxException {
|
||||||
new ReferenceElement(1, 2, null, null, "test@test.test");
|
new ReferenceElement(1, 2, null, null, new URI("xmpp:test@test.test"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in New Issue