001/*
002 *
003 * Copyright 2003-2007 Jive Software.
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 */
017
018package org.jivesoftware.smackx.xroster.provider;
019
020import java.io.IOException;
021import java.util.ArrayList;
022
023import org.jivesoftware.smack.packet.XmlEnvironment;
024import org.jivesoftware.smack.provider.ExtensionElementProvider;
025import org.jivesoftware.smack.util.ParserUtils;
026import org.jivesoftware.smack.xml.XmlPullParser;
027import org.jivesoftware.smack.xml.XmlPullParserException;
028
029import org.jivesoftware.smackx.xroster.RemoteRosterEntry;
030import org.jivesoftware.smackx.xroster.packet.RosterExchange;
031
032import org.jxmpp.JxmppContext;
033import org.jxmpp.jid.Jid;
034
035/**
036 *
037 * The RosterExchangeProvider parses RosterExchange packets.
038 *
039 * @author Gaston Dombiak
040 */
041public class RosterExchangeProvider extends ExtensionElementProvider<RosterExchange> {
042
043    /**
044     * Parses a RosterExchange stanza (extension sub-packet).
045     *
046     * @param parser the XML parser, positioned at the starting element of the extension.
047     * @return a PacketExtension.
048     * @throws IOException if an I/O error occurred.
049     * @throws XmlPullParserException if an error in the XML parser occurred.
050     */
051    @Override
052    public RosterExchange parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext)
053                    throws XmlPullParserException, IOException {
054        RosterExchange rosterExchange = new RosterExchange();
055        boolean done = false;
056        RemoteRosterEntry remoteRosterEntry;
057        Jid jid = null;
058        String name = "";
059        ArrayList<String> groupsName = new ArrayList<>();
060        while (!done) {
061            XmlPullParser.Event eventType = parser.next();
062            if (eventType == XmlPullParser.Event.START_ELEMENT) {
063                if (parser.getName().equals("item")) {
064                    // Reset this variable since they are optional for each item
065                    groupsName = new ArrayList<>();
066                    // Initialize the variables from the parsed XML
067                    jid = ParserUtils.getJidAttribute(parser, jxmppContext);
068                    name = parser.getAttributeValue("", "name");
069                }
070                if (parser.getName().equals("group")) {
071                    groupsName.add(parser.nextText());
072                }
073            } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
074                if (parser.getName().equals("item")) {
075                    // Create packet.
076                    remoteRosterEntry = new RemoteRosterEntry(jid, name, groupsName.toArray(new String[groupsName.size()]));
077                    rosterExchange.addRosterEntry(remoteRosterEntry);
078                }
079                if (parser.getName().equals("x")) {
080                    done = true;
081                }
082            }
083        }
084        return rosterExchange;
085
086    }
087
088}