From 452b2aca0f4f8f451075e7b7602282ac1ddfd4bc Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 7 Oct 2014 21:46:02 +0200 Subject: [PATCH] Add 'PacketParserUtils.addPacketextension()' --- .../provider/EmbeddedExtensionProvider.java | 4 +- .../smack/util/PacketParserUtils.java | 62 +++++++++++++------ .../disco/provider/DiscoverInfoProvider.java | 3 +- .../provider/RegistrationProvider.java | 4 +- .../smackx/muc/provider/MUCOwnerProvider.java | 3 +- .../pubsub/provider/PubSubProvider.java | 4 +- .../smackx/search/UserSearch.java | 7 +-- .../workgroup/ext/forms/WorkgroupForm.java | 3 +- .../workgroup/packet/TranscriptSearch.java | 2 +- 9 files changed, 52 insertions(+), 40 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/provider/EmbeddedExtensionProvider.java b/smack-core/src/main/java/org/jivesoftware/smack/provider/EmbeddedExtensionProvider.java index 957fdb19c..0ad337c82 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/provider/EmbeddedExtensionProvider.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/provider/EmbeddedExtensionProvider.java @@ -102,8 +102,8 @@ abstract public class EmbeddedExtensionProvider exte { tag = parser.next(); - if (tag == XmlPullParser.START_TAG) - extensions.add(PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser)); + if (tag == XmlPullParser.START_TAG) + PacketParserUtils.addPacketExtension(extensions, parser); } while (!(tag == XmlPullParser.END_TAG && parser.getDepth() == initialDepth)); return createReturnExtension(name, namespace, attMap, extensions); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java index 5623e408d..1ab9cf6ac 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java @@ -245,8 +245,7 @@ public class PacketParserUtils { message.setError(parseError(parser)); break; default: - message.addExtension( - PacketParserUtils.parsePacketExtension(elementName, namespace, parser)); + PacketParserUtils.addPacketExtension(message, parser, elementName, namespace); break; } break; @@ -504,15 +503,12 @@ public class PacketParserUtils { // 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 { - presence.addExtension(PacketParserUtils.parsePacketExtension(elementName, namespace, parser)); - } - catch (Exception e) { - LOGGER.log(Level.WARNING, - "Failed to parse extension packet in Presence packet.", - e); - } - break; + try { + PacketParserUtils.addPacketExtension(presence, parser, elementName, namespace); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to parse extension packet in Presence packet.", e); + } + break; } case XmlPullParser.END_TAG: if (parser.getDepth() == initialDepth) { @@ -871,8 +867,8 @@ public class PacketParserUtils { if (namespace.equals(XMPPError.NAMESPACE)) { condition = elementName; } - else { - extensions.add(parsePacketExtension(elementName, namespace, parser)); + else { + PacketParserUtils.addPacketExtension(extensions, parser, elementName, namespace); } } } @@ -906,6 +902,7 @@ public class PacketParserUtils { public static PacketExtension parsePacketExtension(String elementName, String namespace, XmlPullParser parser) throws XmlPullParserException, IOException, SmackException { + ParserUtils.assertAtStartTag(parser); // See if a provider is registered to handle the extension. PacketExtensionProvider provider = ProviderManager.getExtensionProvider(elementName, namespace); if (provider != null) { @@ -923,12 +920,14 @@ public class PacketParserUtils { throw new SmackException(e); } } + + final int initialDepth = parser.getDepth(); // No providers registered, so use a default extension. DefaultPacketExtension extension = new DefaultPacketExtension(elementName, namespace); - boolean done = false; - while (!done) { + outerloop: while (true) { int eventType = parser.next(); - if (eventType == XmlPullParser.START_TAG) { + switch (eventType) { + case XmlPullParser.START_TAG: String name = parser.getName(); // If an empty element, set the value with the empty string. if (parser.isEmptyElementTag()) { @@ -942,10 +941,10 @@ public class PacketParserUtils { extension.setValue(name, value); } } - } - else if (eventType == XmlPullParser.END_TAG) { - if (parser.getName().equals(elementName)) { - done = true; + break; + case XmlPullParser.END_TAG: + if (parser.getDepth() == initialDepth) { + break outerloop; } } } @@ -1022,6 +1021,29 @@ public class PacketParserUtils { return object; } + public static void addPacketExtension(Packet packet, XmlPullParser parser) throws XmlPullParserException, + IOException, SmackException { + ParserUtils.assertAtStartTag(parser); + addPacketExtension(packet, parser, parser.getName(), parser.getNamespace()); + } + + public static void addPacketExtension(Packet packet, XmlPullParser parser, String elementName, String namespace) + throws XmlPullParserException, IOException, SmackException { + PacketExtension packetExtension = parsePacketExtension(elementName, namespace, parser); + packet.addExtension(packetExtension); + } + + public static void addPacketExtension(Collection collection, XmlPullParser parser) + throws XmlPullParserException, IOException, SmackException { + addPacketExtension(collection, parser, parser.getName(), parser.getNamespace()); + } + + public static void addPacketExtension(Collection collection, XmlPullParser parser, + String elementName, String namespace) throws XmlPullParserException, IOException, SmackException { + PacketExtension packetExtension = parsePacketExtension(elementName, namespace, parser); + collection.add(packetExtension); + } + /** * Decodes a String into an object of the specified type. If the object * type is not supported, null will be returned. diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/provider/DiscoverInfoProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/provider/DiscoverInfoProvider.java index 29bcc6c28..463b277cb 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/provider/DiscoverInfoProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/provider/DiscoverInfoProvider.java @@ -61,8 +61,7 @@ public class DiscoverInfoProvider extends IQProvider { } // Otherwise, it must be a packet extension. else { - discoverInfo.addExtension(PacketParserUtils.parsePacketExtension(parser - .getName(), parser.getNamespace(), parser)); + PacketParserUtils.addPacketExtension(discoverInfo, parser); } } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals("identity")) { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/iqregister/provider/RegistrationProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqregister/provider/RegistrationProvider.java index 574eddc56..2df60a139 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/iqregister/provider/RegistrationProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqregister/provider/RegistrationProvider.java @@ -62,9 +62,7 @@ public class RegistrationProvider extends IQProvider { } // Otherwise, it must be a packet extension. else { - packetExtensions.add(PacketParserUtils.parsePacketExtension( - parser.getName(), parser.getNamespace(), - parser)); + PacketParserUtils.addPacketExtension(packetExtensions, parser); } } else if (eventType == XmlPullParser.END_TAG) { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCOwnerProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCOwnerProvider.java index 00c4236b0..cfac31cc0 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCOwnerProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/provider/MUCOwnerProvider.java @@ -49,8 +49,7 @@ public class MUCOwnerProvider extends IQProvider { } // Otherwise, it must be a packet extension. else { - mucOwner.addExtension(PacketParserUtils.parsePacketExtension(parser.getName(), - parser.getNamespace(), parser)); + PacketParserUtils.addPacketExtension(mucOwner, parser); } } else if (eventType == XmlPullParser.END_TAG) { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/provider/PubSubProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/provider/PubSubProvider.java index cff8d1187..806406a9c 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/provider/PubSubProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/provider/PubSubProvider.java @@ -20,7 +20,6 @@ import java.io.IOException; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.packet.IQ; -import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.provider.IQProvider; import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smackx.pubsub.packet.PubSub; @@ -50,8 +49,7 @@ public class PubSubProvider extends IQProvider if (eventType == XmlPullParser.START_TAG) { - PacketExtension ext = PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser); - pubsub.addExtension(ext); + PacketParserUtils.addPacketExtension(pubsub, parser); } else if (eventType == XmlPullParser.END_TAG) { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/search/UserSearch.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/search/UserSearch.java index 2f2e0634c..9b77c8e64 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/search/UserSearch.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/search/UserSearch.java @@ -150,9 +150,7 @@ public class UserSearch extends IQ { else if (eventType == XmlPullParser.START_TAG && parser.getNamespace().equals("jabber:x:data")) { // Otherwise, it must be a packet extension. search = new UserSearch(); - search.addExtension(PacketParserUtils.parsePacketExtension(parser.getName(), - parser.getNamespace(), parser)); - + PacketParserUtils.addPacketExtension(search, parser); } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals("query")) { @@ -205,8 +203,7 @@ public class UserSearch extends IQ { } } else if (eventType == XmlPullParser.START_TAG && parser.getNamespace().equals("jabber:x:data")) { - search.addExtension(PacketParserUtils.parsePacketExtension(parser.getName(), - parser.getNamespace(), parser)); + PacketParserUtils.addPacketExtension(search, parser); done = true; } } diff --git a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/ext/forms/WorkgroupForm.java b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/ext/forms/WorkgroupForm.java index e449335ff..169a6893b 100644 --- a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/ext/forms/WorkgroupForm.java +++ b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/ext/forms/WorkgroupForm.java @@ -67,8 +67,7 @@ public class WorkgroupForm extends IQ { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG) { // Parse the packet extension - answer.addExtension(PacketParserUtils.parsePacketExtension(parser.getName(), - parser.getNamespace(), parser)); + PacketParserUtils.addPacketExtension(answer, parser); } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals(ELEMENT_NAME)) { diff --git a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/TranscriptSearch.java b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/TranscriptSearch.java index e4be77727..7940e3556 100644 --- a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/TranscriptSearch.java +++ b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/TranscriptSearch.java @@ -71,7 +71,7 @@ public class TranscriptSearch extends IQ { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG) { // Parse the packet extension - answer.addExtension(PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser)); + PacketParserUtils.addPacketExtension(answer, parser); } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals(ELEMENT_NAME)) {