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