RosterPacketProvider.java

  1. /**
  2.  *
  3.  * Copyright © 2003-2007 Jive Software, 2014-2015 Florian Schmaus
  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.smack.roster.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.SmackException;
  20. import org.jivesoftware.smack.provider.IQProvider;
  21. import org.jivesoftware.smack.roster.packet.RosterPacket;
  22. import org.jxmpp.jid.Jid;
  23. import org.jxmpp.jid.impl.JidCreate;
  24. import org.xmlpull.v1.XmlPullParser;
  25. import org.xmlpull.v1.XmlPullParserException;

  26. public class RosterPacketProvider extends IQProvider<RosterPacket> {

  27.     public static final RosterPacketProvider INSTANCE = new RosterPacketProvider();

  28.     @Override
  29.     public RosterPacket parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException,
  30.                     SmackException {
  31.         RosterPacket roster = new RosterPacket();
  32.         RosterPacket.Item item = null;

  33.         String version = parser.getAttributeValue("", "ver");
  34.         roster.setVersion(version);

  35.         outerloop: while (true) {
  36.             int eventType = parser.next();
  37.             switch(eventType) {
  38.             case XmlPullParser.START_TAG:
  39.                 String startTag = parser.getName();
  40.                 switch (startTag) {
  41.                 case "item":
  42.                     String jidString = parser.getAttributeValue("", "jid");
  43.                     String name = parser.getAttributeValue("", "name");
  44.                     Jid jid = JidCreate.from(jidString);
  45.                     // Create packet.
  46.                     item = new RosterPacket.Item(jid, name);
  47.                     // Set status.
  48.                     String ask = parser.getAttributeValue("", "ask");
  49.                     RosterPacket.ItemStatus status = RosterPacket.ItemStatus.fromString(ask);
  50.                     item.setItemStatus(status);
  51.                     // Set type.
  52.                     String subscription = parser.getAttributeValue("", "subscription");
  53.                     RosterPacket.ItemType type = RosterPacket.ItemType.valueOf(subscription != null ? subscription : "none");
  54.                     item.setItemType(type);
  55.                     break;
  56.                 case "group":
  57.                     // TODO item!= null
  58.                     final String groupName = parser.nextText();
  59.                     if (groupName != null && groupName.trim().length() > 0) {
  60.                         item.addGroupName(groupName);
  61.                     }
  62.                     break;
  63.                 }
  64.                 break;
  65.             case XmlPullParser.END_TAG:
  66.                 String endTag = parser.getName();
  67.                 switch(endTag) {
  68.                 case "item":
  69.                     roster.addRosterItem(item);
  70.                     break;
  71.                 case "query":
  72.                     break outerloop;
  73.                 }
  74.             }
  75.         }
  76.         return roster;
  77.     }

  78. }