1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 06:12:05 +01:00

Improve how XmlStringBuilder handles enclosing XML environments

to avoid emitting unnecessary attributes like xmlns.

Also add a test for MediaElement that checks that <uri/> does not
carry a xmlns attribute.
This commit is contained in:
Florian Schmaus 2019-09-04 09:48:02 +02:00
parent 935465a11b
commit dd4df0a5ef
3 changed files with 55 additions and 5 deletions

View file

@ -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<? extends Element> elements) {
return append(elements, null);
return append(elements, effectiveXmlEnvironment);
}
public XmlStringBuilder append(Collection<? extends Element> elements, XmlEnvironment enclosingXmlEnvironment) {

View file

@ -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;

View file

@ -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 = "<media xmlns='urn:xmpp:media-element' height='16' width='16'><uri type='test-type'>http://example.org</uri></media>";
assertEquals(expected, xml);
}
}