[core] Fix XmlEnvironment namespace: Use default namespace (not element's)

Assume the element
<foo:bar xmlns='namespace' xmlns:foo='foo-namespace'/>

then the <bar/> element's namespace is 'foo-namespace', but the
default namespace is 'namespace'. And this is the namespace that
scopes into inner elements.
ramabit.avatar
Florian Schmaus 2 years ago
parent ec2ef1facc
commit 84d73e9623
  1. 4
      smack-core/src/main/java/org/jivesoftware/smack/packet/XmlEnvironment.java
  2. 33
      smack-core/src/test/java/org/jivesoftware/smack/xml/XmlPullParserTest.java
  3. 6
      smack-xmlparser/src/main/java/org/jivesoftware/smack/xml/XmlPullParser.java

@ -1,6 +1,6 @@
/**
*
* Copyright 2019 Florian Schmaus
* Copyright 2019-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -133,7 +133,7 @@ public class XmlEnvironment {
}
public static XmlEnvironment from(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) {
String namespace = parser.getNamespace();
String namespace = parser.getDefaultNamespace();
String xmlLang = ParserUtils.getXmlLang(parser);
return new XmlEnvironment(namespace, xmlLang, outerXmlEnvironment);
}

@ -1,6 +1,6 @@
/**
*
* Copyright 2019 Florian Schmaus
* Copyright 2019-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,6 +23,9 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import javax.xml.XMLConstants;
@ -207,6 +210,34 @@ public class XmlPullParserTest {
}
}
@ParameterizedTest
@EnumSource(SmackTestUtil.XmlPullParserKind.class)
public void testGetNamespace(SmackTestUtil.XmlPullParserKind parserKind)
throws XmlPullParserException, IOException {
String xml = "<foo:bar xmlns='namespace' xmlns:foo='foo-namespace'/>";
XmlPullParser parser = SmackTestUtil.getParserFor(xml, parserKind);
String parsedName = parser.getName();
assertEquals("bar", parsedName);
String parsedPrefix = parser.getPrefix();
assertEquals("foo", parsedPrefix);
String parsedPrefixNamespace = parser.getNamespace(parsedPrefix);
assertEquals("foo-namespace", parsedPrefixNamespace);
String parsedNamespace = parser.getNamespace();
assertEquals("foo-namespace", parsedNamespace);
List<Function<XmlPullParser, String>> defaultNamespaceRetrievers = new ArrayList<>();
defaultNamespaceRetrievers.add(p -> p.getNamespace(null));
defaultNamespaceRetrievers.add(p -> p.getDefaultNamespace());
for (Function<XmlPullParser, String> defaultNamespaceRetriever : defaultNamespaceRetrievers) {
String defaultNamespace = defaultNamespaceRetriever.apply(parser);
assertEquals("namespace", defaultNamespace);
}
}
private static void assertAttributeHolds(XmlPullParser parser, int attributeIndex, String expectedLocalpart,
String expectedPrefix, String expectedNamespace) {
QName qname = parser.getAttributeQName(attributeIndex);

@ -1,6 +1,6 @@
/**
*
* Copyright 2019 Florian Schmaus.
* Copyright 2019-2021 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -75,6 +75,10 @@ public interface XmlPullParser {
String getNamespace(String prefix);
default String getDefaultNamespace() {
return getNamespace(null);
}
int getDepth();
String getPositionDescription();

Loading…
Cancel
Save