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 java.io.IOException;

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

  27. import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
  28. import org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder;

  29. /**
  30. * The DiscoverInfoProvider parses Service Discovery information packets.
  31. *
  32. * @author Gaston Dombiak
  33. */
  34. public class DiscoverInfoProvider extends IqProvider<DiscoverInfo> {

  35.     @Override
  36.     public DiscoverInfo parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)
  37.             throws XmlPullParserException, IOException, SmackParsingException {
  38.         DiscoverInfoBuilder discoverInfoBuilder = DiscoverInfo.builder(iqData);

  39.         String node = parser.getAttributeValue("node");
  40.         discoverInfoBuilder.setNode(node);

  41.         outerloop: while (true) {
  42.             XmlPullParser.Event eventType = parser.next();
  43.             if (eventType == XmlPullParser.Event.START_ELEMENT) {
  44.                 final String name = parser.getName();
  45.                 final String namespace = parser.getNamespace();
  46.                 if (namespace.equals(DiscoverInfo.NAMESPACE)) {
  47.                     switch (name) {
  48.                     case "identity":
  49.                         String category = parser.getAttributeValue("category");
  50.                         String identityName = parser.getAttributeValue("name");
  51.                         String type = parser.getAttributeValue("type");
  52.                         String lang = ParserUtils.getXmlLang(parser);
  53.                         DiscoverInfo.Identity identity = new DiscoverInfo.Identity(category, type, identityName, lang);
  54.                         discoverInfoBuilder.addIdentity(identity);
  55.                         break;
  56.                     case "feature":
  57.                         String feature = parser.getAttributeValue("var");
  58.                         discoverInfoBuilder.addFeature(feature);
  59.                         break;
  60.                     }
  61.                 }
  62.                 // Otherwise, it must be a packet extension.
  63.                 else {
  64.                     PacketParserUtils.addExtensionElement(discoverInfoBuilder, parser, xmlEnvironment);
  65.                 }
  66.             } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
  67.                 if (parser.getDepth() == initialDepth) {
  68.                     break outerloop;
  69.                 }
  70.             }
  71.         }

  72.         DiscoverInfo discoverInfo = discoverInfoBuilder.buildWithoutValidiation();
  73.         return discoverInfo;
  74.     }
  75. }