Browse Source

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.
clusterIntTest
Florian Schmaus 1 year ago
parent
commit
65576cf3c2
74 changed files with 654 additions and 524 deletions
  1. +1
    -3
      smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractError.java
  2. +15
    -3
      smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractTextElement.java
  3. +9
    -0
      smack-core/src/main/java/org/jivesoftware/smack/packet/FullyQualifiedElement.java
  4. +43
    -31
      smack-core/src/main/java/org/jivesoftware/smack/packet/IQ.java
  5. +15
    -20
      smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java
  6. +8
    -3
      smack-core/src/main/java/org/jivesoftware/smack/packet/NamedElement.java
  7. +2
    -2
      smack-core/src/main/java/org/jivesoftware/smack/packet/Nonza.java
  8. +1
    -5
      smack-core/src/main/java/org/jivesoftware/smack/packet/Packet.java
  9. +9
    -5
      smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java
  10. +15
    -30
      smack-core/src/main/java/org/jivesoftware/smack/packet/Stanza.java
  11. +2
    -2
      smack-core/src/main/java/org/jivesoftware/smack/packet/TopLevelStreamElement.java
  12. +23
    -0
      smack-core/src/main/java/org/jivesoftware/smack/packet/XmlEnvironment.java
  13. +33
    -16
      smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java
  14. +5
    -0
      smack-core/src/test/java/org/jivesoftware/smack/StanzaCollectorTest.java
  15. +0
    -4
      smack-core/src/test/java/org/jivesoftware/smack/util/PacketParserUtilsTest.java
  16. +5
    -0
      smack-debug/src/main/java/org/jivesoftware/smackx/debugger/EnhancedDebugger.java
  17. +21
    -13
      smack-experimental/src/main/java/org/jivesoftware/smackx/hoxt/packet/AbstractHttpOverXmpp.java
  18. +9
    -4
      smack-experimental/src/main/java/org/jivesoftware/smackx/iot/control/element/SetData.java
  19. +28
    -0
      smack-experimental/src/main/java/org/jivesoftware/smackx/iot/data/element/IoTDataExtensionElement.java
  20. +2
    -3
      smack-experimental/src/main/java/org/jivesoftware/smackx/iot/data/element/IoTDataField.java
  21. +2
    -4
      smack-experimental/src/main/java/org/jivesoftware/smackx/iot/data/element/NodeElement.java
  22. +2
    -3
      smack-experimental/src/main/java/org/jivesoftware/smackx/iot/data/element/TimestampElement.java
  23. +1
    -1
      smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/Checksum.java
  24. +12
    -4
      smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/JingleFileTransferChild.java
  25. +10
    -3
      smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/Range.java
  26. +4
    -5
      smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamElements.java
  27. +1
    -1
      smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamFinIQ.java
  28. +2
    -2
      smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamPrefsIQ.java
  29. +2
    -26
      smack-experimental/src/main/java/org/jivesoftware/smackx/message_markup/element/BlockQuoteElement.java
  30. +2
    -26
      smack-experimental/src/main/java/org/jivesoftware/smackx/message_markup/element/CodeBlockElement.java
  31. +13
    -32
      smack-experimental/src/main/java/org/jivesoftware/smackx/message_markup/element/ListElement.java
  32. +62
    -20
      smack-experimental/src/main/java/org/jivesoftware/smackx/message_markup/element/MarkupElement.java
  33. +4
    -25
      smack-experimental/src/main/java/org/jivesoftware/smackx/message_markup/element/SpanElement.java
  34. +1
    -1
      smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightAffiliationsIQ.java
  35. +1
    -1
      smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightBlockingIQ.java
  36. +1
    -1
      smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightChangeAffiliationsIQ.java
  37. +1
    -1
      smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightConfigurationIQ.java
  38. +2
    -2
      smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightCreateIQ.java
  39. +2
    -2
      smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightElements.java
  40. +2
    -2
      smack-experimental/src/main/java/org/jivesoftware/smackx/muclight/element/MUCLightInfoIQ.java
  41. +1
    -1
      smack-experimental/src/main/java/org/jivesoftware/smackx/push_notifications/element/EnablePushNotificationsIQ.java
  42. +2
    -8
      smack-experimental/src/main/java/org/jivesoftware/smackx/spoiler/element/SpoilerElement.java
  43. +12
    -5
      smack-extensions/src/main/java/org/jivesoftware/smackx/address/MultipleRecipientManager.java
  44. +9
    -4
      smack-extensions/src/main/java/org/jivesoftware/smackx/address/packet/MultipleAddresses.java
  45. +15
    -7
      smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/packet/Bytestream.java
  46. +1
    -1
      smack-extensions/src/main/java/org/jivesoftware/smackx/delay/packet/DelayInformation.java
  47. +2
    -2
      smack-extensions/src/main/java/org/jivesoftware/smackx/forward/packet/Forwarded.java
  48. +12
    -5
      smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContent.java
  49. +3
    -9
      smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentDescriptionChildElement.java
  50. +4
    -3
      smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentTransport.java
  51. +3
    -3
      smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentTransportCandidate.java
  52. +3
    -3
      smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentTransportInfo.java
  53. +13
    -6
      smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleReason.java
  54. +12
    -3
      smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_s5b/elements/JingleS5BTransportCandidate.java
  55. +11
    -4
      smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_s5b/elements/JingleS5BTransportInfo.java
  56. +17
    -9
      smack-extensions/src/main/java/org/jivesoftware/smackx/mood/element/MoodElement.java
  57. +7
    -2
      smack-extensions/src/main/java/org/jivesoftware/smackx/muc/packet/MUCUser.java
  58. +1
    -1
      smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/FormField.java
  59. +2
    -7
      smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java
  60. +20
    -24
      smack-extensions/src/main/java/org/jivesoftware/smackx/xdatalayout/packet/DataLayout.java
  61. +11
    -5
      smack-extensions/src/main/java/org/jivesoftware/smackx/xdatavalidation/packet/ValidateElement.java
  62. +1
    -1
      smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleContentTest.java
  63. +57
    -45
      smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleReasonTest.java
  64. +8
    -3
      smack-im/src/main/java/org/jivesoftware/smack/roster/packet/RosterPacket.java
  65. +0
    -19
      smack-jingle-old/src/main/java/org/jivesoftware/smackx/jingleold/packet/Jingle.java
  66. +1
    -1
      smack-jingle-old/src/main/java/org/jivesoftware/smackx/jingleold/provider/JingleProvider.java
  67. +0
    -8
      smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/AgentStatusRequest.java
  68. +1
    -1
      smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/element/OmemoElement.java
  69. +17
    -7
      smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/element/OmemoHeaderElement.java
  70. +17
    -9
      smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/element/OmemoKeyElement.java
  71. +2
    -2
      smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoVAxolotlProvider.java
  72. +8
    -4
      smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/element/PubkeyElement.java
  73. +7
    -5
      smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/element/PublicKeysListElement.java
  74. +1
    -1
      smack-tcp/src/test/java/org/jivesoftware/smack/sm/provider/ParseStreamManagementTest.java

