001/** 002 * 003 * Copyright © 2003-2007 Jive Software, 2014-2015 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.roster.provider; 018 019import java.io.IOException; 020 021import org.jivesoftware.smack.SmackException; 022import org.jivesoftware.smack.provider.IQProvider; 023import org.jivesoftware.smack.roster.packet.RosterPacket; 024import org.xmlpull.v1.XmlPullParser; 025import org.xmlpull.v1.XmlPullParserException; 026 027public class RosterPacketProvider extends IQProvider<RosterPacket> { 028 029 public static final RosterPacketProvider INSTANCE = new RosterPacketProvider(); 030 031 @Override 032 public RosterPacket parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, 033 SmackException { 034 RosterPacket roster = new RosterPacket(); 035 RosterPacket.Item item = null; 036 037 String version = parser.getAttributeValue("", "ver"); 038 roster.setVersion(version); 039 040 outerloop: while (true) { 041 int eventType = parser.next(); 042 switch(eventType) { 043 case XmlPullParser.START_TAG: 044 String startTag = parser.getName(); 045 switch (startTag) { 046 case "item": 047 String jid = parser.getAttributeValue("", "jid"); 048 String name = parser.getAttributeValue("", "name"); 049 // Create packet. 050 item = new RosterPacket.Item(jid, name); 051 // Set status. 052 String ask = parser.getAttributeValue("", "ask"); 053 RosterPacket.ItemStatus status = RosterPacket.ItemStatus.fromString(ask); 054 item.setItemStatus(status); 055 // Set type. 056 String subscription = parser.getAttributeValue("", "subscription"); 057 RosterPacket.ItemType type = RosterPacket.ItemType.valueOf(subscription != null ? subscription : "none"); 058 item.setItemType(type); 059 break; 060 case "group": 061 // TODO item!= null 062 final String groupName = parser.nextText(); 063 if (groupName != null && groupName.trim().length() > 0) { 064 item.addGroupName(groupName); 065 } 066 break; 067 } 068 break; 069 case XmlPullParser.END_TAG: 070 String endTag = parser.getName(); 071 switch(endTag) { 072 case "item": 073 roster.addRosterItem(item); 074 break; 075 case "query": 076 break outerloop; 077 } 078 } 079 } 080 return roster; 081 } 082 083}