From dd4df0a5ef2435f1c380a1c83c5b838b6986b2ce Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 4 Sep 2019 09:48:02 +0200 Subject: [PATCH] Improve how XmlStringBuilder handles enclosing XML environments to avoid emitting unnecessary attributes like xmlns. Also add a test for MediaElement that checks that does not carry a xmlns attribute. --- .../smack/util/XmlStringBuilder.java | 16 +++++-- .../mediaelement/element/MediaElement.java | 2 +- .../element/MediaElementTest.java | 42 +++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 smack-extensions/src/test/java/org/jivesoftware/smackx/mediaelement/element/MediaElementTest.java diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java index 8724eb8b5..3b7d91c72 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java @@ -56,9 +56,17 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element { halfOpenElement(e.getElementName()); } - public XmlStringBuilder(FullyQualifiedElement ee, XmlEnvironment enclosingXmlEnvironment) { - this(enclosingXmlEnvironment); - prelude(ee); + public XmlStringBuilder(FullyQualifiedElement element, XmlEnvironment enclosingXmlEnvironment) { + sb = new LazyStringBuilder(); + halfOpenElement(element); + if (enclosingXmlEnvironment != null + && !enclosingXmlEnvironment.effectiveNamespaceEquals(element.getNamespace())) { + xmlnsAttribute(element.getNamespace()); + } + effectiveXmlEnvironment = XmlEnvironment.builder() + .withNamespace(element.getNamespace()) + .withNext(enclosingXmlEnvironment) + .build(); } public XmlStringBuilder escapedElement(String name, String escapedContent) { @@ -492,7 +500,7 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element { } public XmlStringBuilder append(Collection elements) { - return append(elements, null); + return append(elements, effectiveXmlEnvironment); } public XmlStringBuilder append(Collection elements, XmlEnvironment enclosingXmlEnvironment) { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/mediaelement/element/MediaElement.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/mediaelement/element/MediaElement.java index 08ebeba32..f82e47c40 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/mediaelement/element/MediaElement.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/mediaelement/element/MediaElement.java @@ -87,7 +87,7 @@ public class MediaElement implements FormFieldChildElement { .optAttribute("width", width) .rightAngleBracket(); - xml.append(uris, xmlEnvironment); + xml.append(uris); xml.closeElement(this); return xml; diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/mediaelement/element/MediaElementTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/mediaelement/element/MediaElementTest.java new file mode 100644 index 000000000..aed828fef --- /dev/null +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/mediaelement/element/MediaElementTest.java @@ -0,0 +1,42 @@ +/** + * + * 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.mediaelement.element; + +import static org.junit.Assert.assertEquals; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.junit.jupiter.api.Test; + +public class MediaElementTest { + + @Test + public void simpleToXmlTest() throws URISyntaxException { + MediaElement.Uri uri = new MediaElement.Uri(new URI("http://example.org"), "test-type"); + + MediaElement mediaElement = MediaElement.builder() + .addUri(uri) + .setHeightAndWidth(16, 16) + .build(); + + String xml = mediaElement.toXML().toString(); + + String expected = "http://example.org"; + assertEquals(expected, xml); + } +}