1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-09 04:12:07 +01:00

Make ExtensionElement marker interface wrt. QNAME field

ExtensionElement is now a marker interface that requires all
implementation non-abstract classes to carry a static final QNAME
field (of type QName). This is verified by a new unit test.

Also FullyQualifiedElement is renamed to simply XmlElement. XmlElement
is used over ExtensionElement when implementing classes do not
statically know the qualified name of the XML elements they
represent. In general, XmlElement should be used sparingly, and every
XML element should be modeled by its own Java class (implementing
ExtensionElement).
This commit is contained in:
Florian Schmaus 2021-04-18 18:58:50 +02:00
parent 5493a22e44
commit 3d4e7938a7
146 changed files with 600 additions and 344 deletions

View file

@ -153,6 +153,7 @@ allprojects {
bouncyCastleVersion = '1.68'
guavaVersion = '30.1-jre'
mockitoVersion = '3.7.7'
orgReflectionsVersion = '0.9.11'
if (project.hasProperty("useSonatype")) {
useSonatype = project.getProperty("useSonatype").toBoolean()

View file

@ -74,7 +74,6 @@ import org.jivesoftware.smack.packet.AbstractStreamOpen;
import org.jivesoftware.smack.packet.Bind;
import org.jivesoftware.smack.packet.ErrorIQ;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.FullyQualifiedElement;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Mechanisms;
import org.jivesoftware.smack.packet.Message;
@ -92,6 +91,7 @@ import org.jivesoftware.smack.packet.StartTls;
import org.jivesoftware.smack.packet.StreamError;
import org.jivesoftware.smack.packet.StreamOpen;
import org.jivesoftware.smack.packet.TopLevelStreamElement;
import org.jivesoftware.smack.packet.XmlElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.packet.id.StanzaIdSource;
import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
@ -239,7 +239,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
protected final Lock connectionLock = new ReentrantLock();
protected final Map<QName, FullyQualifiedElement> streamFeatures = new HashMap<>();
protected final Map<QName, XmlElement> streamFeatures = new HashMap<>();
/**
* The full JID of the authenticated user, as returned by the resource binding response of the server.
@ -1859,7 +1859,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
XmlPullParser.Event eventType = parser.next();
if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getDepth() == initialDepth + 1) {
FullyQualifiedElement streamFeature = null;
XmlElement streamFeature = null;
String name = parser.getName();
String namespace = parser.getNamespace();
switch (name) {
@ -1928,7 +1928,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
@SuppressWarnings("unchecked")
@Override
public <F extends FullyQualifiedElement> F getFeature(QName qname) {
public <F extends XmlElement> F getFeature(QName qname) {
return (F) streamFeatures.get(qname);
}
@ -1937,7 +1937,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
return streamFeatures.containsKey(qname);
}
protected void addStreamFeature(FullyQualifiedElement feature) {
protected void addStreamFeature(XmlElement feature) {
QName key = feature.getQName();
streamFeatures.put(key, feature);
}

View file

@ -27,7 +27,6 @@ import org.jivesoftware.smack.filter.IQReplyFilter;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.iqrequest.IQRequestHandler;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.FullyQualifiedElement;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.MessageBuilder;
@ -36,6 +35,7 @@ import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.PresenceBuilder;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.StanzaFactory;
import org.jivesoftware.smack.packet.XmlElement;
import org.jivesoftware.smack.util.Consumer;
import org.jivesoftware.smack.util.Predicate;
import org.jivesoftware.smack.util.XmppElementUtil;
@ -583,7 +583,7 @@ public interface XMPPConnection {
*/
// TODO: Remove in Smack 4.5.
@Deprecated
default <F extends FullyQualifiedElement> F getFeature(String element, String namespace) {
default <F extends XmlElement> F getFeature(String element, String namespace) {
QName qname = new QName(namespace, element);
return getFeature(qname);
}
@ -597,7 +597,7 @@ public interface XMPPConnection {
* @return a stanza extensions of the feature or <code>null</code>
* @since 4.4
*/
<F extends FullyQualifiedElement> F getFeature(QName qname);
<F extends XmlElement> F getFeature(QName qname);
/**
* Get the feature stanza extensions for a given stream feature of the
@ -608,7 +608,7 @@ public interface XMPPConnection {
* @return a stanza extensions of the feature or <code>null</code>
* @since 4.4
*/
default <F extends FullyQualifiedElement> F getFeature(Class<F> featureClass) {
default <F extends XmlElement> F getFeature(Class<F> featureClass) {
QName qname = XmppElementUtil.getQNameFor(featureClass);
return getFeature(qname);
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2020 Florian Schmaus
* Copyright 2020-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,6 +20,7 @@ import javax.xml.namespace.QName;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.XmlElement;
import org.jivesoftware.smack.util.XmppElementUtil;
public class ExtensionElementFilter<E extends ExtensionElement> implements StanzaFilter {
@ -34,7 +35,7 @@ public class ExtensionElementFilter<E extends ExtensionElement> implements Stanz
@Override
public final boolean accept(Stanza stanza) {
ExtensionElement extensionElement = stanza.getExtension(extensionElementQName);
XmlElement extensionElement = stanza.getExtension(extensionElementQName);
if (extensionElement == null) {
return false;
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2014-2015 Florian Schmaus
* Copyright 2014-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -32,18 +32,18 @@ public class AbstractError {
protected final String textNamespace;
protected final Map<String, String> descriptiveTexts;
protected final List<ExtensionElement> extensions;
protected final List<XmlElement> extensions;
protected AbstractError(Map<String, String> descriptiveTexts) {
this(descriptiveTexts, null);
}
protected AbstractError(Map<String, String> descriptiveTexts, List<ExtensionElement> extensions) {
protected AbstractError(Map<String, String> descriptiveTexts, List<XmlElement> extensions) {
this(descriptiveTexts, null, extensions);
}
protected AbstractError(Map<String, String> descriptiveTexts, String textNamespace, List<ExtensionElement> extensions) {
protected AbstractError(Map<String, String> descriptiveTexts, String textNamespace, List<XmlElement> extensions) {
if (descriptiveTexts != null) {
this.descriptiveTexts = descriptiveTexts;
} else {
@ -108,7 +108,7 @@ public class AbstractError {
* @param <PE> type of the ExtensionElement.
* @return the extension, or <code>null</code> if it doesn't exist.
*/
public <PE extends ExtensionElement> PE getExtension(String elementName, String namespace) {
public <PE extends XmlElement> PE getExtension(String elementName, String namespace) {
return PacketUtil.extensionElementFrom(extensions, elementName, namespace);
}
@ -128,7 +128,7 @@ public class AbstractError {
public abstract static class Builder<B extends Builder<B>> {
protected String textNamespace;
protected Map<String, String> descriptiveTexts;
protected List<ExtensionElement> extensions;
protected List<XmlElement> extensions;
public B setDescriptiveTexts(Map<String, String> descriptiveTexts) {
if (descriptiveTexts == null) {
@ -173,7 +173,7 @@ public class AbstractError {
return getThis();
}
public B setExtensions(List<ExtensionElement> extensions) {
public B setExtensions(List<XmlElement> extensions) {
if (this.extensions == null) {
this.extensions = extensions;
}
@ -183,7 +183,7 @@ public class AbstractError {
return getThis();
}
public B addExtension(ExtensionElement extension) {
public B addExtension(XmlElement extension) {
if (extensions == null) {
extensions = new ArrayList<>();
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2003-2007 Jive Software, 2018 Florian Schmaus.
* Copyright 2003-2007 Jive Software, 2018-2021 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,21 +17,16 @@
package org.jivesoftware.smack.packet;
/**
* Interface to represent extension elements.
* Interface to represent XMPP extension elements. Unlike {@link XmlElement}, every non-abstract class that implements
* {@link ExtensionElement} must have a static final QNAME member of the type {@link javax.xml.namespace.QName}. This
* allows type-safe functions like {@link StanzaView#getExtension(Class)}. Hence this is a marker interface.
* <p>
* An extension element is an XML subdocument
* with a root element name and namespace. Extension elements are used to provide
* extended functionality beyond what is in the base XMPP specification. Examples of
* extensions elements include message events, message properties, and extra presence data.
* IQ stanzas have limited support for extension elements.
* <p>
* This class is used primarily for extended content in XMPP Stanzas, to act as so called "extension elements". For more
* information see <a href="https://tools.ietf.org/html/rfc6120#section-8.4">RFC 6120 § 8.4 Extended Content</a>.
* Use this class when implementing new extension elements when possible. This means that every instance of your
* implemented class must represent an XML element of the same qualified name.
* </p>
*
* @see org.jivesoftware.smack.provider.ExtensionElementProvider
* @author Matt Tucker
* @see <a href="https://tools.ietf.org/html/rfc6120#section-8.4">RFC 6120 § 8.4 Extended Content</a>
*/
public interface ExtensionElement extends FullyQualifiedElement {
public interface ExtensionElement extends XmlElement {
}

View file

@ -211,7 +211,7 @@ public abstract class IQ extends Stanza implements IqView {
xml.append(iqChildElement);
List<ExtensionElement> extensionsXml = getExtensions();
List<XmlElement> extensionsXml = getExtensions();
if (iqChildElement.isEmptyElement) {
if (extensionsXml.isEmpty()) {
xml.closeEmptyElement();

View file

@ -344,7 +344,7 @@ public final class Message extends MessageOrPresence<MessageBuilder>
@Deprecated
// TODO: Remove when stanza builder is ready.
public boolean removeBody(Body body) {
ExtensionElement removedElement = removeExtension(body);
XmlElement removedElement = removeExtension(body);
return removedElement != null;
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2003-2007 Jive Software, 2019-2020 Florian Schmaus
* Copyright 2003-2007 Jive Software, 2019-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -150,9 +150,9 @@ public interface MessageView extends StanzaView {
* @since 3.0.2
*/
default Set<Message.Body> getBodies() {
List<ExtensionElement> bodiesList = getExtensions(Message.Body.QNAME);
List<XmlElement> bodiesList = getExtensions(Message.Body.QNAME);
Set<Message.Body> resultSet = new HashSet<>(bodiesList.size());
for (ExtensionElement extensionElement : bodiesList) {
for (XmlElement extensionElement : bodiesList) {
Message.Body body = (Message.Body) extensionElement;
resultSet.add(body);
}

View file

@ -23,7 +23,7 @@ package org.jivesoftware.smack.packet;
* <p>
* Please note that usage of this interface is <b>discouraged</b>. The reason is that every XML element is fully
* qualified, i.e., it is qualified by a namespace. The namespace may not be explicitly given, but instead, is inherited
* from an outer element. Use {@link FullyQualifiedElement} instead when possible.
* from an outer element. Use {@link XmlElement} instead when possible.
* </p>
*/
public interface NamedElement extends Element {

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2003-2007 Jive Software, 2020 Florian Schmaus.
* Copyright 2003-2007 Jive Software, 2020-2021 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -319,7 +319,7 @@ public final class Presence extends MessageOrPresence<PresenceBuilder>
buf.attribute("type", type);
}
List<ExtensionElement> extensions = getExtensions();
List<XmlElement> extensions = getExtensions();
if (status == null
&& priority == null
&& (mode == null || mode == Mode.available)

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2015-2020 Florian Schmaus.
* Copyright 2015-2021 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -41,7 +41,7 @@ import org.jivesoftware.smack.util.XmlStringBuilder;
* @since 4.2
* @author Florian Schmaus
*/
public final class StandardExtensionElement implements ExtensionElement {
public final class StandardExtensionElement implements XmlElement {
private final String name;
private final String namespace;

View file

@ -64,7 +64,7 @@ public abstract class Stanza implements StanzaView, TopLevelStreamElement {
protected static final String DEFAULT_LANGUAGE =
java.util.Locale.getDefault().getLanguage().toLowerCase(Locale.US);
private final MultiMap<QName, ExtensionElement> extensionElements;
private final MultiMap<QName, XmlElement> extensionElements;
// Assume that all stanzas Smack handles are in the client namespace, since Smack is an XMPP client library. We can
// change this behavior later if it is required.
@ -284,18 +284,18 @@ public abstract class Stanza implements StanzaView, TopLevelStreamElement {
}
@Override
public final List<ExtensionElement> getExtensions() {
public final List<XmlElement> getExtensions() {
synchronized (extensionElements) {
// No need to create a new list, values() will already create a new one for us
return extensionElements.values();
}
}
public final MultiMap<QName, ExtensionElement> getExtensionsMap() {
public final MultiMap<QName, XmlElement> getExtensionsMap() {
return cloneExtensionsMap();
}
final MultiMap<QName, ExtensionElement> cloneExtensionsMap() {
final MultiMap<QName, XmlElement> cloneExtensionsMap() {
synchronized (extensionElements) {
return extensionElements.clone();
}
@ -312,7 +312,7 @@ public abstract class Stanza implements StanzaView, TopLevelStreamElement {
* @return a set of all matching extensions.
* @since 4.1
*/
public final List<ExtensionElement> getExtensions(String elementName, String namespace) {
public final List<XmlElement> getExtensions(String elementName, String namespace) {
requireNotNullNorEmpty(elementName, "elementName must not be null nor empty");
requireNotNullNorEmpty(namespace, "namespace must not be null nor empty");
QName key = new QName(namespace, elementName);
@ -320,8 +320,8 @@ public abstract class Stanza implements StanzaView, TopLevelStreamElement {
}
@Override
public final List<ExtensionElement> getExtensions(QName qname) {
List<ExtensionElement> res;
public final List<XmlElement> getExtensions(QName qname) {
List<XmlElement> res;
synchronized (extensionElements) {
res = extensionElements.getAll(qname);
}
@ -344,7 +344,8 @@ public abstract class Stanza implements StanzaView, TopLevelStreamElement {
* @param namespace the namespace of the extension that is desired.
* @return the stanza extension with the given namespace.
*/
public final ExtensionElement getExtension(String namespace) {
// TODO: Mark this method as deprecated in favor of getExtension(QName).
public final XmlElement getExtension(String namespace) {
return PacketUtil.extensionElementFrom(getExtensions(), null, namespace);
}
@ -361,12 +362,12 @@ public abstract class Stanza implements StanzaView, TopLevelStreamElement {
* @param namespace the XML element namespace of the extension.
* @return the extension, or <code>null</code> if it doesn't exist.
*/
public final ExtensionElement getExtensionElement(String elementName, String namespace) {
public final XmlElement getExtensionElement(String elementName, String namespace) {
if (namespace == null) {
return null;
}
QName key = new QName(namespace, elementName);
ExtensionElement packetExtension = getExtension(key);
XmlElement packetExtension = getExtension(key);
if (packetExtension == null) {
return null;
}
@ -390,7 +391,7 @@ public abstract class Stanza implements StanzaView, TopLevelStreamElement {
}
@Override
public final ExtensionElement getExtension(QName qname) {
public final XmlElement getExtension(QName qname) {
synchronized (extensionElements) {
return extensionElements.getFirst(qname);
}
@ -400,13 +401,13 @@ public abstract class Stanza implements StanzaView, TopLevelStreamElement {
* Adds a stanza extension to the packet. Does nothing if extension is null.
* <p>
* Please note that although this method is not yet marked as deprecated, it is recommended to use
* {@link StanzaBuilder#addExtension(ExtensionElement)} instead.
* {@link StanzaBuilder#addExtension(XmlElement)} instead.
* </p>
*
* @param extension a stanza extension.
*/
// TODO: Mark this as deprecated once StanzaBuilder is ready and all call sites are gone.
public final void addExtension(ExtensionElement extension) {
public final void addExtension(XmlElement extension) {
if (extension == null) return;
QName key = extension.getQName();
synchronized (extensionElements) {
@ -419,7 +420,7 @@ public abstract class Stanza implements StanzaView, TopLevelStreamElement {
* namespace.
* <p>
* Please note that although this method is not yet marked as deprecated, it is recommended to use
* {@link StanzaBuilder#overrideExtension(ExtensionElement)} instead.
* {@link StanzaBuilder#overrideExtension(XmlElement)} instead.
* </p>
*
* @param extension the extension element to add.
@ -427,13 +428,13 @@ public abstract class Stanza implements StanzaView, TopLevelStreamElement {
* @since 4.1.2
*/
// TODO: Mark this as deprecated once StanzaBuilder is ready and all call sites are gone.
public final ExtensionElement overrideExtension(ExtensionElement extension) {
public final XmlElement overrideExtension(XmlElement extension) {
if (extension == null) return null;
synchronized (extensionElements) {
// Note that we need to use removeExtension(String, String) here. If would use
// removeExtension(ExtensionElement) then we would remove based on the equality of ExtensionElement, which
// is not what we want in this case.
ExtensionElement removedExtension = removeExtension(extension.getElementName(), extension.getNamespace());
XmlElement removedExtension = removeExtension(extension.getElementName(), extension.getNamespace());
addExtension(extension);
return removedExtension;
}
@ -445,9 +446,9 @@ public abstract class Stanza implements StanzaView, TopLevelStreamElement {
* @param extensions a collection of stanza extensions
*/
// TODO: Mark this as deprecated once StanzaBuilder is ready and all call sites are gone.
public final void addExtensions(Collection<? extends ExtensionElement> extensions) {
public final void addExtensions(Collection<? extends XmlElement> extensions) {
if (extensions == null) return;
for (ExtensionElement packetExtension : extensions) {
for (XmlElement packetExtension : extensions) {
addExtension(packetExtension);
}
}
@ -476,7 +477,7 @@ public abstract class Stanza implements StanzaView, TopLevelStreamElement {
@Override
public final boolean hasExtension(String namespace) {
synchronized (extensionElements) {
for (ExtensionElement packetExtension : extensionElements.values()) {
for (XmlElement packetExtension : extensionElements.values()) {
if (packetExtension.getNamespace().equals(namespace)) {
return true;
}
@ -493,7 +494,7 @@ public abstract class Stanza implements StanzaView, TopLevelStreamElement {
* @return the removed stanza extension or null.
*/
// TODO: Mark this as deprecated once StanzaBuilder is ready and all call sites are gone.
public final ExtensionElement removeExtension(String elementName, String namespace) {
public final XmlElement removeExtension(String elementName, String namespace) {
QName key = new QName(namespace, elementName);
synchronized (extensionElements) {
return extensionElements.remove(key);
@ -509,10 +510,10 @@ public abstract class Stanza implements StanzaView, TopLevelStreamElement {
*/
@Deprecated
// TODO: Remove in Smack 4.5.
public final ExtensionElement removeExtension(ExtensionElement extension) {
public final XmlElement removeExtension(XmlElement extension) {
QName key = extension.getQName();
synchronized (extensionElements) {
List<ExtensionElement> list = extensionElements.getAll(key);
List<XmlElement> list = extensionElements.getAll(key);
boolean removed = list.remove(extension);
if (removed) {
return extension;

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2019-2020 Florian Schmaus
* Copyright 2019-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -44,7 +44,7 @@ public abstract class StanzaBuilder<B extends StanzaBuilder<B>> implements Stanz
String language;
MultiMap<QName, ExtensionElement> extensionElements = new MultiMap<>();
MultiMap<QName, XmlElement> extensionElements = new MultiMap<>();
protected StanzaBuilder(StanzaBuilder<?> other) {
stanzaIdSource = other.stanzaIdSource;
@ -156,13 +156,13 @@ public abstract class StanzaBuilder<B extends StanzaBuilder<B>> implements Stanz
return getThis();
}
public final B addExtension(ExtensionElement extensionElement) {
public final B addExtension(XmlElement extensionElement) {
QName key = extensionElement.getQName();
extensionElements.put(key, extensionElement);
return getThis();
}
public final B addOptExtensions(Collection<? extends ExtensionElement> extensionElements) {
public final B addOptExtensions(Collection<? extends XmlElement> extensionElements) {
if (extensionElements == null) {
return getThis();
}
@ -170,14 +170,14 @@ public abstract class StanzaBuilder<B extends StanzaBuilder<B>> implements Stanz
return addExtensions(extensionElements);
}
public final B addExtensions(Collection<? extends ExtensionElement> extensionElements) {
for (ExtensionElement extensionElement : extensionElements) {
public final B addExtensions(Collection<? extends XmlElement> extensionElements) {
for (XmlElement extensionElement : extensionElements) {
addExtension(extensionElement);
}
return getThis();
}
public final B overrideExtension(ExtensionElement extensionElement) {
public final B overrideExtension(XmlElement extensionElement) {
QName key = extensionElement.getQName();
extensionElements.remove(key);
extensionElements.put(key, extensionElement);
@ -214,17 +214,17 @@ public abstract class StanzaBuilder<B extends StanzaBuilder<B>> implements Stanz
}
@Override
public final ExtensionElement getExtension(QName qname) {
public final XmlElement getExtension(QName qname) {
return extensionElements.getFirst(qname);
}
@Override
public final List<ExtensionElement> getExtensions() {
public final List<XmlElement> getExtensions() {
return extensionElements.values();
}
@Override
public final List<ExtensionElement> getExtensions(QName qname) {
public final List<XmlElement> getExtensions(QName qname) {
return extensionElements.getAll(qname);
}

View file

@ -125,7 +125,7 @@ public class StanzaError extends AbstractError implements ExtensionElement {
* @param extensions list of stanza extensions
*/
public StanzaError(Condition condition, String conditionText, String errorGenerator, Type type, Map<String, String> descriptiveTexts,
List<ExtensionElement> extensions) {
List<XmlElement> extensions) {
super(descriptiveTexts, ERROR_CONDITION_AND_TEXT_NAMESPACE, extensions);
this.condition = Objects.requireNonNull(condition, "condition must not be null");
// Some implementations may send the condition as non-empty element containing the empty string, that is

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2019-2020 Florian Schmaus
* Copyright 2019-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -61,7 +61,7 @@ public interface StanzaView extends XmlLangElement {
*/
StanzaError getError();
ExtensionElement getExtension(QName qname);
XmlElement getExtension(QName qname);
default boolean hasExtension(QName qname) {
return getExtension(qname) != null;
@ -78,7 +78,7 @@ public interface StanzaView extends XmlLangElement {
* @return true if a stanza extension exists, false otherwise.
*/
default boolean hasExtension(String namespace) {
for (ExtensionElement packetExtension : getExtensions()) {
for (XmlElement packetExtension : getExtensions()) {
if (packetExtension.getNamespace().equals(namespace)) {
return true;
}
@ -89,7 +89,7 @@ public interface StanzaView extends XmlLangElement {
default <E extends ExtensionElement> E getExtension(Class<E> extensionElementClass) {
QName qname = XmppElementUtil.getQNameFor(extensionElementClass);
ExtensionElement extensionElement = getExtension(qname);
XmlElement extensionElement = getExtension(qname);
if (extensionElement == null) {
return null;
@ -103,9 +103,9 @@ public interface StanzaView extends XmlLangElement {
*
* @return a list of all extension elements of this stanza.
*/
List<ExtensionElement> getExtensions();
List<XmlElement> getExtensions();
List<ExtensionElement> getExtensions(QName qname);
List<XmlElement> getExtensions(QName qname);
/**
* Return all extension elements of the given type. Returns the empty list if there a none.

View file

@ -105,7 +105,7 @@ public class StreamError extends AbstractError implements Nonza {
private final Condition condition;
private final String conditionText;
public StreamError(Condition condition, String conditionText, Map<String, String> descriptiveTexts, List<ExtensionElement> extensions) {
public StreamError(Condition condition, String conditionText, Map<String, String> descriptiveTexts, List<XmlElement> extensions) {
super(descriptiveTexts, extensions);
// Some implementations may send the condition as non-empty element containing the empty string, that is
// <condition xmlns='foo'></condition>, in this case the parser may calls this constructor with the empty string

View file

@ -20,6 +20,6 @@ package org.jivesoftware.smack.packet;
* A XMPP top level stream element. This is either a stanza ({@link Stanza}) or
* just a plain stream element ({@link Nonza}).
*/
public interface TopLevelStreamElement extends FullyQualifiedElement {
public interface TopLevelStreamElement extends XmlElement {
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2018-2019 Florian Schmaus
* Copyright 2018-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,7 +18,23 @@ package org.jivesoftware.smack.packet;
import javax.xml.namespace.QName;
public interface FullyQualifiedElement extends NamedElement, XmlLangElement {
/**
* Interface to represent XML elements. Every XML element in XMPP has a qualified XML name ({@link QName}). This name
* can be obtained via {@link #getQName()}.
* <p>
* XMPP uses "extension elements", i.e. XML elements, to provide extended functionality beyond what is in the base XMPP
* specification. Examples of extensions elements include message events, message properties, and extra presence data.
* IQ stanzas have limited support for extension elements. See {@link ExtensionElement} for more information about XMPP
* extension elements.
* </p>
* <p>
* It is recommend to use {@link ExtensionElement} over this class when creating new extension elements.
* </p>
*
* @see org.jivesoftware.smack.provider.ExtensionElementProvider
* @since 4.5
*/
public interface XmlElement extends NamedElement, XmlLangElement {
/**
* Returns the root element XML namespace.

View file

@ -22,7 +22,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.XmlElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.util.PacketParserUtils;
@ -82,7 +82,7 @@ import org.jivesoftware.smack.xml.XmlPullParserException;
*
* @author Robin Collier
*/
public abstract class EmbeddedExtensionProvider<PE extends ExtensionElement> extends ExtensionElementProvider<PE> {
public abstract class EmbeddedExtensionProvider<PE extends XmlElement> extends ExtensionElementProvider<PE> {
@Override
public final PE parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
@ -95,7 +95,7 @@ public abstract class EmbeddedExtensionProvider<PE extends ExtensionElement> ext
attMap.put(parser.getAttributeName(i), parser.getAttributeValue(i));
}
List<ExtensionElement> extensions = new ArrayList<>();
List<XmlElement> extensions = new ArrayList<>();
XmlPullParser.Event event;
do {
event = parser.next();
@ -109,5 +109,5 @@ public abstract class EmbeddedExtensionProvider<PE extends ExtensionElement> ext
}
protected abstract PE createReturnExtension(String currentElement, String currentNamespace,
Map<String, String> attributeMap, List<? extends ExtensionElement> content);
Map<String, String> attributeMap, List<? extends XmlElement> content);
}

View file

@ -17,7 +17,7 @@
package org.jivesoftware.smack.provider;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.XmlElement;
/**
* An abstract class for parsing custom extensions elements. Each ExtensionElementProvider must
@ -26,6 +26,6 @@ import org.jivesoftware.smack.packet.ExtensionElement;
*
* @author Matt Tucker
*/
public abstract class ExtensionElementProvider<EE extends ExtensionElement> extends Provider<EE> {
public abstract class ExtensionElementProvider<E extends XmlElement> extends Provider<E> {
}

View file

@ -48,6 +48,7 @@ import org.jivesoftware.smack.packet.StanzaError;
import org.jivesoftware.smack.packet.StartTls;
import org.jivesoftware.smack.packet.StreamError;
import org.jivesoftware.smack.packet.UnparsedIQ;
import org.jivesoftware.smack.packet.XmlElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.parsing.StandardExtensionElementProvider;
@ -183,7 +184,7 @@ public class PacketParserUtils {
message.setError(parseError(parser, messageXmlEnvironment));
break;
default:
ExtensionElement extensionElement = parseExtensionElement(elementName, namespace, parser, messageXmlEnvironment);
XmlElement extensionElement = parseExtensionElement(elementName, namespace, parser, messageXmlEnvironment);
message.addExtension(extensionElement);
break;
}
@ -474,7 +475,7 @@ public class PacketParserUtils {
// Be extra robust: Skip PacketExtensions that cause Exceptions, instead of
// failing completely here. See SMACK-390 for more information.
try {
ExtensionElement extensionElement = parseExtensionElement(elementName, namespace, parser, presenceXmlEnvironment);
XmlElement extensionElement = parseExtensionElement(elementName, namespace, parser, presenceXmlEnvironment);
presence.addExtension(extensionElement);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to parse extension element in Presence stanza: " + presence, e);
@ -701,7 +702,7 @@ public class PacketParserUtils {
*/
public static StreamError parseStreamError(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
final int initialDepth = parser.getDepth();
List<ExtensionElement> extensions = new ArrayList<>();
List<XmlElement> extensions = new ArrayList<>();
Map<String, String> descriptiveTexts = null;
StreamError.Condition condition = null;
String conditionText = null;
@ -765,7 +766,7 @@ public class PacketParserUtils {
final int initialDepth = parser.getDepth();
Map<String, String> descriptiveTexts = null;
XmlEnvironment stanzaErrorXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
List<ExtensionElement> extensions = new ArrayList<>();
List<XmlElement> extensions = new ArrayList<>();
StanzaError.Builder builder = StanzaError.getBuilder();
// Parse the error header
@ -825,7 +826,7 @@ public class PacketParserUtils {
* @throws IOException if an I/O error occurred.
* @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
*/
public static ExtensionElement parseExtensionElement(String elementName, String namespace,
public static XmlElement parseExtensionElement(String elementName, String namespace,
XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
ParserUtils.assertAtStartTag(parser);
// See if a provider is registered to handle the extension.
@ -907,7 +908,7 @@ public class PacketParserUtils {
public static void addExtensionElement(StanzaBuilder<?> stanzaBuilder, XmlPullParser parser, String elementName,
String namespace, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
ExtensionElement extensionElement = parseExtensionElement(elementName, namespace, parser, outerXmlEnvironment);
XmlElement extensionElement = parseExtensionElement(elementName, namespace, parser, outerXmlEnvironment);
stanzaBuilder.addExtension(extensionElement);
}
@ -919,18 +920,18 @@ public class PacketParserUtils {
public static void addExtensionElement(Stanza packet, XmlPullParser parser, String elementName,
String namespace, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
ExtensionElement packetExtension = parseExtensionElement(elementName, namespace, parser, outerXmlEnvironment);
XmlElement packetExtension = parseExtensionElement(elementName, namespace, parser, outerXmlEnvironment);
packet.addExtension(packetExtension);
}
public static void addExtensionElement(Collection<ExtensionElement> collection, XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
public static void addExtensionElement(Collection<XmlElement> collection, XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException {
addExtensionElement(collection, parser, parser.getName(), parser.getNamespace(), outerXmlEnvironment);
}
public static void addExtensionElement(Collection<ExtensionElement> collection, XmlPullParser parser,
public static void addExtensionElement(Collection<XmlElement> collection, XmlPullParser parser,
String elementName, String namespace, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
ExtensionElement packetExtension = parseExtensionElement(elementName, namespace, parser, outerXmlEnvironment);
XmlElement packetExtension = parseExtensionElement(elementName, namespace, parser, outerXmlEnvironment);
collection.add(packetExtension);
}
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright © 2014-2020 Florian Schmaus
* Copyright © 2014-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,7 +18,7 @@ package org.jivesoftware.smack.util;
import java.util.Collection;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.XmlElement;
public class PacketUtil {
@ -33,9 +33,9 @@ public class PacketUtil {
* @return the extension element
*/
@SuppressWarnings("unchecked")
public static <PE extends ExtensionElement> PE extensionElementFrom(Collection<ExtensionElement> collection,
public static <PE extends XmlElement> PE extensionElementFrom(Collection<XmlElement> collection,
String element, String namespace) {
for (ExtensionElement packetExtension : collection) {
for (XmlElement packetExtension : collection) {
if ((element == null || packetExtension.getElementName().equals(
element))
&& packetExtension.getNamespace().equals(namespace)) {

View file

@ -24,8 +24,8 @@ import java.util.Date;
import java.util.List;
import org.jivesoftware.smack.packet.Element;
import org.jivesoftware.smack.packet.FullyQualifiedElement;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jxmpp.util.XmppDateTime;
@ -42,7 +42,7 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
effectiveXmlEnvironment = null;
}
public XmlStringBuilder(FullyQualifiedElement pe) {
public XmlStringBuilder(XmlElement pe) {
this(pe, null);
}
@ -51,7 +51,7 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
halfOpenElement(e.getElementName());
}
public XmlStringBuilder(FullyQualifiedElement element, XmlEnvironment enclosingXmlEnvironment) {
public XmlStringBuilder(XmlElement element, XmlEnvironment enclosingXmlEnvironment) {
sb = new LazyStringBuilder();
halfOpenElement(element);
@ -516,7 +516,7 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
return escape(text.toString());
}
protected XmlStringBuilder prelude(FullyQualifiedElement pe) {
protected XmlStringBuilder prelude(XmlElement pe) {
return prelude(pe.getElementName(), pe.getNamespace());
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2018-2020 Florian Schmaus
* Copyright 2018-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,19 +24,19 @@ import java.util.logging.Logger;
import javax.xml.namespace.QName;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.FullyQualifiedElement;
import org.jivesoftware.smack.packet.StandardExtensionElement;
import org.jivesoftware.smack.packet.XmlElement;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jxmpp.util.cache.LruCache;
public class XmppElementUtil {
private static final LruCache<Class<? extends FullyQualifiedElement>, QName> CLASS_TO_QNAME_CACHE = new LruCache<>(512);
private static final LruCache<Class<? extends XmlElement>, QName> CLASS_TO_QNAME_CACHE = new LruCache<>(512);
public static final Logger LOGGER = Logger.getLogger(XmppElementUtil.class.getName());
public static QName getQNameFor(Class<? extends FullyQualifiedElement> fullyQualifiedElement) {
public static QName getQNameFor(Class<? extends XmlElement> fullyQualifiedElement) {
QName qname = CLASS_TO_QNAME_CACHE.get(fullyQualifiedElement);
if (qname != null) {
return qname;
@ -72,24 +72,24 @@ public class XmppElementUtil {
}
public static <E extends ExtensionElement> List<E> getElementsFrom(
MultiMap<QName, ExtensionElement> elementMap, Class<E> extensionElementClass) {
MultiMap<QName, XmlElement> elementMap, Class<E> extensionElementClass) {
QName qname = XmppElementUtil.getQNameFor(extensionElementClass);
List<ExtensionElement> extensionElements = elementMap.getAll(qname);
List<XmlElement> extensionElements = elementMap.getAll(qname);
if (extensionElements.isEmpty()) {
return Collections.emptyList();
}
List<E> res = new ArrayList<>(extensionElements.size());
for (ExtensionElement extensionElement : extensionElements) {
for (XmlElement extensionElement : extensionElements) {
E e = castOrThrow(extensionElement, extensionElementClass);
res.add(e);
}
return res;
}
public static <E extends ExtensionElement> E castOrThrow(ExtensionElement extensionElement, Class<E> extensionElementClass) {
public static <E extends ExtensionElement> E castOrThrow(XmlElement extensionElement, Class<E> extensionElementClass) {
if (!extensionElementClass.isInstance(extensionElement)) {
final QName qname = getQNameFor(extensionElementClass);

View file

@ -101,7 +101,7 @@ public class StreamErrorTest {
assertNotNull(error);
assertEquals(Condition.conflict, error.getCondition());
assertEquals("Replaced by new connection", error.getDescriptiveText());
ExtensionElement appSpecificElement = error.getExtension("appSpecificElement", "myns");
XmlElement appSpecificElement = error.getExtension("appSpecificElement", "myns");
assertNotNull(appSpecificElement);
}