Rework XML Element hierarchy and XmlStringBuilder

- Reduce the amount of types that are subtypes of NamedElement. See
javadoc of NamedElement for rationale.
- Work more with XmlEnvironment in XmlStringBuilder.
- Some minor changes to XmlStringBuilder API.
This commit is contained in:
Florian Schmaus 2019-09-07 18:17:08 +02:00
parent e9bcdf3e6d
commit 65576cf3c2
74 changed files with 653 additions and 523 deletions

View File

@ -114,9 +114,7 @@ public class AbstractError {
xml.escape(text); xml.escape(text);
xml.closeElement("text"); xml.closeElement("text");
} }
for (ExtensionElement packetExtension : extensions) { xml.append(extensions);
xml.append(packetExtension.toXML());
}
} }
public abstract static class Builder<B extends Builder<B>> { public abstract static class Builder<B extends Builder<B>> {

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright © 2017 Florian Schmaus * Copyright © 2017-2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -38,8 +38,7 @@ public abstract class AbstractTextElement implements ExtensionElement {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.optXmlLangAttribute(lang);
xml.rightAngleBracket(); xml.rightAngleBracket();
xml.escape(text); xml.escape(text);
xml.closeElement(this); xml.closeElement(this);
@ -50,6 +49,19 @@ public abstract class AbstractTextElement implements ExtensionElement {
return text; return text;
} }
@Override
public final String getLanguage() {
return lang;
}
/**
* Deprecated.
*
* @return deprecated
* @deprecated use {@link #getLanguage()} instead.
*/
@Deprecated
// TODO: Remove in Smack 4.5.
public final String getLang() { public final String getLang() {
return lang; return lang;
} }

View File

@ -32,4 +32,13 @@ public interface FullyQualifiedElement extends NamedElement {
String localPart = getElementName(); String localPart = getElementName();
return new QName(namespaceURI, localPart); return new QName(namespaceURI, localPart);
} }
/**
* Returns the xml:lang of this XML element, or null if one has not been set.
*
* @return the xml:lang of this XML element, or null.
*/
default String getLanguage() {
return null;
}
} }

View File

