RosterExchangeProvider.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.xroster.provider;

  18. import java.io.IOException;
  19. import java.util.ArrayList;

  20. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  21. import org.jivesoftware.smack.util.ParserUtils;
  22. import org.jivesoftware.smackx.xroster.RemoteRosterEntry;
  23. import org.jivesoftware.smackx.xroster.packet.RosterExchange;
  24. import org.jxmpp.jid.Jid;
  25. import org.xmlpull.v1.XmlPullParser;
  26. import org.xmlpull.v1.XmlPullParserException;

  27. /**
  28.  *
  29.  * The RosterExchangeProvider parses RosterExchange packets.
  30.  *
  31.  * @author Gaston Dombiak
  32.  */
  33. public class RosterExchangeProvider extends ExtensionElementProvider<RosterExchange> {

  34.     /**
  35.      * Parses a RosterExchange packet (extension sub-packet).
  36.      *
  37.      * @param parser the XML parser, positioned at the starting element of the extension.
  38.      * @return a PacketExtension.
  39.      * @throws IOException
  40.      * @throws XmlPullParserException
  41.      */
  42.     @Override
  43.     public RosterExchange parse(XmlPullParser parser, int initialDepth)
  44.                     throws XmlPullParserException, IOException {

  45.         RosterExchange rosterExchange = new RosterExchange();
  46.         boolean done = false;
  47.         RemoteRosterEntry remoteRosterEntry = null;
  48.         Jid jid = null;
  49.         String name = "";
  50.         ArrayList<String> groupsName = new ArrayList<String>();
  51.         while (!done) {
  52.             int eventType = parser.next();
  53.             if (eventType == XmlPullParser.START_TAG) {
  54.                 if (parser.getName().equals("item")) {
  55.                     // Reset this variable since they are optional for each item
  56.                     groupsName = new ArrayList<String>();
  57.                     // Initialize the variables from the parsed XML
  58.                     jid = ParserUtils.getJidAttribute(parser);
  59.                     name = parser.getAttributeValue("", "name");
  60.                 }
  61.                 if (parser.getName().equals("group")) {
  62.                     groupsName.add(parser.nextText());
  63.                 }
  64.             } else if (eventType == XmlPullParser.END_TAG) {
  65.                 if (parser.getName().equals("item")) {
  66.                     // Create packet.
  67.                     remoteRosterEntry = new RemoteRosterEntry(jid, name, (String[]) groupsName.toArray(new String[groupsName.size()]));
  68.                     rosterExchange.addRosterEntry(remoteRosterEntry);
  69.                 }
  70.                 if (parser.getName().equals("x")) {
  71.                     done = true;
  72.                 }
  73.             }
  74.         }

  75.         return rosterExchange;

  76.     }

  77. }