+ 1
- 3
smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractError.java View File

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

+ 15
- 3
smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractTextElement.java 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");
* you may not use this file except in compliance with the License.
@ -38,8 +38,7 @@ public abstract class AbstractTextElement implements ExtensionElement {
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this);
xml.optXmlLangAttribute(lang);
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.rightAngleBracket();
xml.escape(text);
xml.closeElement(this);
@ -50,6 +49,19 @@ public abstract class AbstractTextElement implements ExtensionElement {
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() {
return lang;
}

+ 9
- 0
smack-core/src/main/java/org/jivesoftware/smack/packet/FullyQualifiedElement.java View File

@ -32,4 +32,13 @@ public interface FullyQualifiedElement extends NamedElement {
String localPart = getElementName();
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;
}
}

+ 43
- 31
smack-core/src/main/java/org/jivesoftware/smack/packet/IQ.java View File

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

+ 15
- 20
smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java View File

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

+ 8
- 3
smack-core/src/main/java/org/jivesoftware/smack/packet/NamedElement.java 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");
* you may not use this file except in compliance with the License.
@ -18,8 +18,13 @@
package org.jivesoftware.smack.packet;
/**
* Interface to represent a XML element. This is similar to {@link ExtensionElement}, but does not
* carry a namespace and is usually included as child element of an stanza extension.
* Interface to represent a XML element. This is similar to {@link ExtensionElement}, but does not carry a single
* 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 {

+ 2
- 2
smack-core/src/main/java/org/jivesoftware/smack/packet/Nonza.java 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");
* you may not use this file except in compliance with the License.
@ -30,6 +30,6 @@ package org.jivesoftware.smack.packet;
* @author Florian Schmaus
* @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 {
}

+ 1
- 5
smack-core/src/main/java/org/jivesoftware/smack/packet/Packet.java View File

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

+ 9
- 5
smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java View File

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

+ 15
- 30
smack-core/src/main/java/org/jivesoftware/smack/packet/Stanza.java View File

@ -59,6 +59,10 @@ public abstract class Stanza implements TopLevelStreamElement {
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 Jid to;
private Jid from;
@ -283,11 +287,7 @@ public abstract class Stanza implements TopLevelStreamElement {
error = xmppErrorBuilder.build();
}
/**
* Returns the xml:lang of this Stanza, or null if one has not been set.
*
* @return the xml:lang of this Stanza, or null.
*/
@Override
public String getLanguage() {
return language;
}
@ -491,6 +491,11 @@ public abstract class Stanza implements TopLevelStreamElement {
@Override
public abstract String toString();
@Override
public final String getNamespace() {
return namespace;
}
/**
* 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 enclosingXmlEnvironment the enclosing XML namespace.
* @return the XML environment for this stanza.
*/
protected XmlEnvironment addCommonAttributes(XmlStringBuilder xml, XmlEnvironment enclosingXmlEnvironment) {
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);
*/
protected final void addCommonAttributes(XmlStringBuilder xml) {
xml.optAttribute("to", getTo());
xml.optAttribute("from", getFrom());
xml.optAttribute("id", getStanzaId());
xml.xmllangAttribute(language);
return new XmlEnvironment(namespace, language);
}
protected void logCommonAttributes(StringBuilder sb) {
@ -546,12 +532,11 @@ public abstract class Stanza implements TopLevelStreamElement {
* Append an XMPPError is this stanza has one set.
*
* @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();
if (error != null) {
xml.append(error.toXML(enclosingXmlEnvironment));
xml.append(error);
}
}
}

+ 2
- 2
smack-core/src/main/java/org/jivesoftware/smack/packet/TopLevelStreamElement.java 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");
* 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
* just a plain stream element ({@link Nonza}).
*/
public interface TopLevelStreamElement extends Element {
public interface TopLevelStreamElement extends FullyQualifiedElement {
}

+ 23
- 0
smack-core/src/main/java/org/jivesoftware/smack/packet/XmlEnvironment.java View File

@ -106,6 +106,29 @@ public class XmlEnvironment {
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) {
return from(parser, null);
}

+ 33
- 16
smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java View File

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

+ 5
- 0
smack-core/src/test/java/org/jivesoftware/smack/StanzaCollectorTest.java View File

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

+ 0
- 4
smack-core/src/test/java/org/jivesoftware/smack/util/PacketParserUtilsTest.java View File

@ -738,10 +738,6 @@ public class PacketParserUtilsTest {
.a("type", "chat")
.a("xml:lang", "en")
.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.")
.up()
.e("body")

+ 5
- 0
smack-debug/src/main/java/org/jivesoftware/smackx/debugger/EnhancedDebugger.java View File

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

+ 21
- 13
smack-experimental/src/main/java/org/jivesoftware/smackx/hoxt/packet/AbstractHttpOverXmpp.java View File

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

+ 9
- 4
smack-experimental/src/main/java/org/jivesoftware/smackx/iot/control/element/SetData.java 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");
* 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 org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.util.XmlStringBuilder;
public abstract class SetData implements NamedElement {
public abstract class SetData implements ExtensionElement {
public enum Type {
BOOL,
@ -76,6 +76,11 @@ public abstract class SetData implements NamedElement {
return getType().toString();
}
@Override
public final String getNamespace() {
return IoTSetRequest.NAMESPACE;
}
/**
* Returns the XML representation of this Element.
*
@ -83,7 +88,7 @@ public abstract class SetData implements NamedElement {
*/
@Override
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("value", value);
xml.closeEmptyElement();

+ 28
- 0
smack-experimental/src/main/java/org/jivesoftware/smackx/iot/data/element/IoTDataExtensionElement.java 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;
}
}

+ 2
- 3
smack-experimental/src/main/java/org/jivesoftware/smackx/iot/data/element/IoTDataField.java View File

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

+ 2
- 4
smack-experimental/src/main/java/org/jivesoftware/smackx/iot/data/element/NodeElement.java View File

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

+ 2
- 3
smack-experimental/src/main/java/org/jivesoftware/smackx/iot/data/element/TimestampElement.java View File

@ -20,10 +20,9 @@ import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.XmlStringBuilder;
public class TimestampElement implements NamedElement {
public class TimestampElement extends IoTDataExtensionElement {
public static final String ELEMENT = "timestamp";
@ -46,7 +45,7 @@ public class TimestampElement implements NamedElement {
@Override
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.rightAngleBracket();

+ 1
- 1
smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/Checksum.java View File

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

+ 12
- 4
smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/JingleFileTransferChild.java 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");
* 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.util.Date;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.hashes.element.HashElement;
@ -27,8 +28,10 @@ import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildEleme
/**
* Content of type File.
*/
public class JingleFileTransferChild extends JingleContentDescriptionChildElement {
public class JingleFileTransferChild implements JingleContentDescriptionChildElement {
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_DESC = "desc";
public static final String ELEM_MEDIA_TYPE = "media-type";
@ -87,8 +90,13 @@ public class JingleFileTransferChild extends JingleContentDescriptionChildElemen
}
@Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder sb = new XmlStringBuilder(this);
public String getNamespace() {
return NAMESPACE;
}
@Override
public XmlStringBuilder toXML(XmlEnvironment enclosingNamespace) {
XmlStringBuilder sb = new XmlStringBuilder(this, enclosingNamespace);
sb.rightAngleBracket();
sb.optElement(ELEM_DATE, date);

+ 10
- 3
smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/Range.java View File

@ -16,7 +16,7 @@
*/
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.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.
*/
public class Range implements NamedElement {
public class Range implements FullyQualifiedElement {
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_LENGTH = "length";
@ -99,6 +101,11 @@ public class Range implements NamedElement {
return ELEMENT;
}
@Override
public String getNamespace() {
return NAMESPACE;
}
@Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder sb = new XmlStringBuilder(this);
@ -108,7 +115,7 @@ public class Range implements NamedElement {
if (hash != null) {
sb.rightAngleBracket();
sb.element(hash);
sb.append(hash);
sb.closeElement(this);
} else {
sb.closeEmptyElement();

+ 4
- 5
smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamElements.java View File

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

+ 1
- 1
smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamFinIQ.java View File

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

+ 2
- 2
smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamPrefsIQ.java View File

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

+ 2
- 26
smack-experimental/src/main/java/org/jivesoftware/smackx/message_markup/element/BlockQuoteElement.java View File

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

+ 2
- 26
smack-experimental/src/main/java/org/jivesoftware/smackx/message_markup/element/CodeBlockElement.java View File

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

+ 13
- 32
smack-experimental/src/main/java/org/jivesoftware/smackx/message_markup/element/ListElement.java View File

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

+ 62
- 20
smack-experimental/src/main/java/org/jivesoftware/smackx/message_markup/element/MarkupElement.java View File

@ -22,7 +22,6 @@ import java.util.List;
import java.util.Set;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.util.XmlStringBuilder;
public class MarkupElement implements ExtensionElement {
@ -267,46 +266,89 @@ public class MarkupElement implements ExtensionElement {
}
}
/**
* Interface for child elements.
*/
public interface MarkupChildElement extends NamedElement {
public abstract static class MarkupChildElement implements ExtensionElement {
String ATTR_START = "start";
String ATTR_END = "end";
public static final String ATTR_START = "start";
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 start index
*/
int getStart();
public final int getStart() {
return start;
}
/**
* Return the end index of this element.
*
* @return end index
*/
int getEnd();
public final int getEnd() {
return end;
}
@Override
public final