PubSubProvider.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.pubsub.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.packet.IQ;
  20. import org.jivesoftware.smack.packet.IqData;
  21. import org.jivesoftware.smack.packet.XmlEnvironment;
  22. import org.jivesoftware.smack.parsing.SmackParsingException;
  23. import org.jivesoftware.smack.provider.IqProvider;
  24. import org.jivesoftware.smack.util.PacketParserUtils;
  25. import org.jivesoftware.smack.xml.XmlPullParser;
  26. import org.jivesoftware.smack.xml.XmlPullParserException;

  27. import org.jivesoftware.smackx.pubsub.packet.PubSub;
  28. import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;

  29. /**
  30.  * Parses the root PubSub stanza extensions of the {@link IQ} stanza and returns
  31.  * a {@link PubSub} instance.
  32.  *
  33.  * @author Robin Collier
  34.  */
  35. public class PubSubProvider extends IqProvider<PubSub> {
  36.     @Override
  37.     public PubSub parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
  38.         String namespace = parser.getNamespace();
  39.         PubSubNamespace pubSubNamespace = PubSubNamespace.valueOfFromXmlns(namespace);
  40.         PubSub pubsub = new PubSub(pubSubNamespace);

  41.         outerloop: while (true)  {
  42.             XmlPullParser.Event eventType = parser.next();
  43.             switch (eventType) {
  44.             case START_ELEMENT:
  45.                 PacketParserUtils.addExtensionElement(pubsub, parser, xmlEnvironment);
  46.                 break;
  47.             case END_ELEMENT:
  48.                 if (parser.getDepth() == initialDepth) {
  49.                     break outerloop;
  50.                 }
  51.                 break;
  52.             default:
  53.                 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
  54.                 break;
  55.             }
  56.         }
  57.         return pubsub;
  58.     }
  59. }