@ -136,6 +136,11 @@ public abstract class IQ extends Stanza {
return childElementNamespace; return childElementNamespace;
} }
@Override
public final String getElementName() {
return IQ_ELEMENT;
}
@Override @Override
public final String toString() { public final String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -150,9 +155,8 @@ public abstract class IQ extends Stanza {
@Override @Override
public final XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) { public final XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
XmlStringBuilder buf = new XmlStringBuilder(enclosingXmlEnvironment); XmlStringBuilder buf = new XmlStringBuilder(this, enclosingXmlEnvironment);
buf.halfOpenElement(IQ_ELEMENT); addCommonAttributes(buf);
addCommonAttributes(buf, enclosingXmlEnvironment);
if (type == null) { if (type == null) {
buf.attribute("type", "get"); buf.attribute("type", "get");
} }
@ -160,7 +164,7 @@ public abstract class IQ extends Stanza {
buf.attribute("type", type.toString()); buf.attribute("type", type.toString());
} }
buf.rightAngleBracket(); buf.rightAngleBracket();
buf.append(getChildElementXML(enclosingXmlEnvironment)); appendInnerXml(buf);
buf.closeElement(IQ_ELEMENT); buf.closeElement(IQ_ELEMENT);
return buf; return buf;
} }
@ -171,44 +175,52 @@ public abstract class IQ extends Stanza {
* *
* @return the child element section of the IQ XML. * @return the child element section of the IQ XML.
*/ */
// TODO: This method should not be part of the public API as it is mostly used for testing purposes, with the one
// exception of AdHocCommand.getRaw().
public final XmlStringBuilder getChildElementXML() { public final XmlStringBuilder getChildElementXML() {
return getChildElementXML(null); XmlStringBuilder xml = new XmlStringBuilder();
appendInnerXml(xml);
return xml;
} }
/** /**
* Returns the sub-element XML section of the IQ packet, or the empty String if there * Append the sub-element XML section of the IQ stanza.
* isn't one.
* *
* @param enclosingXmlEnvironment the enclosing XML namespace. * @param xml the XmlStringBuilder to append to.
* @return the child element section of the IQ XML.
* @since 4.3.0
*/ */
public final XmlStringBuilder getChildElementXML(XmlEnvironment enclosingXmlEnvironment) { private void appendInnerXml(XmlStringBuilder xml) {
XmlStringBuilder xml = new XmlStringBuilder();
if (type == Type.error) { if (type == Type.error) {
// Add the error sub-packet, if there is one. // Add the error sub-packet, if there is one.
appendErrorIfExists(xml, enclosingXmlEnvironment); appendErrorIfExists(xml);
return;
}
if (childElementName == null) {
return;
} }
else if (childElementName != null) {
// Add the query section if there is one.
IQChildElementXmlStringBuilder iqChildElement = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this));
if (iqChildElement != null) {
xml.append(iqChildElement);
List<ExtensionElement> extensionsXml = getExtensions(); // Add the query section if there is one.
if (iqChildElement.isEmptyElement) { IQChildElementXmlStringBuilder iqChildElement = getIQChildElementBuilder(
if (extensionsXml.isEmpty()) { new IQChildElementXmlStringBuilder(this));
xml.closeEmptyElement(); // TOOD: Document the cases where iqChildElement is null but childElementName not. And if there are none, change
return xml; // the logic.
} else { if (iqChildElement == null) {
xml.rightAngleBracket(); return;
}
}
xml.append(extensionsXml);
xml.closeElement(iqChildElement.element);
}
} }
return xml;
xml.append(iqChildElement);
List<ExtensionElement> extensionsXml = getExtensions();
if (iqChildElement.isEmptyElement) {
if (extensionsXml.isEmpty()) {
xml.closeEmptyElement();
return;
}
xml.rightAngleBracket();
}
xml.append(extensionsXml);
xml.closeElement(iqChildElement.element);
} }
/** /**

View File

@ -467,6 +467,11 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
return language; return language;
} }
@Override
public String getElementName() {
return ELEMENT;
}
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -481,9 +486,8 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
@Override @Override
public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) { public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
XmlStringBuilder buf = new XmlStringBuilder(enclosingXmlEnvironment); XmlStringBuilder buf = new XmlStringBuilder(this, enclosingXmlEnvironment);
buf.halfOpenElement(ELEMENT); addCommonAttributes(buf);
enclosingXmlEnvironment = addCommonAttributes(buf, enclosingXmlEnvironment);
buf.optAttribute("type", type); buf.optAttribute("type", type);
buf.rightAngleBracket(); buf.rightAngleBracket();
@ -497,16 +501,16 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
// Skip the default language // Skip the default language
if (subject.equals(defaultSubject)) if (subject.equals(defaultSubject))
continue; continue;
buf.append(subject.toXML()); buf.append(subject);
} }
buf.optElement("thread", thread); buf.optElement("thread", thread);
// Append the error subpacket if the message type is an error. // Append the error subpacket if the message type is an error.
if (type == Type.error) { if (type == Type.error) {
appendErrorIfExists(buf, enclosingXmlEnvironment); appendErrorIfExists(buf);
} }
// Add extension elements, if any are defined. // Add extension elements, if any are defined.
buf.append(getExtensions(), enclosingXmlEnvironment); buf.append(getExtensions());
buf.closeElement(ELEMENT); buf.closeElement(ELEMENT);
return buf; return buf;
@ -544,11 +548,7 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
this.subject = subject; this.subject = subject;
} }
/** @Override
* Returns the language of this message subject.
*
* @return the language of this message subject.
*/
public String getLanguage() { public String getLanguage() {
return language; return language;
} }
@ -592,8 +592,8 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.halfOpenElement(getElementName()).optXmlLangAttribute(getLanguage()).rightAngleBracket(); xml.rightAngleBracket();
xml.escape(subject); xml.escape(subject);
xml.closeElement(getElementName()); xml.closeElement(getElementName());
return xml; return xml;
@ -642,12 +642,7 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
this.namespace = Objects.requireNonNull(namespace); this.namespace = Objects.requireNonNull(namespace);
} }
/** @Override
* Returns the language of this message body or {@code null} if the body extension element does not explicitly
* set a language, but instead inherits it from the outer element (usually a {@link Message} stanza).
*
* @return the language of this message body or {@code null}.
*/
public String getLanguage() { public String getLanguage() {
return language; return language;
} }
@ -692,7 +687,7 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
@Override @Override
public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) { public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingXmlEnvironment); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingXmlEnvironment);
xml.optXmlLangAttribute(getLanguage()).rightAngleBracket(); xml.rightAngleBracket();
xml.escape(message); xml.escape(message);
xml.closeElement(getElementName()); xml.closeElement(getElementName());
return xml; return xml;

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright © 2014 Florian Schmaus * Copyright © 2014-2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,8 +18,13 @@
package org.jivesoftware.smack.packet; package org.jivesoftware.smack.packet;
/** /**
* Interface to represent a XML element. This is similar to {@link ExtensionElement}, but does not * Interface to represent a XML element. This is similar to {@link ExtensionElement}, but does not carry a single
* carry a namespace and is usually included as child element of an stanza extension. * namespace, but instead is used with multiple namespaces. Examples for this include MUC's &lt;destroy/&gt; element.
* <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.
* </p>
*/ */
public interface NamedElement extends Element { public interface NamedElement extends Element {

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright © 2014-2015 Florian Schmaus * Copyright © 2014-2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,6 +30,6 @@ package org.jivesoftware.smack.packet;
* @author Florian Schmaus * @author Florian Schmaus
* @see <a href="http://xmpp.org/extensions/xep-0360.html">XEP-0360: Nonzas (are not Stanzas)</a> * @see <a href="http://xmpp.org/extensions/xep-0360.html">XEP-0360: Nonzas (are not Stanzas)</a>
*/ */
public interface Nonza extends TopLevelStreamElement, FullyQualifiedElement { public interface Nonza extends TopLevelStreamElement {
} }

View File

@ -113,11 +113,7 @@ public interface Packet extends TopLevelStreamElement {
*/ */
void setError(StanzaError error); void setError(StanzaError error);
/** @Override
* Returns the xml:lang of this Stanza, or null if one has not been set.
*
* @return the xml:lang of this Stanza, or null.
*/
String getLanguage(); String getLanguage();
/** /**

View File

@ -256,6 +256,11 @@ public final class Presence extends Stanza implements TypedCloneable<Presence> {
this.mode = mode; this.mode = mode;
} }
@Override
public String getElementName() {
return ELEMENT;
}
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -277,9 +282,8 @@ public final class Presence extends Stanza implements TypedCloneable<Presence> {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder buf = new XmlStringBuilder(enclosingNamespace); XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
buf.halfOpenElement(ELEMENT); addCommonAttributes(buf);
addCommonAttributes(buf, enclosingNamespace);
if (type != Type.available) { if (type != Type.available) {
buf.attribute("type", type); buf.attribute("type", type);
} }
@ -291,10 +295,10 @@ public final class Presence extends Stanza implements TypedCloneable<Presence> {
buf.element("show", mode); buf.element("show", mode);
} }
buf.append(getExtensions(), enclosingNamespace); buf.append(getExtensions());
// Add the error sub-packet, if there is one. // Add the error sub-packet, if there is one.
appendErrorIfExists(buf, enclosingNamespace); appendErrorIfExists(buf);
buf.closeElement(ELEMENT); buf.closeElement(ELEMENT);

View File

@ -59,6 +59,10 @@ public abstract class Stanza implements TopLevelStreamElement {
private final MultiMap<QName, ExtensionElement> extensionElements = new MultiMap<>(); private final MultiMap<QName, ExtensionElement> extensionElements = new MultiMap<>();
// 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.
private final String namespace = StreamOpen.CLIENT_NAMESPACE;
private String id = null; private String id = null;
private Jid to; private Jid to;
private Jid from; private Jid from;
@ -283,11 +287,7 @@ public abstract class Stanza implements TopLevelStreamElement {
error = xmppErrorBuilder.build(); error = xmppErrorBuilder.build();
} }
/** @Override
* Returns the xml:lang of this Stanza, or null if one has not been set.
*
* @return the xml:lang of this Stanza, or null.
*/
public String getLanguage() { public String getLanguage() {
return language; return language;
} }
@ -491,6 +491,11 @@ public abstract class Stanza implements TopLevelStreamElement {
@Override @Override
public abstract String toString(); public abstract String toString();
@Override
public final String getNamespace() {
return namespace;
}
/** /**
* Returns the default language used for all messages containing localized content. * Returns the default language used for all messages containing localized content.
* *
@ -501,33 +506,14 @@ public abstract class Stanza implements TopLevelStreamElement {
} }
/** /**
* Add to, from, id and 'xml:lang' attributes * Add to, from, and id attributes.
* *
* @param xml the {@link XmlStringBuilder}. * @param xml the {@link XmlStringBuilder}.
* @param enclosingXmlEnvironment the enclosing XML namespace.
* @return the XML environment for this stanza.
*/ */
protected XmlEnvironment addCommonAttributes(XmlStringBuilder xml, XmlEnvironment enclosingXmlEnvironment) { protected final void addCommonAttributes(XmlStringBuilder xml) {
String language = getLanguage();
String namespace = StreamOpen.CLIENT_NAMESPACE;
if (enclosingXmlEnvironment != null) {
String effectiveEnclosingNamespace = enclosingXmlEnvironment.getEffectiveNamespaceOrUse(namespace);
switch (effectiveEnclosingNamespace) {
case StreamOpen.CLIENT_NAMESPACE:
case StreamOpen.SERVER_NAMESPACE:
break;
default:
namespace = effectiveEnclosingNamespace;
}
}
xml.xmlnsAttribute(namespace);
xml.optAttribute("to", getTo()); xml.optAttribute("to", getTo());
xml.optAttribute("from", getFrom()); xml.optAttribute("from", getFrom());
xml.optAttribute("id", getStanzaId()); xml.optAttribute("id", getStanzaId());
xml.xmllangAttribute(language);
return new XmlEnvironment(namespace, language);
} }
protected void logCommonAttributes(StringBuilder sb) { protected void logCommonAttributes(StringBuilder sb) {
@ -546,12 +532,11 @@ public abstract class Stanza implements TopLevelStreamElement {
* Append an XMPPError is this stanza has one set. * Append an XMPPError is this stanza has one set.
* *
* @param xml the XmlStringBuilder to append the error to. * @param xml the XmlStringBuilder to append the error to.
* @param enclosingXmlEnvironment the enclosing XML environment.
*/ */
protected void appendErrorIfExists(XmlStringBuilder xml, XmlEnvironment enclosingXmlEnvironment) { protected void appendErrorIfExists(XmlStringBuilder xml) {
StanzaError error = getError(); StanzaError error = getError();
if (error != null) { if (error != null) {
xml.append(error.toXML(enclosingXmlEnvironment)); xml.append(error);
} }
} }
} }

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright © 2014-2018 Florian Schmaus * Copyright © 2014-2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,6 +20,6 @@ package org.jivesoftware.smack.packet;
* A XMPP top level stream element. This is either a stanza ({@link Stanza}) or * A XMPP top level stream element. This is either a stanza ({@link Stanza}) or
* just a plain stream element ({@link Nonza}). * just a plain stream element ({@link Nonza}).
*/ */
public interface TopLevelStreamElement extends Element { public interface TopLevelStreamElement extends FullyQualifiedElement {
} }

View File

@ -106,6 +106,29 @@ public class XmlEnvironment {
return effectiveLanguage; return effectiveLanguage;
} }
public boolean effectiveLanguageEquals(String language) {
String effectiveLanguage = getEffectiveLanguage();
if (effectiveLanguage == null) {
return false;
}
return effectiveLanguage.equals(language);
}
private transient String toStringCache;
@Override
public String toString() {
if (toStringCache == null) {
StringBuilder sb = new StringBuilder();
sb.append(XmlEnvironment.class.getSimpleName()).append(' ');
sb.append("xmlns=").append(getEffectiveNamespace()).append(' ');
sb.append("xmllang=").append(getEffectiveLanguage()).append(' ');
toStringCache = sb.toString();
}
return toStringCache;
}
public static XmlEnvironment from(XmlPullParser parser) { public static XmlEnvironment from(XmlPullParser parser) {
return from(parser, null); return from(parser, null);
} }

View File

@ -38,17 +38,12 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
private final XmlEnvironment effectiveXmlEnvironment; private final XmlEnvironment effectiveXmlEnvironment;
public XmlStringBuilder() { public XmlStringBuilder() {
this((XmlEnvironment) null);
}
public XmlStringBuilder(XmlEnvironment effectiveXmlEnvironment) {
sb = new LazyStringBuilder(); sb = new LazyStringBuilder();
this.effectiveXmlEnvironment = effectiveXmlEnvironment; effectiveXmlEnvironment = null;
} }
public XmlStringBuilder(ExtensionElement pe) { public XmlStringBuilder(ExtensionElement pe) {
this(); this(pe, null);
prelude(pe);
} }
public XmlStringBuilder(NamedElement e) { public XmlStringBuilder(NamedElement e) {
@ -59,12 +54,24 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
public XmlStringBuilder(FullyQualifiedElement element, XmlEnvironment enclosingXmlEnvironment) { public XmlStringBuilder(FullyQualifiedElement element, XmlEnvironment enclosingXmlEnvironment) {
sb = new LazyStringBuilder(); sb = new LazyStringBuilder();
halfOpenElement(element); halfOpenElement(element);
if (enclosingXmlEnvironment != null
&& !enclosingXmlEnvironment.effectiveNamespaceEquals(element.getNamespace())) { String xmlNs = element.getNamespace();
xmlnsAttribute(element.getNamespace()); String xmlLang = element.getLanguage();
if (enclosingXmlEnvironment == null) {
xmlnsAttribute(xmlNs);
xmllangAttribute(xmlLang);
} else {
if (!enclosingXmlEnvironment.effectiveNamespaceEquals(xmlNs)) {
xmlnsAttribute(xmlNs);
}
if (!enclosingXmlEnvironment.effectiveLanguageEquals(xmlLang)) {
xmllangAttribute(xmlLang);
}
} }
effectiveXmlEnvironment = XmlEnvironment.builder() effectiveXmlEnvironment = XmlEnvironment.builder()
.withNamespace(element.getNamespace()) .withNamespace(xmlNs)
.withLanguage(xmlLang)
.withNext(enclosingXmlEnvironment) .withNext(enclosingXmlEnvironment)
.build(); .build();
} }
@ -124,6 +131,15 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
return this; return this;
} }
/**
* Deprecated.
*
* @param element deprecated.
* @return deprecated.
* @deprecated use {@link #append(Element)} instead.
*/
@Deprecated
// TODO: Remove in Smack 4.5.
public XmlStringBuilder element(Element element) { public XmlStringBuilder element(Element element) {
assert element != null; assert element != null;
return append(element.toXML()); return append(element.toXML());
@ -161,7 +177,7 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
public XmlStringBuilder optElement(Element element) { public XmlStringBuilder optElement(Element element) {
if (element != null) { if (element != null) {
append(element.toXML()); append(element);
} }
return this; return this;
} }
@ -435,6 +451,7 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
} }
public XmlStringBuilder xmllangAttribute(String value) { public XmlStringBuilder xmllangAttribute(String value) {
// TODO: This should probably be attribute(), not optAttribute().
optAttribute("xml:lang", value); optAttribute("xml:lang", value);
return this; return this;
} }
@ -499,13 +516,13 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
return this; return this;
} }
public XmlStringBuilder append(Collection<? extends Element> elements) { public XmlStringBuilder append(Element element) {
return append(elements, effectiveXmlEnvironment); return append(element.toXML(effectiveXmlEnvironment));
} }
public XmlStringBuilder append(Collection<? extends Element> elements, XmlEnvironment enclosingXmlEnvironment) { public XmlStringBuilder append(Collection<? extends Element> elements) {
for (Element element : elements) { for (Element element : elements) {
append(element.toXML(enclosingXmlEnvironment)); append(element);
} }
return this; return this;
} }

View File

@ -188,5 +188,10 @@ public class StanzaCollectorTest {
public String toString() { public String toString() {
return toXML().toString(); return toXML().toString();
} }
@Override
public String getElementName() {
return "packetId";
}
} }
} }

View File

