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.packet.IQ;
023import org.jivesoftware.smack.provider.IQProvider;
024import org.jivesoftware.smack.roster.packet.RosterPacket;
025import org.jivesoftware.smack.util.ParserUtils;
026
027import org.jxmpp.jid.BareJid;
028import org.jxmpp.jid.impl.JidCreate;
029import org.xmlpull.v1.XmlPullParser;
030import org.xmlpull.v1.XmlPullParserException;
031
032public class RosterPacketProvider extends IQProvider<RosterPacket> {
033
034    public static final RosterPacketProvider INSTANCE = new RosterPacketProvider();
035
036    @Override
037    public RosterPacket parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException,
038                    SmackException {
039        RosterPacket roster = new RosterPacket();
040        String version = parser.getAttributeValue("", "ver");
041        roster.setVersion(version);
042
043        outerloop: while (true) {
044            int eventType = parser.next();
045            switch (eventType) {
046            case XmlPullParser.START_TAG:
047                String startTag = parser.getName();
048                switch (startTag) {
049                case "item":
050                    RosterPacket.Item item = parseItem(parser);
051                    roster.addRosterItem(item);
052                    break;
053                }
054                break;
055            case XmlPullParser.END_TAG:
056                String endTag = parser.getName();
057                switch (endTag) {
058                case IQ.QUERY_ELEMENT:
059                    if (parser.getDepth() == initialDepth) {
060                        break outerloop;
061                    }
062                }
063            }
064        }
065        return roster;
066    }
067
068    public static RosterPacket.Item parseItem(XmlPullParser parser) throws XmlPullParserException, IOException {
069        ParserUtils.assertAtStartTag(parser, RosterPacket.Item.ELEMENT);
070        final int initialDepth = parser.getDepth();
071        String jidString = parser.getAttributeValue("", "jid");
072        String itemName = parser.getAttributeValue("", "name");
073        BareJid jid = JidCreate.bareFrom(jidString);
074
075        // Create item.
076        RosterPacket.Item item = new RosterPacket.Item(jid, itemName);
077        // Set status.
078        String ask = parser.getAttributeValue("", "ask");
079        item.setSubscriptionPending("subscribe".equals(ask));
080        // Set type.
081        String subscription = parser.getAttributeValue("", "subscription");
082        RosterPacket.ItemType type = RosterPacket.ItemType.fromString(subscription);
083        item.setItemType(type);
084        // Set approval status.
085        boolean approved = ParserUtils.getBooleanAttribute(parser, "approved", false);
086        item.setApproved(approved);
087
088        outerloop: while (true) {
089            int eventType = parser.next();
090            switch (eventType) {
091            case XmlPullParser.START_TAG:
092                String name = parser.getName();
093                switch (name) {
094                case RosterPacket.Item.GROUP:
095                    final String groupName = parser.nextText();
096                    if (groupName != null && groupName.trim().length() > 0) {
097                        item.addGroupName(groupName);
098                    }
099                    break;
100                }
101                break;
102            case XmlPullParser.END_TAG:
103                if (parser.getDepth() == initialDepth) {
104                    break outerloop;
105                }
106                break;
107            }
108        }
109        ParserUtils.assertAtEndTag(parser);
110        assert (item != null);
111        return item;
112    }
113}