1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-09 18:29:45 +02:00

Use XmlEnvironment in parsing/provider subsystem

This commit is contained in:
Florian Schmaus 2019-02-15 23:21:30 +01:00
parent 43bb418d99
commit 8df69bd3ce
188 changed files with 486 additions and 264 deletions

View file

@ -104,6 +104,7 @@ import org.jivesoftware.smack.packet.StartTls;
import org.jivesoftware.smack.packet.StreamError;
import org.jivesoftware.smack.packet.StreamOpen;
import org.jivesoftware.smack.packet.TopLevelStreamElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
@ -226,6 +227,8 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
private final Map<StanzaListener, InterceptorWrapper> interceptors =
new HashMap<>();
private XmlEnvironment incomingStreamXmlEnvironment;
final Map<String, NonzaCallback> nonzaCallbacks = new HashMap<>();
protected final Lock connectionLock = new ReentrantLock();
@ -1187,7 +1190,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
return;
}
Nonza nonza = nonzaProvider.parse(parser);
Nonza nonza = nonzaProvider.parse(parser, incomingStreamXmlEnvironment);
nonzaCallback.onNonzaReceived(nonza);
}
@ -1198,7 +1201,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
int parserDepth = parser.getDepth();
Stanza stanza = null;
try {
stanza = PacketParserUtils.parseStanza(parser);
stanza = PacketParserUtils.parseStanza(parser, incomingStreamXmlEnvironment);
}
catch (Exception e) {
CharSequence content = PacketParserUtils.parseContentDepth(parser,
@ -1596,7 +1599,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
default:
ExtensionElementProvider<ExtensionElement> provider = ProviderManager.getStreamFeatureProvider(name, namespace);
if (provider != null) {
streamFeature = provider.parse(parser);
streamFeature = provider.parse(parser, incomingStreamXmlEnvironment);
}
break;
}
@ -1844,6 +1847,8 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
// We found an opening stream.
if ("jabber:client".equals(parser.getNamespace(null))) {
streamId = parser.getAttributeValue("", "id");
incomingStreamXmlEnvironment = XmlEnvironment.from(parser);
String reportedServerDomain = parser.getAttributeValue("", "from");
assert (config.getXMPPServiceDomain().equals(reportedServerDomain));
}

View file

@ -17,6 +17,7 @@
package org.jivesoftware.smack.compress.provider;
import org.jivesoftware.smack.compress.packet.Compressed;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.provider.NonzaProvider;
import org.xmlpull.v1.XmlPullParser;
@ -29,7 +30,7 @@ public final class CompressedProvider extends NonzaProvider<Compressed> {
}
@Override
public Compressed parse(XmlPullParser parser, int initialDepth) {
public Compressed parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) {
return Compressed.INSTANCE;
}

View file

@ -22,6 +22,7 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.compress.packet.Failure;
import org.jivesoftware.smack.packet.StanzaError;
import org.jivesoftware.smack.packet.StreamOpen;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.provider.NonzaProvider;
import org.jivesoftware.smack.util.PacketParserUtils;
@ -39,9 +40,10 @@ public final class FailureProvider extends NonzaProvider<Failure> {
}
@Override
public Failure parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackParsingException {
public Failure parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
Failure.CompressFailureError compressFailureError = null;
StanzaError stanzaError = null;
XmlEnvironment failureXmlEnvironment = XmlEnvironment.from(parser, xmlEnvironment);
outerloop: while (true) {
int eventType = parser.next();
@ -60,7 +62,7 @@ public final class FailureProvider extends NonzaProvider<Failure> {
case StreamOpen.SERVER_NAMESPACE:
switch (name) {
case StanzaError.ERROR:
StanzaError.Builder stanzaErrorBuilder = PacketParserUtils.parseError(parser);
StanzaError.Builder stanzaErrorBuilder = PacketParserUtils.parseError(parser, failureXmlEnvironment);
stanzaError = stanzaErrorBuilder.build();
break;
default:

View file

@ -328,7 +328,7 @@ public abstract class AbstractXmppStateMachineConnection extends AbstractXMPPCon
}
break;
case "error":
StreamError streamError = PacketParserUtils.parseStreamError(parser);
StreamError streamError = PacketParserUtils.parseStreamError(parser, null);
saslFeatureReceived.reportFailure(new StreamErrorException(streamError));
throw new StreamErrorException(streamError);
case "features":

View file

@ -16,8 +16,11 @@
*/
package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smack.util.StringUtils;
import org.xmlpull.v1.XmlPullParser;
public class XmlEnvironment {
public static final XmlEnvironment EMPTY = new XmlEnvironment((String) null);
@ -43,7 +46,7 @@ public class XmlEnvironment {
this(builder.namespace, builder.language, builder.next);
}
private XmlEnvironment(String namespace, String language, XmlEnvironment next) {
public XmlEnvironment(String namespace, String language, XmlEnvironment next) {
this.namespace = namespace;
this.language = language;
this.next = next;
@ -103,6 +106,16 @@ public class XmlEnvironment {
return effectiveLanguage;
}
public static XmlEnvironment from(XmlPullParser parser) {
return from(parser, null);
}
public static XmlEnvironment from(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) {
String namespace = parser.getNamespace();
String xmlLang = ParserUtils.getXmlLang(parser);
return new XmlEnvironment(namespace, xmlLang, outerXmlEnvironment);
}
public static Builder builder() {
return new Builder();
}

View file

@ -21,6 +21,7 @@ import java.util.LinkedHashMap;
import java.util.Map;
import org.jivesoftware.smack.packet.StandardExtensionElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smack.util.StringUtils;
@ -39,7 +40,7 @@ public class StandardExtensionElementProvider extends ExtensionElementProvider<S
public static StandardExtensionElementProvider INSTANCE = new StandardExtensionElementProvider();
@Override
public StandardExtensionElement parse(final XmlPullParser parser, final int initialDepth)
public StandardExtensionElement parse(final XmlPullParser parser, final int initialDepth, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException {
// Unlike most (all?) other providers, we don't know the name and namespace of the element
// we are parsing here.
@ -79,7 +80,7 @@ public class StandardExtensionElementProvider extends ExtensionElementProvider<S
int event = parser.next();
switch (event) {
case XmlPullParser.START_TAG:
builder.addElement(parse(parser, parser.getDepth()));
builder.addElement(parse(parser, parser.getDepth(), xmlEnvironment));
break;
case XmlPullParser.TEXT:
builder.setText(parser.getText());

View file

@ -19,6 +19,7 @@ package org.jivesoftware.smack.provider;
import java.io.IOException;
import org.jivesoftware.smack.packet.Bind;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jxmpp.jid.EntityFullJid;
import org.jxmpp.jid.impl.JidCreate;
@ -29,7 +30,7 @@ import org.xmlpull.v1.XmlPullParserException;
public class BindIQProvider extends IQProvider<Bind> {
@Override
public Bind parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
public Bind parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
String name;
Bind bind = null;
outerloop: while (true) {

View file

@ -21,6 +21,7 @@ import static org.jivesoftware.smack.util.PacketParserUtils.parseElementText;
import java.io.IOException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.ParserUtils;
import org.xmlpull.v1.XmlPullParser;
@ -29,7 +30,7 @@ import org.xmlpull.v1.XmlPullParserException;
public class BodyElementProvider extends ExtensionElementProvider<Message.Body> {
@Override
public Message.Body parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
public Message.Body parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
String xmlLang = ParserUtils.getXmlLang(parser);
String body = parseElementText(parser);

View file

@ -23,6 +23,7 @@ import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.util.PacketParserUtils;
@ -85,7 +86,7 @@ import org.xmlpull.v1.XmlPullParserException;
public abstract class EmbeddedExtensionProvider<PE extends ExtensionElement> extends ExtensionElementProvider<PE> {
@Override
public final PE parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackParsingException {
public final PE parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
final String namespace = parser.getNamespace();
final String name = parser.getName();
final int attributeCount = parser.getAttributeCount();
@ -101,7 +102,7 @@ public abstract class EmbeddedExtensionProvider<PE extends ExtensionElement> ext
event = parser.next();
if (event == XmlPullParser.START_TAG)
PacketParserUtils.addExtensionElement(extensions, parser);
PacketParserUtils.addExtensionElement(extensions, parser, xmlEnvironment);
}
while (!(event == XmlPullParser.END_TAG && parser.getDepth() == initialDepth));

View file

@ -21,6 +21,7 @@ import java.lang.reflect.InvocationTargetException;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.ParserUtils;
import org.xmlpull.v1.XmlPullParser;
@ -39,7 +40,7 @@ public class IntrospectionProvider{
@SuppressWarnings("unchecked")
@Override
public I parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
public I parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
try {
return (I) parseWithIntrospection(elementClass, parser, initialDepth);
}
@ -60,7 +61,7 @@ public class IntrospectionProvider{
@SuppressWarnings("unchecked")
@Override
public PE parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
public PE parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
try {
return (PE) parseWithIntrospection(elementClass, parser, initialDepth);
}

View file

@ -22,6 +22,7 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import org.jivesoftware.smack.packet.Element;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.util.ParserUtils;
@ -31,7 +32,7 @@ import org.xmlpull.v1.XmlPullParserException;
/**
* Smack provider are the parsers used to deserialize raw XMPP into the according Java {@link Element}s.
* <p>
* At any time when {@link #parse(XmlPullParser, int)} is invoked any type of exception can be thrown. If the parsed
* At any time when {@link #parse(XmlPullParser, int, XmlEnvironment)} is invoked any type of exception can be thrown. If the parsed
* element does not follow the specification, for example by putting a string where only integers are allowed, then a
* {@link org.jivesoftware.smack.SmackException} should be thrown.
* </p>
@ -62,16 +63,22 @@ public abstract class Provider<E extends Element> {
}
public final E parse(XmlPullParser parser) throws IOException, XmlPullParserException, SmackParsingException {
return parse(parser, null);
}
public final E parse(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws IOException, XmlPullParserException, SmackParsingException {
// XPP3 calling convention assert: Parser should be at start tag
ParserUtils.assertAtStartTag(parser);
final int initialDepth = parser.getDepth();
E e = parse(parser, initialDepth);
final XmlEnvironment xmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
E e = parse(parser, initialDepth, xmlEnvironment);
// XPP3 calling convention assert: Parser should be at end tag of the consumed/parsed element
ParserUtils.forwardToEndTagOfDepth(parser, initialDepth);
return e;
}
public abstract E parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackParsingException;
public abstract E parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException;
}

View file

@ -17,6 +17,7 @@
package org.jivesoftware.smack.provider;
import org.jivesoftware.smack.packet.TlsProceed;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.xmlpull.v1.XmlPullParser;
@ -28,7 +29,7 @@ public final class TlsFailureProvider extends NonzaProvider<TlsProceed> {
}
@Override
public TlsProceed parse(XmlPullParser parser, int initialDepth) {
public TlsProceed parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) {
return TlsProceed.INSTANCE;
}

View file

@ -17,6 +17,7 @@
package org.jivesoftware.smack.provider;
import org.jivesoftware.smack.packet.TlsFailure;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.xmlpull.v1.XmlPullParser;
@ -28,7 +29,7 @@ public final class TlsProceedProvider extends NonzaProvider<TlsFailure> {
}
@Override
public TlsFailure parse(XmlPullParser parser, int initialDepth) {
public TlsFailure parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) {
return TlsFailure.INSTANCE;
}

View file

@ -42,6 +42,7 @@ import org.jivesoftware.smack.packet.StanzaError;
import org.jivesoftware.smack.packet.StartTls;
import org.jivesoftware.smack.packet.StreamError;
import org.jivesoftware.smack.packet.UnparsedIQ;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.parsing.StandardExtensionElementProvider;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
@ -137,7 +138,7 @@ public class PacketParserUtils {
@SuppressWarnings("unchecked")
public static <S extends Stanza> S parseStanza(String stanza) throws Exception {
return (S) parseStanza(getParserFor(stanza));
return (S) parseStanza(getParserFor(stanza), null);
}
/**
@ -146,19 +147,20 @@ public class PacketParserUtils {
* connection is optional and is used to return feature-not-implemented errors for unknown IQ stanzas.
*
* @param parser
* @param outerXmlEnvironment the outer XML environment (optional).
* @return a stanza which is either a Message, IQ or Presence.
* @throws Exception
*/
public static Stanza parseStanza(XmlPullParser parser) throws Exception {
public static Stanza parseStanza(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws Exception {
ParserUtils.assertAtStartTag(parser);
final String name = parser.getName();
switch (name) {
case Message.ELEMENT:
return parseMessage(parser);
return parseMessage(parser, outerXmlEnvironment);
case IQ.IQ_ELEMENT:
return parseIQ(parser);
return parseIQ(parser, outerXmlEnvironment);
case Presence.ELEMENT:
return parsePresence(parser);
return parsePresence(parser, outerXmlEnvironment);
default:
throw new IllegalArgumentException("Can only parse message, iq or presence, not " + name);
}
@ -211,19 +213,25 @@ public class PacketParserUtils {
return parser;
}
public static Message parseMessage(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
return parseMessage(parser, null);
}
/**
* Parses a message packet.
*
* @param parser the XML parser, positioned at the start of a message packet.
* @param outerXmlEnvironment the outer XML environment (optional).
* @return a Message packet.
* @throws XmlPullParserException
* @throws IOException
* @throws SmackParsingException
*/
public static Message parseMessage(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
public static Message parseMessage(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
ParserUtils.assertAtStartTag(parser);
assert (parser.getName().equals(Message.ELEMENT));
XmlEnvironment messageXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
final int initialDepth = parser.getDepth();
Message message = new Message();
message.setStanzaId(parser.getAttributeValue("", "id"));
@ -261,10 +269,10 @@ public class PacketParserUtils {
}
break;
case "error":
message.setError(parseError(parser));
message.setError(parseError(parser, messageXmlEnvironment));
break;
default:
PacketParserUtils.addExtensionElement(message, parser, elementName, namespace);
PacketParserUtils.addExtensionElement(message, parser, elementName, namespace, messageXmlEnvironment);
break;
}
break;
@ -497,18 +505,24 @@ public class PacketParserUtils {
return sb;
}
public static Presence parsePresence(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
return parsePresence(parser, null);
}
/**
* Parses a presence packet.
*
* @param parser the XML parser, positioned at the start of a presence packet.
* @param outerXmlEnvironment the outer XML environment (optional).
* @return a Presence packet.
* @throws IOException
* @throws XmlPullParserException
* @throws SmackParsingException
*/
public static Presence parsePresence(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
public static Presence parsePresence(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
ParserUtils.assertAtStartTag(parser);
final int initialDepth = parser.getDepth();
XmlEnvironment presenceXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
Presence.Type type = Presence.Type.available;
String typeString = parser.getAttributeValue("", "type");
@ -558,14 +572,14 @@ public class PacketParserUtils {
}
break;
case "error":
presence.setError(parseError(parser));
presence.setError(parseError(parser, presenceXmlEnvironment));
break;
default:
// Otherwise, it must be a packet extension.
// Be extra robust: Skip PacketExtensions that cause Exceptions, instead of
// failing completely here. See SMACK-390 for more information.
try {
PacketParserUtils.addExtensionElement(presence, parser, elementName, namespace);
PacketParserUtils.addExtensionElement(presence, parser, elementName, namespace, presenceXmlEnvironment);
} catch (Exception e) {
LOGGER.warning("Failed to parse extension element in Presence stanza: \"" + e + "\" from: '"
+ presence.getFrom() + " id: '" + presence.getStanzaId() + "'");
@ -583,16 +597,22 @@ public class PacketParserUtils {
return presence;
}
public static IQ parseIQ(XmlPullParser parser) throws Exception {
return parseIQ(parser, null);
}
/**
* Parses an IQ packet.
*
* @param parser the XML parser, positioned at the start of an IQ packet.
* @param outerXmlEnvironment the outer XML environment (optional).
* @return an IQ object.
* @throws Exception
*/
public static IQ parseIQ(XmlPullParser parser) throws Exception {
public static IQ parseIQ(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws Exception {
ParserUtils.assertAtStartTag(parser);
final int initialDepth = parser.getDepth();
XmlEnvironment iqXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
IQ iqPacket = null;
StanzaError.Builder error = null;
@ -610,14 +630,14 @@ public class PacketParserUtils {
String namespace = parser.getNamespace();
switch (elementName) {
case "error":
error = PacketParserUtils.parseError(parser);
error = PacketParserUtils.parseError(parser, iqXmlEnvironment);
break;
// Otherwise, see if there is a registered provider for
// this element name and namespace.
default:
IQProvider<IQ> provider = ProviderManager.getIQProvider(elementName, namespace);
if (provider != null) {
iqPacket = provider.parse(parser);
iqPacket = provider.parse(parser, outerXmlEnvironment);
}
// Note that if we reach this code, it is guranteed that the result IQ contained a child element
// (RFC 6120 § 8.2.3 6) because otherwhise we would have reached the END_TAG first.
@ -784,21 +804,27 @@ public class PacketParserUtils {
return new SASLFailure(condition, descriptiveTexts);
}
public static StreamError parseStreamError(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
return parseStreamError(parser, null);
}
/**
* Parses stream error packets.
*
* @param parser the XML parser.
* @param outerXmlEnvironment the outer XML environment (optional).
* @return an stream error packet.
* @throws IOException
* @throws XmlPullParserException
* @throws SmackParsingException
*/
public static StreamError parseStreamError(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
public static StreamError parseStreamError(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
final int initialDepth = parser.getDepth();
List<ExtensionElement> extensions = new ArrayList<>();
Map<String, String> descriptiveTexts = null;
StreamError.Condition condition = null;
String conditionText = null;
XmlEnvironment streamErrorXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
outerloop: while (true) {
int eventType = parser.next();
switch (eventType) {
@ -822,7 +848,7 @@ public class PacketParserUtils {
}
break;
default:
PacketParserUtils.addExtensionElement(extensions, parser, name, namespace);
PacketParserUtils.addExtensionElement(extensions, parser, name, namespace, streamErrorXmlEnvironment);
break;
}
break;
@ -836,18 +862,24 @@ public class PacketParserUtils {
return new StreamError(condition, conditionText, descriptiveTexts, extensions);
}
public static StanzaError.Builder parseError(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
return parseError(parser, null);
}
/**
* Parses error sub-packets.
*
* @param parser the XML parser.
* @param outerXmlEnvironment the outer XML environment (optional).
* @return an error sub-packet.
* @throws IOException
* @throws XmlPullParserException
* @throws SmackParsingException
*/
public static StanzaError.Builder parseError(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
public static StanzaError.Builder parseError(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
final int initialDepth = parser.getDepth();
Map<String, String> descriptiveTexts = null;
XmlEnvironment stanzaErrorXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
List<ExtensionElement> extensions = new ArrayList<>();
StanzaError.Builder builder = StanzaError.getBuilder();
@ -876,7 +908,7 @@ public class PacketParserUtils {
}
break;
default:
PacketParserUtils.addExtensionElement(extensions, parser, name, namespace);
PacketParserUtils.addExtensionElement(extensions, parser, name, namespace, stanzaErrorXmlEnvironment);
}
break;
case XmlPullParser.END_TAG:
@ -895,23 +927,7 @@ public class PacketParserUtils {
* @param elementName the XML element name of the extension element.
* @param namespace the XML namespace of the stanza extension.
* @param parser the XML parser, positioned at the starting element of the extension.
*
* @return an extension element.
* @throws Exception when an error occurs during parsing.
* @deprecated use {@link #parseExtensionElement(String, String, XmlPullParser)} instead.
*/
@Deprecated
public static ExtensionElement parsePacketExtension(String elementName, String namespace,
XmlPullParser parser) throws Exception {
return parseExtensionElement(elementName, namespace, parser);
}
/**
* Parses an extension element.
*
* @param elementName the XML element name of the extension element.
* @param namespace the XML namespace of the stanza extension.
* @param parser the XML parser, positioned at the starting element of the extension.
* @param outerXmlEnvironment the outer XML environment (optional).
*
* @return an extension element.
* @throws XmlPullParserException
@ -919,16 +935,16 @@ public class PacketParserUtils {
* @throws SmackParsingException
*/
public static ExtensionElement parseExtensionElement(String elementName, String namespace,
XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
ParserUtils.assertAtStartTag(parser);
// See if a provider is registered to handle the extension.
ExtensionElementProvider<ExtensionElement> provider = ProviderManager.getExtensionProvider(elementName, namespace);
if (provider != null) {
return provider.parse(parser);
return provider.parse(parser, outerXmlEnvironment);
}
// No providers registered, so use a default extension.
return StandardExtensionElementProvider.INSTANCE.parse(parser);
return StandardExtensionElementProvider.INSTANCE.parse(parser, outerXmlEnvironment);
}
public static StartTls parseStartTlsFeature(XmlPullParser parser)
@ -987,26 +1003,26 @@ public class PacketParserUtils {
return new Session.Feature(optional);
}
public static void addExtensionElement(Stanza packet, XmlPullParser parser)
public static void addExtensionElement(Stanza packet, XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException {
ParserUtils.assertAtStartTag(parser);
addExtensionElement(packet, parser, parser.getName(), parser.getNamespace());
addExtensionElement(packet, parser, parser.getName(), parser.getNamespace(), outerXmlEnvironment);
}
public static void addExtensionElement(Stanza packet, XmlPullParser parser, String elementName,
String namespace) throws XmlPullParserException, IOException, SmackParsingException {
ExtensionElement packetExtension = parseExtensionElement(elementName, namespace, parser);
String namespace, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
ExtensionElement packetExtension = parseExtensionElement(elementName, namespace, parser, outerXmlEnvironment);
packet.addExtension(packetExtension);
}
public static void addExtensionElement(Collection<ExtensionElement> collection, XmlPullParser parser)
public static void addExtensionElement(Collection<ExtensionElement> collection, XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException {
addExtensionElement(collection, parser, parser.getName(), parser.getNamespace());
addExtensionElement(collection, parser, parser.getName(), parser.getNamespace(), outerXmlEnvironment);
}
public static void addExtensionElement(Collection<ExtensionElement> collection, XmlPullParser parser,
String elementName, String namespace) throws XmlPullParserException, IOException, SmackParsingException {
ExtensionElement packetExtension = parseExtensionElement(elementName, namespace, parser);
String elementName, String namespace, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
ExtensionElement packetExtension = parseExtensionElement(elementName, namespace, parser, outerXmlEnvironment);
collection.add(packetExtension);
}
}

View file

@ -22,6 +22,7 @@ import static org.junit.Assert.assertThat;
import java.io.IOException;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.test.util.TestUtils;
@ -79,7 +80,7 @@ public class ParsingExceptionTest {
public static final String NAMESPACE = "http://smack.jivesoftware.org/exception";
@Override
public ExtensionElement parse(XmlPullParser parser, int initialDepth) throws IOException {
public ExtensionElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws IOException {
throw new IOException("Test Exception");
}

View file

@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.Collection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.FileUtils;
import org.junit.Assert;
@ -62,7 +63,7 @@ public class ProviderConfigTest {
public static class TestIQProvider extends IQProvider<IQ> {
@Override
public IQ parse(XmlPullParser parser, int initialDepth) {
public IQ parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) {
return null;
}

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.
@ -20,6 +20,7 @@ import static org.junit.Assert.assertTrue;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
@ -38,7 +39,7 @@ public class ProviderManagerTest {
public static class TestIQProvider extends IQProvider<IQ> {
@Override
public IQ parse(XmlPullParser parser, int initialDepth) {
public IQ parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) {
return null;
}

View file

@ -21,6 +21,7 @@ import java.io.Reader;
import java.io.StringReader;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.ParserUtils;
@ -80,15 +81,15 @@ public final class TestUtils {
public static <EE extends ExtensionElement> EE parseExtensionElement(String elementString)
throws Exception {
return parseExtensionElement(getParser(elementString));
return parseExtensionElement(getParser(elementString), null);
}
@SuppressWarnings("unchecked")
public static <EE extends ExtensionElement> EE parseExtensionElement(XmlPullParser parser)
public static <EE extends ExtensionElement> EE parseExtensionElement(XmlPullParser parser, XmlEnvironment outerXmlEnvironment)
throws Exception {
ParserUtils.assertAtStartTag(parser);
final String elementName = parser.getName();
final String namespace = parser.getNamespace();
return (EE) PacketParserUtils.parseExtensionElement(elementName, namespace, parser);
return (EE) PacketParserUtils.parseExtensionElement(elementName, namespace, parser, outerXmlEnvironment);
}
}