mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-16 01:02:06 +01:00
Suggested improvements (SMACK-799)
This commit is contained in:
parent
32b3212a9b
commit
5f13ece9bd
4 changed files with 85 additions and 72 deletions
|
@ -16,8 +16,6 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.reference;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
|
@ -25,12 +23,7 @@ import org.jivesoftware.smack.ConnectionCreationListener;
|
|||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
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.reference.element.ReferenceElement;
|
||||
|
||||
import org.jxmpp.jid.BareJid;
|
||||
|
||||
public final class ReferenceManager extends Manager {
|
||||
|
||||
|
@ -66,44 +59,4 @@ public final class ReferenceManager extends 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;
|
||||
|
||||
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.Stanza;
|
||||
import org.jivesoftware.smack.util.Objects;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
import org.jivesoftware.smackx.reference.ReferenceManager;
|
||||
|
||||
import org.jxmpp.jid.BareJid;
|
||||
|
||||
public class ReferenceElement implements ExtensionElement {
|
||||
|
||||
public static final String ELEMENT = "reference";
|
||||
|
@ -38,7 +47,7 @@ public class ReferenceElement implements ExtensionElement {
|
|||
private final Integer end;
|
||||
private final Type type;
|
||||
private final String anchor;
|
||||
private final String uri;
|
||||
private final URI uri;
|
||||
|
||||
// Non-XEP-compliant, but needed for SIMS
|
||||
private final ExtensionElement child;
|
||||
|
@ -53,7 +62,7 @@ public class ReferenceElement implements ExtensionElement {
|
|||
* @param uri
|
||||
* @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) {
|
||||
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) {
|
||||
throw new IllegalArgumentException("Attribute 'begin' MUST be smaller than attribute 'end'.");
|
||||
}
|
||||
if (type == null) {
|
||||
throw new NullPointerException("Attribute 'type' MUST NOT be null.");
|
||||
}
|
||||
Objects.requireNonNull(type);
|
||||
// TODO: The uri attribute is not mandatory according to SIMS, but it is according to references.
|
||||
/*if (uri == null) {
|
||||
throw new NullPointerException("Attribute 'uri' MUST NOT be null.");
|
||||
|
@ -87,7 +94,7 @@ public class ReferenceElement implements ExtensionElement {
|
|||
* @param anchor
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
@ -107,10 +114,55 @@ public class ReferenceElement implements ExtensionElement {
|
|||
return anchor;
|
||||
}
|
||||
|
||||
public String getUri() {
|
||||
public URI getUri() {
|
||||
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
|
||||
public String getNamespace() {
|
||||
return ReferenceManager.NAMESPACE;
|
||||
|
@ -128,7 +180,7 @@ public class ReferenceElement implements ExtensionElement {
|
|||
.optIntAttribute(ATTR_END, end != null ? end : -1)
|
||||
.attribute(ATTR_TYPE, type.toString())
|
||||
.optAttribute(ATTR_ANCHOR, anchor)
|
||||
.optAttribute(ATTR_URI, uri);
|
||||
.optAttribute(ATTR_URI, uri != null ? uri.toString() : null);
|
||||
|
||||
if (child == null) {
|
||||
return xml.closeEmptyElement();
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.reference.provider;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||
import org.jivesoftware.smack.provider.ExtensionElementProvider;
|
||||
import org.jivesoftware.smack.provider.ProviderManager;
|
||||
|
@ -35,9 +37,10 @@ public class ReferenceProvider extends ExtensionElementProvider<ReferenceElement
|
|||
String typeString = parser.getAttributeValue(null, ReferenceElement.ATTR_TYPE);
|
||||
ReferenceElement.Type type = ReferenceElement.Type.valueOf(typeString);
|
||||
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;
|
||||
while (true) {
|
||||
outerloop: while (true) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == XmlPullParser.START_TAG) {
|
||||
String elementName = parser.getName();
|
||||
|
@ -48,8 +51,10 @@ public class ReferenceProvider extends ExtensionElementProvider<ReferenceElement
|
|||
}
|
||||
}
|
||||
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 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.TestUtils;
|
||||
import org.jivesoftware.smackx.reference.element.ReferenceElement;
|
||||
|
@ -36,14 +39,14 @@ public class ReferenceTest extends SmackTestSuite {
|
|||
"end='78' " +
|
||||
"type='mention' " +
|
||||
"uri='xmpp:juliet@capulet.lit' />";
|
||||
ReferenceElement element = new ReferenceElement(72, 78, ReferenceElement.Type.mention, null,
|
||||
"xmpp:juliet@capulet.lit");
|
||||
URI uri = new URI("xmpp:juliet@capulet.lit");
|
||||
ReferenceElement element = new ReferenceElement(72, 78, ReferenceElement.Type.mention, null, uri);
|
||||
assertXMLEqual(xml, element.toXML().toString());
|
||||
assertEquals(72, (int) element.getBegin());
|
||||
assertEquals(78, (int) element.getEnd());
|
||||
assertEquals(ReferenceElement.Type.mention, element.getType());
|
||||
assertNull(element.getAnchor());
|
||||
assertEquals("xmpp:juliet@capulet.lit", element.getUri());
|
||||
assertEquals(uri, element.getUri());
|
||||
|
||||
ReferenceElement parsed = ReferenceProvider.TEST_PROVIDER.parse(TestUtils.getParser(xml));
|
||||
assertXMLEqual(xml, parsed.toXML().toString());
|
||||
|
@ -59,38 +62,38 @@ public class ReferenceTest extends SmackTestSuite {
|
|||
String xml = "<reference xmlns='urn:xmpp:reference:0' " +
|
||||
"type='data' " +
|
||||
"uri='xmpp:fdp.shakespeare.lit?;node=fdp/submitted/stan.isode.net/accidentreport;item=ndina872be' />";
|
||||
ReferenceElement element = new ReferenceElement(null, null, ReferenceElement.Type.data, null,
|
||||
"xmpp:fdp.shakespeare.lit?;node=fdp/submitted/stan.isode.net/accidentreport;item=ndina872be");
|
||||
URI uri = new 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);
|
||||
assertXMLEqual(xml, element.toXML().toString());
|
||||
|
||||
assertNull(element.getBegin());
|
||||
assertNull(element.getEnd());
|
||||
assertNull(element.getAnchor());
|
||||
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));
|
||||
assertXMLEqual(xml, parsed.toXML().toString());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void beginGreaterEndIllegalTest() {
|
||||
new ReferenceElement(100, 10, ReferenceElement.Type.mention, null, "test@test.test");
|
||||
public void beginGreaterEndIllegalTest() throws URISyntaxException {
|
||||
new ReferenceElement(100, 10, ReferenceElement.Type.mention, null, new URI("xmpp:test@test.test"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void beginSmallerZeroTest() {
|
||||
new ReferenceElement(-1, 12, ReferenceElement.Type.data, null, "test@test.test");
|
||||
public void beginSmallerZeroTest() throws URISyntaxException {
|
||||
new ReferenceElement(-1, 12, ReferenceElement.Type.data, null, new URI("xmpp:test@test.test"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void endSmallerZeroTest() {
|
||||
new ReferenceElement(12, -2, ReferenceElement.Type.mention, null, "test@test.test");
|
||||
public void endSmallerZeroTest() throws URISyntaxException {
|
||||
new ReferenceElement(12, -2, ReferenceElement.Type.mention, null, new URI("xmpp:test@test.test"));
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void typeArgumentNullTest() {
|
||||
new ReferenceElement(1, 2, null, null, "test@test.test");
|
||||
public void typeArgumentNullTest() throws URISyntaxException {
|
||||
new ReferenceElement(1, 2, null, null, new URI("xmpp:test@test.test"));
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in a new issue