@ -738,10 +738,6 @@ public class PacketParserUtilsTest {
.a("type", "chat") .a("type", "chat")
.a("xml:lang", "en") .a("xml:lang", "en")
.e("body") .e("body")
// TODO: Remove the following xml:lang once Smack's serialization toXml() API is aware of a potential
// scoping xml:lang value. The out message stanza already declares an xml:lang with the exact same
// value, hence this statement is redundant.
.a("xml:lang", "en")
.t("This is a test of the emergency broadcast system, 1.") .t("This is a test of the emergency broadcast system, 1.")
.up() .up()
.e("body") .e("body")

View File

@ -943,6 +943,11 @@ public class EnhancedDebugger extends SmackDebugger {
public String toString() { public String toString() {
return toXML((XmlEnvironment) null); return toXML((XmlEnvironment) null);
} }
@Override
public String getElementName() {
return null;
}
} }
/** /**

View File

@ -16,6 +16,7 @@
*/ */
package org.jivesoftware.smackx.hoxt.packet; package org.jivesoftware.smackx.hoxt.packet;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.Objects;
@ -145,12 +146,19 @@ public abstract class AbstractHttpOverXmpp extends IQ {
protected abstract B getThis(); protected abstract B getThis();
} }
private abstract static class HoxExtensionElement implements ExtensionElement {
@Override
public final String getNamespace() {
return NAMESPACE;
}
}
/** /**
* Representation of Data element. * Representation of Data element.
* <p> * <p>
* This class is immutable. * This class is immutable.
*/ */
public static class Data implements NamedElement { public static class Data extends HoxExtensionElement {
public static final String ELEMENT = "data"; public static final String ELEMENT = "data";
@ -172,9 +180,9 @@ public abstract class AbstractHttpOverXmpp extends IQ {
*/ */
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.rightAngleBracket(); xml.rightAngleBracket();
xml.element(child); xml.append(child);
xml.closeElement(this); xml.closeElement(this);
return xml; return xml;
} }
@ -199,7 +207,7 @@ public abstract class AbstractHttpOverXmpp extends IQ {
* <p> * <p>
* This class is immutable. * This class is immutable.
*/ */
public static class Text implements NamedElement { public static class Text extends HoxExtensionElement {
public static final String ELEMENT = "text"; public static final String ELEMENT = "text";
@ -216,7 +224,7 @@ public abstract class AbstractHttpOverXmpp extends IQ {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.rightAngleBracket(); xml.rightAngleBracket();
xml.optAppend(text); xml.optAppend(text);
xml.closeElement(this); xml.closeElement(this);
@ -243,7 +251,7 @@ public abstract class AbstractHttpOverXmpp extends IQ {
* <p> * <p>
* This class is immutable. * This class is immutable.
*/ */
public static class Base64 implements NamedElement { public static class Base64 extends HoxExtensionElement {
public static final String ELEMENT = "base64"; public static final String ELEMENT = "base64";
@ -260,7 +268,7 @@ public abstract class AbstractHttpOverXmpp extends IQ {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.rightAngleBracket(); xml.rightAngleBracket();
xml.optAppend(text); xml.optAppend(text);
xml.closeElement(this); xml.closeElement(this);
@ -287,7 +295,7 @@ public abstract class AbstractHttpOverXmpp extends IQ {
* <p> * <p>
* This class is immutable. * This class is immutable.
*/ */
public static class Xml implements NamedElement { public static class Xml extends HoxExtensionElement {
public static final String ELEMENT = "xml"; public static final String ELEMENT = "xml";
@ -304,7 +312,7 @@ public abstract class AbstractHttpOverXmpp extends IQ {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.rightAngleBracket(); xml.rightAngleBracket();
xml.optAppend(text); xml.optAppend(text);
xml.closeElement(this); xml.closeElement(this);
@ -331,7 +339,7 @@ public abstract class AbstractHttpOverXmpp extends IQ {
* <p> * <p>
* This class is immutable. * This class is immutable.
*/ */
public static class ChunkedBase64 implements NamedElement { public static class ChunkedBase64 extends HoxExtensionElement {
public static final String ELEMENT = "chunkedBase64"; public static final String ELEMENT = "chunkedBase64";
@ -348,7 +356,7 @@ public abstract class AbstractHttpOverXmpp extends IQ {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.attribute("streamId", streamId); xml.attribute("streamId", streamId);
xml.closeEmptyElement(); xml.closeEmptyElement();
return xml; return xml;
@ -374,7 +382,7 @@ public abstract class AbstractHttpOverXmpp extends IQ {
* <p> * <p>
* This class is immutable. * This class is immutable.
*/ */
public static class Ibb implements NamedElement { public static class Ibb extends HoxExtensionElement {
public static final String ELEMENT = "ibb"; public static final String ELEMENT = "ibb";
@ -391,7 +399,7 @@ public abstract class AbstractHttpOverXmpp extends IQ {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.attribute("sid", sid); xml.attribute("sid", sid);
xml.closeEmptyElement(); xml.closeEmptyElement();
return xml; return xml;

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright © 2016 Florian Schmaus * Copyright © 2016-2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,10 +18,10 @@ package org.jivesoftware.smackx.iot.control.element;
import java.util.Locale; import java.util.Locale;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
public abstract class SetData implements NamedElement { public abstract class SetData implements ExtensionElement {
public enum Type { public enum Type {
BOOL, BOOL,
@ -76,6 +76,11 @@ public abstract class SetData implements NamedElement {
return getType().toString(); return getType().toString();
} }
@Override
public final String getNamespace() {
return IoTSetRequest.NAMESPACE;
}
/** /**
* Returns the XML representation of this Element. * Returns the XML representation of this Element.
* *
@ -83,7 +88,7 @@ public abstract class SetData implements NamedElement {
*/ */
@Override @Override
public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.attribute("name", name); xml.attribute("name", name);
xml.attribute("value", value); xml.attribute("value", value);
xml.closeEmptyElement(); xml.closeEmptyElement();

View File

@ -0,0 +1,28 @@
/**
*
* Copyright 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.
* 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.iot.data.element;
import org.jivesoftware.smack.packet.ExtensionElement;
public abstract class IoTDataExtensionElement implements ExtensionElement {
@Override
public final String getNamespace() {
return IoTFieldsExtension.NAMESPACE;
}
}

View File

@ -16,10 +16,9 @@
*/ */
package org.jivesoftware.smackx.iot.data.element; package org.jivesoftware.smackx.iot.data.element;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
public abstract class IoTDataField implements NamedElement { public abstract class IoTDataField extends IoTDataExtensionElement {
enum Type { enum Type {
integer("int"), integer("int"),
@ -53,7 +52,7 @@ public abstract class IoTDataField implements NamedElement {
@Override @Override
public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.attribute("name", name).attribute("value", getValueString()); xml.attribute("name", name).attribute("value", getValueString());
// TODO handle 'unit' attribute as special case if <numeric/> is implemented. // TODO handle 'unit' attribute as special case if <numeric/> is implemented.
xml.closeEmptyElement(); xml.closeEmptyElement();

View File

@ -19,12 +19,10 @@ package org.jivesoftware.smackx.iot.data.element;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.iot.element.NodeInfo; import org.jivesoftware.smackx.iot.element.NodeInfo;
public class NodeElement implements NamedElement { public class NodeElement extends IoTDataExtensionElement {
public static final String ELEMENT = "node"; public static final String ELEMENT = "node";
@ -51,7 +49,7 @@ public class NodeElement implements NamedElement {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
nodeInfo.appendTo(xml); nodeInfo.appendTo(xml);
xml.rightAngleBracket(); xml.rightAngleBracket();

View File

@ -20,10 +20,9 @@ import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
public class TimestampElement implements NamedElement { public class TimestampElement extends IoTDataExtensionElement {
public static final String ELEMENT = "timestamp"; public static final String ELEMENT = "timestamp";
@ -46,7 +45,7 @@ public class TimestampElement implements NamedElement {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.attribute("value", date); xml.attribute("value", date);
xml.rightAngleBracket(); xml.rightAngleBracket();

View File

@ -52,7 +52,7 @@ public class Checksum implements ExtensionElement {
sb.optAttribute(ATTR_CREATOR, creator); sb.optAttribute(ATTR_CREATOR, creator);
sb.optAttribute(ATTR_NAME, name); sb.optAttribute(ATTR_NAME, name);
sb.rightAngleBracket(); sb.rightAngleBracket();
sb.element(file); sb.append(file);
sb.closeElement(this); sb.closeElement(this);
return sb; return sb;
} }

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2017 Paul Schaub * Copyright 2017 Paul Schaub, 2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.jivesoftware.smackx.jingle_filetransfer.element;
import java.io.File; import java.io.File;
import java.util.Date; import java.util.Date;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.hashes.element.HashElement; import org.jivesoftware.smackx.hashes.element.HashElement;
@ -27,8 +28,10 @@ import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildEleme
/** /**
* Content of type File. * Content of type File.
*/ */
public class JingleFileTransferChild extends JingleContentDescriptionChildElement { public class JingleFileTransferChild implements JingleContentDescriptionChildElement {
public static final String ELEMENT = "file"; public static final String ELEMENT = "file";
public static final String NAMESPACE = JingleFileTransfer.NAMESPACE_V5;
public static final String ELEM_DATE = "date"; public static final String ELEM_DATE = "date";
public static final String ELEM_DESC = "desc"; public static final String ELEM_DESC = "desc";
public static final String ELEM_MEDIA_TYPE = "media-type"; public static final String ELEM_MEDIA_TYPE = "media-type";
@ -87,8 +90,13 @@ public class JingleFileTransferChild extends JingleContentDescriptionChildElemen
} }
@Override @Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public String getNamespace() {
XmlStringBuilder sb = new XmlStringBuilder(this); return NAMESPACE;
}
@Override
public XmlStringBuilder toXML(XmlEnvironment enclosingNamespace) {
XmlStringBuilder sb = new XmlStringBuilder(this, enclosingNamespace);
sb.rightAngleBracket(); sb.rightAngleBracket();
sb.optElement(ELEM_DATE, date); sb.optElement(ELEM_DATE, date);

View File

@ -16,7 +16,7 @@
*/ */
package org.jivesoftware.smackx.jingle_filetransfer.element; package org.jivesoftware.smackx.jingle_filetransfer.element;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.FullyQualifiedElement;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.hashes.element.HashElement; import org.jivesoftware.smackx.hashes.element.HashElement;
@ -24,9 +24,11 @@ import org.jivesoftware.smackx.hashes.element.HashElement;
/** /**
* RangeElement which specifies, which range of a file shall be transferred. * RangeElement which specifies, which range of a file shall be transferred.
*/ */
public class Range implements NamedElement { public class Range implements FullyQualifiedElement {
public static final String ELEMENT = "range"; public static final String ELEMENT = "range";
public static final String NAMESPACE = JingleFileTransferChild.NAMESPACE;
public static final String ATTR_OFFSET = "offset"; public static final String ATTR_OFFSET = "offset";
public static final String ATTR_LENGTH = "length"; public static final String ATTR_LENGTH = "length";
@ -99,6 +101,11 @@ public class Range implements NamedElement {
return ELEMENT; return ELEMENT;
} }
@Override
public String getNamespace() {
return NAMESPACE;
}
@Override @Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder sb = new XmlStringBuilder(this); XmlStringBuilder sb = new XmlStringBuilder(this);
@ -108,7 +115,7 @@ public class Range implements NamedElement {
if (hash != null) { if (hash != null) {
sb.rightAngleBracket(); sb.rightAngleBracket();
sb.element(hash); sb.append(hash);
sb.closeElement(this); sb.closeElement(this);
} else { } else {
sb.closeEmptyElement(); sb.closeEmptyElement();

View File

@ -126,15 +126,14 @@ public class MamElements {
} }
@Override @Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.halfOpenElement(this);
xml.xmlnsAttribute(NAMESPACE);
xml.optAttribute("queryid", getQueryId()); xml.optAttribute("queryid", getQueryId());
xml.optAttribute("id", getId()); xml.optAttribute("id", getId());
xml.rightAngleBracket(); xml.rightAngleBracket();
xml.element(getForwarded()); xml.append(getForwarded());
xml.closeElement(this); xml.closeElement(this);
return xml; return xml;

View File

@ -124,7 +124,7 @@ public class MamFinIQ extends IQ {
xml.setEmptyElement(); xml.setEmptyElement();
} else { } else {
xml.rightAngleBracket(); xml.rightAngleBracket();
xml.element(rsmSet); xml.append(rsmSet);
} }
return xml; return xml;
} }

View File

@ -134,12 +134,12 @@ public class MamPrefsIQ extends IQ {
if (alwaysJids != null) { if (alwaysJids != null) {
MamElements.AlwaysJidListElement alwaysElement = new AlwaysJidListElement(alwaysJids); MamElements.AlwaysJidListElement alwaysElement = new AlwaysJidListElement(alwaysJids);
xml.element(alwaysElement); xml.append(alwaysElement);
} }
if (neverJids != null) { if (neverJids != null) {
MamElements.NeverJidListElement neverElement = new NeverJidListElement(neverJids); MamElements.NeverJidListElement neverElement = new NeverJidListElement(neverJids);
xml.element(neverElement); xml.append(neverElement);
} }
return xml; return xml;

View File

@ -16,14 +16,10 @@
*/ */
package org.jivesoftware.smackx.message_markup.element; package org.jivesoftware.smackx.message_markup.element;
import org.jivesoftware.smack.util.XmlStringBuilder; public class BlockQuoteElement extends MarkupElement.BlockLevelMarkupElement {
public class BlockQuoteElement implements MarkupElement.BlockLevelMarkupElement {
public static final String ELEMENT = "bquote"; public static final String ELEMENT = "bquote";
private final int start, end;
/** /**
* Create a new Block Quote element. * Create a new Block Quote element.
* *
@ -31,18 +27,7 @@ public class BlockQuoteElement implements MarkupElement.BlockLevelMarkupElement
* @param end end index * @param end end index
*/ */
public BlockQuoteElement(int start, int end) { public BlockQuoteElement(int start, int end) {
this.start = start; super(start, end);
this.end = end;
}
@Override
public int getStart() {
return start;
}
@Override
public int getEnd() {
return end;
} }
@Override @Override
@ -50,13 +35,4 @@ public class BlockQuoteElement implements MarkupElement.BlockLevelMarkupElement
return ELEMENT; return ELEMENT;
} }
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(this);
xml.attribute(ATTR_START, getStart());
xml.attribute(ATTR_END, getEnd());
xml.closeEmptyElement();
return xml;
}
} }

View File

@ -16,14 +16,10 @@
*/ */
package org.jivesoftware.smackx.message_markup.element; package org.jivesoftware.smackx.message_markup.element;
import org.jivesoftware.smack.util.XmlStringBuilder; public class CodeBlockElement extends MarkupElement.BlockLevelMarkupElement {
public class CodeBlockElement implements MarkupElement.BlockLevelMarkupElement {
public static final String ELEMENT = "bcode"; public static final String ELEMENT = "bcode";
private final int start, end;
/** /**
* Create a new Code Block element. * Create a new Code Block element.
* *
@ -31,18 +27,7 @@ public class CodeBlockElement implements MarkupElement.BlockLevelMarkupElement {
* @param end end index * @param end end index
*/ */
public CodeBlockElement(int start, int end) { public CodeBlockElement(int start, int end) {
this.start = start; super(start, end);
this.end = end;
}
@Override
public int getStart() {
return start;
}
@Override
public int getEnd() {
return end;
} }
@Override @Override
@ -50,13 +35,4 @@ public class CodeBlockElement implements MarkupElement.BlockLevelMarkupElement {
return ELEMENT; return ELEMENT;
} }
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(this);
xml.attribute(ATTR_START, getStart());
xml.attribute(ATTR_END, getEnd());
xml.closeEmptyElement();
return xml;
}
} }

View File

@ -19,15 +19,14 @@ package org.jivesoftware.smackx.message_markup.element;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
public class ListElement implements MarkupElement.MarkupChildElement { public class ListElement extends MarkupElement.NonEmptyChildElement {
public static final String ELEMENT = "list"; public static final String ELEMENT = "list";
public static final String ELEM_LI = "li"; public static final String ELEM_LI = "li";
private final int start, end;
private final List<ListEntryElement> entries; private final List<ListEntryElement> entries;
/** /**
@ -38,21 +37,10 @@ public class ListElement implements MarkupElement.MarkupChildElement {
* @param entries list entries * @param entries list entries
*/ */
public ListElement(int start, int end, List<ListEntryElement> entries) { public ListElement(int start, int end, List<ListEntryElement> entries) {
this.start = start; super(start, end);
this.end = end;
this.entries = Collections.unmodifiableList(entries); this.entries = Collections.unmodifiableList(entries);
} }
@Override
public int getStart() {
return start;
}
@Override
public int getEnd() {
return end;
}
/** /**
* Return a list of all list entries. * Return a list of all list entries.
* *
@ -68,22 +56,11 @@ public class ListElement implements MarkupElement.MarkupChildElement {
} }
@Override @Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public void appendInnerXml(XmlStringBuilder xml) {
XmlStringBuilder xml = new XmlStringBuilder(); xml.append(getEntries());
xml.halfOpenElement(this);
xml.attribute(ATTR_START, getStart());
xml.attribute(ATTR_END, getEnd());
xml.rightAngleBracket();
for (ListEntryElement li : getEntries()) {
xml.append(li.toXML());
}
xml.closeElement(this);
return xml;
} }
public static class ListEntryElement implements NamedElement { public static class ListEntryElement implements ExtensionElement {
private final int start; private final int start;
@ -109,11 +86,15 @@ public class ListElement implements MarkupElement.MarkupChildElement {
return ELEM_LI; return ELEM_LI;
} }
@Override
public String getNamespace() {
return MarkupElement.NAMESPACE;
}
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.halfOpenElement(this); xml.attribute(MarkupElement.MarkupChildElement.ATTR_START, getStart());
xml.attribute(ATTR_START, getStart());
xml.closeEmptyElement(); xml.closeEmptyElement();
return xml; return xml;
} }

View File

@ -22,7 +22,6 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
public class MarkupElement implements ExtensionElement { public class MarkupElement implements ExtensionElement {
@ -267,46 +266,89 @@ public class MarkupElement implements ExtensionElement {
} }
} }
/** /**
* Interface for child elements. * Interface for child elements.
*/ */
public interface MarkupChildElement extends NamedElement { public abstract static class MarkupChildElement implements ExtensionElement {
String ATTR_START = "start"; public static final String ATTR_START = "start";
String ATTR_END = "end"; public static final String ATTR_END = "end";
private final int start, end;
protected MarkupChildElement(int start, int end) {
this.start = start;
this.end = end;
}
/** /**
* Return the start index of this element. * Return the start index of this element.
* *
* @return start index * @return start index
*/ */
int getStart(); public final int getStart() {
return start;
}
/** /**
* Return the end index of this element. * Return the end index of this element.
* *
* @return end index * @return end index
*/ */
int getEnd(); public final int getEnd() {
return end;
}
@Override
public final String getNamespace() {
return NAMESPACE;
}
@Override
public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.attribute(ATTR_START, getStart());
xml.attribute(ATTR_END, getEnd());
afterXmlPrelude(xml);
return xml;
}
protected abstract void afterXmlPrelude(XmlStringBuilder xml);
}
public abstract static class NonEmptyChildElement extends MarkupChildElement {
protected NonEmptyChildElement(int start, int end) {
super(start, end);
}
@Override
protected final void afterXmlPrelude(XmlStringBuilder xml) {
xml.rightAngleBracket();
appendInnerXml(xml);
xml.closeElement(this);
}
protected abstract void appendInnerXml(XmlStringBuilder xml);
} }
/** /**
* Interface for block level child elements. * Interface for block level child elements.
*/ */
public interface BlockLevelMarkupElement extends MarkupChildElement { public abstract static class BlockLevelMarkupElement extends MarkupChildElement {
protected BlockLevelMarkupElement(int start, int end) {
super(start, end);
}
@Override
protected final void afterXmlPrelude(XmlStringBuilder xml) {
xml.closeEmptyElement();
}
} }
} }

View File

@ -21,11 +21,10 @@ import java.util.Set;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
public class SpanElement implements MarkupElement.MarkupChildElement { public class SpanElement extends MarkupElement.NonEmptyChildElement {
public static final String ELEMENT = "span"; public static final String ELEMENT = "span";
private final int start, end;
private final Set<SpanStyle> styles; private final Set<SpanStyle> styles;
/** /**
@ -36,21 +35,10 @@ public class SpanElement implements MarkupElement.MarkupChildElement {
* @param styles list of styles that apply to this span * @param styles list of styles that apply to this span
*/ */
public SpanElement(int start, int end, Set<SpanStyle> styles) { public SpanElement(int start, int end, Set<SpanStyle> styles) {
this.start = start; super(start, end);
this.end = end;
this.styles = Collections.unmodifiableSet(styles); this.styles = Collections.unmodifiableSet(styles);
} }
@Override
public int getStart() {
return start;
}
@Override
public int getEnd() {
return end;
}
/** /**
* Return all styles of this span. * Return all styles of this span.
* *
@ -76,18 +64,9 @@ public class SpanElement implements MarkupElement.MarkupChildElement {
} }
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { protected void appendInnerXml(XmlStringBuilder xml) {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(this);
xml.attribute(ATTR_START, getStart());
xml.attribute(ATTR_END, getEnd());
xml.rightAngleBracket();
for (SpanStyle style : getStyles()) { for (SpanStyle style : getStyles()) {
xml.halfOpenElement(style.toString()).closeEmptyElement(); xml.emptyElement(style);
} }
xml.closeElement(this);
return xml;
} }
} }

View File

@ -62,7 +62,7 @@ public class MUCLightAffiliationsIQ extends IQ {
Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = affiliations.entrySet().iterator(); Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = affiliations.entrySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry<Jid, MUCLightAffiliation> pair = it.next(); Map.Entry<Jid, MUCLightAffiliation> pair = it.next();
xml.element(new UserWithAffiliationElement(pair.getKey(), pair.getValue())); xml.append(new UserWithAffiliationElement(pair.getKey(), pair.getValue()));
} }
return xml; return xml;

View File

@ -90,7 +90,7 @@ public class MUCLightBlockingIQ extends IQ {
Iterator<Map.Entry<Jid, Boolean>> it = map.entrySet().iterator(); Iterator<Map.Entry<Jid, Boolean>> it = map.entrySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry<Jid, Boolean> pair = it.next(); Map.Entry<Jid, Boolean> pair = it.next();
xml.element(new BlockingElement(pair.getKey(), pair.getValue(), isRoom)); xml.append(new BlockingElement(pair.getKey(), pair.getValue(), isRoom));
} }
} }

View File

@ -71,7 +71,7 @@ public class MUCLightChangeAffiliationsIQ extends IQ {
Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = affiliations.entrySet().iterator(); Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = affiliations.entrySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry<Jid, MUCLightAffiliation> pair = it.next(); Map.Entry<Jid, MUCLightAffiliation> pair = it.next();
xml.element(new UserWithAffiliationElement(pair.getKey(), pair.getValue())); xml.append(new UserWithAffiliationElement(pair.getKey(), pair.getValue()));
} }
} }

View File

@ -52,7 +52,7 @@ public class MUCLightConfigurationIQ extends IQ {
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
xml.rightAngleBracket(); xml.rightAngleBracket();
xml.optElement("version", version); xml.optElement("version", version);
xml.element(new ConfigurationElement(configuration)); xml.append(new ConfigurationElement(configuration));
return xml; return xml;
} }

View File

@ -99,10 +99,10 @@ public class MUCLightCreateIQ extends IQ {
@Override @Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
xml.rightAngleBracket(); xml.rightAngleBracket();
xml.element(new ConfigurationElement(configuration)); xml.append(new ConfigurationElement(configuration));
if (!occupants.isEmpty()) { if (!occupants.isEmpty()) {
xml.element(new OccupantsElement(occupants)); xml.append(new OccupantsElement(occupants));
} }
return xml; return xml;

View File

@ -104,7 +104,7 @@ public abstract class MUCLightElements {
Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = affiliations.entrySet().iterator(); Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = affiliations.entrySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry<Jid, MUCLightAffiliation> pair = it.next(); Map.Entry<Jid, MUCLightAffiliation> pair = it.next();
xml.element(new UserWithAffiliationElement(pair.getKey(), pair.getValue())); xml.append(new UserWithAffiliationElement(pair.getKey(), pair.getValue()));
} }
xml.closeElement(this); xml.closeElement(this);
@ -303,7 +303,7 @@ public abstract class MUCLightElements {
Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = occupants.entrySet().iterator(); Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = occupants.entrySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry<Jid, MUCLightAffiliation> pair = it.next(); Map.Entry<Jid, MUCLightAffiliation> pair = it.next();
xml.element(new UserWithAffiliationElement(pair.getKey(), pair.getValue())); xml.append(new UserWithAffiliationElement(pair.getKey(), pair.getValue()));
} }
xml.closeElement("occupants"); xml.closeElement("occupants");

View File

@ -62,8 +62,8 @@ public class MUCLightInfoIQ extends IQ {
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
xml.rightAngleBracket(); xml.rightAngleBracket();
xml.optElement("version", version); xml.optElement("version", version);
xml.element(new ConfigurationElement(configuration)); xml.append(new ConfigurationElement(configuration));
xml.element(new OccupantsElement(occupants)); xml.append(new OccupantsElement(occupants));
return xml; return xml;
} }

View File

@ -115,7 +115,7 @@ public class EnablePushNotificationsIQ extends IQ {
dataForm.addField(field.build()); dataForm.addField(field.build());
} }
xml.element(dataForm); xml.append(dataForm);
} }
return xml; return xml;

View File

@ -131,12 +131,7 @@ public class SpoilerElement implements ExtensionElement {
return map; return map;
} }
/** @Override
* Return the language of the hint.
* May be null.
*
* @return language of hint text
*/
public String getLanguage() { public String getLanguage() {
return language; return language;
} }
@ -153,8 +148,7 @@ public class SpoilerElement implements ExtensionElement {
@Override @Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.optXmlLangAttribute(getLanguage());
if (getHint() == null) { if (getHint() == null) {
xml.closeEmptyElement(); xml.closeEmptyElement();
} else { } else {

View File

@ -212,19 +212,19 @@ public class MultipleRecipientManager {
if (to != null) { if (to != null) {
for (Jid jid : to) { for (Jid jid : to) {
packet.setTo(jid); packet.setTo(jid);
connection.sendStanza(new PacketCopy(packet.toXML())); connection.sendStanza(new PacketCopy(packet));
} }
} }
if (cc != null) { if (cc != null) {
for (Jid jid : cc) { for (Jid jid : cc) {
packet.setTo(jid); packet.setTo(jid);
connection.sendStanza(new PacketCopy(packet.toXML())); connection.sendStanza(new PacketCopy(packet));
} }
} }
if (bcc != null) { if (bcc != null) {
for (Jid jid : bcc) { for (Jid jid : bcc) {
packet.setTo(jid); packet.setTo(jid);
connection.sendStanza(new PacketCopy(packet.toXML())); connection.sendStanza(new PacketCopy(packet));
} }
} }
} }
@ -297,6 +297,7 @@ public class MultipleRecipientManager {
*/ */
private static final class PacketCopy extends Stanza { private static final class PacketCopy extends Stanza {
private final String elementName;
private final CharSequence text; private final CharSequence text;
/** /**
@ -305,8 +306,9 @@ public class MultipleRecipientManager {
* *
* @param text the whole text of the stanza to send * @param text the whole text of the stanza to send
*/ */
private PacketCopy(CharSequence text) { private PacketCopy(Stanza stanza) {
this.text = text; this.elementName = stanza.getElementName();
this.text = stanza.toXML();
} }
@Override @Override
@ -319,6 +321,11 @@ public class MultipleRecipientManager {
return toXML().toString(); return toXML().toString();
} }
@Override
public String getElementName() {
return elementName;
}
} }
} }

View File

@ -21,7 +21,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jxmpp.jid.Jid; import org.jxmpp.jid.Jid;
@ -129,7 +128,7 @@ public class MultipleAddresses implements ExtensionElement {
return buf; return buf;
} }
public static final class Address implements NamedElement { public static final class Address implements ExtensionElement {
public static final String ELEMENT = "address"; public static final String ELEMENT = "address";
@ -193,10 +192,15 @@ public class MultipleAddresses implements ExtensionElement {
return ELEMENT; return ELEMENT;
} }
@Override
public String getNamespace() {
return NAMESPACE;
}
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder buf = new XmlStringBuilder(); XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
buf.halfOpenElement(this).attribute("type", type); buf.attribute("type", type);
buf.optAttribute("jid", jid); buf.optAttribute("jid", jid);
buf.optAttribute("node", node); buf.optAttribute("node", node);
buf.optAttribute("desc", description); buf.optAttribute("desc", description);
@ -209,5 +213,6 @@ public class MultipleAddresses implements ExtensionElement {
buf.closeEmptyElement(); buf.closeEmptyElement();
return buf; return buf;
} }
} }
} }

View File

@ -21,8 +21,8 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.InternetAddress; import org.jivesoftware.smack.util.InternetAddress;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
@ -260,13 +260,20 @@ public class Bytestream extends IQ {
return xml; return xml;
} }
private abstract static class BytestreamExtensionElement implements ExtensionElement {
@Override
public final String getNamespace() {
return NAMESPACE;
}
}
/** /**
* Stanza extension that represents a potential SOCKS5 proxy for the file transfer. Stream hosts * Stanza extension that represents a potential SOCKS5 proxy for the file transfer. Stream hosts
* are forwarded to the target of the file transfer who then chooses and connects to one. * are forwarded to the target of the file transfer who then chooses and connects to one.
* *
* @author Alexander Wenckus * @author Alexander Wenckus
*/ */
public static class StreamHost implements NamedElement { public static class StreamHost extends BytestreamExtensionElement {
public static String ELEMENTNAME = "streamhost"; public static String ELEMENTNAME = "streamhost";
@ -342,7 +349,7 @@ public class Bytestream extends IQ {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.attribute("jid", getJID()); xml.attribute("jid", getJID());
xml.attribute("host", address); xml.attribute("host", address);
if (getPort() != 0) { if (getPort() != 0) {
@ -366,7 +373,7 @@ public class Bytestream extends IQ {
* *
* @author Alexander Wenckus * @author Alexander Wenckus
*/ */
public static class StreamHostUsed implements NamedElement { public static class StreamHostUsed extends BytestreamExtensionElement {
public static String ELEMENTNAME = "streamhost-used"; public static String ELEMENTNAME = "streamhost-used";
@ -397,7 +404,7 @@ public class Bytestream extends IQ {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.attribute("jid", getJID()); xml.attribute("jid", getJID());
xml.closeEmptyElement(); xml.closeEmptyElement();
return xml; return xml;
@ -409,7 +416,7 @@ public class Bytestream extends IQ {
* *
* @author Alexander Wenckus * @author Alexander Wenckus
*/ */
public static class Activate implements NamedElement { public static class Activate extends BytestreamExtensionElement {
public static String ELEMENTNAME = "activate"; public static String ELEMENTNAME = "activate";
@ -440,12 +447,13 @@ public class Bytestream extends IQ {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.rightAngleBracket(); xml.rightAngleBracket();
xml.escape(getTarget()); xml.escape(getTarget());
xml.closeElement(this); xml.closeElement(this);
return xml; return xml;
} }
} }
/** /**

View File

@ -103,7 +103,7 @@ public class DelayInformation implements ExtensionElement {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.attribute("stamp", XmppDateTime.formatXEP0082Date(stamp)); xml.attribute("stamp", XmppDateTime.formatXEP0082Date(stamp));
xml.optAttribute("from", from); xml.optAttribute("from", from);
xml.rightAngleBracket(); xml.rightAngleBracket();

View File

@ -72,10 +72,10 @@ public class Forwarded implements ExtensionElement {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.rightAngleBracket(); xml.rightAngleBracket();
xml.optElement(getDelayInformation()); xml.optElement(getDelayInformation());
xml.append(forwardedPacket.toXML(NAMESPACE)); xml.append(forwardedPacket);
xml.closeElement(this); xml.closeElement(this);
return xml; return xml;
} }

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2017 Florian Schmaus * Copyright 2017-2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,8 @@
*/ */
package org.jivesoftware.smackx.jingle.element; package org.jivesoftware.smackx.jingle.element;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.FullyQualifiedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
@ -24,9 +25,10 @@ import org.jivesoftware.smack.util.XmlStringBuilder;
/** /**
* Jingle content element. * Jingle content element.
*/ */
public final class JingleContent implements NamedElement { public final class JingleContent implements FullyQualifiedElement {
public static final String ELEMENT = "content"; public static final String ELEMENT = "content";
public static final String NAMESPACE = Jingle.NAMESPACE;
public static final String CREATOR_ATTRIBUTE_NAME = "creator"; public static final String CREATOR_ATTRIBUTE_NAME = "creator";
@ -132,8 +134,13 @@ public final class JingleContent implements NamedElement {
} }
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public String getNamespace() {
XmlStringBuilder xml = new XmlStringBuilder(this); return NAMESPACE;
}
@Override
public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingXmlEnvironment);
xml.attribute(CREATOR_ATTRIBUTE_NAME, creator); xml.attribute(CREATOR_ATTRIBUTE_NAME, creator);
xml.optAttribute(DISPOSITION_ATTRIBUTE_NAME, disposition); xml.optAttribute(DISPOSITION_ATTRIBUTE_NAME, disposition);
xml.attribute(NAME_ATTRIBUTE_NAME, name); xml.attribute(NAME_ATTRIBUTE_NAME, name);

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright © 2014-2017 Florian Schmaus * Copyright © 2014-2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,18 +16,12 @@
*/ */
package org.jivesoftware.smackx.jingle.element; package org.jivesoftware.smackx.jingle.element;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.FullyQualifiedElement;
/** /**
* An element found usually in 'description' elements. * An element found usually in 'description' elements.
* *
*/ */
public abstract class JingleContentDescriptionChildElement implements NamedElement { public interface JingleContentDescriptionChildElement extends FullyQualifiedElement {
public static final String ELEMENT = "payload-type";
@Override
public String getElementName() {
return ELEMENT;
}
} }

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2017 Florian Schmaus * Copyright 2017-2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,6 +20,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
/** /**
@ -66,8 +67,8 @@ public abstract class JingleContentTransport implements ExtensionElement {
} }
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment);
addExtraAttributes(xml); addExtraAttributes(xml);
if (candidates.isEmpty() && info == null) { if (candidates.isEmpty() && info == null) {

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2017 Florian Schmaus * Copyright 2017-2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,13 +16,13 @@
*/ */
package org.jivesoftware.smackx.jingle.element; package org.jivesoftware.smackx.jingle.element;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.FullyQualifiedElement;
/** /**
* An element found usually in Jingle 'transport' elements. * An element found usually in Jingle 'transport' elements.
* *
*/ */
public abstract class JingleContentTransportCandidate implements NamedElement { public abstract class JingleContentTransportCandidate implements FullyQualifiedElement {
public static final String ELEMENT = "candidate"; public static final String ELEMENT = "candidate";

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2017 Paul Schaub * Copyright 2017 Paul Schaub, 2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,11 +16,11 @@
*/ */
package org.jivesoftware.smackx.jingle.element; package org.jivesoftware.smackx.jingle.element;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.FullyQualifiedElement;
/** /**
* Abstract JingleContentTransportInfo element. * Abstract JingleContentTransportInfo element.
*/ */
public abstract class JingleContentTransportInfo implements NamedElement { public interface JingleContentTransportInfo extends FullyQualifiedElement {
} }

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2017 Florian Schmaus * Copyright 2017-2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,7 +19,8 @@ package org.jivesoftware.smackx.jingle.element;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.FullyQualifiedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
@ -29,9 +30,10 @@ import org.jivesoftware.smack.util.XmlStringBuilder;
* @see <a href="https://xmpp.org/extensions/xep-0166.html#def-reason">XEP-0166 § 7.4</a> * @see <a href="https://xmpp.org/extensions/xep-0166.html#def-reason">XEP-0166 § 7.4</a>
* *
*/ */
public class JingleReason implements NamedElement { public class JingleReason implements FullyQualifiedElement {
public static final String ELEMENT = "reason"; public static final String ELEMENT = "reason";
public static final String NAMESPACE = Jingle.NAMESPACE;
public static AlternativeSession AlternativeSession(String sessionId) { public static AlternativeSession AlternativeSession(String sessionId) {
return new AlternativeSession(sessionId); return new AlternativeSession(sessionId);
@ -114,11 +116,16 @@ public class JingleReason implements NamedElement {
} }
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public String getNamespace() {
XmlStringBuilder xml = new XmlStringBuilder(this); return NAMESPACE;
}
@Override
public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingXmlEnvironment);
xml.rightAngleBracket(); xml.rightAngleBracket();
xml.emptyElement(reason.asString); xml.emptyElement(reason);
xml.closeElement(this); xml.closeElement(this);
return xml; return xml;

View File

@ -16,6 +16,7 @@
*/ */
package org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements; package org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.InternetAddress; import org.jivesoftware.smack.util.InternetAddress;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
@ -33,6 +34,8 @@ import org.jxmpp.stringprep.XmppStringprepException;
*/ */
public final class JingleS5BTransportCandidate extends JingleContentTransportCandidate { public final class JingleS5BTransportCandidate extends JingleContentTransportCandidate {
public static final String NAMESPACE = JingleS5BTransport.NAMESPACE_V1;
public static final String ATTR_CID = "cid"; public static final String ATTR_CID = "cid";
public static final String ATTR_HOST = "host"; public static final String ATTR_HOST = "host";
public static final String ATTR_JID = "jid"; public static final String ATTR_JID = "jid";
@ -130,10 +133,15 @@ public final class JingleS5BTransportCandidate extends JingleContentTransportCan
return new Bytestream.StreamHost(jid, host, port); return new Bytestream.StreamHost(jid, host, port);
} }
@Override @Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public String getNamespace() {
XmlStringBuilder xml = new XmlStringBuilder(); return NAMESPACE;
xml.halfOpenElement(this); }
@Override
public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingXmlEnvironment);
xml.attribute(ATTR_CID, cid); xml.attribute(ATTR_CID, cid);
xml.attribute(ATTR_HOST, host); xml.attribute(ATTR_HOST, host);
xml.attribute(ATTR_JID, jid); xml.attribute(ATTR_JID, jid);
@ -207,4 +215,5 @@ public final class JingleS5BTransportCandidate extends JingleContentTransportCan
return new JingleS5BTransportCandidate(cid, host, jid, port, priority, type); return new JingleS5BTransportCandidate(cid, host, jid, port, priority, type);
} }
} }
} }

View File

@ -16,6 +16,7 @@
*/ */
package org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements; package org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.jingle.element.JingleContentTransportInfo; import org.jivesoftware.smackx.jingle.element.JingleContentTransportInfo;
@ -23,7 +24,14 @@ import org.jivesoftware.smackx.jingle.element.JingleContentTransportInfo;
/** /**
* Class representing possible SOCKS5 TransportInfo elements. * Class representing possible SOCKS5 TransportInfo elements.
*/ */
public abstract class JingleS5BTransportInfo extends JingleContentTransportInfo { public abstract class JingleS5BTransportInfo implements JingleContentTransportInfo {
public static final String NAMESPACE = JingleS5BTransport.NAMESPACE_V1;
@Override
public final String getNamespace() {
return NAMESPACE;
}
public abstract static class JingleS5BCandidateTransportInfo extends JingleS5BTransportInfo { public abstract static class JingleS5BCandidateTransportInfo extends JingleS5BTransportInfo {
public static final String ATTR_CID = "cid"; public static final String ATTR_CID = "cid";
@ -39,9 +47,8 @@ public abstract class JingleS5BTransportInfo extends JingleContentTransportInfo
} }
@Override @Override
public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public final XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
XmlStringBuilder xml = new XmlStringBuilder(); XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment);
xml.halfOpenElement(this);
xml.attribute(ATTR_CID, getCandidateId()); xml.attribute(ATTR_CID, getCandidateId());
xml.closeEmptyElement(); xml.closeEmptyElement();
return xml; return xml;

View File

@ -17,8 +17,10 @@
package org.jivesoftware.smackx.mood.element; package org.jivesoftware.smackx.mood.element;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.FullyQualifiedElement;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
@ -107,8 +109,8 @@ public class MoodElement implements ExtensionElement {
} }
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment);
if (mood == null && text == null) { if (mood == null && text == null) {
// Empty mood element used as STOP signal // Empty mood element used as STOP signal
@ -152,7 +154,7 @@ public class MoodElement implements ExtensionElement {
* {@link NamedElement} which represents the mood. * {@link NamedElement} which represents the mood.
* This element has the element name of the mood selected from {@link Mood}. * This element has the element name of the mood selected from {@link Mood}.
*/ */
public static class MoodSubjectElement implements NamedElement { public static class MoodSubjectElement implements FullyQualifiedElement {
private final Mood mood; private final Mood mood;
private final MoodConcretisation concretisation; private final MoodConcretisation concretisation;
@ -168,16 +170,17 @@ public class MoodElement implements ExtensionElement {
} }
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
XmlStringBuilder xml = new XmlStringBuilder(); XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment);
if (concretisation == null) { if (concretisation == null) {
return xml.emptyElement(getElementName()); return xml.closeEmptyElement();
} }
return xml.openElement(getElementName()) xml.rightAngleBracket()
.append(concretisation.toXML()) .append(concretisation)
.closeElement(getElementName()); .closeElement(this);
return xml;
} }
/** /**
@ -197,5 +200,10 @@ public class MoodElement implements ExtensionElement {
public MoodConcretisation getConcretisation() { public MoodConcretisation getConcretisation() {
return concretisation; return concretisation;
} }
@Override
public String getNamespace() {
return NAMESPACE;
}
} }
} }

View File

@ -323,7 +323,7 @@ public class MUCUser implements ExtensionElement {
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public static class Decline implements NamedElement { public static class Decline implements ExtensionElement {
public static final String ELEMENT = "decline"; public static final String ELEMENT = "decline";
private final String reason; private final String reason;
@ -370,7 +370,7 @@ public class MUCUser implements ExtensionElement {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.optAttribute("to", getTo()); xml.optAttribute("to", getTo());
xml.optAttribute("from", getFrom()); xml.optAttribute("from", getFrom());
xml.rightAngleBracket(); xml.rightAngleBracket();
@ -383,6 +383,11 @@ public class MUCUser implements ExtensionElement {
public String getElementName() { public String getElementName() {
return ELEMENT; return ELEMENT;
} }
@Override
public String getNamespace() {
return NAMESPACE;
}
} }
/** /**

View File

@ -417,7 +417,7 @@ public final class FormField implements FullyQualifiedElement {
} else { } else {
buf.rightAngleBracket(); buf.rightAngleBracket();
buf.append(formFieldChildElements, enclosingNamespace); buf.append(formFieldChildElements);
buf.closeElement(this); buf.closeElement(this);
} }

View File

@ -325,15 +325,10 @@ public class DataForm implements ExtensionElement {
@Override @Override
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) { public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
XmlStringBuilder buf = new XmlStringBuilder(this); XmlStringBuilder buf = new XmlStringBuilder(this, xmlEnvironment);
buf.attribute("type", getType()); buf.attribute("type", getType());
buf.rightAngleBracket(); buf.rightAngleBracket();
XmlEnvironment dataFormxmlEnvironment = XmlEnvironment.builder()
.withNamespace(NAMESPACE)
.withNext(xmlEnvironment)
.build();
buf.optElement("title", getTitle()); buf.optElement("title", getTitle());
for (String instruction : getInstructions()) { for (String instruction : getInstructions()) {
buf.element("instructions", instruction); buf.element("instructions", instruction);
@ -347,7 +342,7 @@ public class DataForm implements ExtensionElement {
buf.append(item.toXML()); buf.append(item.toXML());
} }
// Add all form fields. // Add all form fields.
buf.append(getFields(), dataFormxmlEnvironment); buf.append(getFields());
for (Element element : extensionElements) { for (Element element : extensionElements) {
buf.append(element.toXML()); buf.append(element.toXML());
} }

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2014 Anno van Vliet * Copyright 2014 Anno van Vliet, 2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,7 +20,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
/** /**
@ -96,24 +95,14 @@ public class DataLayout implements ExtensionElement {
buf.optAttribute("label", getLabel()); buf.optAttribute("label", getLabel());
buf.rightAngleBracket(); buf.rightAngleBracket();
walkList(buf, getPageLayout()); buf.append(getPageLayout());
buf.closeElement(this); buf.closeElement(this);
return buf; return buf;
} }
/** public static class Fieldref extends DataFormLayoutElement{
* @param buf TODO javadoc me please
* @param pageLayout TODO javadoc me please
*/
private static void walkList(XmlStringBuilder buf, List<DataFormLayoutElement> pageLayout) {
for (DataFormLayoutElement object : pageLayout) {
buf.append(object.toXML());
}
}
public static class Fieldref implements DataFormLayoutElement{
public static final String ELEMENT = "fieldref"; public static final String ELEMENT = "fieldref";
private final String var; private final String var;
@ -128,7 +117,7 @@ public class DataLayout implements ExtensionElement {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder buf = new XmlStringBuilder(this); XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
buf.attribute("var", getVar()); buf.attribute("var", getVar());
buf.closeEmptyElement(); buf.closeEmptyElement();
return buf; return buf;
@ -150,7 +139,7 @@ public class DataLayout implements ExtensionElement {
} }
public static class Section implements DataFormLayoutElement{ public static class Section extends DataFormLayoutElement{
public static final String ELEMENT = "section"; public static final String ELEMENT = "section";
private final List<DataFormLayoutElement> sectionLayout = new ArrayList<>(); private final List<DataFormLayoutElement> sectionLayout = new ArrayList<>();
@ -188,11 +177,12 @@ public class DataLayout implements ExtensionElement {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder buf = new XmlStringBuilder(this); XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
buf.optAttribute("label", getLabel()); buf.optAttribute("label", getLabel());
buf.rightAngleBracket(); buf.rightAngleBracket();
walkList(buf, getSectionLayout()); buf.append(getSectionLayout());
buf.closeElement(ELEMENT); buf.closeElement(ELEMENT);
return buf; return buf;
} }
@ -213,13 +203,13 @@ public class DataLayout implements ExtensionElement {
} }
public static class Reportedref implements DataFormLayoutElement{ public static class Reportedref extends DataFormLayoutElement{
public static final String ELEMENT = "reportedref"; public static final String ELEMENT = "reportedref";
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder buf = new XmlStringBuilder(this); XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
buf.closeEmptyElement(); buf.closeEmptyElement();
return buf; return buf;
} }
@ -231,7 +221,7 @@ public class DataLayout implements ExtensionElement {
} }
public static class Text implements DataFormLayoutElement{ public static class Text extends DataFormLayoutElement{
public static final String ELEMENT = "text"; public static final String ELEMENT = "text";
private final String text; private final String text;
@ -245,8 +235,10 @@ public class DataLayout implements ExtensionElement {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder buf = new XmlStringBuilder(); XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
buf.element(ELEMENT, getText()); buf.rightAngleBracket();
buf.escape(getText());
buf.closeElement(this);
return buf; return buf;
} }
@ -266,7 +258,11 @@ public class DataLayout implements ExtensionElement {
} }
public interface DataFormLayoutElement extends NamedElement { public abstract static class DataFormLayoutElement implements ExtensionElement {
@Override
public final String getNamespace() {
return NAMESPACE;
}
} }
} }

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2014 Anno van Vliet * Copyright 2014 Anno van Vliet, 2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,7 +19,8 @@ package org.jivesoftware.smackx.xdatavalidation.packet;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import org.jivesoftware.smack.datatypes.UInt32; import org.jivesoftware.smack.datatypes.UInt32;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.FullyQualifiedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
@ -336,7 +337,7 @@ public abstract class ValidateElement implements FormFieldChildElement {
* This element indicates for "list-multi", that a minimum and maximum number of options should be selected and/or * This element indicates for "list-multi", that a minimum and maximum number of options should be selected and/or
* entered. * entered.
*/ */
public static class ListRange implements NamedElement { public static class ListRange implements FullyQualifiedElement {
public static final String ELEMENT = "list-range"; public static final String ELEMENT = "list-range";
private final UInt32 min; private final UInt32 min;
@ -363,8 +364,8 @@ public abstract class ValidateElement implements FormFieldChildElement {
} }
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
XmlStringBuilder buf = new XmlStringBuilder(this); XmlStringBuilder buf = new XmlStringBuilder(this, enclosingXmlEnvironment);
buf.optAttribute("min", getMin()); buf.optAttribute("min", getMin());
buf.optAttribute("max", getMax()); buf.optAttribute("max", getMax());
buf.closeEmptyElement(); buf.closeEmptyElement();
@ -394,6 +395,11 @@ public abstract class ValidateElement implements FormFieldChildElement {
return max; return max;
} }
@Override
public String getNamespace() {
return NAMESPACE;
}
} }
/** /**

View File

@ -70,7 +70,7 @@ public class JingleContentTest extends SmackTestSuite {
assertEquals(content1.toXML().toString(), builder.build().toXML().toString()); assertEquals(content1.toXML().toString(), builder.build().toXML().toString());
String xml = String xml =
"<content creator='initiator' disposition='session' name='A name' senders='both'>" + "<content xmlns='urn:xmpp:jingle:1' creator='initiator' disposition='session' name='A name' senders='both'>" +
"</content>"; "</content>";
assertEquals(xml, content1.toXML().toString()); assertEquals(xml, content1.toXML().toString());
} }

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2017 Paul Schaub * Copyright 2017-2019 Paul Schaub, 2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,13 +16,14 @@
*/ */
package org.jivesoftware.smackx.jingle; package org.jivesoftware.smackx.jingle;
import static junit.framework.TestCase.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.jingle.element.JingleReason; import org.jivesoftware.smackx.jingle.element.JingleReason;
import org.junit.Test; import org.junit.jupiter.api.Test;
/** /**
* Test JingleReason functionality. * Test JingleReason functionality.
@ -31,56 +32,67 @@ public class JingleReasonTest extends SmackTestSuite {
@Test @Test
public void parserTest() { public void parserTest() {
assertEquals("<reason><success/></reason>", assertReasonXml("<reason><success/></reason>",
JingleReason.Success.toXML().toString()); JingleReason.Success);
assertEquals("<reason><busy/></reason>", assertReasonXml("<reason><busy/></reason>",
JingleReason.Busy.toXML().toString()); JingleReason.Busy);
assertEquals("<reason><cancel/></reason>", assertReasonXml("<reason><cancel/></reason>",
JingleReason.Cancel.toXML().toString()); JingleReason.Cancel);
assertEquals("<reason><connectivity-error/></reason>", assertReasonXml("<reason><connectivity-error/></reason>",
JingleReason.ConnectivityError.toXML().toString()); JingleReason.ConnectivityError);
assertEquals("<reason><decline/></reason>", assertReasonXml("<reason><decline/></reason>",
JingleReason.Decline.toXML().toString()); JingleReason.Decline);
assertEquals("<reason><expired/></reason>", assertReasonXml("<reason><expired/></reason>",
JingleReason.Expired.toXML().toString()); JingleReason.Expired);
assertEquals("<reason><unsupported-transports/></reason>", assertReasonXml("<reason><unsupported-transports/></reason>",
JingleReason.UnsupportedTransports.toXML().toString()); JingleReason.UnsupportedTransports);
assertEquals("<reason><failed-transport/></reason>", assertReasonXml("<reason><failed-transport/></reason>",
JingleReason.FailedTransport.toXML().toString()); JingleReason.FailedTransport);
assertEquals("<reason><general-error/></reason>", assertReasonXml("<reason><general-error/></reason>",
JingleReason.GeneralError.toXML().toString()); JingleReason.GeneralError);
assertEquals("<reason><gone/></reason>", assertReasonXml("<reason><gone/></reason>",
JingleReason.Gone.toXML().toString()); JingleReason.Gone);
assertEquals("<reason><media-error/></reason>", assertReasonXml("<reason><media-error/></reason>",
JingleReason.MediaError.toXML().toString()); JingleReason.MediaError);
assertEquals("<reason><security-error/></reason>", assertReasonXml("<reason><security-error/></reason>",
JingleReason.SecurityError.toXML().toString()); JingleReason.SecurityError);
assertEquals("<reason><unsupported-applications/></reason>", assertReasonXml("<reason><unsupported-applications/></reason>",
JingleReason.UnsupportedApplications.toXML().toString()); JingleReason.UnsupportedApplications);
assertEquals("<reason><timeout/></reason>", assertReasonXml("<reason><timeout/></reason>",
JingleReason.Timeout.toXML().toString()); JingleReason.Timeout);
assertEquals("<reason><failed-application/></reason>", assertReasonXml("<reason><failed-application/></reason>",
JingleReason.FailedApplication.toXML().toString()); JingleReason.FailedApplication);
assertEquals("<reason><incompatible-parameters/></reason>", assertReasonXml("<reason><incompatible-parameters/></reason>",
JingleReason.IncompatibleParameters.toXML().toString()); JingleReason.IncompatibleParameters);
assertEquals("<reason><alternative-session><sid>1234</sid></alternative-session></reason>", assertReasonXml("<reason><alternative-session><sid>1234</sid></alternative-session></reason>",
JingleReason.AlternativeSession("1234").toXML().toString()); JingleReason.AlternativeSession("1234"));
} }
@Test(expected = NullPointerException.class) private static void assertReasonXml(String expected, JingleReason reason) {
String actualXml = reason.toXML(JingleReason.NAMESPACE).toString();
assertEquals(expected, actualXml);
}
@Test
public void alternativeSessionEmptyStringTest() { public void alternativeSessionEmptyStringTest() {
// Alternative sessionID must not be empty assertThrows(NullPointerException.class, () ->
JingleReason.AlternativeSession(""); // Alternative sessionID must not be empty
JingleReason.AlternativeSession("")
);
} }
@Test(expected = NullPointerException.class) @Test
public void alternativeSessionNullStringTest() { public void alternativeSessionNullStringTest() {
// Alternative sessionID must not be null assertThrows(NullPointerException.class, () ->
JingleReason.AlternativeSession(null); // Alternative sessionID must not be null
JingleReason.AlternativeSession(null)
);
} }
@Test(expected = IllegalArgumentException.class) @Test
public void illegalArgumentTest() { public void illegalArgumentTest() {
JingleReason.Reason.fromString("illegal-reason"); assertThrows(IllegalArgumentException.class, () ->
JingleReason.Reason.fromString("illegal-reason")
);
} }
} }

View File

@ -24,8 +24,8 @@ import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.util.EqualsUtil; import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode; import org.jivesoftware.smack.util.HashCode;
@ -112,7 +112,7 @@ public final class RosterPacket extends IQ {
* the groups the roster item belongs to. * the groups the roster item belongs to.
*/ */
// TODO Make this class immutable. // TODO Make this class immutable.
public static final class Item implements NamedElement { public static final class Item implements ExtensionElement {
/** /**
* The constant value "{@value}". * The constant value "{@value}".
@ -163,6 +163,11 @@ public final class RosterPacket extends IQ {
return ELEMENT; return ELEMENT;
} }
@Override
public String getNamespace() {
return NAMESPACE;
}
/** /**
* Returns the user. * Returns the user.
* *
@ -275,7 +280,7 @@ public final class RosterPacket extends IQ {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.attribute("jid", jid); xml.attribute("jid", jid);
xml.optAttribute("name", name); xml.optAttribute("name", name);
xml.optAttribute("subscription", itemType); xml.optAttribute("subscription", itemType);

View File

@ -182,25 +182,6 @@ public class Jingle extends IQ {
return sid; return sid;
} }
/**
* Returns the XML element name of the extension sub-packet root element.
* Always returns "jingle"
*
* @return the XML element name of the stanza extension.
*/
public static String getElementName() {
return NODENAME;
}
/**
* Returns the XML namespace of the extension sub-packet root element.
*
* @return the XML namespace of the stanza extension.
*/
public static String getNamespace() {
return NAMESPACE;
}
/** /**
* Jingle content info. * Jingle content info.
* *

View File

@ -120,7 +120,7 @@ public class JingleProvider extends IQProvider<Jingle> {
} }
} else if (eventType == XmlPullParser.Event.END_ELEMENT) { } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
if (parser.getName().equals(Jingle.getElementName())) { if (parser.getName().equals(Jingle.NODENAME)) {
done = true; done = true;
} }
} }

View File

@ -65,14 +65,6 @@ public class AgentStatusRequest extends IQ {
return Collections.unmodifiableSet(agents); return Collections.unmodifiableSet(agents);
} }
public String getElementName() {
return ELEMENT_NAME;
}
public String getNamespace() {
return NAMESPACE;
}
@Override @Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
buf.rightAngleBracket(); buf.rightAngleBracket();

View File

@ -76,7 +76,7 @@ public abstract class OmemoElement implements ExtensionElement {
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder sb = new XmlStringBuilder(this, enclosingNamespace).rightAngleBracket(); XmlStringBuilder sb = new XmlStringBuilder(this, enclosingNamespace).rightAngleBracket();
sb.element(header); sb.append(header);
if (payload != null) { if (payload != null) {
sb.openElement(ATTR_PAYLOAD).append(Base64.encodeToString(payload)).closeElement(ATTR_PAYLOAD); sb.openElement(ATTR_PAYLOAD).append(Base64.encodeToString(payload)).closeElement(ATTR_PAYLOAD);

View File

@ -19,17 +19,22 @@ package org.jivesoftware.smackx.omemo.element;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.FullyQualifiedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smack.util.stringencoder.Base64; import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.omemo.util.OmemoConstants;
/** /**
* Header element of the message. The header contains information about the sender and the encrypted keys for * Header element of the message. The header contains information about the sender and the encrypted keys for
* the recipients, as well as the iv element for AES. * the recipients, as well as the iv element for AES.
*/ */
public abstract class OmemoHeaderElement implements NamedElement { public abstract class OmemoHeaderElement implements FullyQualifiedElement {
public static final String ELEMENT = "header";
public static final String NAMESPACE = OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL;
public static final String NAME_HEADER = "header";
public static final String ATTR_SID = "sid"; public static final String ATTR_SID = "sid";
public static final String ATTR_IV = "iv"; public static final String ATTR_IV = "iv";
@ -62,16 +67,21 @@ public abstract class OmemoHeaderElement implements NamedElement {
@Override @Override
public String getElementName() { public String getElementName() {
return NAME_HEADER; return ELEMENT;
} }
@Override @Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public String getNamespace() {
XmlStringBuilder sb = new XmlStringBuilder(this); return NAMESPACE;
}
@Override
public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
XmlStringBuilder sb = new XmlStringBuilder(this, enclosingXmlEnvironment);
sb.attribute(ATTR_SID, getSid()).rightAngleBracket(); sb.attribute(ATTR_SID, getSid()).rightAngleBracket();
for (OmemoKeyElement k : getKeys()) { for (OmemoKeyElement k : getKeys()) {
sb.element(k); sb.append(k);
} }
sb.openElement(ATTR_IV).append(Base64.encodeToString(getIv())).closeElement(ATTR_IV); sb.openElement(ATTR_IV).append(Base64.encodeToString(getIv())).closeElement(ATTR_IV);

View File

@ -16,16 +16,21 @@
*/ */
package org.jivesoftware.smackx.omemo.element; package org.jivesoftware.smackx.omemo.element;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.FullyQualifiedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smack.util.stringencoder.Base64; import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.omemo.util.OmemoConstants;
/** /**
* Small class to collect key (byte[]), its id and whether its a preKey or not. * Small class to collect key (byte[]), its id and whether its a preKey or not.
*/ */
public class OmemoKeyElement implements NamedElement { public class OmemoKeyElement implements FullyQualifiedElement {
public static final String ELEMENT = "key";
public static final String NAMESPACE = OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL;
public static final String NAME_KEY = "key";
public static final String ATTR_RID = "rid"; public static final String ATTR_RID = "rid";
public static final String ATTR_PREKEY = "prekey"; public static final String ATTR_PREKEY = "prekey";
@ -34,9 +39,7 @@ public class OmemoKeyElement implements NamedElement {
private final boolean preKey; private final boolean preKey;
public OmemoKeyElement(byte[] data, int id) { public OmemoKeyElement(byte[] data, int id) {
this.data = data; this(data, id, false);
this.id = id;
this.preKey = false;
} }
public OmemoKeyElement(byte[] data, int id, boolean preKey) { public OmemoKeyElement(byte[] data, int id, boolean preKey) {
@ -64,12 +67,17 @@ public class OmemoKeyElement implements NamedElement {
@Override @Override
public String getElementName() { public String getElementName() {
return NAME_KEY; return ELEMENT;
} }
@Override @Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public String getNamespace() {
XmlStringBuilder sb = new XmlStringBuilder(this); return NAMESPACE;
}
@Override
public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
XmlStringBuilder sb = new XmlStringBuilder(this, enclosingXmlEnvironment);
if (isPreKey()) { if (isPreKey()) {
sb.attribute(ATTR_PREKEY, true); sb.attribute(ATTR_PREKEY, true);

View File

@ -54,14 +54,14 @@ public class OmemoVAxolotlProvider extends ExtensionElementProvider<OmemoElement
switch (tag) { switch (tag) {
case START_ELEMENT: case START_ELEMENT:
switch (name) { switch (name) {
case OmemoHeaderElement.NAME_HEADER: case OmemoHeaderElement.ELEMENT:
for (int i = 0; i < parser.getAttributeCount(); i++) { for (int i = 0; i < parser.getAttributeCount(); i++) {
if (parser.getAttributeName(i).equals(OmemoHeaderElement.ATTR_SID)) { if (parser.getAttributeName(i).equals(OmemoHeaderElement.ATTR_SID)) {
sid = Integer.parseInt(parser.getAttributeValue(i)); sid = Integer.parseInt(parser.getAttributeValue(i));
} }
} }
break; break;
case OmemoKeyElement.NAME_KEY: case OmemoKeyElement.ELEMENT:
boolean prekey = false; boolean prekey = false;
int rid = -1; int rid = -1;
for (int i = 0; i < parser.getAttributeCount(); i++) { for (int i = 0; i < parser.getAttributeCount(); i++) {

View File

@ -20,7 +20,6 @@ import java.nio.charset.Charset;
import java.util.Date; import java.util.Date;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
@ -77,7 +76,7 @@ public class PubkeyElement implements ExtensionElement {
XmlStringBuilder xml = new XmlStringBuilder(this) XmlStringBuilder xml = new XmlStringBuilder(this)
.optAttribute(ATTR_DATE, date) .optAttribute(ATTR_DATE, date)
.rightAngleBracket() .rightAngleBracket()
.element(getDataElement()) .append(getDataElement())
.closeElement(this); .closeElement(this);
return xml; return xml;
} }
@ -85,7 +84,7 @@ public class PubkeyElement implements ExtensionElement {
/** /**
* Element that contains the base64 encoded public key. * Element that contains the base64 encoded public key.
*/ */
public static class PubkeyDataElement implements NamedElement { public static class PubkeyDataElement implements ExtensionElement {
public static final String ELEMENT = "data"; public static final String ELEMENT = "data";
@ -109,9 +108,14 @@ public class PubkeyElement implements ExtensionElement {
return ELEMENT; return ELEMENT;
} }
@Override
public String getNamespace() {
return NAMESPACE;
}
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this) XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace)
.rightAngleBracket() .rightAngleBracket()
.append(new String(b64Data, Charset.forName("UTF-8"))) .append(new String(b64Data, Charset.forName("UTF-8")))
.closeElement(this); .closeElement(this);

View File

@ -22,7 +22,6 @@ import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
@ -66,9 +65,7 @@ public final class PublicKeysListElement implements ExtensionElement {
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this).rightAngleBracket(); XmlStringBuilder xml = new XmlStringBuilder(this).rightAngleBracket();
for (PubkeyMetadataElement metadataElement : metadata.values()) { xml.append(metadata.values());
xml.element(metadataElement);
}
xml.closeElement(this); xml.closeElement(this);
return xml; return xml;
} }
@ -92,7 +89,7 @@ public final class PublicKeysListElement implements ExtensionElement {
} }
} }
public static class PubkeyMetadataElement implements NamedElement { public static class PubkeyMetadataElement implements ExtensionElement {
public static final String ELEMENT = "pubkey-metadata"; public static final String ELEMENT = "pubkey-metadata";
public static final String ATTR_V4_FINGERPRINT = "v4-fingerprint"; public static final String ATTR_V4_FINGERPRINT = "v4-fingerprint";
@ -123,6 +120,11 @@ public final class PublicKeysListElement implements ExtensionElement {
return ELEMENT; return ELEMENT;
} }
@Override
public String getNamespace() {
return NAMESPACE;
}
@Override @Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this) XmlStringBuilder xml = new XmlStringBuilder(this)

View File

@ -122,7 +122,7 @@ public class ParseStreamManagementTest {
StanzaErrorTextElement textElement = textElements.get(0); StanzaErrorTextElement textElement = textElements.get(0);
assertEquals("Previous session timed out", textElement.getText()); assertEquals("Previous session timed out", textElement.getText());
assertEquals("en", textElement.getLang()); assertEquals("en", textElement.getLanguage());
} }
@Test @Test