DiscoverItemsProvider.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  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.disco.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.provider.IQProvider;
  20. import org.jivesoftware.smack.util.ParserUtils;
  21. import org.jivesoftware.smackx.disco.packet.DiscoverItems;
  22. import org.jxmpp.jid.Jid;
  23. import org.xmlpull.v1.XmlPullParser;
  24. import org.xmlpull.v1.XmlPullParserException;

  25. /**
  26. * The DiscoverInfoProvider parses Service Discovery items packets.
  27. *
  28. * @author Gaston Dombiak
  29. */
  30. public class DiscoverItemsProvider extends IQProvider<DiscoverItems> {

  31.     @Override
  32.     public DiscoverItems parse(XmlPullParser parser, int initialDepth)
  33.                     throws XmlPullParserException, IOException {
  34.         DiscoverItems discoverItems = new DiscoverItems();
  35.         boolean done = false;
  36.         DiscoverItems.Item item;
  37.         Jid jid = null;
  38.         String name = "";
  39.         String action = "";
  40.         String node = "";
  41.         discoverItems.setNode(parser.getAttributeValue("", "node"));
  42.         while (!done) {
  43.             int eventType = parser.next();

  44.             if (eventType == XmlPullParser.START_TAG && "item".equals(parser.getName())) {
  45.                 // Initialize the variables from the parsed XML
  46.                 jid = ParserUtils.getJidAttribute(parser);
  47.                 name = parser.getAttributeValue("", "name");
  48.                 node = parser.getAttributeValue("", "node");
  49.                 action = parser.getAttributeValue("", "action");
  50.             }
  51.             else if (eventType == XmlPullParser.END_TAG && "item".equals(parser.getName())) {
  52.                 // Create a new Item and add it to DiscoverItems.
  53.                 item = new DiscoverItems.Item(jid);
  54.                 item.setName(name);
  55.                 item.setNode(node);
  56.                 item.setAction(action);
  57.                 discoverItems.addItem(item);
  58.             }
  59.             else if (eventType == XmlPullParser.END_TAG && "query".equals(parser.getName())) {
  60.                 done = true;
  61.             }
  62.         }

  63.         return discoverItems;
  64.     }
  65. }