DiscoverInfoProvider.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 org.jivesoftware.smack.provider.IQProvider;
  19. import org.jivesoftware.smack.util.PacketParserUtils;
  20. import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
  21. import org.xmlpull.v1.XmlPullParser;

  22. /**
  23. * The DiscoverInfoProvider parses Service Discovery information packets.
  24. *
  25. * @author Gaston Dombiak
  26. */
  27. public class DiscoverInfoProvider extends IQProvider<DiscoverInfo> {

  28.     @Override
  29.     public DiscoverInfo parse(XmlPullParser parser, int initialDepth)
  30.                     throws Exception {
  31.         DiscoverInfo discoverInfo = new DiscoverInfo();
  32.         boolean done = false;
  33.         DiscoverInfo.Identity identity = null;
  34.         String category = "";
  35.         String name = "";
  36.         String type = "";
  37.         String variable = "";
  38.         String lang = "";
  39.         discoverInfo.setNode(parser.getAttributeValue("", "node"));
  40.         while (!done) {
  41.             int eventType = parser.next();
  42.             if (eventType == XmlPullParser.START_TAG) {
  43.                 if (parser.getName().equals("identity")) {
  44.                     // Initialize the variables from the parsed XML
  45.                     category = parser.getAttributeValue("", "category");
  46.                     name = parser.getAttributeValue("", "name");
  47.                     type = parser.getAttributeValue("", "type");
  48.                     lang = parser.getAttributeValue(parser.getNamespace("xml"), "lang");
  49.                 }
  50.                 else if (parser.getName().equals("feature")) {
  51.                     // Initialize the variables from the parsed XML
  52.                     variable = parser.getAttributeValue("", "var");
  53.                 }
  54.                 // Otherwise, it must be a packet extension.
  55.                 else {
  56.                     PacketParserUtils.addExtensionElement(discoverInfo, parser);
  57.                 }
  58.             } else if (eventType == XmlPullParser.END_TAG) {
  59.                 if (parser.getName().equals("identity")) {
  60.                     // Create a new identity and add it to the discovered info.
  61.                     identity = new DiscoverInfo.Identity(category, type, name, lang);
  62.                     discoverInfo.addIdentity(identity);
  63.                 }
  64.                 if (parser.getName().equals("feature")) {
  65.                     // Create a new feature and add it to the discovered info.
  66.                     boolean notADuplicateFeature = discoverInfo.addFeature(variable);
  67.                     assert(notADuplicateFeature);
  68.                 }
  69.                 if (parser.getName().equals("query")) {
  70.                     done = true;
  71.                 }
  72.             }
  73.         }

  74.         return discoverInfo;
  75.     }
  76. }