Smack/smack-experimental/src/main/java/org/jivesoftware/smackx/reference/element/ReferenceElement.java

194 lines
5.9 KiB
Java
Raw Normal View History

2018-02-04 23:08:52 +01:00
/**
*
* Copyright 2018 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.reference.element;
2018-02-07 22:02:51 +01:00
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
2018-02-04 23:08:52 +01:00
import org.jivesoftware.smack.packet.ExtensionElement;
2018-02-07 22:02:51 +01:00
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.util.Objects;
2018-02-04 23:08:52 +01:00
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.reference.ReferenceManager;
2018-02-07 22:02:51 +01:00
import org.jxmpp.jid.BareJid;
2018-02-04 23:08:52 +01:00
public class ReferenceElement implements ExtensionElement {
public static final String ELEMENT = "reference";
public static final String ATTR_BEGIN = "begin";
public static final String ATTR_END = "end";
public static final String ATTR_TYPE = "type";
public static final String ATTR_ANCHOR = "anchor";
public static final String ATTR_URI = "uri";
public enum Type {
mention,
data
}
private final Integer begin;
private final Integer end;
private final Type type;
private final String anchor;
2018-02-07 22:02:51 +01:00
private final URI uri;
2018-02-04 23:08:52 +01:00
// Non-XEP-compliant, but needed for SIMS
private final ExtensionElement child;
/**
* XEP-incompliant (v0.2) constructor. This is needed for SIMS.
*
* @param begin
* @param end
* @param type
* @param anchor
* @param uri
* @param child
*/
2018-02-07 22:02:51 +01:00
public ReferenceElement(Integer begin, Integer end, Type type, String anchor, URI uri, ExtensionElement child) {
2018-02-04 23:08:52 +01:00
if (begin != null && begin < 0) {
throw new IllegalArgumentException("Attribute 'begin' MUST NOT be smaller than 0.");
}
if (end != null && end < 0) {
throw new IllegalArgumentException("Attribute 'end' MUST NOT be smaller than 0.");
}
if (begin != null && end != null && begin >= end) {
throw new IllegalArgumentException("Attribute 'begin' MUST be smaller than attribute 'end'.");
}
2018-02-07 22:02:51 +01:00
Objects.requireNonNull(type);
2018-02-04 23:08:52 +01:00
// 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.");
}*/
this.begin = begin;
this.end = end;
this.type = type;
this.anchor = anchor;
this.uri = uri;
this.child = child;
}
/**
* XEP-Compliant constructor.
*
* @param begin
* @param end
* @param type
* @param anchor
* @param uri
*/
2018-02-07 22:02:51 +01:00
public ReferenceElement(Integer begin, Integer end, Type type, String anchor, URI uri) {
2018-02-04 23:08:52 +01:00
this(begin, end, type, anchor, uri, null);
}
public Integer getBegin() {
return begin;
}
public Integer getEnd() {
return end;
}
public Type getType() {
return type;
}
public String getAnchor() {
return anchor;
}
2018-02-07 22:02:51 +01:00
public URI getUri() {
2018-02-04 23:08:52 +01:00
return uri;
}
2018-02-07 22:02:51 +01:00
/**
* 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;
}
2018-02-04 23:08:52 +01:00
@Override
public String getNamespace() {
return ReferenceManager.NAMESPACE;
}
@Override
public String getElementName() {
return ELEMENT;
}
@Override
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this)
.optIntAttribute(ATTR_BEGIN, begin != null ? begin : -1)
.optIntAttribute(ATTR_END, end != null ? end : -1)
.attribute(ATTR_TYPE, type.toString())
.optAttribute(ATTR_ANCHOR, anchor)
2018-02-07 22:02:51 +01:00
.optAttribute(ATTR_URI, uri != null ? uri.toString() : null);
2018-02-04 23:08:52 +01:00
if (child == null) {
return xml.closeEmptyElement();
} else {
return xml.rightAngleBracket()
.append(child.toXML())
.closeElement(this);
}
}
}